blob: 9e97cbb4f00668ceffa7cc80aa4653cef876c806 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
17#include <linux/mpage.h>
18#include <linux/swap.h>
19#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040021#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050022#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040023#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040024#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020026#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050027#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000028#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050029#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040030#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080031#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040032#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050033#include <linux/iversion.h>
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040039#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040040#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040041#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020042#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040043#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050044#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050045#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080046#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000047#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080050#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051
52struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080053 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040054 struct btrfs_root *root;
55};
56
Filipe Mananaf28a4922015-12-08 19:23:20 +000057struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000058 u64 reserve;
59 u64 unsubmitted_oe_range_start;
60 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080061 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000062};
63
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070064static const struct inode_operations btrfs_dir_inode_operations;
65static const struct inode_operations btrfs_symlink_inode_operations;
66static const struct inode_operations btrfs_dir_ro_inode_operations;
67static const struct inode_operations btrfs_special_inode_operations;
68static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070069static const struct address_space_operations btrfs_aops;
70static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010072static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010080static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040081 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88};
89
Eric Sandeen3972f262013-01-12 02:57:22 +000090static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000091static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040092static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050093static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080095 u64 start, u64 end, u64 delalloc_end,
96 int *page_started, unsigned long *nr_written,
97 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -080098static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
99 u64 orig_start, u64 block_start,
100 u64 block_len, u64 orig_block_len,
101 u64 ram_bytes, int compress_type,
102 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400103
Qu Wenruo524272602017-03-08 10:25:52 +0800104static void __endio_write_update_ordered(struct inode *inode,
105 const u64 offset, const u64 bytes,
106 const bool uptodate);
107
108/*
109 * Cleanup all submitted ordered extents in specified range to handle errors
110 * from the fill_dellaloc() callback.
111 *
112 * NOTE: caller must ensure that when an error happens, it can not call
113 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
114 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
115 * to be released, which we want to happen only when finishing the ordered
116 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
117 * fill_delalloc() callback already does proper cleanup for the first page of
118 * the range, that is, it invokes the callback writepage_end_io_hook() for the
119 * range of the first page.
120 */
121static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
122 const u64 offset,
123 const u64 bytes)
124{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900125 unsigned long index = offset >> PAGE_SHIFT;
126 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
127 struct page *page;
128
129 while (index <= end_index) {
130 page = find_get_page(inode->i_mapping, index);
131 index++;
132 if (!page)
133 continue;
134 ClearPagePrivate2(page);
135 put_page(page);
136 }
Qu Wenruo524272602017-03-08 10:25:52 +0800137 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
138 bytes - PAGE_SIZE, false);
139}
140
Eric Sandeen48a3b632013-04-25 20:41:01 +0000141static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400142
Josef Bacik6a3891c2015-03-16 17:38:52 -0400143#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
144void btrfs_test_inode_set_ops(struct inode *inode)
145{
146 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
147}
148#endif
149
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000150static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500151 struct inode *inode, struct inode *dir,
152 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500153{
154 int err;
155
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000156 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500157 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500158 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500159 return err;
160}
161
Chris Masond352ac62008-09-29 15:18:18 -0400162/*
Chris Masonc8b97812008-10-29 14:49:59 -0400163 * this does all the hard work for inserting an inline extent into
164 * the btree. The caller should have done a btrfs_drop_extents so that
165 * no overlapping inline items exist in the btree
166 */
Chris Mason40f76582014-05-21 13:35:51 -0700167static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000168 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct btrfs_root *root, struct inode *inode,
170 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000171 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct page **compressed_pages)
173{
Chris Masonc8b97812008-10-29 14:49:59 -0400174 struct extent_buffer *leaf;
175 struct page *page = NULL;
176 char *kaddr;
177 unsigned long ptr;
178 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400179 int ret;
180 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400181 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400182
Li Zefanfe3f5662011-03-28 08:30:38 +0000183 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400184 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400185
Chris Masonc8b97812008-10-29 14:49:59 -0400186 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000187
188 if (!extent_inserted) {
189 struct btrfs_key key;
190 size_t datasize;
191
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200192 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000193 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200194 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195
196 datasize = btrfs_file_extent_calc_inline_size(cur_size);
197 path->leave_spinning = 1;
198 ret = btrfs_insert_empty_item(trans, root, path, &key,
199 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200200 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400202 }
203 leaf = path->nodes[0];
204 ei = btrfs_item_ptr(leaf, path->slots[0],
205 struct btrfs_file_extent_item);
206 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
207 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
208 btrfs_set_file_extent_encryption(leaf, ei, 0);
209 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
210 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
211 ptr = btrfs_file_extent_inline_start(ei);
212
Li Zefan261507a02010-12-17 14:21:50 +0800213 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400214 struct page *cpage;
215 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500216 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400217 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500218 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400220
Cong Wang7ac687d2011-11-25 23:14:28 +0800221 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400222 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
225 i++;
226 ptr += cur_size;
227 compressed_size -= cur_size;
228 }
229 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800230 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 } else {
232 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800238 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300239 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 }
241 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000242 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400243
Yan, Zhengc2167752009-11-12 09:34:21 +0000244 /*
245 * we're an inline extent, so nobody can
246 * extend the file past i_size without locking
247 * a page we already have locked.
248 *
249 * We must do any isize and inode updates
250 * before we unlock the pages. Otherwise we
251 * could end up racing with unlink.
252 */
Chris Masonc8b97812008-10-29 14:49:59 -0400253 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000255
Chris Masonc8b97812008-10-29 14:49:59 -0400256fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200257 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400258}
259
260
261/*
262 * conditionally insert an inline extent into the file. This
263 * does the checks required to make sure the data is small enough
264 * to fit as an inline extent.
265 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200266static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400267 u64 end, size_t compressed_size,
268 int compress_type,
269 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400270{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200271 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400273 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400274 u64 isize = i_size_read(inode);
275 u64 actual_end = min(end + 1, isize);
276 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400277 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400278 u64 data_len = inline_len;
279 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 struct btrfs_path *path;
281 int extent_inserted = 0;
282 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400283
284 if (compressed_size)
285 data_len = compressed_size;
286
287 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 actual_end > fs_info->sectorsize ||
289 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400290 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400291 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400292 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400294 return 1;
295 }
296
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000297 path = btrfs_alloc_path();
298 if (!path)
299 return -ENOMEM;
300
Josef Bacik00361582013-08-14 14:02:47 -0400301 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 if (IS_ERR(trans)) {
303 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400304 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400306 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400307
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 if (compressed_size && compressed_pages)
309 extent_item_size = btrfs_file_extent_calc_inline_size(
310 compressed_size);
311 else
312 extent_item_size = btrfs_file_extent_calc_inline_size(
313 inline_len);
314
315 ret = __btrfs_drop_extents(trans, root, inode, path,
316 start, aligned_end, NULL,
317 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400318 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400319 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400320 goto out;
321 }
Chris Masonc8b97812008-10-29 14:49:59 -0400322
323 if (isize > actual_end)
324 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000325 ret = insert_inline_extent(trans, path, extent_inserted,
326 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400327 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000328 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400329 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400330 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400331 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400332 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400333 ret = 1;
334 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100335 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400336
Josef Bacikbdc20e62013-02-28 13:23:38 -0500337 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200338 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400339out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800340 /*
341 * Don't forget to free the reserved space, as for inlined extent
342 * it won't count as data extent, free them directly here.
343 * And at reserve time, it's always aligned to page size, so
344 * just free one page here.
345 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800346 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000347 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400348 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400349 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400350}
351
Chris Mason771ed682008-11-06 22:02:51 -0500352struct async_extent {
353 u64 start;
354 u64 ram_size;
355 u64 compressed_size;
356 struct page **pages;
357 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500359 struct list_head list;
360};
361
362struct async_cow {
363 struct inode *inode;
364 struct btrfs_root *root;
365 struct page *locked_page;
366 u64 start;
367 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600368 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500369 struct list_head extents;
370 struct btrfs_work work;
371};
372
373static noinline int add_async_extent(struct async_cow *cow,
374 u64 start, u64 ram_size,
375 u64 compressed_size,
376 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800377 unsigned long nr_pages,
378 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500379{
380 struct async_extent *async_extent;
381
382 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100383 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500384 async_extent->start = start;
385 async_extent->ram_size = ram_size;
386 async_extent->compressed_size = compressed_size;
387 async_extent->pages = pages;
388 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800389 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500390 list_add_tail(&async_extent->list, &cow->extents);
391 return 0;
392}
393
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300394static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800397
398 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400399 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800400 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200401 /* defrag ioctl */
402 if (BTRFS_I(inode)->defrag_compress)
403 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800404 /* bad compression ratios */
405 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
406 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400407 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800408 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200409 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300410 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800411 return 0;
412}
413
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200414static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800415 u64 start, u64 end, u64 num_bytes, u64 small_write)
416{
417 /* If this is a small write inside eof, kick off a defrag */
418 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200419 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800420 btrfs_add_inode_defrag(NULL, inode);
421}
422
Chris Masonc8b97812008-10-29 14:49:59 -0400423/*
Chris Mason771ed682008-11-06 22:02:51 -0500424 * we create compressed extents in two phases. The first
425 * phase compresses a range of pages that have already been
426 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400427 *
Chris Mason771ed682008-11-06 22:02:51 -0500428 * This is done inside an ordered work queue, and the compression
429 * is spread across many cpus. The actual IO submission is step
430 * two, and the ordered work queue takes care of making sure that
431 * happens in the same order things were put onto the queue by
432 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400433 *
Chris Mason771ed682008-11-06 22:02:51 -0500434 * If this code finds it can't get good compression, it puts an
435 * entry onto the work queue to write the uncompressed bytes. This
436 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300437 * are written in the same order that the flusher thread sent them
438 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400439 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100440static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500441 struct page *locked_page,
442 u64 start, u64 end,
443 struct async_cow *async_cow,
444 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400448 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500449 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400450 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400451 struct page **pages = NULL;
452 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400453 unsigned long total_compressed = 0;
454 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400455 int i;
456 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400457 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400458 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400459
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200460 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
461 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400462
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400464again:
465 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300466 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100467 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
468 nr_pages = min_t(unsigned long, nr_pages,
469 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400470
Chris Masonf03d9301f2009-02-04 09:31:06 -0500471 /*
472 * we don't want to send crud past the end of i_size through
473 * compression, that's just a waste of CPU time. So, if the
474 * end of the file is before the start of our current
475 * requested range of bytes, we bail out to the uncompressed
476 * cleanup code that can deal with all of this.
477 *
478 * It isn't really the fastest way to fix things, but this is a
479 * very uncommon corner.
480 */
481 if (actual_end <= start)
482 goto cleanup_and_bail_uncompressed;
483
Chris Masonc8b97812008-10-29 14:49:59 -0400484 total_compressed = actual_end - start;
485
Shilong Wang4bcbb332014-10-07 18:44:35 -0400486 /*
487 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400488 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400489 */
490 if (total_compressed <= blocksize &&
491 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
492 goto cleanup_and_bail_uncompressed;
493
David Sterba069eac72017-02-14 19:36:54 +0100494 total_compressed = min_t(unsigned long, total_compressed,
495 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400496 total_in = 0;
497 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400498
Chris Mason771ed682008-11-06 22:02:51 -0500499 /*
500 * we do compression for mount -o compress and when the
501 * inode has not been flagged as nocompress. This flag can
502 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400503 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300504 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400505 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100506 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800507 if (!pages) {
508 /* just bail out to the uncompressed code */
509 goto cont;
510 }
Chris Mason179e29e2007-11-01 11:28:41 -0400511
David Sterbaeec63c62017-07-17 19:41:31 +0200512 if (BTRFS_I(inode)->defrag_compress)
513 compress_type = BTRFS_I(inode)->defrag_compress;
514 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200515 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800516
Chris Mason4adaa612013-03-26 13:07:00 -0400517 /*
518 * we need to call clear_page_dirty_for_io on each
519 * page in the range. Otherwise applications with the file
520 * mmap'd can wander in and change the page contents while
521 * we are compressing them.
522 *
523 * If the compression fails for any reason, we set the pages
524 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300525 *
526 * Note that the remaining part is redirtied, the start pointer
527 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400528 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300529 if (!redirty) {
530 extent_range_clear_dirty_for_io(inode, start, end);
531 redirty = 1;
532 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200533
534 /* Compression level is applied here and only here */
535 ret = btrfs_compress_pages(
536 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800537 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100538 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100539 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800540 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100541 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400542
543 if (!ret) {
544 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300545 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100546 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400547 char *kaddr;
548
549 /* zero the tail end of the last page, we might be
550 * sending it down to disk
551 */
552 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800553 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300555 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800556 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400557 }
558 will_compress = 1;
559 }
560 }
Li Zefan560f7d72011-09-08 10:22:01 +0800561cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400562 if (start == 0) {
563 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300564 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400565 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500566 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400567 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200568 ret = cow_file_range_inline(inode, start, end, 0,
569 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400570 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500571 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200572 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000573 total_compressed,
574 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400575 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100576 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400577 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400578 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
579 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100580 unsigned long page_error_op;
581
Filipe Mananae6eb4312014-10-10 10:45:12 +0100582 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400583
Chris Mason771ed682008-11-06 22:02:51 -0500584 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 * inline extent creation worked or returned error,
586 * we don't need to create any more async work items.
587 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400588 *
589 * We use DO_ACCOUNTING here because we need the
590 * delalloc_release_metadata to be done _after_ we drop
591 * our outstanding extent for clearing delalloc for this
592 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500593 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800594 extent_clear_unlock_delalloc(inode, start, end, end,
595 NULL, clear_flags,
596 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400597 PAGE_CLEAR_DIRTY |
598 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100599 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400600 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400601 goto free_pages_out;
602 }
603 }
604
605 if (will_compress) {
606 /*
607 * we aren't doing an inline extent round the compressed size
608 * up to a block size boundary so the allocator does sane
609 * things
610 */
Qu Wenruofda28322013-02-26 08:10:22 +0000611 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400612
613 /*
614 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300615 * win, compare the page count read with the blocks on disk,
616 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400617 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300618 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300619 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700620 *num_added += 1;
621
622 /*
623 * The async work queues will take care of doing actual
624 * allocation on disk for these compressed pages, and
625 * will submit them to the elevator.
626 */
Timofey Titovets11708622017-10-03 18:06:01 +0300627 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100628 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700629 compress_type);
630
Timofey Titovets11708622017-10-03 18:06:01 +0300631 if (start + total_in < end) {
632 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700633 pages = NULL;
634 cond_resched();
635 goto again;
636 }
637 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400638 }
639 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700640 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400641 /*
642 * the compression code ran but failed to make things smaller,
643 * free any pages it allocated and our page pointer array
644 */
David Sterba4d3a8002017-02-14 19:04:07 +0100645 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400646 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300647 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400648 }
649 kfree(pages);
650 pages = NULL;
651 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100652 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400653
654 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400655 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200656 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500657 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500658 }
Chris Masonc8b97812008-10-29 14:49:59 -0400659 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500660cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700661 /*
662 * No compression, but we still need to write the pages in the file
663 * we've been given so far. redirty the locked page if it corresponds
664 * to our extent and set things up for the async work queue to run
665 * cow_file_range to do the normal delalloc dance.
666 */
667 if (page_offset(locked_page) >= start &&
668 page_offset(locked_page) <= end)
669 __set_page_dirty_nobuffers(locked_page);
670 /* unlocked later on in the async handlers */
671
672 if (redirty)
673 extent_range_redirty_for_io(inode, start, end);
674 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
675 BTRFS_COMPRESS_NONE);
676 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500677
Filipe Mananac44f6492014-10-09 21:15:44 +0100678 return;
Chris Mason771ed682008-11-06 22:02:51 -0500679
680free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100681 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500682 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300683 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500684 }
Chris Masond3977122009-01-05 21:25:51 -0500685 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500686}
Chris Mason771ed682008-11-06 22:02:51 -0500687
Filipe Manana40ae8372014-10-06 22:14:24 +0100688static void free_async_extent_pages(struct async_extent *async_extent)
689{
690 int i;
691
692 if (!async_extent->pages)
693 return;
694
695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500702}
703
704/*
705 * phase two of compressed writeback. This is the ordered portion
706 * of the code, which only gets called in the order the work was
707 * queued. We walk all the async extents created by compress_file_range
708 * and send them down to the disk.
709 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100710static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct async_cow *async_cow)
712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500714 struct async_extent *async_extent;
715 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500716 struct btrfs_key ins;
717 struct extent_map *em;
718 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500719 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500720 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500721
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500722again:
Chris Masond3977122009-01-05 21:25:51 -0500723 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500724 async_extent = list_entry(async_cow->extents.next,
725 struct async_extent, list);
726 list_del(&async_extent->list);
727
728 io_tree = &BTRFS_I(inode)->io_tree;
729
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500730retry:
Chris Mason771ed682008-11-06 22:02:51 -0500731 /* did the compression code fall back to uncompressed IO? */
732 if (!async_extent->pages) {
733 int page_started = 0;
734 unsigned long nr_written = 0;
735
736 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000737 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100738 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500739
740 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500741 ret = cow_file_range(inode, async_cow->locked_page,
742 async_extent->start,
743 async_extent->start +
744 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800745 async_extent->start +
746 async_extent->ram_size - 1,
747 &page_started, &nr_written, 0,
748 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500749
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100750 /* JDM XXX */
751
Chris Mason771ed682008-11-06 22:02:51 -0500752 /*
753 * if page_started, cow_file_range inserted an
754 * inline extent and took care of all the unlocking
755 * and IO for us. Otherwise, we need to submit
756 * all those pages down to the drive.
757 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500758 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200759 extent_write_locked_range(inode,
760 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500761 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500762 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500763 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500764 else if (ret)
765 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500766 kfree(async_extent);
767 cond_resched();
768 continue;
769 }
770
771 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100772 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500773
Wang Xiaoguang18513092016-07-25 15:51:40 +0800774 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500775 async_extent->compressed_size,
776 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800777 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500778 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100779 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500780
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400781 if (ret == -ENOSPC) {
782 unlock_extent(io_tree, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800785
786 /*
787 * we need to redirty the pages if we decide to
788 * fallback to uncompressed IO, otherwise we
789 * will not submit these pages down to lower
790 * layers.
791 */
792 extent_range_redirty_for_io(inode,
793 async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1);
796
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400798 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500800 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000801 /*
802 * here we're doing allocation and writeback of the
803 * compressed pages
804 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800805 em = create_io_em(inode, async_extent->start,
806 async_extent->ram_size, /* len */
807 async_extent->start, /* orig_start */
808 ins.objectid, /* block_start */
809 ins.offset, /* block_len */
810 ins.offset, /* orig_block_len */
811 async_extent->ram_size, /* ram_bytes */
812 async_extent->compress_type,
813 BTRFS_ORDERED_COMPRESSED);
814 if (IS_ERR(em))
815 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800817 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500818
Li Zefan261507a02010-12-17 14:21:50 +0800819 ret = btrfs_add_ordered_extent_compress(inode,
820 async_extent->start,
821 ins.objectid,
822 async_extent->ram_size,
823 ins.offset,
824 BTRFS_ORDERED_COMPRESSED,
825 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100826 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200827 btrfs_drop_extent_cache(BTRFS_I(inode),
828 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 async_extent->start +
830 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500831 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100832 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400833 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500834
Chris Mason771ed682008-11-06 22:02:51 -0500835 /*
836 * clear dirty, set writeback and unlock the pages.
837 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400838 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400839 async_extent->start +
840 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800841 async_extent->start +
842 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400843 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
844 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400845 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200846 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500847 async_extent->start,
848 async_extent->ram_size,
849 ins.objectid,
850 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600851 async_extent->nr_pages,
852 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100853 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
854 struct page *p = async_extent->pages[0];
855 const u64 start = async_extent->start;
856 const u64 end = start + async_extent->ram_size - 1;
857
858 p->mapping = inode->i_mapping;
859 tree->ops->writepage_end_io_hook(p, start, end,
860 NULL, 0);
861 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800862 extent_clear_unlock_delalloc(inode, start, end, end,
863 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 PAGE_END_WRITEBACK |
865 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100866 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 }
Chris Mason771ed682008-11-06 22:02:51 -0500868 alloc_hint = ins.objectid + ins.offset;
869 kfree(async_extent);
870 cond_resched();
871 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100872 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500873out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400874 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400875 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400877 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500878 async_extent->start +
879 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800880 async_extent->start +
881 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400882 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100883 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400884 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
885 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100886 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
887 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100888 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500890 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500891}
892
Josef Bacik4b46fce2010-05-23 11:00:55 -0400893static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
894 u64 num_bytes)
895{
896 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
897 struct extent_map *em;
898 u64 alloc_hint = 0;
899
900 read_lock(&em_tree->lock);
901 em = search_extent_mapping(em_tree, start, num_bytes);
902 if (em) {
903 /*
904 * if block start isn't an actual block number then find the
905 * first block in this inode and use that as a hint. If that
906 * block is also bogus then just don't worry about it.
907 */
908 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
909 free_extent_map(em);
910 em = search_extent_mapping(em_tree, 0, 0);
911 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
912 alloc_hint = em->block_start;
913 if (em)
914 free_extent_map(em);
915 } else {
916 alloc_hint = em->block_start;
917 free_extent_map(em);
918 }
919 }
920 read_unlock(&em_tree->lock);
921
922 return alloc_hint;
923}
924
Chris Mason771ed682008-11-06 22:02:51 -0500925/*
926 * when extent_io.c finds a delayed allocation range in the file,
927 * the call backs end up in this code. The basic idea is to
928 * allocate extents on disk for the range, and create ordered data structs
929 * in ram to track those extents.
930 *
931 * locked_page is the page that writepage had locked already. We use
932 * it to make sure we don't do extra locks or unlocks.
933 *
934 * *page_started is set to one if we unlock locked_page and do everything
935 * required to start IO on it. It may be clean and already done with
936 * IO when we return.
937 */
Josef Bacik00361582013-08-14 14:02:47 -0400938static noinline int cow_file_range(struct inode *inode,
939 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800940 u64 start, u64 end, u64 delalloc_end,
941 int *page_started, unsigned long *nr_written,
942 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500943{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400945 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500946 u64 alloc_hint = 0;
947 u64 num_bytes;
948 unsigned long ram_size;
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);
Anand Jain566b1762018-02-15 18:07:59 +0800966 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
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 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200972 ret = cow_file_range_inline(inode, start, end, 0,
973 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500974 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400975 /*
976 * We use DO_ACCOUNTING here because we need the
977 * delalloc_release_metadata to be run _after_ we drop
978 * our outstanding extent for clearing delalloc for this
979 * range.
980 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800981 extent_clear_unlock_delalloc(inode, start, end,
982 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400983 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400984 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
985 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400986 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
987 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500988 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300989 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500990 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500991 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100992 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100993 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500994 }
995 }
Chris Masonc8b97812008-10-29 14:49:59 -0400996
Josef Bacik4b46fce2010-05-23 11:00:55 -0400997 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200998 btrfs_drop_extent_cache(BTRFS_I(inode), start,
999 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001000
Anand Jain3752d222018-02-15 12:29:38 +08001001 while (num_bytes > 0) {
1002 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001003 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001004 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001005 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001006 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001008 cur_alloc_size = ins.offset;
1009 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001010
Chris Mason771ed682008-11-06 22:02:51 -05001011 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001012 em = create_io_em(inode, start, ins.offset, /* len */
1013 start, /* orig_start */
1014 ins.objectid, /* block_start */
1015 ins.offset, /* block_len */
1016 ins.offset, /* orig_block_len */
1017 ram_size, /* ram_bytes */
1018 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001019 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001020 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001021 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001022 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023
Chris Masone6dcd2d2008-07-17 12:53:50 -04001024 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001025 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001026 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001027 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001028
Yan Zheng17d217f2008-12-12 10:03:38 -05001029 if (root->root_key.objectid ==
1030 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1031 ret = btrfs_reloc_clone_csums(inode, start,
1032 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001033 /*
1034 * Only drop cache here, and process as normal.
1035 *
1036 * We must not allow extent_clear_unlock_delalloc()
1037 * at out_unlock label to free meta of this ordered
1038 * extent, as its meta should be freed by
1039 * btrfs_finish_ordered_io().
1040 *
1041 * So we must continue until @start is increased to
1042 * skip current ordered extent.
1043 */
Josef Bacik00361582013-08-14 14:02:47 -04001044 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001045 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1046 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 }
1048
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001049 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001050
Chris Masonc8b97812008-10-29 14:49:59 -04001051 /* we're not doing compressed IO, don't unlock the first
1052 * page (which the caller expects to stay locked), don't
1053 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001054 *
1055 * Do set the Private2 bit so we know this page was properly
1056 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001057 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001058 page_ops = unlock ? PAGE_UNLOCK : 0;
1059 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001060
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001062 start + ram_size - 1,
1063 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001064 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001065 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001066 if (num_bytes < cur_alloc_size)
1067 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001068 else
Anand Jain3752d222018-02-15 12:29:38 +08001069 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001070 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 Masonb888db2b2007-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,
Liu Bof82b7352017-10-23 23:18:16 -06001181 unsigned long *nr_written,
1182 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001185 struct async_cow *async_cow;
1186 struct btrfs_root *root = BTRFS_I(inode)->root;
1187 unsigned long nr_pages;
1188 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001189
Chris Masona3429ab2009-10-08 12:30:20 -04001190 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001191 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001192 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001193 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001195 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001196 async_cow->root = root;
1197 async_cow->locked_page = locked_page;
1198 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001199 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001200
Wang Shilongf79707b2014-07-17 11:44:09 +08001201 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001202 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001203 cur_end = end;
1204 else
Byongho Leeee221842015-12-15 01:42:10 +09001205 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001206
1207 async_cow->end = cur_end;
1208 INIT_LIST_HEAD(&async_cow->extents);
1209
Liu Bo9e0af232014-08-15 23:36:53 +08001210 btrfs_init_work(&async_cow->work,
1211 btrfs_delalloc_helper,
1212 async_cow_start, async_cow_submit,
1213 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001215 nr_pages = (cur_end - start + PAGE_SIZE) >>
1216 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001218
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001219 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001220
Chris Mason771ed682008-11-06 22:02:51 -05001221 *nr_written += nr_pages;
1222 start = cur_end + 1;
1223 }
1224 *page_started = 1;
1225 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001226}
1227
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001228static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001229 u64 bytenr, u64 num_bytes)
1230{
1231 int ret;
1232 struct btrfs_ordered_sum *sums;
1233 LIST_HEAD(list);
1234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001235 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001236 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001237 if (ret == 0 && list_empty(&list))
1238 return 0;
1239
1240 while (!list_empty(&list)) {
1241 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1242 list_del(&sums->list);
1243 kfree(sums);
1244 }
Liu Bo58113752018-01-31 17:09:13 -07001245 if (ret < 0)
1246 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001247 return 1;
1248}
1249
Chris Masond352ac62008-09-29 15:18:18 -04001250/*
1251 * when nowcow writeback call back. This checks for snapshots or COW copies
1252 * of the extents that exist in the file, and COWs the file as required.
1253 *
1254 * If no cow copies or snapshots exist, we write directly to the existing
1255 * blocks on disk
1256 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001257static noinline int run_delalloc_nocow(struct inode *inode,
1258 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001259 u64 start, u64 end, int *page_started, int force,
1260 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001261{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001263 struct btrfs_root *root = BTRFS_I(inode)->root;
1264 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001265 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001267 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001268 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 u64 cow_start;
1270 u64 cur_offset;
1271 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001272 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 u64 disk_bytenr;
1274 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001275 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001276 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001279 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 int nocow;
1281 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001282 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001283 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
1285 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001286 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001287 extent_clear_unlock_delalloc(inode, start, end, end,
1288 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001289 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001290 EXTENT_DO_ACCOUNTING |
1291 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001292 PAGE_CLEAR_DIRTY |
1293 PAGE_SET_WRITEBACK |
1294 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001295 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001296 }
Li Zefan82d59022011-04-20 10:33:24 +08001297
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001298 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 cow_start = (u64)-1;
1301 cur_offset = start;
1302 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001303 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001305 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1308 leaf = path->nodes[0];
1309 btrfs_item_key_to_cpu(leaf, &found_key,
1310 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001311 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 found_key.type == BTRFS_EXTENT_DATA_KEY)
1313 path->slots[0]--;
1314 }
1315 check_prev = 0;
1316next_slot:
1317 leaf = path->nodes[0];
1318 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1319 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001320 if (ret < 0) {
1321 if (cow_start != (u64)-1)
1322 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001324 }
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 Bo58113752018-01-31 17:09:13 -07001379 ret = btrfs_cross_ref_exist(root, ino,
1380 found_key.offset -
1381 extent_offset, disk_bytenr);
1382 if (ret) {
1383 /*
1384 * ret could be -EIO if the above fails to read
1385 * metadata.
1386 */
1387 if (ret < 0) {
1388 if (cow_start != (u64)-1)
1389 cur_offset = cow_start;
1390 goto error;
1391 }
1392
1393 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001395 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 disk_bytenr += cur_offset - found_key.offset;
1398 num_bytes = min(end + 1, extent_end) - cur_offset;
1399 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001400 * if there are pending snapshots for this root,
1401 * we fall into common COW way.
1402 */
1403 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001404 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 if (!err)
1406 goto out_check;
1407 }
1408 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001409 * force cow if csum exists in the range.
1410 * this ensure that csum for a given extent are
1411 * either valid or do not exist.
1412 */
Liu Bo58113752018-01-31 17:09:13 -07001413 ret = csum_exist_in_range(fs_info, disk_bytenr,
1414 num_bytes);
1415 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001416 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001418
1419 /*
1420 * ret could be -EIO if the above fails to read
1421 * metadata.
1422 */
1423 if (ret < 0) {
1424 if (cow_start != (u64)-1)
1425 cur_offset = cow_start;
1426 goto error;
1427 }
1428 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001429 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001430 }
1431 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1432 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001433 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001434 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001435 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 nocow = 1;
1437 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1438 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001439 btrfs_file_extent_inline_len(leaf,
1440 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001441 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001442 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 } else {
1444 BUG_ON(1);
1445 }
1446out_check:
1447 if (extent_end <= start) {
1448 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001449 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001450 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001451 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001452 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001453 goto next_slot;
1454 }
1455 if (!nocow) {
1456 if (cow_start == (u64)-1)
1457 cow_start = cur_offset;
1458 cur_offset = extent_end;
1459 if (cur_offset > end)
1460 break;
1461 path->slots[0]++;
1462 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001463 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001464
David Sterbab3b4aa72011-04-21 01:20:15 +02001465 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001467 ret = cow_file_range(inode, locked_page,
1468 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001469 end, page_started, nr_written, 1,
1470 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001471 if (ret) {
1472 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001473 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001474 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001475 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001476 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001477 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001478 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001479 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001480 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001481
Yan Zhengd899e052008-10-30 14:25:28 -04001482 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001483 u64 orig_start = found_key.offset - extent_offset;
1484
1485 em = create_io_em(inode, cur_offset, num_bytes,
1486 orig_start,
1487 disk_bytenr, /* block_start */
1488 num_bytes, /* block_len */
1489 disk_num_bytes, /* orig_block_len */
1490 ram_bytes, BTRFS_COMPRESS_NONE,
1491 BTRFS_ORDERED_PREALLOC);
1492 if (IS_ERR(em)) {
1493 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001494 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001495 if (nocow)
1496 btrfs_dec_nocow_writers(fs_info,
1497 disk_bytenr);
1498 ret = PTR_ERR(em);
1499 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001500 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001501 free_extent_map(em);
1502 }
1503
1504 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001505 type = BTRFS_ORDERED_PREALLOC;
1506 } else {
1507 type = BTRFS_ORDERED_NOCOW;
1508 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001509
1510 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001511 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001512 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001513 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001514 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001515
Yan, Zhengefa56462010-05-16 10:49:59 -04001516 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001517 BTRFS_DATA_RELOC_TREE_OBJECTID)
1518 /*
1519 * Error handled later, as we must prevent
1520 * extent_clear_unlock_delalloc() in error handler
1521 * from freeing metadata of created ordered extent.
1522 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001523 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1524 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001525
Josef Bacikc2790a22013-07-29 11:20:47 -04001526 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001527 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001528 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001529 EXTENT_DELALLOC |
1530 EXTENT_CLEAR_DATA_RESV,
1531 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1532
Wang Shilonge9894fd2014-03-27 11:12:25 +08001533 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001534 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001536
1537 /*
1538 * btrfs_reloc_clone_csums() error, now we're OK to call error
1539 * handler, as metadata for created ordered extent will only
1540 * be freed by btrfs_finish_ordered_io().
1541 */
1542 if (ret)
1543 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 if (cur_offset > end)
1545 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001546 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001547 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001548
Josef Bacik17ca04a2012-05-31 15:58:55 -04001549 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001550 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001551 cur_offset = end;
1552 }
1553
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001555 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1556 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001557 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001558 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001559 }
1560
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001561error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001562 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001563 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001564 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001565 EXTENT_DELALLOC | EXTENT_DEFRAG |
1566 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1567 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001568 PAGE_SET_WRITEBACK |
1569 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001570 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001571 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001572}
1573
Wang Shilong47059d92014-07-03 18:22:07 +08001574static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1575{
1576
1577 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1578 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1579 return 0;
1580
1581 /*
1582 * @defrag_bytes is a hint value, no spinlock held here,
1583 * if is not zero, it means the file is defragging.
1584 * Force cow if given extent needs to be defragged.
1585 */
1586 if (BTRFS_I(inode)->defrag_bytes &&
1587 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1588 EXTENT_DEFRAG, 0, NULL))
1589 return 1;
1590
1591 return 0;
1592}
1593
Chris Masond352ac62008-09-29 15:18:18 -04001594/*
1595 * extent_io.c call back to do delayed allocation processing
1596 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001597static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001598 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001599 unsigned long *nr_written,
1600 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001601{
Josef Bacikc6100a42017-05-05 11:57:13 -04001602 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001603 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001604 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001605 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001606
Wang Shilong47059d92014-07-03 18:22:07 +08001607 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001608 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001609 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001610 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001611 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001612 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001613 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001614 ret = cow_file_range(inode, locked_page, start, end, end,
1615 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001616 } else {
1617 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1618 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001619 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001620 page_started, nr_written,
1621 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001622 }
Qu Wenruo524272602017-03-08 10:25:52 +08001623 if (ret)
1624 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001625 return ret;
1626}
1627
Josef Bacikc6100a42017-05-05 11:57:13 -04001628static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001629 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001630{
Josef Bacikc6100a42017-05-05 11:57:13 -04001631 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001632 u64 size;
1633
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001634 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001636 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001637
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 size = orig->end - orig->start + 1;
1639 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001640 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 u64 new_size;
1642
1643 /*
Josef Bacikba117212015-03-13 15:01:24 -04001644 * See the explanation in btrfs_merge_extent_hook, the same
1645 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001646 */
1647 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001648 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001649 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001650 num_extents += count_max_extents(new_size);
1651 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652 return;
1653 }
1654
Josef Bacik9e0baf62011-07-15 15:16:44 +00001655 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001656 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001657 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658}
1659
1660/*
1661 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1662 * extents so we can keep track of new extents that are just merged onto old
1663 * extents, such as when we are doing sequential writes, so we can properly
1664 * account for the metadata space we'll need.
1665 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001666static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001667 struct extent_state *new,
1668 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001669{
Josef Bacikc6100a42017-05-05 11:57:13 -04001670 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001672 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674 /* not delalloc, ignore it */
1675 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001676 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001677
Josef Bacik8461a3d2015-03-13 15:12:08 -04001678 if (new->start > other->start)
1679 new_size = new->end - other->start + 1;
1680 else
1681 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001682
1683 /* we're not bigger than the max, unreserve the space and go */
1684 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1685 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001686 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001687 spin_unlock(&BTRFS_I(inode)->lock);
1688 return;
1689 }
1690
1691 /*
Josef Bacikba117212015-03-13 15:01:24 -04001692 * We have to add up either side to figure out how many extents were
1693 * accounted for before we merged into one big extent. If the number of
1694 * extents we accounted for is <= the amount we need for the new range
1695 * then we can return, otherwise drop. Think of it like this
1696 *
1697 * [ 4k][MAX_SIZE]
1698 *
1699 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1700 * need 2 outstanding extents, on one side we have 1 and the other side
1701 * we have 1 so they are == and we can return. But in this case
1702 *
1703 * [MAX_SIZE+4k][MAX_SIZE+4k]
1704 *
1705 * Each range on their own accounts for 2 extents, but merged together
1706 * they are only 3 extents worth of accounting, so we need to drop in
1707 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001708 */
Josef Bacikba117212015-03-13 15:01:24 -04001709 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001710 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001711 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001712 num_extents += count_max_extents(old_size);
1713 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001714 return;
1715
Josef Bacik9e0baf62011-07-15 15:16:44 +00001716 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001717 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001718 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001719}
1720
Miao Xieeb73c1b2013-05-15 07:48:22 +00001721static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1722 struct inode *inode)
1723{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1725
Miao Xieeb73c1b2013-05-15 07:48:22 +00001726 spin_lock(&root->delalloc_lock);
1727 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1728 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1729 &root->delalloc_inodes);
1730 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1731 &BTRFS_I(inode)->runtime_flags);
1732 root->nr_delalloc_inodes++;
1733 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 BUG_ON(!list_empty(&root->delalloc_root));
1736 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 &fs_info->delalloc_roots);
1738 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 }
1740 }
1741 spin_unlock(&root->delalloc_lock);
1742}
1743
1744static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001745 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001747 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001750 if (!list_empty(&inode->delalloc_inodes)) {
1751 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001753 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001754 root->nr_delalloc_inodes--;
1755 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001756 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001757 BUG_ON(list_empty(&root->delalloc_root));
1758 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001759 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001760 }
1761 }
1762 spin_unlock(&root->delalloc_lock);
1763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
1766 * extent_io.c set_bit_hook, used to track delayed allocation
1767 * bytes in this file, and to maintain the list of inodes that
1768 * have pending delalloc work to be done.
1769 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001770static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001771 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001772{
Josef Bacikc6100a42017-05-05 11:57:13 -04001773 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001774
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001775 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1776
Wang Shilong47059d92014-07-03 18:22:07 +08001777 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1778 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001779 /*
1780 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001781 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001782 * bit, which is only set or cleared with irqs on
1783 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001784 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001785 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001786 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001787 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001788 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001789
Josef Bacik8b62f872017-10-19 14:15:55 -04001790 spin_lock(&BTRFS_I(inode)->lock);
1791 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1792 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001793
Josef Bacik6a3891c2015-03-16 17:38:52 -04001794 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001795 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001796 return;
1797
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001798 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1799 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001800 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001801 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001802 if (*bits & EXTENT_DEFRAG)
1803 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001804 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001805 &BTRFS_I(inode)->runtime_flags))
1806 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001807 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001808 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001809
1810 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1811 (*bits & EXTENT_DELALLOC_NEW)) {
1812 spin_lock(&BTRFS_I(inode)->lock);
1813 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1814 state->start;
1815 spin_unlock(&BTRFS_I(inode)->lock);
1816 }
Chris Mason291d6732008-01-29 15:55:23 -05001817}
1818
Chris Masond352ac62008-09-29 15:18:18 -04001819/*
1820 * extent_io.c clear_bit_hook, see set_bit_hook for why
1821 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001822static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001823 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001824 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001825{
Josef Bacikc6100a42017-05-05 11:57:13 -04001826 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001827 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001828 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001829 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001830
Filipe Manana4a4b9642017-07-27 19:52:55 +01001831 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1832 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001833 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001834 spin_unlock(&inode->lock);
1835 }
Wang Shilong47059d92014-07-03 18:22:07 +08001836
Chris Mason75eff682008-12-15 15:54:40 -05001837 /*
1838 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001839 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001840 * bit, which is only set or cleared with irqs on
1841 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001842 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001843 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001844 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001845
Josef Bacik8b62f872017-10-19 14:15:55 -04001846 spin_lock(&inode->lock);
1847 btrfs_mod_outstanding_extents(inode, -num_extents);
1848 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001849
Josef Bacikb6d08f02013-09-27 14:57:43 -04001850 /*
1851 * We don't reserve metadata space for space cache inodes so we
1852 * don't need to call dellalloc_release_metadata if there is an
1853 * error.
1854 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001855 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001856 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001857 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001858
Josef Bacik6a3891c2015-03-16 17:38:52 -04001859 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001860 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001861 return;
1862
Filipe Mananaa315e682017-03-06 23:04:20 +00001863 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1864 do_list && !(state->state & EXTENT_NORESERVE) &&
1865 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001866 btrfs_free_reserved_data_space_noquota(
1867 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001868 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001869
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001870 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1871 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001872 spin_lock(&inode->lock);
1873 inode->delalloc_bytes -= len;
1874 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001875 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001876 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001877 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001878 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001879 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001880
1881 if ((state->state & EXTENT_DELALLOC_NEW) &&
1882 (*bits & EXTENT_DELALLOC_NEW)) {
1883 spin_lock(&inode->lock);
1884 ASSERT(inode->new_delalloc_bytes >= len);
1885 inode->new_delalloc_bytes -= len;
1886 spin_unlock(&inode->lock);
1887 }
Chris Mason291d6732008-01-29 15:55:23 -05001888}
1889
Chris Masond352ac62008-09-29 15:18:18 -04001890/*
1891 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1892 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001893 *
1894 * return 1 if page cannot be merged to bio
1895 * return 0 if page can be merged to bio
1896 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001897 */
Mike Christie81a75f672016-06-05 14:31:54 -05001898int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001899 size_t size, struct bio *bio,
1900 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001901{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001902 struct inode *inode = page->mapping->host;
1903 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001904 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001905 u64 length = 0;
1906 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001907 int ret;
1908
Chris Mason771ed682008-11-06 22:02:51 -05001909 if (bio_flags & EXTENT_BIO_COMPRESSED)
1910 return 0;
1911
Kent Overstreet4f024f32013-10-11 15:44:27 -07001912 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001913 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001914 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1915 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001916 if (ret < 0)
1917 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001918 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001919 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001920 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001921}
1922
Chris Masond352ac62008-09-29 15:18:18 -04001923/*
1924 * in order to insert checksums into the metadata in large chunks,
1925 * we wait until bio submission time. All the pages in the bio are
1926 * checksummed and sums are attached onto the ordered extent record.
1927 *
1928 * At IO completion time the cums attached on the ordered extent record
1929 * are inserted into the btree
1930 */
David Sterbad0ee3932018-03-08 14:35:48 +01001931static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001932 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001933{
Josef Bacikc6100a42017-05-05 11:57:13 -04001934 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001935 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001936
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001937 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001938 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001939 return 0;
1940}
Chris Masone0156402008-04-16 11:15:20 -04001941
Chris Mason4a69a412008-11-06 22:03:00 -05001942/*
1943 * in order to insert checksums into the metadata in large chunks,
1944 * we wait until bio submission time. All the pages in the bio are
1945 * checksummed and sums are attached onto the ordered extent record.
1946 *
1947 * At IO completion time the cums attached on the ordered extent record
1948 * are inserted into the btree
1949 */
David Sterbad0ee3932018-03-08 14:35:48 +01001950static blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001951 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001952{
Josef Bacikc6100a42017-05-05 11:57:13 -04001953 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001955 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001956
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001957 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001958 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001959 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001960 bio_endio(bio);
1961 }
Stefan Behrens61891922012-11-05 18:51:52 +01001962 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001963}
1964
Chris Masond352ac62008-09-29 15:18:18 -04001965/*
Chris Masoncad321a2008-12-17 14:51:42 -05001966 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001967 * on write, or reading the csums from the tree before a read.
1968 *
1969 * Rules about async/sync submit,
1970 * a) read: sync submit
1971 *
1972 * b) write without checksum: sync submit
1973 *
1974 * c) write with checksum:
1975 * c-1) if bio is issued by fsync: sync submit
1976 * (sync_writers != 0)
1977 *
1978 * c-2) if root is reloc root: sync submit
1979 * (only in case of buffered IO)
1980 *
1981 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001982 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001983static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001984 int mirror_num, unsigned long bio_flags,
1985 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001986{
Josef Bacikc6100a42017-05-05 11:57:13 -04001987 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001988 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001989 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301990 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001991 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001992 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001993 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001994
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001995 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001996
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001997 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301998 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001999
Mike Christie37226b22016-06-05 14:31:52 -05002000 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002001 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002002 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002003 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002004
Chris Masond20f7042008-12-08 16:58:54 -05002005 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002006 ret = btrfs_submit_compressed_read(inode, bio,
2007 mirror_num,
2008 bio_flags);
2009 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002010 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002011 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002012 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002013 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002014 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002015 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002016 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002017 /* csum items have already been cloned */
2018 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2019 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002020 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002021 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2022 bio_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01002023 btrfs_submit_bio_start,
2024 btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002025 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002026 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002027 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002028 if (ret)
2029 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002030 }
2031
Chris Mason0b86a832008-03-24 15:01:56 -04002032mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002033 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002034
2035out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002036 if (ret) {
2037 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002038 bio_endio(bio);
2039 }
Stefan Behrens61891922012-11-05 18:51:52 +01002040 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002041}
Chris Mason6885f302008-02-20 16:11:05 -05002042
Chris Masond352ac62008-09-29 15:18:18 -04002043/*
2044 * given a list of ordered sums record them in the inode. This happens
2045 * at IO completion time based on sums calculated at bio submission time.
2046 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002047static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002048 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002049{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002050 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002051 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002052
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002053 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002054 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002055 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002056 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002057 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002058 if (ret)
2059 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002060 }
2061 return 0;
2062}
2063
Josef Bacik2ac55d42010-02-03 19:33:23 +00002064int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002065 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002066 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002067{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002068 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002069 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002070 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002071}
2072
Chris Masond352ac62008-09-29 15:18:18 -04002073/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002074struct btrfs_writepage_fixup {
2075 struct page *page;
2076 struct btrfs_work work;
2077};
2078
Christoph Hellwigb2950862008-12-02 09:54:17 -05002079static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002080{
2081 struct btrfs_writepage_fixup *fixup;
2082 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002083 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002084 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002085 struct page *page;
2086 struct inode *inode;
2087 u64 page_start;
2088 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002089 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002090
2091 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2092 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002093again:
Chris Mason247e7432008-07-17 12:53:51 -04002094 lock_page(page);
2095 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2096 ClearPageChecked(page);
2097 goto out_page;
2098 }
2099
2100 inode = page->mapping->host;
2101 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002102 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002103
David Sterbaff13db42015-12-03 14:30:40 +01002104 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002105 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002106
2107 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002108 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002109 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002110
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002111 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002112 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002113 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002114 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002115 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002116 unlock_page(page);
2117 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002118 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002119 goto again;
2120 }
Chris Mason247e7432008-07-17 12:53:51 -04002121
Qu Wenruo364ecf32017-02-27 15:10:38 +08002122 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002123 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002124 if (ret) {
2125 mapping_set_error(page->mapping, ret);
2126 end_extent_writepage(page, ret, page_start, page_end);
2127 ClearPageChecked(page);
2128 goto out;
2129 }
2130
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002131 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2132 &cached_state, 0);
2133 if (ret) {
2134 mapping_set_error(page->mapping, ret);
2135 end_extent_writepage(page, ret, page_start, page_end);
2136 ClearPageChecked(page);
2137 goto out;
2138 }
2139
Chris Mason247e7432008-07-17 12:53:51 -04002140 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002141 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002142 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002143out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002144 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002145 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002146out_page:
2147 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002148 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002149 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002150 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002151}
2152
2153/*
2154 * There are a few paths in the higher layers of the kernel that directly
2155 * set the page dirty bit without asking the filesystem if it is a
2156 * good idea. This causes problems because we want to make sure COW
2157 * properly happens and the data=ordered rules are followed.
2158 *
Chris Masonc8b97812008-10-29 14:49:59 -04002159 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002160 * hasn't been properly setup for IO. We kick off an async process
2161 * to fix it up. The async helper will wait for ordered extents, set
2162 * the delalloc bit and make it safe to write the page.
2163 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002164static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002165{
2166 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002168 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002169
Chris Mason8b62b722009-09-02 16:53:46 -04002170 /* this page is properly in the ordered list */
2171 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002172 return 0;
2173
2174 if (PageChecked(page))
2175 return -EAGAIN;
2176
2177 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2178 if (!fixup)
2179 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002180
Chris Mason247e7432008-07-17 12:53:51 -04002181 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002182 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002183 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2184 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002185 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002186 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002187 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002188}
2189
Yan Zhengd899e052008-10-30 14:25:28 -04002190static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2191 struct inode *inode, u64 file_pos,
2192 u64 disk_bytenr, u64 disk_num_bytes,
2193 u64 num_bytes, u64 ram_bytes,
2194 u8 compression, u8 encryption,
2195 u16 other_encoding, int extent_type)
2196{
2197 struct btrfs_root *root = BTRFS_I(inode)->root;
2198 struct btrfs_file_extent_item *fi;
2199 struct btrfs_path *path;
2200 struct extent_buffer *leaf;
2201 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002202 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002203 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002204 int ret;
2205
2206 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002207 if (!path)
2208 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002209
Chris Masona1ed8352009-09-11 12:27:37 -04002210 /*
2211 * we may be replacing one extent in the tree with another.
2212 * The new extent is pinned in the extent map, and we don't want
2213 * to drop it from the cache until it is completely in the btree.
2214 *
2215 * So, tell btrfs_drop_extents to leave this extent in the cache.
2216 * the caller is expected to unpin it and allow it to be merged
2217 * with the others.
2218 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002219 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2220 file_pos + num_bytes, NULL, 0,
2221 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002222 if (ret)
2223 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002224
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002225 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002226 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002227 ins.offset = file_pos;
2228 ins.type = BTRFS_EXTENT_DATA_KEY;
2229
2230 path->leave_spinning = 1;
2231 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2232 sizeof(*fi));
2233 if (ret)
2234 goto out;
2235 }
Yan Zhengd899e052008-10-30 14:25:28 -04002236 leaf = path->nodes[0];
2237 fi = btrfs_item_ptr(leaf, path->slots[0],
2238 struct btrfs_file_extent_item);
2239 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2240 btrfs_set_file_extent_type(leaf, fi, extent_type);
2241 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2242 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2243 btrfs_set_file_extent_offset(leaf, fi, 0);
2244 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2245 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2246 btrfs_set_file_extent_compression(leaf, fi, compression);
2247 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2248 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002249
Yan Zhengd899e052008-10-30 14:25:28 -04002250 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002251 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002252
2253 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002254
2255 ins.objectid = disk_bytenr;
2256 ins.offset = disk_num_bytes;
2257 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002258
Qu Wenruo297d7502015-09-08 17:08:37 +08002259 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002260 * Release the reserved range from inode dirty range map, as it is
2261 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002262 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002263 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2264 if (ret < 0)
2265 goto out;
2266 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002267 ret = btrfs_alloc_reserved_file_extent(trans, root,
2268 btrfs_ino(BTRFS_I(inode)),
2269 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002270out:
Yan Zhengd899e052008-10-30 14:25:28 -04002271 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002272
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002273 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002274}
2275
Liu Bo38c227d2013-01-29 03:18:40 +00002276/* snapshot-aware defrag */
2277struct sa_defrag_extent_backref {
2278 struct rb_node node;
2279 struct old_sa_defrag_extent *old;
2280 u64 root_id;
2281 u64 inum;
2282 u64 file_pos;
2283 u64 extent_offset;
2284 u64 num_bytes;
2285 u64 generation;
2286};
2287
2288struct old_sa_defrag_extent {
2289 struct list_head list;
2290 struct new_sa_defrag_extent *new;
2291
2292 u64 extent_offset;
2293 u64 bytenr;
2294 u64 offset;
2295 u64 len;
2296 int count;
2297};
2298
2299struct new_sa_defrag_extent {
2300 struct rb_root root;
2301 struct list_head head;
2302 struct btrfs_path *path;
2303 struct inode *inode;
2304 u64 file_pos;
2305 u64 len;
2306 u64 bytenr;
2307 u64 disk_len;
2308 u8 compress_type;
2309};
2310
2311static int backref_comp(struct sa_defrag_extent_backref *b1,
2312 struct sa_defrag_extent_backref *b2)
2313{
2314 if (b1->root_id < b2->root_id)
2315 return -1;
2316 else if (b1->root_id > b2->root_id)
2317 return 1;
2318
2319 if (b1->inum < b2->inum)
2320 return -1;
2321 else if (b1->inum > b2->inum)
2322 return 1;
2323
2324 if (b1->file_pos < b2->file_pos)
2325 return -1;
2326 else if (b1->file_pos > b2->file_pos)
2327 return 1;
2328
2329 /*
2330 * [------------------------------] ===> (a range of space)
2331 * |<--->| |<---->| =============> (fs/file tree A)
2332 * |<---------------------------->| ===> (fs/file tree B)
2333 *
2334 * A range of space can refer to two file extents in one tree while
2335 * refer to only one file extent in another tree.
2336 *
2337 * So we may process a disk offset more than one time(two extents in A)
2338 * and locate at the same extent(one extent in B), then insert two same
2339 * backrefs(both refer to the extent in B).
2340 */
2341 return 0;
2342}
2343
2344static void backref_insert(struct rb_root *root,
2345 struct sa_defrag_extent_backref *backref)
2346{
2347 struct rb_node **p = &root->rb_node;
2348 struct rb_node *parent = NULL;
2349 struct sa_defrag_extent_backref *entry;
2350 int ret;
2351
2352 while (*p) {
2353 parent = *p;
2354 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2355
2356 ret = backref_comp(backref, entry);
2357 if (ret < 0)
2358 p = &(*p)->rb_left;
2359 else
2360 p = &(*p)->rb_right;
2361 }
2362
2363 rb_link_node(&backref->node, parent, p);
2364 rb_insert_color(&backref->node, root);
2365}
2366
2367/*
2368 * Note the backref might has changed, and in this case we just return 0.
2369 */
2370static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2371 void *ctx)
2372{
2373 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002374 struct old_sa_defrag_extent *old = ctx;
2375 struct new_sa_defrag_extent *new = old->new;
2376 struct btrfs_path *path = new->path;
2377 struct btrfs_key key;
2378 struct btrfs_root *root;
2379 struct sa_defrag_extent_backref *backref;
2380 struct extent_buffer *leaf;
2381 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002383 int slot;
2384 int ret;
2385 u64 extent_offset;
2386 u64 num_bytes;
2387
2388 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002389 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002390 return 0;
2391
2392 key.objectid = root_id;
2393 key.type = BTRFS_ROOT_ITEM_KEY;
2394 key.offset = (u64)-1;
2395
Liu Bo38c227d2013-01-29 03:18:40 +00002396 root = btrfs_read_fs_root_no_name(fs_info, &key);
2397 if (IS_ERR(root)) {
2398 if (PTR_ERR(root) == -ENOENT)
2399 return 0;
2400 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002401 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002402 inum, offset, root_id);
2403 return PTR_ERR(root);
2404 }
2405
2406 key.objectid = inum;
2407 key.type = BTRFS_EXTENT_DATA_KEY;
2408 if (offset > (u64)-1 << 32)
2409 key.offset = 0;
2410 else
2411 key.offset = offset;
2412
2413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302414 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002415 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002416 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002417
2418 while (1) {
2419 cond_resched();
2420
2421 leaf = path->nodes[0];
2422 slot = path->slots[0];
2423
2424 if (slot >= btrfs_header_nritems(leaf)) {
2425 ret = btrfs_next_leaf(root, path);
2426 if (ret < 0) {
2427 goto out;
2428 } else if (ret > 0) {
2429 ret = 0;
2430 goto out;
2431 }
2432 continue;
2433 }
2434
2435 path->slots[0]++;
2436
2437 btrfs_item_key_to_cpu(leaf, &key, slot);
2438
2439 if (key.objectid > inum)
2440 goto out;
2441
2442 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2443 continue;
2444
2445 extent = btrfs_item_ptr(leaf, slot,
2446 struct btrfs_file_extent_item);
2447
2448 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2449 continue;
2450
Liu Boe68afa42013-07-01 22:13:26 +08002451 /*
2452 * 'offset' refers to the exact key.offset,
2453 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2454 * (key.offset - extent_offset).
2455 */
2456 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002457 continue;
2458
Liu Boe68afa42013-07-01 22:13:26 +08002459 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002460 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002461
Liu Bo38c227d2013-01-29 03:18:40 +00002462 if (extent_offset >= old->extent_offset + old->offset +
2463 old->len || extent_offset + num_bytes <=
2464 old->extent_offset + old->offset)
2465 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002466 break;
2467 }
2468
2469 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2470 if (!backref) {
2471 ret = -ENOENT;
2472 goto out;
2473 }
2474
2475 backref->root_id = root_id;
2476 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002477 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002478 backref->num_bytes = num_bytes;
2479 backref->extent_offset = extent_offset;
2480 backref->generation = btrfs_file_extent_generation(leaf, extent);
2481 backref->old = old;
2482 backref_insert(&new->root, backref);
2483 old->count++;
2484out:
2485 btrfs_release_path(path);
2486 WARN_ON(ret);
2487 return ret;
2488}
2489
2490static noinline bool record_extent_backrefs(struct btrfs_path *path,
2491 struct new_sa_defrag_extent *new)
2492{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002494 struct old_sa_defrag_extent *old, *tmp;
2495 int ret;
2496
2497 new->path = path;
2498
2499 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002500 ret = iterate_inodes_from_logical(old->bytenr +
2501 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002502 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002503 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002504 if (ret < 0 && ret != -ENOENT)
2505 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002506
2507 /* no backref to be processed for this extent */
2508 if (!old->count) {
2509 list_del(&old->list);
2510 kfree(old);
2511 }
2512 }
2513
2514 if (list_empty(&new->head))
2515 return false;
2516
2517 return true;
2518}
2519
2520static int relink_is_mergable(struct extent_buffer *leaf,
2521 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002522 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002523{
Liu Bo116e0022013-08-02 16:30:40 +08002524 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002525 return 0;
2526
2527 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2528 return 0;
2529
Liu Bo116e0022013-08-02 16:30:40 +08002530 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2531 return 0;
2532
2533 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002534 btrfs_file_extent_other_encoding(leaf, fi))
2535 return 0;
2536
2537 return 1;
2538}
2539
2540/*
2541 * Note the backref might has changed, and in this case we just return 0.
2542 */
2543static noinline int relink_extent_backref(struct btrfs_path *path,
2544 struct sa_defrag_extent_backref *prev,
2545 struct sa_defrag_extent_backref *backref)
2546{
2547 struct btrfs_file_extent_item *extent;
2548 struct btrfs_file_extent_item *item;
2549 struct btrfs_ordered_extent *ordered;
2550 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002551 struct btrfs_root *root;
2552 struct btrfs_key key;
2553 struct extent_buffer *leaf;
2554 struct old_sa_defrag_extent *old = backref->old;
2555 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002556 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002557 struct inode *inode;
2558 struct extent_state *cached = NULL;
2559 int ret = 0;
2560 u64 start;
2561 u64 len;
2562 u64 lock_start;
2563 u64 lock_end;
2564 bool merge = false;
2565 int index;
2566
2567 if (prev && prev->root_id == backref->root_id &&
2568 prev->inum == backref->inum &&
2569 prev->file_pos + prev->num_bytes == backref->file_pos)
2570 merge = true;
2571
2572 /* step 1: get root */
2573 key.objectid = backref->root_id;
2574 key.type = BTRFS_ROOT_ITEM_KEY;
2575 key.offset = (u64)-1;
2576
Liu Bo38c227d2013-01-29 03:18:40 +00002577 index = srcu_read_lock(&fs_info->subvol_srcu);
2578
2579 root = btrfs_read_fs_root_no_name(fs_info, &key);
2580 if (IS_ERR(root)) {
2581 srcu_read_unlock(&fs_info->subvol_srcu, index);
2582 if (PTR_ERR(root) == -ENOENT)
2583 return 0;
2584 return PTR_ERR(root);
2585 }
Liu Bo38c227d2013-01-29 03:18:40 +00002586
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002587 if (btrfs_root_readonly(root)) {
2588 srcu_read_unlock(&fs_info->subvol_srcu, index);
2589 return 0;
2590 }
2591
Liu Bo38c227d2013-01-29 03:18:40 +00002592 /* step 2: get inode */
2593 key.objectid = backref->inum;
2594 key.type = BTRFS_INODE_ITEM_KEY;
2595 key.offset = 0;
2596
2597 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2598 if (IS_ERR(inode)) {
2599 srcu_read_unlock(&fs_info->subvol_srcu, index);
2600 return 0;
2601 }
2602
2603 srcu_read_unlock(&fs_info->subvol_srcu, index);
2604
2605 /* step 3: relink backref */
2606 lock_start = backref->file_pos;
2607 lock_end = backref->file_pos + backref->num_bytes - 1;
2608 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002609 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002610
2611 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2612 if (ordered) {
2613 btrfs_put_ordered_extent(ordered);
2614 goto out_unlock;
2615 }
2616
2617 trans = btrfs_join_transaction(root);
2618 if (IS_ERR(trans)) {
2619 ret = PTR_ERR(trans);
2620 goto out_unlock;
2621 }
2622
2623 key.objectid = backref->inum;
2624 key.type = BTRFS_EXTENT_DATA_KEY;
2625 key.offset = backref->file_pos;
2626
2627 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2628 if (ret < 0) {
2629 goto out_free_path;
2630 } else if (ret > 0) {
2631 ret = 0;
2632 goto out_free_path;
2633 }
2634
2635 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2636 struct btrfs_file_extent_item);
2637
2638 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2639 backref->generation)
2640 goto out_free_path;
2641
2642 btrfs_release_path(path);
2643
2644 start = backref->file_pos;
2645 if (backref->extent_offset < old->extent_offset + old->offset)
2646 start += old->extent_offset + old->offset -
2647 backref->extent_offset;
2648
2649 len = min(backref->extent_offset + backref->num_bytes,
2650 old->extent_offset + old->offset + old->len);
2651 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2652
2653 ret = btrfs_drop_extents(trans, root, inode, start,
2654 start + len, 1);
2655 if (ret)
2656 goto out_free_path;
2657again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002658 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002659 key.type = BTRFS_EXTENT_DATA_KEY;
2660 key.offset = start;
2661
Liu Boa09a0a72013-03-11 09:20:58 +00002662 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002663 if (merge) {
2664 struct btrfs_file_extent_item *fi;
2665 u64 extent_len;
2666 struct btrfs_key found_key;
2667
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002668 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002669 if (ret < 0)
2670 goto out_free_path;
2671
2672 path->slots[0]--;
2673 leaf = path->nodes[0];
2674 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2675
2676 fi = btrfs_item_ptr(leaf, path->slots[0],
2677 struct btrfs_file_extent_item);
2678 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2679
Liu Bo116e0022013-08-02 16:30:40 +08002680 if (extent_len + found_key.offset == start &&
2681 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002682 btrfs_set_file_extent_num_bytes(leaf, fi,
2683 extent_len + len);
2684 btrfs_mark_buffer_dirty(leaf);
2685 inode_add_bytes(inode, len);
2686
2687 ret = 1;
2688 goto out_free_path;
2689 } else {
2690 merge = false;
2691 btrfs_release_path(path);
2692 goto again;
2693 }
2694 }
2695
2696 ret = btrfs_insert_empty_item(trans, root, path, &key,
2697 sizeof(*extent));
2698 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002699 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002700 goto out_free_path;
2701 }
2702
2703 leaf = path->nodes[0];
2704 item = btrfs_item_ptr(leaf, path->slots[0],
2705 struct btrfs_file_extent_item);
2706 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2707 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2708 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2709 btrfs_set_file_extent_num_bytes(leaf, item, len);
2710 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2711 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2712 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2713 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2714 btrfs_set_file_extent_encryption(leaf, item, 0);
2715 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2716
2717 btrfs_mark_buffer_dirty(leaf);
2718 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002719 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002720
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002721 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002722 new->disk_len, 0,
2723 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002724 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002725 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002726 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002727 goto out_free_path;
2728 }
2729
2730 ret = 1;
2731out_free_path:
2732 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002733 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002734 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002735out_unlock:
2736 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002737 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002738 iput(inode);
2739 return ret;
2740}
2741
Liu Bo6f519562013-10-29 10:45:05 +08002742static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2743{
2744 struct old_sa_defrag_extent *old, *tmp;
2745
2746 if (!new)
2747 return;
2748
2749 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002750 kfree(old);
2751 }
2752 kfree(new);
2753}
2754
Liu Bo38c227d2013-01-29 03:18:40 +00002755static void relink_file_extents(struct new_sa_defrag_extent *new)
2756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002757 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002758 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002759 struct sa_defrag_extent_backref *backref;
2760 struct sa_defrag_extent_backref *prev = NULL;
2761 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002762 struct rb_node *node;
2763 int ret;
2764
2765 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002766
2767 path = btrfs_alloc_path();
2768 if (!path)
2769 return;
2770
2771 if (!record_extent_backrefs(path, new)) {
2772 btrfs_free_path(path);
2773 goto out;
2774 }
2775 btrfs_release_path(path);
2776
2777 while (1) {
2778 node = rb_first(&new->root);
2779 if (!node)
2780 break;
2781 rb_erase(node, &new->root);
2782
2783 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2784
2785 ret = relink_extent_backref(path, prev, backref);
2786 WARN_ON(ret < 0);
2787
2788 kfree(prev);
2789
2790 if (ret == 1)
2791 prev = backref;
2792 else
2793 prev = NULL;
2794 cond_resched();
2795 }
2796 kfree(prev);
2797
2798 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002799out:
Liu Bo6f519562013-10-29 10:45:05 +08002800 free_sa_defrag_extent(new);
2801
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002802 atomic_dec(&fs_info->defrag_running);
2803 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002804}
2805
2806static struct new_sa_defrag_extent *
2807record_old_file_extents(struct inode *inode,
2808 struct btrfs_ordered_extent *ordered)
2809{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002810 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002811 struct btrfs_root *root = BTRFS_I(inode)->root;
2812 struct btrfs_path *path;
2813 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002814 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002815 struct new_sa_defrag_extent *new;
2816 int ret;
2817
2818 new = kmalloc(sizeof(*new), GFP_NOFS);
2819 if (!new)
2820 return NULL;
2821
2822 new->inode = inode;
2823 new->file_pos = ordered->file_offset;
2824 new->len = ordered->len;
2825 new->bytenr = ordered->start;
2826 new->disk_len = ordered->disk_len;
2827 new->compress_type = ordered->compress_type;
2828 new->root = RB_ROOT;
2829 INIT_LIST_HEAD(&new->head);
2830
2831 path = btrfs_alloc_path();
2832 if (!path)
2833 goto out_kfree;
2834
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002835 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002836 key.type = BTRFS_EXTENT_DATA_KEY;
2837 key.offset = new->file_pos;
2838
2839 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2840 if (ret < 0)
2841 goto out_free_path;
2842 if (ret > 0 && path->slots[0] > 0)
2843 path->slots[0]--;
2844
2845 /* find out all the old extents for the file range */
2846 while (1) {
2847 struct btrfs_file_extent_item *extent;
2848 struct extent_buffer *l;
2849 int slot;
2850 u64 num_bytes;
2851 u64 offset;
2852 u64 end;
2853 u64 disk_bytenr;
2854 u64 extent_offset;
2855
2856 l = path->nodes[0];
2857 slot = path->slots[0];
2858
2859 if (slot >= btrfs_header_nritems(l)) {
2860 ret = btrfs_next_leaf(root, path);
2861 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002862 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002863 else if (ret > 0)
2864 break;
2865 continue;
2866 }
2867
2868 btrfs_item_key_to_cpu(l, &key, slot);
2869
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002870 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002871 break;
2872 if (key.type != BTRFS_EXTENT_DATA_KEY)
2873 break;
2874 if (key.offset >= new->file_pos + new->len)
2875 break;
2876
2877 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2878
2879 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2880 if (key.offset + num_bytes < new->file_pos)
2881 goto next;
2882
2883 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2884 if (!disk_bytenr)
2885 goto next;
2886
2887 extent_offset = btrfs_file_extent_offset(l, extent);
2888
2889 old = kmalloc(sizeof(*old), GFP_NOFS);
2890 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002891 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002892
2893 offset = max(new->file_pos, key.offset);
2894 end = min(new->file_pos + new->len, key.offset + num_bytes);
2895
2896 old->bytenr = disk_bytenr;
2897 old->extent_offset = extent_offset;
2898 old->offset = offset - key.offset;
2899 old->len = end - offset;
2900 old->new = new;
2901 old->count = 0;
2902 list_add_tail(&old->list, &new->head);
2903next:
2904 path->slots[0]++;
2905 cond_resched();
2906 }
2907
2908 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002909 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002910
2911 return new;
2912
Liu Bo38c227d2013-01-29 03:18:40 +00002913out_free_path:
2914 btrfs_free_path(path);
2915out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002916 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002917 return NULL;
2918}
2919
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002920static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002921 u64 start, u64 len)
2922{
2923 struct btrfs_block_group_cache *cache;
2924
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002926 ASSERT(cache);
2927
2928 spin_lock(&cache->lock);
2929 cache->delalloc_bytes -= len;
2930 spin_unlock(&cache->lock);
2931
2932 btrfs_put_block_group(cache);
2933}
2934
Chris Masond352ac62008-09-29 15:18:18 -04002935/* as ordered data IO finishes, this gets called so we can finish
2936 * an ordered extent if the range of bytes in the file it covers are
2937 * fully written.
2938 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002939static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002940{
Josef Bacik5fd02042012-05-02 14:00:54 -04002941 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002942 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002943 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002944 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002945 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002946 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002947 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002948 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002949 int ret = 0;
2950 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002951 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002952 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002953 bool range_locked = false;
2954 bool clear_new_delalloc_bytes = false;
2955
2956 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2957 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2958 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2959 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002960
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002961 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002962
Josef Bacik5fd02042012-05-02 14:00:54 -04002963 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2964 ret = -EIO;
2965 goto out;
2966 }
2967
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002968 btrfs_free_io_failure_record(BTRFS_I(inode),
2969 ordered_extent->file_offset,
2970 ordered_extent->file_offset +
2971 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002972
Josef Bacik77cef2e2013-08-29 13:57:21 -04002973 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2974 truncated = true;
2975 logical_len = ordered_extent->truncated_len;
2976 /* Truncated the entire extent, don't bother adding */
2977 if (!logical_len)
2978 goto out;
2979 }
2980
Yan, Zhengc2167752009-11-12 09:34:21 +00002981 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002982 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002983
2984 /*
2985 * For mwrite(mmap + memset to write) case, we still reserve
2986 * space for NOCOW range.
2987 * As NOCOW won't cause a new delayed ref, just free the space
2988 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002989 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002990 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002991 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2992 if (nolock)
2993 trans = btrfs_join_transaction_nolock(root);
2994 else
2995 trans = btrfs_join_transaction(root);
2996 if (IS_ERR(trans)) {
2997 ret = PTR_ERR(trans);
2998 trans = NULL;
2999 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003000 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003001 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003002 ret = btrfs_update_inode_fallback(trans, root, inode);
3003 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003004 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003005 goto out;
3006 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003007
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003008 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003009 lock_extent_bits(io_tree, ordered_extent->file_offset,
3010 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003011 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003012
Liu Bo38c227d2013-01-29 03:18:40 +00003013 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3014 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003015 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003016 if (ret) {
3017 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003018 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003019 /* the inode is shared */
3020 new = record_old_file_extents(inode, ordered_extent);
3021
3022 clear_extent_bit(io_tree, ordered_extent->file_offset,
3023 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003024 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003025 }
3026
Josef Bacik0cb59c92010-07-02 12:14:14 -04003027 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003028 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003029 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003030 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 if (IS_ERR(trans)) {
3032 ret = PTR_ERR(trans);
3033 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003034 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003035 }
Chris Masona79b7d42014-05-22 16:18:52 -07003036
Josef Bacik69fe2d72017-10-19 14:15:57 -04003037 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003038
Chris Masonc8b97812008-10-29 14:49:59 -04003039 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003040 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003041 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003042 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003043 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3044 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003045 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003046 ordered_extent->file_offset,
3047 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003048 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003049 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003050 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003051 ret = insert_reserved_file_extent(trans, inode,
3052 ordered_extent->file_offset,
3053 ordered_extent->start,
3054 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003055 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003056 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003057 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003058 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003059 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003060 ordered_extent->start,
3061 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003062 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003063 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3064 ordered_extent->file_offset, ordered_extent->len,
3065 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003066 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003067 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003068 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003069 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003070
Nikolay Borisovac01f262018-01-08 10:59:43 +02003071 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3072 if (ret) {
3073 btrfs_abort_transaction(trans, ret);
3074 goto out;
3075 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003076
Josef Bacik6c760c02012-11-09 10:53:21 -05003077 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3078 ret = btrfs_update_inode_fallback(trans, root, inode);
3079 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003080 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003081 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003082 }
3083 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003084out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003085 if (range_locked || clear_new_delalloc_bytes) {
3086 unsigned int clear_bits = 0;
3087
3088 if (range_locked)
3089 clear_bits |= EXTENT_LOCKED;
3090 if (clear_new_delalloc_bytes)
3091 clear_bits |= EXTENT_DELALLOC_NEW;
3092 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3093 ordered_extent->file_offset,
3094 ordered_extent->file_offset +
3095 ordered_extent->len - 1,
3096 clear_bits,
3097 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003098 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003099 }
3100
Miao Xiea698d0752012-09-20 01:51:59 -06003101 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003102 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003103
Josef Bacik77cef2e2013-08-29 13:57:21 -04003104 if (ret || truncated) {
3105 u64 start, end;
3106
3107 if (truncated)
3108 start = ordered_extent->file_offset + logical_len;
3109 else
3110 start = ordered_extent->file_offset;
3111 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003112 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003113
3114 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003115 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003116
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003117 /*
3118 * If the ordered extent had an IOERR or something else went
3119 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003120 * back to the allocator. We only free the extent in the
3121 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003122 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003123 if ((ret || !logical_len) &&
3124 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003125 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003126 btrfs_free_reserved_extent(fs_info,
3127 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003128 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003129 }
3130
3131
Josef Bacik5fd02042012-05-02 14:00:54 -04003132 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003133 * This needs to be done to make sure anybody waiting knows we are done
3134 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003135 */
3136 btrfs_remove_ordered_extent(inode, ordered_extent);
3137
Liu Bo38c227d2013-01-29 03:18:40 +00003138 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003139 if (new) {
3140 if (ret) {
3141 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003143 } else {
3144 relink_file_extents(new);
3145 }
3146 }
Liu Bo38c227d2013-01-29 03:18:40 +00003147
Chris Masone6dcd2d2008-07-17 12:53:50 -04003148 /* once for us */
3149 btrfs_put_ordered_extent(ordered_extent);
3150 /* once for the tree */
3151 btrfs_put_ordered_extent(ordered_extent);
3152
Josef Bacik5fd02042012-05-02 14:00:54 -04003153 return ret;
3154}
3155
3156static void finish_ordered_fn(struct btrfs_work *work)
3157{
3158 struct btrfs_ordered_extent *ordered_extent;
3159 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3160 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003161}
3162
David Sterbac3988d62017-02-17 15:18:32 +01003163static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003164 struct extent_state *state, int uptodate)
3165{
Josef Bacik5fd02042012-05-02 14:00:54 -04003166 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003168 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003169 struct btrfs_workqueue *wq;
3170 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003171
liubo1abe9b82011-03-24 11:18:59 +00003172 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3173
Chris Mason8b62b722009-09-02 16:53:46 -04003174 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003175 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3176 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003177 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003178
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003179 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003180 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003181 func = btrfs_freespace_write_helper;
3182 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003183 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003184 func = btrfs_endio_write_helper;
3185 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003186
Liu Bo9e0af232014-08-15 23:36:53 +08003187 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3188 NULL);
3189 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003190}
3191
Miao Xiedc380ae2014-09-12 18:43:55 +08003192static int __readpage_endio_check(struct inode *inode,
3193 struct btrfs_io_bio *io_bio,
3194 int icsum, struct page *page,
3195 int pgoff, u64 start, size_t len)
3196{
3197 char *kaddr;
3198 u32 csum_expected;
3199 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003200
3201 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3202
3203 kaddr = kmap_atomic(page);
3204 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003205 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003206 if (csum != csum_expected)
3207 goto zeroit;
3208
3209 kunmap_atomic(kaddr);
3210 return 0;
3211zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003212 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003213 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003214 memset(kaddr + pgoff, 1, len);
3215 flush_dcache_page(page);
3216 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003217 return -EIO;
3218}
3219
Chris Masond352ac62008-09-29 15:18:18 -04003220/*
Chris Masond352ac62008-09-29 15:18:18 -04003221 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003222 * if there's a match, we allow the bio to finish. If not, the code in
3223 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003224 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003225static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3226 u64 phy_offset, struct page *page,
3227 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003228{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003229 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003230 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003231 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003232 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003233
Chris Masond20f7042008-12-08 16:58:54 -05003234 if (PageChecked(page)) {
3235 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003236 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003237 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003238
3239 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003240 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003241
Yan Zheng17d217f2008-12-12 10:03:38 -05003242 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003243 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003244 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003245 return 0;
3246 }
3247
Miao Xiefacc8a222013-07-25 19:22:34 +08003248 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003249 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3250 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003251}
Chris Masonb888db2b2007-08-27 16:49:44 -04003252
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003253/*
3254 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3255 *
3256 * @inode: The inode we want to perform iput on
3257 *
3258 * This function uses the generic vfs_inode::i_count to track whether we should
3259 * just decrement it (in case it's > 1) or if this is the last iput then link
3260 * the inode to the delayed iput machinery. Delayed iputs are processed at
3261 * transaction commit time/superblock commit/cleaner kthread.
3262 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003263void btrfs_add_delayed_iput(struct inode *inode)
3264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003265 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003266 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003267
3268 if (atomic_add_unless(&inode->i_count, -1, 1))
3269 return;
3270
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003271 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003272 ASSERT(list_empty(&binode->delayed_iput));
3273 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003274 spin_unlock(&fs_info->delayed_iput_lock);
3275}
3276
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003277void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003278{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003279
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003280 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003281 while (!list_empty(&fs_info->delayed_iputs)) {
3282 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283
David Sterba8089fe62015-11-19 14:15:51 +01003284 inode = list_first_entry(&fs_info->delayed_iputs,
3285 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003286 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003287 spin_unlock(&fs_info->delayed_iput_lock);
3288 iput(&inode->vfs_inode);
3289 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003290 }
David Sterba8089fe62015-11-19 14:15:51 +01003291 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003292}
3293
Yan, Zhengd68fc572010-05-16 10:49:58 -04003294/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003295 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 * files in the subvolume, it removes orphan item and frees block_rsv
3297 * structure.
3298 */
3299void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3300 struct btrfs_root *root)
3301{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003302 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003303 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003304 int ret;
3305
Josef Bacik8a35d952012-05-23 14:26:42 -04003306 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3308 return;
3309
Josef Bacik90290e12011-12-02 15:44:12 -05003310 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003311 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003312 spin_unlock(&root->orphan_lock);
3313 return;
3314 }
3315
3316 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3317 spin_unlock(&root->orphan_lock);
3318 return;
3319 }
3320
3321 block_rsv = root->orphan_block_rsv;
3322 root->orphan_block_rsv = NULL;
3323 spin_unlock(&root->orphan_lock);
3324
Miao Xie27cdeb72014-04-02 19:51:05 +08003325 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003326 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003327 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003328 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003329 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003330 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003331 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003332 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3333 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 }
3335
Josef Bacik90290e12011-12-02 15:44:12 -05003336 if (block_rsv) {
3337 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003338 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 }
3340}
3341
3342/*
Josef Bacik7b128762008-07-24 12:17:14 -04003343 * This creates an orphan entry for the given inode in case something goes
3344 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 *
3346 * NOTE: caller of this function should reserve 5 units of metadata for
3347 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003348 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003349int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3350 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003351{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3353 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 struct btrfs_block_rsv *block_rsv = NULL;
3355 int reserve = 0;
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003356 bool insert = false;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003358
Yan, Zhengd68fc572010-05-16 10:49:58 -04003359 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003360 block_rsv = btrfs_alloc_block_rsv(fs_info,
3361 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003362 if (!block_rsv)
3363 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003364 }
3365
Josef Bacik8a35d952012-05-23 14:26:42 -04003366 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003367 &inode->runtime_flags))
3368 insert = true;
Josef Bacik7b128762008-07-24 12:17:14 -04003369
Josef Bacik72ac3c02012-05-23 14:13:11 -04003370 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003371 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003373
3374 spin_lock(&root->orphan_lock);
3375 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3376 if (!root->orphan_block_rsv) {
3377 root->orphan_block_rsv = block_rsv;
3378 } else if (block_rsv) {
3379 btrfs_free_block_rsv(fs_info, block_rsv);
3380 block_rsv = NULL;
3381 }
3382
3383 if (insert)
3384 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003386
Yan, Zhengd68fc572010-05-16 10:49:58 -04003387 /* grab metadata reservation from transaction handle */
3388 if (reserve) {
3389 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003390 ASSERT(!ret);
3391 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003392 /*
3393 * dec doesn't need spin_lock as ->orphan_block_rsv
3394 * would be released only if ->orphan_inodes is
3395 * zero.
3396 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003397 atomic_dec(&root->orphan_inodes);
3398 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003399 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003400 if (insert)
3401 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003402 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003403 return ret;
3404 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003405 }
3406
3407 /* insert an orphan item to track this unlinked/truncated file */
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003408 if (insert) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003409 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003410 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003411 if (reserve) {
3412 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003413 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003414 btrfs_orphan_release_metadata(inode);
3415 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003416 /*
3417 * btrfs_orphan_commit_root may race with us and set
3418 * ->orphan_block_rsv to zero, in order to avoid that,
3419 * decrease ->orphan_inodes after everything is done.
3420 */
3421 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003422 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003423 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003424 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003425 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003426 return ret;
3427 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003428 }
3429 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003430 }
3431
Yan, Zhengd68fc572010-05-16 10:49:58 -04003432 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003433}
3434
3435/*
3436 * We have done the truncate/delete so we can go ahead and remove the orphan
3437 * item for this particular inode.
3438 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003439static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003440 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003441{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003442 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003443 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003444 int ret = 0;
3445
Josef Bacik8a35d952012-05-23 14:26:42 -04003446 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003447 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003448 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003449
Liu Bo1a932ef2018-01-25 11:02:54 -07003450 if (delete_item && trans)
3451 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3452
Josef Bacik72ac3c02012-05-23 14:13:11 -04003453 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003454 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003455 btrfs_orphan_release_metadata(inode);
3456
Liu Bo1a932ef2018-01-25 11:02:54 -07003457 /*
3458 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3459 * to zero, in order to avoid that, decrease ->orphan_inodes after
3460 * everything is done.
3461 */
3462 if (delete_item)
3463 atomic_dec(&root->orphan_inodes);
3464
Josef Bacik4ef31a42013-08-13 14:10:08 -04003465 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003466}
3467
3468/*
3469 * this cleans up any orphans that may be left on the list from the last use
3470 * of this root.
3471 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003473{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003474 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003475 struct btrfs_path *path;
3476 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003477 struct btrfs_key key, found_key;
3478 struct btrfs_trans_handle *trans;
3479 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003480 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003481 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3482
Yan, Zhengd68fc572010-05-16 10:49:58 -04003483 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003485
3486 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487 if (!path) {
3488 ret = -ENOMEM;
3489 goto out;
3490 }
David Sterbae4058b52015-11-27 16:31:35 +01003491 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003492
3493 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003494 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003495 key.offset = (u64)-1;
3496
Josef Bacik7b128762008-07-24 12:17:14 -04003497 while (1) {
3498 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 if (ret < 0)
3500 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003501
3502 /*
3503 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003504 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003505 * find the key and see if we have stuff that matches
3506 */
3507 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003508 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003509 if (path->slots[0] == 0)
3510 break;
3511 path->slots[0]--;
3512 }
3513
3514 /* pull out the item */
3515 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003516 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3517
3518 /* make sure the item matches what we want */
3519 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3520 break;
David Sterba962a2982014-06-04 18:41:45 +02003521 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003522 break;
3523
3524 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003525 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003526
3527 /*
3528 * this is where we are basically btrfs_lookup, without the
3529 * crossing root thing. we store the inode number in the
3530 * offset of the orphan item.
3531 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003532
3533 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 btrfs_err(fs_info,
3535 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003536 ret = -EINVAL;
3537 goto out;
3538 }
3539
3540 last_objectid = found_key.offset;
3541
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003542 found_key.objectid = found_key.offset;
3543 found_key.type = BTRFS_INODE_ITEM_KEY;
3544 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003545 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303546 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003547 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003548 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003549
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003550 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003551 struct btrfs_root *dead_root;
3552 struct btrfs_fs_info *fs_info = root->fs_info;
3553 int is_dead_root = 0;
3554
3555 /*
3556 * this is an orphan in the tree root. Currently these
3557 * could come from 2 sources:
3558 * a) a snapshot deletion in progress
3559 * b) a free space cache inode
3560 * We need to distinguish those two, as the snapshot
3561 * orphan must not get deleted.
3562 * find_dead_roots already ran before us, so if this
3563 * is a snapshot deletion, we should find the root
3564 * in the dead_roots list
3565 */
3566 spin_lock(&fs_info->trans_lock);
3567 list_for_each_entry(dead_root, &fs_info->dead_roots,
3568 root_list) {
3569 if (dead_root->root_key.objectid ==
3570 found_key.objectid) {
3571 is_dead_root = 1;
3572 break;
3573 }
3574 }
3575 spin_unlock(&fs_info->trans_lock);
3576 if (is_dead_root) {
3577 /* prevent this orphan from being found again */
3578 key.offset = found_key.objectid - 1;
3579 continue;
3580 }
3581 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003582 /*
3583 * Inode is already gone but the orphan item is still there,
3584 * kill the orphan item.
3585 */
Filipe Manana67710892016-06-06 11:51:25 +01003586 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003587 trans = btrfs_start_transaction(root, 1);
3588 if (IS_ERR(trans)) {
3589 ret = PTR_ERR(trans);
3590 goto out;
3591 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003592 btrfs_debug(fs_info, "auto deleting %Lu",
3593 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003594 ret = btrfs_del_orphan_item(trans, root,
3595 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003596 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003597 if (ret)
3598 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003599 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003600 }
Josef Bacik7b128762008-07-24 12:17:14 -04003601
Josef Bacik7b128762008-07-24 12:17:14 -04003602 /*
3603 * add this inode to the orphan list so btrfs_orphan_del does
3604 * the proper thing when we hit it
3605 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003606 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3607 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003608 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003609
Josef Bacik7b128762008-07-24 12:17:14 -04003610 /* if we have links, this was a truncate, lets do that */
3611 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303612 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003613 iput(inode);
3614 continue;
3615 }
Josef Bacik7b128762008-07-24 12:17:14 -04003616 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003617
3618 /* 1 for the orphan item deletion. */
3619 trans = btrfs_start_transaction(root, 1);
3620 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003621 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003622 ret = PTR_ERR(trans);
3623 goto out;
3624 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003625 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003626 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003627 if (ret) {
3628 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003629 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003630 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003631
Filipe Manana213e8c52018-02-06 20:40:31 +00003632 ret = btrfs_truncate(inode, false);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003633 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003634 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003635 } else {
3636 nr_unlink++;
3637 }
3638
3639 /* this will do delete_inode and everything for us */
3640 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003641 if (ret)
3642 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003643 }
Miao Xie3254c872011-11-10 20:45:05 -05003644 /* release the path since we're done with it */
3645 btrfs_release_path(path);
3646
Yan, Zhengd68fc572010-05-16 10:49:58 -04003647 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3648
3649 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003650 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003651 (u64)-1);
3652
Miao Xie27cdeb72014-04-02 19:51:05 +08003653 if (root->orphan_block_rsv ||
3654 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003655 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003656 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003657 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003658 }
Josef Bacik7b128762008-07-24 12:17:14 -04003659
3660 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003661 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003662 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003664
3665out:
3666 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003667 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003668 btrfs_free_path(path);
3669 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003670}
3671
Chris Masond352ac62008-09-29 15:18:18 -04003672/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003673 * very simple check to peek ahead in the leaf looking for xattrs. If we
3674 * don't find any xattrs, we know there can't be any acls.
3675 *
3676 * slot is the slot the inode is in, objectid is the objectid of the inode
3677 */
3678static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 int slot, u64 objectid,
3680 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003681{
3682 u32 nritems = btrfs_header_nritems(leaf);
3683 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003684 static u64 xattr_access = 0;
3685 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003686 int scanned = 0;
3687
Josef Bacikf23b5a52013-06-19 10:16:26 -04003688 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003689 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3690 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3691 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3692 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003693 }
3694
Chris Mason46a53cc2009-04-27 11:47:50 -04003695 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003696 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003697 while (slot < nritems) {
3698 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3699
3700 /* we found a different objectid, there must not be acls */
3701 if (found_key.objectid != objectid)
3702 return 0;
3703
3704 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003705 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003706 if (*first_xattr_slot == -1)
3707 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003708 if (found_key.offset == xattr_access ||
3709 found_key.offset == xattr_default)
3710 return 1;
3711 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003712
3713 /*
3714 * we found a key greater than an xattr key, there can't
3715 * be any acls later on
3716 */
3717 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3718 return 0;
3719
3720 slot++;
3721 scanned++;
3722
3723 /*
3724 * it goes inode, inode backrefs, xattrs, extents,
3725 * so if there are a ton of hard links to an inode there can
3726 * be a lot of backrefs. Don't waste time searching too hard,
3727 * this is just an optimization
3728 */
3729 if (scanned >= 8)
3730 break;
3731 }
3732 /* we hit the end of the leaf before we found an xattr or
3733 * something larger than an xattr. We have to assume the inode
3734 * has acls
3735 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003736 if (*first_xattr_slot == -1)
3737 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003738 return 1;
3739}
3740
3741/*
Chris Masond352ac62008-09-29 15:18:18 -04003742 * read an inode from the btree into the in-memory inode
3743 */
Filipe Manana67710892016-06-06 11:51:25 +01003744static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003745{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003747 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003748 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003749 struct btrfs_inode_item *inode_item;
3750 struct btrfs_root *root = BTRFS_I(inode)->root;
3751 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003752 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003753 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003754 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003756 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003757 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003758
3759 ret = btrfs_fill_inode(inode, &rdev);
3760 if (!ret)
3761 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003762
3763 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003764 if (!path) {
3765 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003766 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003767 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003768
Chris Mason39279cc2007-06-12 06:35:45 -04003769 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003770
Chris Mason39279cc2007-06-12 06:35:45 -04003771 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003772 if (ret) {
3773 if (ret > 0)
3774 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003775 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003776 }
Chris Mason39279cc2007-06-12 06:35:45 -04003777
Chris Mason5f39d392007-10-15 16:14:19 -04003778 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003779
3780 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003781 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003782
Chris Mason5f39d392007-10-15 16:14:19 -04003783 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3784 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003785 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003786 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003787 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3788 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003789 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003790
David Sterbaa937b972014-12-12 17:39:12 +01003791 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3792 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
David Sterbaa937b972014-12-12 17:39:12 +01003794 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3795 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003796
David Sterbaa937b972014-12-12 17:39:12 +01003797 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3798 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
chandan r9cc97d62012-07-04 12:48:07 +05303800 BTRFS_I(inode)->i_otime.tv_sec =
3801 btrfs_timespec_sec(leaf, &inode_item->otime);
3802 BTRFS_I(inode)->i_otime.tv_nsec =
3803 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003804
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003805 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003806 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003807 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3808
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003809 inode_set_iversion_queried(inode,
3810 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003811 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003812 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003813 rdev = btrfs_inode_rdev(leaf, inode_item);
3814
Josef Bacikaec74772008-07-24 12:12:38 -04003815 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003816 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003817
3818cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003819 /*
3820 * If we were modified in the current generation and evicted from memory
3821 * and then re-read we need to do a full sync since we don't have any
3822 * idea about which extents were modified before we were evicted from
3823 * cache.
3824 *
3825 * This is required for both inode re-read from disk and delayed inode
3826 * in delayed_nodes_tree.
3827 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003828 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003829 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3830 &BTRFS_I(inode)->runtime_flags);
3831
Filipe Mananabde6c242015-07-24 00:00:19 +01003832 /*
3833 * We don't persist the id of the transaction where an unlink operation
3834 * against the inode was last made. So here we assume the inode might
3835 * have been evicted, and therefore the exact value of last_unlink_trans
3836 * lost, and set it to last_trans to avoid metadata inconsistencies
3837 * between the inode and its parent if the inode is fsync'ed and the log
3838 * replayed. For example, in the scenario:
3839 *
3840 * touch mydir/foo
3841 * ln mydir/foo mydir/bar
3842 * sync
3843 * unlink mydir/bar
3844 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3845 * xfs_io -c fsync mydir/foo
3846 * <power failure>
3847 * mount fs, triggers fsync log replay
3848 *
3849 * We must make sure that when we fsync our inode foo we also log its
3850 * parent inode, otherwise after log replay the parent still has the
3851 * dentry with the "bar" name but our inode foo has a link count of 1
3852 * and doesn't have an inode ref with the name "bar" anymore.
3853 *
3854 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003855 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003856 * transaction commits on fsync if our inode is a directory, or if our
3857 * inode is not a directory, logging its parent unnecessarily.
3858 */
3859 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3860
Miao Xie67de1172013-12-26 13:07:06 +08003861 path->slots[0]++;
3862 if (inode->i_nlink != 1 ||
3863 path->slots[0] >= btrfs_header_nritems(leaf))
3864 goto cache_acl;
3865
3866 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003867 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003868 goto cache_acl;
3869
3870 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3871 if (location.type == BTRFS_INODE_REF_KEY) {
3872 struct btrfs_inode_ref *ref;
3873
3874 ref = (struct btrfs_inode_ref *)ptr;
3875 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3876 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3877 struct btrfs_inode_extref *extref;
3878
3879 extref = (struct btrfs_inode_extref *)ptr;
3880 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3881 extref);
3882 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003883cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003884 /*
3885 * try to precache a NULL acl entry for files that don't have
3886 * any xattrs or acls
3887 */
Li Zefan33345d012011-04-20 10:31:50 +08003888 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003889 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003890 if (first_xattr_slot != -1) {
3891 path->slots[0] = first_xattr_slot;
3892 ret = btrfs_load_inode_props(inode, path);
3893 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003894 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003895 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003896 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003897 root->root_key.objectid, ret);
3898 }
3899 btrfs_free_path(path);
3900
Al Viro72c04902009-06-24 16:58:48 -04003901 if (!maybe_acls)
3902 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003903
Chris Mason39279cc2007-06-12 06:35:45 -04003904 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003905 case S_IFREG:
3906 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003907 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 inode->i_fop = &btrfs_file_operations;
3909 inode->i_op = &btrfs_file_inode_operations;
3910 break;
3911 case S_IFDIR:
3912 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003913 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 break;
3915 case S_IFLNK:
3916 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003917 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003918 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3919 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003920 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003921 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003922 init_special_inode(inode, inode->i_mode, rdev);
3923 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003925
3926 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003927 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003928
3929make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003930 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003932 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003933}
3934
Chris Masond352ac62008-09-29 15:18:18 -04003935/*
3936 * given a leaf and an inode, copy the inode fields into the leaf
3937 */
Chris Masone02119d2008-09-05 16:13:11 -04003938static void fill_inode_item(struct btrfs_trans_handle *trans,
3939 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003940 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003941 struct inode *inode)
3942{
Liu Bo51fab692012-12-27 09:01:21 +00003943 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003944
Liu Bo51fab692012-12-27 09:01:21 +00003945 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003946
Liu Bo51fab692012-12-27 09:01:21 +00003947 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3948 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3949 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3950 &token);
3951 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3952 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003953
David Sterbaa937b972014-12-12 17:39:12 +01003954 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003955 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003956 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003957 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003958
David Sterbaa937b972014-12-12 17:39:12 +01003959 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003960 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003961 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003962 inode->i_mtime.tv_nsec, &token);
3963
David Sterbaa937b972014-12-12 17:39:12 +01003964 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003965 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003966 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003967 inode->i_ctime.tv_nsec, &token);
3968
chandan r9cc97d62012-07-04 12:48:07 +05303969 btrfs_set_token_timespec_sec(leaf, &item->otime,
3970 BTRFS_I(inode)->i_otime.tv_sec, &token);
3971 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3972 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3973
Liu Bo51fab692012-12-27 09:01:21 +00003974 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3975 &token);
3976 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3977 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003978 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3979 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003980 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3981 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3982 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3983 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003984}
3985
Chris Masond352ac62008-09-29 15:18:18 -04003986/*
3987 * copy everything in the in-memory inode into the btree.
3988 */
Chris Mason21151332011-11-10 20:39:08 -05003989static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003990 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003991{
3992 struct btrfs_inode_item *inode_item;
3993 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003994 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003995 int ret;
3996
3997 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003998 if (!path)
3999 return -ENOMEM;
4000
Chris Masonb9473432009-03-13 11:00:37 -04004001 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004002 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4003 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004004 if (ret) {
4005 if (ret > 0)
4006 ret = -ENOENT;
4007 goto failed;
4008 }
4009
Chris Mason5f39d392007-10-15 16:14:19 -04004010 leaf = path->nodes[0];
4011 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004012 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004013
Chris Masone02119d2008-09-05 16:13:11 -04004014 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004015 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004016 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004017 ret = 0;
4018failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004019 btrfs_free_path(path);
4020 return ret;
4021}
4022
Chris Masond352ac62008-09-29 15:18:18 -04004023/*
Chris Mason21151332011-11-10 20:39:08 -05004024 * copy everything in the in-memory inode into the btree.
4025 */
4026noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4027 struct btrfs_root *root, struct inode *inode)
4028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004029 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004030 int ret;
4031
4032 /*
4033 * If the inode is a free space inode, we can deadlock during commit
4034 * if we put it into the delayed code.
4035 *
4036 * The data relocation inode should also be directly updated
4037 * without delay
4038 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004039 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004040 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004041 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004042 btrfs_update_root_times(trans, root);
4043
Chris Mason21151332011-11-10 20:39:08 -05004044 ret = btrfs_delayed_update_inode(trans, root, inode);
4045 if (!ret)
4046 btrfs_set_inode_last_trans(trans, inode);
4047 return ret;
4048 }
4049
4050 return btrfs_update_inode_item(trans, root, inode);
4051}
4052
Josef Bacikbe6aef62012-10-22 15:43:12 -04004053noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4054 struct btrfs_root *root,
4055 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004056{
4057 int ret;
4058
4059 ret = btrfs_update_inode(trans, root, inode);
4060 if (ret == -ENOSPC)
4061 return btrfs_update_inode_item(trans, root, inode);
4062 return ret;
4063}
4064
4065/*
Chris Masond352ac62008-09-29 15:18:18 -04004066 * unlink helper that gets used here in inode.c and in the tree logging
4067 * recovery code. It remove a link in a directory with a given name, and
4068 * also drops the back refs in the inode to the directory
4069 */
Al Viro92986792011-03-04 17:14:37 +00004070static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4071 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004072 struct btrfs_inode *dir,
4073 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004074 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004075{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004076 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004077 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004078 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004079 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004081 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004082 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004083 u64 ino = btrfs_ino(inode);
4084 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004085
4086 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004087 if (!path) {
4088 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004089 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004090 }
4091
Chris Masonb9473432009-03-13 11:00:37 -04004092 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004093 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004094 name, name_len, -1);
4095 if (IS_ERR(di)) {
4096 ret = PTR_ERR(di);
4097 goto err;
4098 }
4099 if (!di) {
4100 ret = -ENOENT;
4101 goto err;
4102 }
Chris Mason5f39d392007-10-15 16:14:19 -04004103 leaf = path->nodes[0];
4104 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004105 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004106 if (ret)
4107 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004108 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004109
Miao Xie67de1172013-12-26 13:07:06 +08004110 /*
4111 * If we don't have dir index, we have to get it by looking up
4112 * the inode ref, since we get the inode ref, remove it directly,
4113 * it is unnecessary to do delayed deletion.
4114 *
4115 * But if we have dir index, needn't search inode ref to get it.
4116 * Since the inode ref is close to the inode item, it is better
4117 * that we delay to delete it, and just do this deletion when
4118 * we update the inode item.
4119 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004120 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004121 ret = btrfs_delayed_delete_inode_ref(inode);
4122 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004123 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004124 goto skip_backref;
4125 }
4126 }
4127
Li Zefan33345d012011-04-20 10:31:50 +08004128 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4129 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004130 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004131 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004132 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004133 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004134 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004135 goto err;
4136 }
Miao Xie67de1172013-12-26 13:07:06 +08004137skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004138 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004140 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004141 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 }
Chris Mason39279cc2007-06-12 06:35:45 -04004143
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004144 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4145 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004147 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 goto err;
4149 }
Chris Masone02119d2008-09-05 16:13:11 -04004150
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004151 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4152 index);
Chris Mason6418c962010-10-30 07:34:24 -04004153 if (ret == -ENOENT)
4154 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004155 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004156 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004157err:
4158 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004159 if (ret)
4160 goto out;
4161
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004162 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004163 inode_inc_iversion(&inode->vfs_inode);
4164 inode_inc_iversion(&dir->vfs_inode);
4165 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4166 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4167 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004168out:
Chris Mason39279cc2007-06-12 06:35:45 -04004169 return ret;
4170}
4171
Al Viro92986792011-03-04 17:14:37 +00004172int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4173 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004174 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004175 const char *name, int name_len)
4176{
4177 int ret;
4178 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4179 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004180 drop_nlink(&inode->vfs_inode);
4181 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004182 }
4183 return ret;
4184}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004185
4186/*
4187 * helper to start transaction for unlink and rmdir.
4188 *
Josef Bacikd52be812013-05-29 14:54:47 -04004189 * unlink and rmdir are special in btrfs, they do not always free space, so
4190 * if we cannot make our reservations the normal way try and see if there is
4191 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4192 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004193 */
Josef Bacikd52be812013-05-29 14:54:47 -04004194static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004195{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004196 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004197
Josef Bacike70bea52011-10-11 14:18:24 -04004198 /*
4199 * 1 for the possible orphan item
4200 * 1 for the dir item
4201 * 1 for the dir index
4202 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004203 * 1 for the inode
4204 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004205 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206}
4207
Chris Mason39279cc2007-06-12 06:35:45 -04004208static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4209{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004211 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004212 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004213 int ret;
4214
Josef Bacikd52be812013-05-29 14:54:47 -04004215 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004216 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004217 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004218
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004219 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4220 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004221
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004222 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4223 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4224 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004225 if (ret)
4226 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004227
Yan, Zhenga22285a2010-05-16 10:48:46 -04004228 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004229 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004230 if (ret)
4231 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004232 }
Josef Bacik7b128762008-07-24 12:17:14 -04004233
Tsutomu Itohb5324022011-07-19 07:27:20 +00004234out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004235 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004236 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004237 return ret;
4238}
4239
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4241 struct btrfs_root *root,
4242 struct inode *dir, u64 objectid,
4243 const char *name, int name_len)
4244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004245 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 struct btrfs_path *path;
4247 struct extent_buffer *leaf;
4248 struct btrfs_dir_item *di;
4249 struct btrfs_key key;
4250 u64 index;
4251 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004252 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253
4254 path = btrfs_alloc_path();
4255 if (!path)
4256 return -ENOMEM;
4257
Li Zefan33345d012011-04-20 10:31:50 +08004258 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004260 if (IS_ERR_OR_NULL(di)) {
4261 if (!di)
4262 ret = -ENOENT;
4263 else
4264 ret = PTR_ERR(di);
4265 goto out;
4266 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004267
4268 leaf = path->nodes[0];
4269 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4270 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4271 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004273 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274 goto out;
4275 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004276 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004277
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004278 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4279 root->root_key.objectid, dir_ino,
4280 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004281 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004283 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004284 goto out;
4285 }
Li Zefan33345d012011-04-20 10:31:50 +08004286 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288 if (IS_ERR_OR_NULL(di)) {
4289 if (!di)
4290 ret = -ENOENT;
4291 else
4292 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004293 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 goto out;
4295 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004296
4297 leaf = path->nodes[0];
4298 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004299 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004300 index = key.offset;
4301 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004302 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004303
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004304 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004305 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004306 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 goto out;
4308 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004309
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004310 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004311 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004312 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004313 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004314 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004315 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004316out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004317 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004318 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004319}
4320
Chris Mason39279cc2007-06-12 06:35:45 -04004321static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4322{
David Howells2b0143b2015-03-17 22:25:59 +00004323 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004324 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004325 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004326 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004327 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004328
David Sterbab3ae2442012-09-13 16:04:34 -06004329 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004330 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004331 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004332 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004333
Josef Bacikd52be812013-05-29 14:54:47 -04004334 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004335 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004336 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004337
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004338 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004339 err = btrfs_unlink_subvol(trans, root, dir,
4340 BTRFS_I(inode)->location.objectid,
4341 dentry->d_name.name,
4342 dentry->d_name.len);
4343 goto out;
4344 }
4345
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004346 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004347 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004348 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004349
Filipe Manana44f714d2016-06-06 16:11:13 +01004350 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4351
Chris Mason39279cc2007-06-12 06:35:45 -04004352 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004353 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4354 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4355 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004356 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004357 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004358 /*
4359 * Propagate the last_unlink_trans value of the deleted dir to
4360 * its parent directory. This is to prevent an unrecoverable
4361 * log tree in the case we do something like this:
4362 * 1) create dir foo
4363 * 2) create snapshot under dir foo
4364 * 3) delete the snapshot
4365 * 4) rmdir foo
4366 * 5) mkdir foo
4367 * 6) fsync foo or some file inside foo
4368 */
4369 if (last_unlink_trans >= trans->transid)
4370 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4371 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004372out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004373 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004374 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004375
Chris Mason39279cc2007-06-12 06:35:45 -04004376 return err;
4377}
4378
Chris Mason28f75a02015-02-04 06:59:29 -08004379static int truncate_space_check(struct btrfs_trans_handle *trans,
4380 struct btrfs_root *root,
4381 u64 bytes_deleted)
4382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004383 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004384 int ret;
4385
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004386 /*
4387 * This is only used to apply pressure to the enospc system, we don't
4388 * intend to use this reservation at all.
4389 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004390 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004391 bytes_deleted *= fs_info->nodesize;
4392 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004393 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004394 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004395 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004396 trans->transid,
4397 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004398 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004399 }
Chris Mason28f75a02015-02-04 06:59:29 -08004400 return ret;
4401
4402}
4403
Josef Bacikddfae632017-10-19 14:16:02 -04004404/*
4405 * Return this if we need to call truncate_block for the last bit of the
4406 * truncate.
4407 */
4408#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004409
Chris Mason323ac952008-10-01 19:05:46 -04004410/*
Chris Mason39279cc2007-06-12 06:35:45 -04004411 * this can truncate away extent items, csum items and directory items.
4412 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004413 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004414 *
4415 * csum items that cross the new i_size are truncated to the new size
4416 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004417 *
4418 * min_type is the minimum key type to truncate down to. If set to 0, this
4419 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004420 */
Yan, Zheng80825102009-11-12 09:35:36 +00004421int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4422 struct btrfs_root *root,
4423 struct inode *inode,
4424 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004426 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004428 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004430 struct btrfs_key key;
4431 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004432 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004433 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004435 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004436 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004437 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004438 int found_extent;
4439 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004440 int pending_del_nr = 0;
4441 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004442 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004443 int ret;
4444 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004445 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004446 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004447 bool be_nice = false;
4448 bool should_throttle = false;
4449 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004450
4451 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004452
Chris Mason28ed1342014-12-17 09:41:04 -08004453 /*
4454 * for non-free space inodes and ref cows, we want to back off from
4455 * time to time
4456 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004457 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004458 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004459 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004460
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004461 path = btrfs_alloc_path();
4462 if (!path)
4463 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004464 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004465
Josef Bacik5dc562c2012-08-17 13:14:17 -04004466 /*
4467 * We want to drop from the next block forward in case this new size is
4468 * not block aligned since we will be keeping the last block of the
4469 * extent just the way it is.
4470 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004471 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004472 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004473 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004474 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004475 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004476
Miao Xie16cdcec2011-04-22 18:12:22 +08004477 /*
4478 * This function is also used to drop the items in the log tree before
4479 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4480 * it is used to drop the loged items. So we shouldn't kill the delayed
4481 * items.
4482 */
4483 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004484 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004485
Li Zefan33345d012011-04-20 10:31:50 +08004486 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004487 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004488 key.type = (u8)-1;
4489
Chris Mason85e21ba2008-01-29 15:11:36 -05004490search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004491 /*
4492 * with a 16K leaf size and 128MB extents, you can actually queue
4493 * up a huge file in a single leaf. Most of the time that
4494 * bytes_deleted is > 0, it will be huge by the time we get here
4495 */
Byongho Leeee221842015-12-15 01:42:10 +09004496 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004497 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004498 err = -EAGAIN;
4499 goto error;
4500 }
4501 }
4502
4503
Chris Masonb9473432009-03-13 11:00:37 -04004504 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004505 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004506 if (ret < 0) {
4507 err = ret;
4508 goto out;
4509 }
Chris Masond3977122009-01-05 21:25:51 -05004510
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004512 /* there are no items in the tree for us to truncate, we're
4513 * done
4514 */
Yan, Zheng80825102009-11-12 09:35:36 +00004515 if (path->slots[0] == 0)
4516 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004517 path->slots[0]--;
4518 }
4519
Chris Masond3977122009-01-05 21:25:51 -05004520 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004521 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004522 leaf = path->nodes[0];
4523 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004524 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004525
Li Zefan33345d012011-04-20 10:31:50 +08004526 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004527 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004528
Chris Mason85e21ba2008-01-29 15:11:36 -05004529 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004530 break;
4531
Chris Mason5f39d392007-10-15 16:14:19 -04004532 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004533 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004534 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004535 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004536 extent_type = btrfs_file_extent_type(leaf, fi);
4537 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004538 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004539 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004540
4541 trace_btrfs_truncate_show_fi_regular(
4542 BTRFS_I(inode), leaf, fi,
4543 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004544 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004545 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004546 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004547
4548 trace_btrfs_truncate_show_fi_inline(
4549 BTRFS_I(inode), leaf, fi, path->slots[0],
4550 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004551 }
Yan008630c2007-11-07 13:31:09 -05004552 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004553 }
Yan, Zheng80825102009-11-12 09:35:36 +00004554 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004555 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004556 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004557 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004558 break;
4559 if (found_key.offset >= new_size)
4560 del_item = 1;
4561 else
4562 del_item = 0;
4563 }
Chris Mason39279cc2007-06-12 06:35:45 -04004564 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004565 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004566 if (found_type != BTRFS_EXTENT_DATA_KEY)
4567 goto delete;
4568
4569 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004570 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004571 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004572 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004573 u64 orig_num_bytes =
4574 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004575 extent_num_bytes = ALIGN(new_size -
4576 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004577 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004578 btrfs_set_file_extent_num_bytes(leaf, fi,
4579 extent_num_bytes);
4580 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004581 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004582 if (test_bit(BTRFS_ROOT_REF_COWS,
4583 &root->state) &&
4584 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004585 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004586 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004587 } else {
Chris Masondb945352007-10-15 16:15:53 -04004588 extent_num_bytes =
4589 btrfs_file_extent_disk_num_bytes(leaf,
4590 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004591 extent_offset = found_key.offset -
4592 btrfs_file_extent_offset(leaf, fi);
4593
Chris Mason39279cc2007-06-12 06:35:45 -04004594 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004595 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004596 if (extent_start != 0) {
4597 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004598 if (test_bit(BTRFS_ROOT_REF_COWS,
4599 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004600 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
4602 }
Chris Mason90692182008-02-08 13:49:28 -05004603 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004604 /*
4605 * we can't truncate inline items that have had
4606 * special encodings
4607 */
4608 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004609 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004610 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4611 btrfs_file_extent_compression(leaf, fi) == 0) {
4612 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004613
Josef Bacikddfae632017-10-19 14:16:02 -04004614 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4615 size = btrfs_file_extent_calc_inline_size(size);
4616 btrfs_truncate_item(root->fs_info, path, size, 1);
4617 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004618 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004619 * We have to bail so the last_size is set to
4620 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004621 */
Josef Bacikddfae632017-10-19 14:16:02 -04004622 err = NEED_TRUNCATE_BLOCK;
4623 break;
Chris Mason90692182008-02-08 13:49:28 -05004624 }
Josef Bacikddfae632017-10-19 14:16:02 -04004625
4626 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4627 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004628 }
Chris Mason179e29e2007-11-01 11:28:41 -04004629delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004630 if (del_item)
4631 last_size = found_key.offset;
4632 else
4633 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004634 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004635 if (!pending_del_nr) {
4636 /* no pending yet, add ourselves */
4637 pending_del_slot = path->slots[0];
4638 pending_del_nr = 1;
4639 } else if (pending_del_nr &&
4640 path->slots[0] + 1 == pending_del_slot) {
4641 /* hop on the pending chunk */
4642 pending_del_nr++;
4643 pending_del_slot = path->slots[0];
4644 } else {
Chris Masond3977122009-01-05 21:25:51 -05004645 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004646 }
Chris Mason39279cc2007-06-12 06:35:45 -04004647 } else {
4648 break;
4649 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004650 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004651
Miao Xie27cdeb72014-04-02 19:51:05 +08004652 if (found_extent &&
4653 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004655 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004656 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004657 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004658 extent_num_bytes, 0,
4659 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004660 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004661 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004662 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4663 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004664 trans->delayed_ref_updates * 2,
4665 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004666 if (be_nice) {
4667 if (truncate_space_check(trans, root,
4668 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004669 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004670 }
4671 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004672 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004673 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004674 }
Chris Mason39279cc2007-06-12 06:35:45 -04004675 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004676
Yan, Zheng80825102009-11-12 09:35:36 +00004677 if (found_type == BTRFS_INODE_ITEM_KEY)
4678 break;
4679
4680 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004681 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004682 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004683 if (pending_del_nr) {
4684 ret = btrfs_del_items(trans, root, path,
4685 pending_del_slot,
4686 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004687 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004688 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004689 goto error;
4690 }
Yan, Zheng80825102009-11-12 09:35:36 +00004691 pending_del_nr = 0;
4692 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004693 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004694 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004695 unsigned long updates = trans->delayed_ref_updates;
4696 if (updates) {
4697 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004698 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004699 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004700 if (ret && !err)
4701 err = ret;
4702 }
4703 }
Chris Mason28f75a02015-02-04 06:59:29 -08004704 /*
4705 * if we failed to refill our space rsv, bail out
4706 * and let the transaction restart
4707 */
4708 if (should_end) {
4709 err = -EAGAIN;
4710 goto error;
4711 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004712 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004713 } else {
4714 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004715 }
Chris Mason39279cc2007-06-12 06:35:45 -04004716 }
Yan, Zheng80825102009-11-12 09:35:36 +00004717out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004718 if (pending_del_nr) {
4719 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4720 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004721 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004722 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004723 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004724error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004725 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4726 ASSERT(last_size >= new_size);
4727 if (!err && last_size > new_size)
4728 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004729 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004730 }
Chris Mason28ed1342014-12-17 09:41:04 -08004731
Chris Mason39279cc2007-06-12 06:35:45 -04004732 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004733
Byongho Leeee221842015-12-15 01:42:10 +09004734 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004735 unsigned long updates = trans->delayed_ref_updates;
4736 if (updates) {
4737 trans->delayed_ref_updates = 0;
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02004738 ret = btrfs_run_delayed_refs(trans, updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004739 if (ret && !err)
4740 err = ret;
4741 }
4742 }
Yan, Zheng80825102009-11-12 09:35:36 +00004743 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004744}
4745
Chris Masona52d9a82007-08-27 16:49:44 -04004746/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004748 * @inode - inode that we're zeroing
4749 * @from - the offset to start zeroing
4750 * @len - the length to zero, 0 to zero the entire range respective to the
4751 * offset
4752 * @front - zero up to the offset instead of from the offset on
4753 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304754 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004755 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004756 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304757int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004758 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004759{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004761 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004762 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4763 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004764 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004765 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004766 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004767 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004768 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004770 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004771 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004772 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304773 u64 block_start;
4774 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004775
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004776 if (IS_ALIGNED(offset, blocksize) &&
4777 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004778 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779
Josef Bacik8b62f872017-10-19 14:15:55 -04004780 block_start = round_down(from, blocksize);
4781 block_end = block_start + blocksize - 1;
4782
Qu Wenruo364ecf32017-02-27 15:10:38 +08004783 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004784 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004785 if (ret)
4786 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004787
Chris Mason211c17f2008-05-15 09:13:45 -04004788again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004789 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004790 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004791 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004792 block_start, blocksize, true);
4793 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004794 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004795 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004796 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797
Chris Masona52d9a82007-08-27 16:49:44 -04004798 if (!PageUptodate(page)) {
4799 ret = btrfs_readpage(NULL, page);
4800 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004801 if (page->mapping != mapping) {
4802 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004803 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004804 goto again;
4805 }
Chris Masona52d9a82007-08-27 16:49:44 -04004806 if (!PageUptodate(page)) {
4807 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004808 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004809 }
4810 }
Chris Mason211c17f2008-05-15 09:13:45 -04004811 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004812
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004814 set_page_extent_mapped(page);
4815
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004817 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004819 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004820 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004821 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004822 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004823 btrfs_put_ordered_extent(ordered);
4824 goto again;
4825 }
4826
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004828 EXTENT_DIRTY | EXTENT_DELALLOC |
4829 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004830 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004831
Filipe Mananae3b8a482017-11-04 00:16:59 +00004832 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004833 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004834 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304835 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004836 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004837 goto out_unlock;
4838 }
4839
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004841 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304842 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004843 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004844 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304845 memset(kaddr + (block_start - page_offset(page)),
4846 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004847 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304848 memset(kaddr + (block_start - page_offset(page)) + offset,
4849 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004850 flush_dcache_page(page);
4851 kunmap(page);
4852 }
Chris Mason247e7432008-07-17 12:53:51 -04004853 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004854 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004855 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004856
Chris Mason89642222008-07-24 09:41:53 -04004857out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004858 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004859 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004860 blocksize, true);
4861 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004862 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004863 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004864out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004865 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004866 return ret;
4867}
4868
Josef Bacik16e75492013-10-22 12:18:51 -04004869static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4870 u64 offset, u64 len)
4871{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004873 struct btrfs_trans_handle *trans;
4874 int ret;
4875
4876 /*
4877 * Still need to make sure the inode looks like it's been updated so
4878 * that any holes get logged if we fsync.
4879 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004880 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4881 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004882 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4883 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4884 return 0;
4885 }
4886
4887 /*
4888 * 1 - for the one we're dropping
4889 * 1 - for the one we're adding
4890 * 1 - for updating the inode.
4891 */
4892 trans = btrfs_start_transaction(root, 3);
4893 if (IS_ERR(trans))
4894 return PTR_ERR(trans);
4895
4896 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4897 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004898 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004899 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004900 return ret;
4901 }
4902
David Sterbaf85b7372017-01-20 14:54:07 +01004903 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4904 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004905 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004906 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004907 else
4908 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004909 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004910 return ret;
4911}
4912
Josef Bacik695a0d02011-03-04 15:46:53 -05004913/*
4914 * This function puts in dummy file extents for the area we're creating a hole
4915 * for. So if we are truncating this file to a larger size we need to insert
4916 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4917 * the range between oldsize and size
4918 */
Josef Bacika41ad392011-01-31 15:30:16 -05004919int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004922 struct btrfs_root *root = BTRFS_I(inode)->root;
4923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004924 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004925 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004927 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4928 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004929 u64 last_byte;
4930 u64 cur_offset;
4931 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004932 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004933
Josef Bacika71754f2013-06-17 17:14:39 -04004934 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 * If our size started in the middle of a block we need to zero out the
4936 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004937 * expose stale data.
4938 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304939 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004940 if (err)
4941 return err;
4942
Yan Zheng9036c102008-10-30 14:19:41 -04004943 if (size <= hole_start)
4944 return 0;
4945
Yan Zheng9036c102008-10-30 14:19:41 -04004946 while (1) {
4947 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004948
David Sterbaff13db42015-12-03 14:30:40 +01004949 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004950 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004951 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004952 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004953 if (!ordered)
4954 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004955 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004956 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004957 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004958 btrfs_put_ordered_extent(ordered);
4959 }
4960
Yan Zheng9036c102008-10-30 14:19:41 -04004961 cur_offset = hole_start;
4962 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004963 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004964 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004965 if (IS_ERR(em)) {
4966 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004967 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004968 break;
4969 }
Yan Zheng9036c102008-10-30 14:19:41 -04004970 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004971 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004972 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004973 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004974 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004975
Josef Bacik16e75492013-10-22 12:18:51 -04004976 err = maybe_insert_hole(root, inode, cur_offset,
4977 hole_size);
4978 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004979 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004980 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004981 cur_offset + hole_size - 1, 0);
4982 hole_em = alloc_extent_map();
4983 if (!hole_em) {
4984 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4985 &BTRFS_I(inode)->runtime_flags);
4986 goto next;
4987 }
4988 hole_em->start = cur_offset;
4989 hole_em->len = hole_size;
4990 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004991
Josef Bacik5dc562c2012-08-17 13:14:17 -04004992 hole_em->block_start = EXTENT_MAP_HOLE;
4993 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004994 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004995 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004996 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004997 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004998 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004999
5000 while (1) {
5001 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005002 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005003 write_unlock(&em_tree->lock);
5004 if (err != -EEXIST)
5005 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005006 btrfs_drop_extent_cache(BTRFS_I(inode),
5007 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005008 cur_offset +
5009 hole_size - 1, 0);
5010 }
5011 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005012 }
Josef Bacik16e75492013-10-22 12:18:51 -04005013next:
Yan Zheng9036c102008-10-30 14:19:41 -04005014 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005015 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005016 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005017 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005018 break;
5019 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005020 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005021 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005022 return err;
5023}
5024
Eric Sandeen3972f262013-01-12 02:57:22 +00005025static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005026{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005027 struct btrfs_root *root = BTRFS_I(inode)->root;
5028 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005029 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005030 loff_t newsize = attr->ia_size;
5031 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005032 int ret;
5033
Eric Sandeen3972f262013-01-12 02:57:22 +00005034 /*
5035 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5036 * special case where we need to update the times despite not having
5037 * these flags set. For all other operations the VFS set these flags
5038 * explicitly if it wants a timestamp update.
5039 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005040 if (newsize != oldsize) {
5041 inode_inc_iversion(inode);
5042 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5043 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005044 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005045 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005046
Josef Bacika41ad392011-01-31 15:30:16 -05005047 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005048 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005049 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005050 * This is to ensure the snapshot captures a fully consistent
5051 * state of this file - if the snapshot captures this expanding
5052 * truncation, it must capture all writes that happened before
5053 * this truncation.
5054 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005055 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005056 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005057 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005058 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005059 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005060 }
Yan, Zheng80825102009-11-12 09:35:36 +00005061
Miao Xief4a2f4c2011-12-14 20:12:01 -05005062 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005063 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005064 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005065 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005066 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005067
5068 i_size_write(inode, newsize);
5069 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305070 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005071 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005072 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005073 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005074 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005075
Josef Bacika41ad392011-01-31 15:30:16 -05005076 /*
5077 * We're truncating a file that used to have good data down to
5078 * zero. Make sure it gets into the ordered flush list so that
5079 * any new writes get down to disk quickly.
5080 */
5081 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005082 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5083 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005084
Josef Bacikf3fe8202013-01-07 17:03:21 -05005085 /*
5086 * 1 for the orphan item we're going to add
5087 * 1 for the orphan item deletion.
5088 */
5089 trans = btrfs_start_transaction(root, 2);
5090 if (IS_ERR(trans))
5091 return PTR_ERR(trans);
5092
5093 /*
5094 * We need to do this in case we fail at _any_ point during the
5095 * actual truncate. Once we do the truncate_setsize we could
5096 * invalidate pages which forces any outstanding ordered io to
5097 * be instantly completed which will give us extents that need
5098 * to be truncated. If we fail to get an orphan inode down we
5099 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005100 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005101 * will be consistent.
5102 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005103 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005104 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005105 if (ret)
5106 return ret;
5107
Josef Bacika41ad392011-01-31 15:30:16 -05005108 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5109 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005110
5111 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005112 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005113 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005114 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005115
Filipe Manana213e8c52018-02-06 20:40:31 +00005116 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005117 if (ret && inode->i_nlink) {
5118 int err;
5119
Liu Bo19fd2df2016-12-01 13:01:02 -08005120 /* To get a stable disk_i_size */
5121 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5122 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005123 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005124 return err;
5125 }
5126
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005127 /*
5128 * failed to truncate, disk_i_size is only adjusted down
5129 * as we remove extents, so it should represent the true
5130 * size of the inode, so reset the in memory size and
5131 * delete our orphan entry.
5132 */
5133 trans = btrfs_join_transaction(root);
5134 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005135 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005136 return ret;
5137 }
5138 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005139 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005140 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005141 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005142 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005143 }
Yan, Zheng80825102009-11-12 09:35:36 +00005144 }
5145
Josef Bacika41ad392011-01-31 15:30:16 -05005146 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005147}
5148
Chris Mason39279cc2007-06-12 06:35:45 -04005149static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5150{
David Howells2b0143b2015-03-17 22:25:59 +00005151 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005152 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005153 int err;
5154
Li Zefanb83cc962010-12-20 16:04:08 +08005155 if (btrfs_root_readonly(root))
5156 return -EROFS;
5157
Jan Kara31051c82016-05-26 16:55:18 +02005158 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005159 if (err)
5160 return err;
5161
Chris Mason5a3f23d2009-03-31 13:27:11 -04005162 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005163 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005164 if (err)
5165 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005166 }
Yan Zheng9036c102008-10-30 14:19:41 -04005167
Christoph Hellwig10257742010-06-04 11:30:02 +02005168 if (attr->ia_valid) {
5169 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005170 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005171 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005172
Josef Bacik22c44fe2011-11-30 10:45:38 -05005173 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005174 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005175 }
5176
Chris Mason39279cc2007-06-12 06:35:45 -04005177 return err;
5178}
Chris Mason61295eb2008-01-14 16:24:38 -05005179
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005180/*
5181 * While truncating the inode pages during eviction, we get the VFS calling
5182 * btrfs_invalidatepage() against each page of the inode. This is slow because
5183 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5184 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5185 * extent_state structures over and over, wasting lots of time.
5186 *
5187 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5188 * those expensive operations on a per page basis and do only the ordered io
5189 * finishing, while we release here the extent_map and extent_state structures,
5190 * without the excessive merging and splitting.
5191 */
5192static void evict_inode_truncate_pages(struct inode *inode)
5193{
5194 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5195 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5196 struct rb_node *node;
5197
5198 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005199 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005200
5201 write_lock(&map_tree->lock);
5202 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5203 struct extent_map *em;
5204
5205 node = rb_first(&map_tree->map);
5206 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005207 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5208 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005209 remove_extent_mapping(map_tree, em);
5210 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005211 if (need_resched()) {
5212 write_unlock(&map_tree->lock);
5213 cond_resched();
5214 write_lock(&map_tree->lock);
5215 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005216 }
5217 write_unlock(&map_tree->lock);
5218
Filipe Manana6ca07092015-05-26 00:55:42 +01005219 /*
5220 * Keep looping until we have no more ranges in the io tree.
5221 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005222 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5223 * still in progress (unlocked the pages in the bio but did not yet
5224 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005225 * ranges can still be locked and eviction started because before
5226 * submitting those bios, which are executed by a separate task (work
5227 * queue kthread), inode references (inode->i_count) were not taken
5228 * (which would be dropped in the end io callback of each bio).
5229 * Therefore here we effectively end up waiting for those bios and
5230 * anyone else holding locked ranges without having bumped the inode's
5231 * reference count - if we don't do it, when they access the inode's
5232 * io_tree to unlock a range it may be too late, leading to an
5233 * use-after-free issue.
5234 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005235 spin_lock(&io_tree->lock);
5236 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5237 struct extent_state *state;
5238 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005239 u64 start;
5240 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005241
5242 node = rb_first(&io_tree->state);
5243 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005244 start = state->start;
5245 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005246 spin_unlock(&io_tree->lock);
5247
David Sterbaff13db42015-12-03 14:30:40 +01005248 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005249
5250 /*
5251 * If still has DELALLOC flag, the extent didn't reach disk,
5252 * and its reserved space won't be freed by delayed_ref.
5253 * So we need to free its reserved space here.
5254 * (Refer to comment in btrfs_invalidatepage, case 2)
5255 *
5256 * Note, end is the bytenr of last byte, so we need + 1 here.
5257 */
5258 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005259 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005260
Filipe Manana6ca07092015-05-26 00:55:42 +01005261 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005262 EXTENT_LOCKED | EXTENT_DIRTY |
5263 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005264 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005265
Filipe Manana7064dd52014-08-08 02:47:05 +01005266 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005267 spin_lock(&io_tree->lock);
5268 }
5269 spin_unlock(&io_tree->lock);
5270}
5271
Al Virobd555972010-06-07 11:35:40 -04005272void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005273{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005275 struct btrfs_trans_handle *trans;
5276 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005277 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005278 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005279 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005280 int ret;
5281
liubo1abe9b82011-03-24 11:18:59 +00005282 trace_btrfs_inode_evict(inode);
5283
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005284 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005285 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005286 return;
5287 }
5288
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005289 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005290
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005291 evict_inode_truncate_pages(inode);
5292
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005293 if (inode->i_nlink &&
5294 ((btrfs_root_refs(&root->root_item) != 0 &&
5295 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005296 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005297 goto no_delete;
5298
Chris Mason39279cc2007-06-12 06:35:45 -04005299 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005300 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005301 goto no_delete;
5302 }
Al Virobd555972010-06-07 11:35:40 -04005303 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005304 if (!special_file(inode->i_mode))
5305 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005306
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005307 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005308
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005309 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005310 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005311 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005312 goto no_delete;
5313 }
5314
Yan, Zheng76dda932009-09-21 16:00:26 -04005315 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005316 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5317 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005318 goto no_delete;
5319 }
5320
Nikolay Borisovaa790212017-01-10 20:35:40 +02005321 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005322 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005323 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005324 goto no_delete;
5325 }
5326
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005327 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005328 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005329 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005330 goto no_delete;
5331 }
Josef Bacik4a338542011-08-29 11:01:31 -04005332 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005333 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005334 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005335
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005336 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005337
Josef Bacik4289a662011-08-05 13:22:24 -04005338 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005339 * This is a bit simpler than btrfs_truncate since we've already
5340 * reserved our space for our orphan item in the unlink, so we just
5341 * need to reserve some slack space in case we add bytes and update
5342 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005343 */
Yan, Zheng80825102009-11-12 09:35:36 +00005344 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005345 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5346 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005347
Josef Bacik726c35f2011-09-26 15:46:06 -04005348 /*
5349 * Try and steal from the global reserve since we will
5350 * likely not use this space anyway, we want to try as
5351 * hard as possible to get this to work.
5352 */
5353 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005354 steal_from_global++;
5355 else
5356 steal_from_global = 0;
5357 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005358
Josef Bacik3bce8762015-02-24 12:35:51 -08005359 /*
5360 * steal_from_global == 0: we reserved stuff, hooray!
5361 * steal_from_global == 1: we didn't reserve stuff, boo!
5362 * steal_from_global == 2: we've committed, still not a lot of
5363 * room but maybe we'll have room in the global reserve this
5364 * time.
5365 * steal_from_global == 3: abandon all hope!
5366 */
5367 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005368 btrfs_warn(fs_info,
5369 "Could not get space for a delete, will truncate on mount %d",
5370 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005371 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005372 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005373 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005374 }
5375
Miao Xie0e8c36a2012-12-19 06:59:51 +00005376 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005377 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005378 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005379 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005380 goto no_delete;
5381 }
5382
Josef Bacik3bce8762015-02-24 12:35:51 -08005383 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005384 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005385 * sure there is room to do it, if not we need to commit and try
5386 * again.
5387 */
5388 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005389 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005390 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005391 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005392 else
5393 ret = -ENOSPC;
5394 }
5395
5396 /*
5397 * Couldn't steal from the global reserve, we have too much
5398 * pending stuff built up, commit the transaction and try it
5399 * again.
5400 */
5401 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005402 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005403 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005404 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005405 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005406 goto no_delete;
5407 }
5408 continue;
5409 } else {
5410 steal_from_global = 0;
5411 }
5412
Josef Bacik4289a662011-08-05 13:22:24 -04005413 trans->block_rsv = rsv;
5414
Yan, Zhengd68fc572010-05-16 10:49:58 -04005415 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005416 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005417 break;
5418
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005419 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005420 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005421 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005422 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005423 }
5424
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005425 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005426
Josef Bacik4ef31a42013-08-13 14:10:08 -04005427 /*
5428 * Errors here aren't a big deal, it just means we leave orphan items
5429 * in the tree. They will be cleaned up on the next mount.
5430 */
Yan, Zheng80825102009-11-12 09:35:36 +00005431 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005432 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005433 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005434 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005435 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005436 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005437
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005438 trans->block_rsv = &fs_info->trans_block_rsv;
5439 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005440 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005441 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005442
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005443 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005444 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005445no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005446 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005447 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005448}
5449
5450/*
5451 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005452 * If no dir entries were found, returns -ENOENT.
5453 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005454 */
5455static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5456 struct btrfs_key *location)
5457{
5458 const char *name = dentry->d_name.name;
5459 int namelen = dentry->d_name.len;
5460 struct btrfs_dir_item *di;
5461 struct btrfs_path *path;
5462 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005463 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005464
5465 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005466 if (!path)
5467 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005468
David Sterbaf85b7372017-01-20 14:54:07 +01005469 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5470 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005471 if (!di) {
5472 ret = -ENOENT;
5473 goto out;
5474 }
5475 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005476 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005477 goto out;
5478 }
Chris Masond3977122009-01-05 21:25:51 -05005479
Chris Mason5f39d392007-10-15 16:14:19 -04005480 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005481 if (location->type != BTRFS_INODE_ITEM_KEY &&
5482 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005483 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005484 btrfs_warn(root->fs_info,
5485"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5486 __func__, name, btrfs_ino(BTRFS_I(dir)),
5487 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005488 }
Chris Mason39279cc2007-06-12 06:35:45 -04005489out:
Chris Mason39279cc2007-06-12 06:35:45 -04005490 btrfs_free_path(path);
5491 return ret;
5492}
5493
5494/*
5495 * when we hit a tree root in a directory, the btrfs part of the inode
5496 * needs to be changed to reflect the root directory of the tree root. This
5497 * is kind of like crossing a mount point.
5498 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005499static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 struct inode *dir,
5501 struct dentry *dentry,
5502 struct btrfs_key *location,
5503 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005504{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 struct btrfs_path *path;
5506 struct btrfs_root *new_root;
5507 struct btrfs_root_ref *ref;
5508 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005509 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 int ret;
5511 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005512
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 path = btrfs_alloc_path();
5514 if (!path) {
5515 err = -ENOMEM;
5516 goto out;
5517 }
Chris Mason39279cc2007-06-12 06:35:45 -04005518
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005520 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5521 key.type = BTRFS_ROOT_REF_KEY;
5522 key.offset = location->objectid;
5523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005524 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 if (ret) {
5526 if (ret < 0)
5527 err = ret;
5528 goto out;
5529 }
Chris Mason39279cc2007-06-12 06:35:45 -04005530
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 leaf = path->nodes[0];
5532 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005533 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005534 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5535 goto out;
5536
5537 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5538 (unsigned long)(ref + 1),
5539 dentry->d_name.len);
5540 if (ret)
5541 goto out;
5542
David Sterbab3b4aa72011-04-21 01:20:15 +02005543 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005545 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 if (IS_ERR(new_root)) {
5547 err = PTR_ERR(new_root);
5548 goto out;
5549 }
5550
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 *sub_root = new_root;
5552 location->objectid = btrfs_root_dirid(&new_root->root_item);
5553 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005554 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005555 err = 0;
5556out:
5557 btrfs_free_path(path);
5558 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005559}
5560
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561static void inode_tree_add(struct inode *inode)
5562{
5563 struct btrfs_root *root = BTRFS_I(inode)->root;
5564 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005565 struct rb_node **p;
5566 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005567 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005568 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569
Al Viro1d3382cb2010-10-23 15:19:20 -04005570 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005571 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005572 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005574 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 while (*p) {
5576 parent = *p;
5577 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5578
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005579 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005580 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005581 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583 else {
5584 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005585 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005586 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005587 RB_CLEAR_NODE(parent);
5588 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005589 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 }
5591 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005592 rb_link_node(new, parent, p);
5593 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 spin_unlock(&root->inode_lock);
5595}
5596
5597static void inode_tree_del(struct inode *inode)
5598{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005599 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005601 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005603 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005607 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005609 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005610
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005611 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005612 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005613 spin_lock(&root->inode_lock);
5614 empty = RB_EMPTY_ROOT(&root->inode_tree);
5615 spin_unlock(&root->inode_lock);
5616 if (empty)
5617 btrfs_add_dead_root(root);
5618 }
5619}
5620
Jeff Mahoney143bede2012-03-01 14:56:26 +01005621void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005622{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005623 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005624 struct rb_node *node;
5625 struct rb_node *prev;
5626 struct btrfs_inode *entry;
5627 struct inode *inode;
5628 u64 objectid = 0;
5629
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005630 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005631 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005632
5633 spin_lock(&root->inode_lock);
5634again:
5635 node = root->inode_tree.rb_node;
5636 prev = NULL;
5637 while (node) {
5638 prev = node;
5639 entry = rb_entry(node, struct btrfs_inode, rb_node);
5640
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005641 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005642 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005643 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005644 node = node->rb_right;
5645 else
5646 break;
5647 }
5648 if (!node) {
5649 while (prev) {
5650 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005651 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005652 node = prev;
5653 break;
5654 }
5655 prev = rb_next(prev);
5656 }
5657 }
5658 while (node) {
5659 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005660 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005661 inode = igrab(&entry->vfs_inode);
5662 if (inode) {
5663 spin_unlock(&root->inode_lock);
5664 if (atomic_read(&inode->i_count) > 1)
5665 d_prune_aliases(inode);
5666 /*
Al Viro45321ac2010-06-07 13:43:19 -04005667 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005668 * the inode cache when its usage count
5669 * hits zero.
5670 */
5671 iput(inode);
5672 cond_resched();
5673 spin_lock(&root->inode_lock);
5674 goto again;
5675 }
5676
5677 if (cond_resched_lock(&root->inode_lock))
5678 goto again;
5679
5680 node = rb_next(node);
5681 }
5682 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005683}
5684
Chris Masone02119d2008-09-05 16:13:11 -04005685static int btrfs_init_locked_inode(struct inode *inode, void *p)
5686{
5687 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005688 inode->i_ino = args->location->objectid;
5689 memcpy(&BTRFS_I(inode)->location, args->location,
5690 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005691 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005692 return 0;
5693}
5694
5695static int btrfs_find_actor(struct inode *inode, void *opaque)
5696{
5697 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005698 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005699 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005700}
5701
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005702static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005703 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005704 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005705{
5706 struct inode *inode;
5707 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005708 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005709
Chris Mason90d3e592014-01-09 17:28:00 -08005710 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005711 args.root = root;
5712
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005713 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005714 btrfs_init_locked_inode,
5715 (void *)&args);
5716 return inode;
5717}
5718
Balaji Rao1a54ef82008-07-21 02:01:04 +05305719/* Get an inode object given its location and corresponding root.
5720 * Returns in *is_new if the inode was read from disk
5721 */
5722struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005723 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305724{
5725 struct inode *inode;
5726
Chris Mason90d3e592014-01-09 17:28:00 -08005727 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305728 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005729 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305730
5731 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005732 int ret;
5733
5734 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005735 if (!is_bad_inode(inode)) {
5736 inode_tree_add(inode);
5737 unlock_new_inode(inode);
5738 if (new)
5739 *new = 1;
5740 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005741 unlock_new_inode(inode);
5742 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005743 ASSERT(ret < 0);
5744 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005745 }
5746 }
5747
Balaji Rao1a54ef82008-07-21 02:01:04 +05305748 return inode;
5749}
5750
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005751static struct inode *new_simple_dir(struct super_block *s,
5752 struct btrfs_key *key,
5753 struct btrfs_root *root)
5754{
5755 struct inode *inode = new_inode(s);
5756
5757 if (!inode)
5758 return ERR_PTR(-ENOMEM);
5759
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760 BTRFS_I(inode)->root = root;
5761 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005762 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763
5764 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005765 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005766 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 inode->i_fop = &simple_dir_operations;
5768 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005769 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305770 inode->i_atime = inode->i_mtime;
5771 inode->i_ctime = inode->i_mtime;
5772 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773
5774 return inode;
5775}
5776
Chris Mason3de45862008-11-17 21:02:50 -05005777struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005778{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005779 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005780 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005781 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005782 struct btrfs_root *sub_root = root;
5783 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005784 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005785 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005786
5787 if (dentry->d_name.len > BTRFS_NAME_LEN)
5788 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005789
Jeff Layton39e3c952012-11-28 11:30:53 -05005790 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005791 if (ret < 0)
5792 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005793
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005795 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005797 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005799 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005800 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801 &location, &sub_root);
5802 if (ret < 0) {
5803 if (ret != -ENOENT)
5804 inode = ERR_PTR(ret);
5805 else
5806 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5807 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005808 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005809 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005810 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005811
Julia Lawall34d19ba2011-01-24 19:55:19 +00005812 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005813 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005814 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005815 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005816 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005817 if (ret) {
5818 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005819 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005820 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005821 }
5822
Chris Mason3de45862008-11-17 21:02:50 -05005823 return inode;
5824}
5825
Nick Pigginfe15ce42011-01-07 17:49:23 +11005826static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005827{
5828 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005829 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005830
Li Zefan848cce02012-02-21 17:04:28 +08005831 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005832 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005833
Li Zefan848cce02012-02-21 17:04:28 +08005834 if (inode) {
5835 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005836 if (btrfs_root_refs(&root->root_item) == 0)
5837 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005838
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005839 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005840 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005841 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005842 return 0;
5843}
5844
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005845static void btrfs_dentry_release(struct dentry *dentry)
5846{
Daeseok Youn944a4512014-04-14 15:37:02 +09005847 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005848}
5849
Chris Mason3de45862008-11-17 21:02:50 -05005850static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005851 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005852{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005853 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005854
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005855 inode = btrfs_lookup_dentry(dir, dentry);
5856 if (IS_ERR(inode)) {
5857 if (PTR_ERR(inode) == -ENOENT)
5858 inode = NULL;
5859 else
5860 return ERR_CAST(inode);
5861 }
5862
Al Viro41d28bc2014-10-12 22:24:21 -04005863 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005864}
5865
Miao Xie16cdcec2011-04-22 18:12:22 +08005866unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005867 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5868};
5869
Josef Bacik23b5ec72017-07-24 15:14:25 -04005870/*
5871 * All this infrastructure exists because dir_emit can fault, and we are holding
5872 * the tree lock when doing readdir. For now just allocate a buffer and copy
5873 * our information into that, and then dir_emit from the buffer. This is
5874 * similar to what NFS does, only we don't keep the buffer around in pagecache
5875 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5876 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5877 * tree lock.
5878 */
5879static int btrfs_opendir(struct inode *inode, struct file *file)
5880{
5881 struct btrfs_file_private *private;
5882
5883 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5884 if (!private)
5885 return -ENOMEM;
5886 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5887 if (!private->filldir_buf) {
5888 kfree(private);
5889 return -ENOMEM;
5890 }
5891 file->private_data = private;
5892 return 0;
5893}
5894
5895struct dir_entry {
5896 u64 ino;
5897 u64 offset;
5898 unsigned type;
5899 int name_len;
5900};
5901
5902static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5903{
5904 while (entries--) {
5905 struct dir_entry *entry = addr;
5906 char *name = (char *)(entry + 1);
5907
5908 ctx->pos = entry->offset;
5909 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5910 entry->type))
5911 return 1;
5912 addr += sizeof(struct dir_entry) + entry->name_len;
5913 ctx->pos++;
5914 }
5915 return 0;
5916}
5917
Al Viro9cdda8d2013-05-22 16:48:09 -04005918static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005919{
Al Viro9cdda8d2013-05-22 16:48:09 -04005920 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005921 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005922 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005923 struct btrfs_dir_item *di;
5924 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005925 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005927 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005928 struct list_head ins_list;
5929 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005931 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005932 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005933 char *name_ptr;
5934 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005935 int entries = 0;
5936 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005937 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005938 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005939
Al Viro9cdda8d2013-05-22 16:48:09 -04005940 if (!dir_emit_dots(file, ctx))
5941 return 0;
5942
David Woodhouse49593bf2008-08-17 17:08:36 +01005943 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005944 if (!path)
5945 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005946
Josef Bacik23b5ec72017-07-24 15:14:25 -04005947 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005948 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005949
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005950 INIT_LIST_HEAD(&ins_list);
5951 INIT_LIST_HEAD(&del_list);
5952 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005953
Josef Bacik23b5ec72017-07-24 15:14:25 -04005954again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005955 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005956 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005957 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005958
Chris Mason39279cc2007-06-12 06:35:45 -04005959 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5960 if (ret < 0)
5961 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005962
5963 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005964 struct dir_entry *entry;
5965
Chris Mason5f39d392007-10-15 16:14:19 -04005966 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005967 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005968 if (slot >= btrfs_header_nritems(leaf)) {
5969 ret = btrfs_next_leaf(root, path);
5970 if (ret < 0)
5971 goto err;
5972 else if (ret > 0)
5973 break;
5974 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 }
Chris Mason3de45862008-11-17 21:02:50 -05005976
Chris Mason5f39d392007-10-15 16:14:19 -04005977 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5978
5979 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005980 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005982 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005983 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005984 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005985 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005986 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005987 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005988 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005989 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5990 PAGE_SIZE) {
5991 btrfs_release_path(path);
5992 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5993 if (ret)
5994 goto nopos;
5995 addr = private->filldir_buf;
5996 entries = 0;
5997 total_len = 0;
5998 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005999 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006000
6001 entry = addr;
6002 entry->name_len = name_len;
6003 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006004 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6005 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006006 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006007 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006008 entry->ino = location.objectid;
6009 entry->offset = found_key.offset;
6010 entries++;
6011 addr += sizeof(struct dir_entry) + name_len;
6012 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006013next:
6014 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006015 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006016 btrfs_release_path(path);
6017
6018 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6019 if (ret)
6020 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006021
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006022 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006023 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006024 goto nopos;
6025
Zach Browndb62efb2013-07-11 16:19:42 -07006026 /*
6027 * Stop new entries from being returned after we return the last
6028 * entry.
6029 *
6030 * New directory entries are assigned a strictly increasing
6031 * offset. This means that new entries created during readdir
6032 * are *guaranteed* to be seen in the future by that readdir.
6033 * This has broken buggy programs which operate on names as
6034 * they're returned by readdir. Until we re-use freed offsets
6035 * we have this hack to stop new entries from being returned
6036 * under the assumption that they'll never reach this huge
6037 * offset.
6038 *
6039 * This is being careful not to overflow 32bit loff_t unless the
6040 * last entry requires it because doing so has broken 32bit apps
6041 * in the past.
6042 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006043 if (ctx->pos >= INT_MAX)
6044 ctx->pos = LLONG_MAX;
6045 else
6046 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006047nopos:
6048 ret = 0;
6049err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006050 if (put)
6051 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006052 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006053 return ret;
6054}
6055
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006056int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006057{
6058 struct btrfs_root *root = BTRFS_I(inode)->root;
6059 struct btrfs_trans_handle *trans;
6060 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006061 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006062
Josef Bacik72ac3c02012-05-23 14:13:11 -04006063 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006064 return 0;
6065
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006066 if (btrfs_fs_closing(root->fs_info) &&
6067 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006068 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006069
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006070 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006071 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006072 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006073 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006074 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006075 if (IS_ERR(trans))
6076 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006077 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006078 }
6079 return ret;
6080}
6081
6082/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006083 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006084 * inode changes. But, it is most likely to find the inode in cache.
6085 * FIXME, needs more benchmarking...there are no reasons other than performance
6086 * to keep or drop this code.
6087 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006088static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006089{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006090 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006091 struct btrfs_root *root = BTRFS_I(inode)->root;
6092 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006093 int ret;
6094
Josef Bacik72ac3c02012-05-23 14:13:11 -04006095 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006096 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006098 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006099 if (IS_ERR(trans))
6100 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006101
6102 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006103 if (ret && ret == -ENOSPC) {
6104 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006105 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006106 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006107 if (IS_ERR(trans))
6108 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006109
Chris Mason94b60442010-05-26 11:02:00 -04006110 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006111 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006112 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006113 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006114 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006115
6116 return ret;
6117}
6118
6119/*
6120 * This is a copy of file_update_time. We need this so we can return error on
6121 * ENOSPC for updating the inode in the case of file write and mmap writes.
6122 */
Josef Bacike41f9412012-03-26 09:46:47 -04006123static int btrfs_update_time(struct inode *inode, struct timespec *now,
6124 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125{
Alexander Block2bc5565282012-06-15 09:49:33 +02006126 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006127 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006128
6129 if (btrfs_root_readonly(root))
6130 return -EROFS;
6131
Josef Bacike41f9412012-03-26 09:46:47 -04006132 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006133 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006134 if (flags & S_CTIME)
6135 inode->i_ctime = *now;
6136 if (flags & S_MTIME)
6137 inode->i_mtime = *now;
6138 if (flags & S_ATIME)
6139 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006140 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006141}
6142
Chris Masond352ac62008-09-29 15:18:18 -04006143/*
6144 * find the highest existing sequence number in a directory
6145 * and then set the in-memory index_cnt variable to reflect
6146 * free sequence numbers
6147 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006148static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006149{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006150 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006151 struct btrfs_key key, found_key;
6152 struct btrfs_path *path;
6153 struct extent_buffer *leaf;
6154 int ret;
6155
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006156 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006157 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006158 key.offset = (u64)-1;
6159
6160 path = btrfs_alloc_path();
6161 if (!path)
6162 return -ENOMEM;
6163
6164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6165 if (ret < 0)
6166 goto out;
6167 /* FIXME: we should be able to handle this */
6168 if (ret == 0)
6169 goto out;
6170 ret = 0;
6171
6172 /*
6173 * MAGIC NUMBER EXPLANATION:
6174 * since we search a directory based on f_pos we have to start at 2
6175 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6176 * else has to start at 2
6177 */
6178 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006179 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006180 goto out;
6181 }
6182
6183 path->slots[0]--;
6184
6185 leaf = path->nodes[0];
6186 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6187
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006188 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006189 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006190 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006191 goto out;
6192 }
6193
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006194 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006195out:
6196 btrfs_free_path(path);
6197 return ret;
6198}
6199
Chris Masond352ac62008-09-29 15:18:18 -04006200/*
6201 * helper to find a free sequence number in a given directory. This current
6202 * code is very simple, later versions will do smarter things in the btree
6203 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006204int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006205{
6206 int ret = 0;
6207
Nikolay Borisov877574e2017-02-20 13:50:33 +02006208 if (dir->index_cnt == (u64)-1) {
6209 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006210 if (ret) {
6211 ret = btrfs_set_inode_index_count(dir);
6212 if (ret)
6213 return ret;
6214 }
Josef Bacikaec74772008-07-24 12:12:38 -04006215 }
6216
Nikolay Borisov877574e2017-02-20 13:50:33 +02006217 *index = dir->index_cnt;
6218 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006219
6220 return ret;
6221}
6222
Chris Masonb0d5d102014-09-08 13:08:51 -07006223static int btrfs_insert_inode_locked(struct inode *inode)
6224{
6225 struct btrfs_iget_args args;
6226 args.location = &BTRFS_I(inode)->location;
6227 args.root = BTRFS_I(inode)->root;
6228
6229 return insert_inode_locked4(inode,
6230 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6231 btrfs_find_actor, &args);
6232}
6233
Anand Jain19aee8d2017-07-18 17:37:05 +08006234/*
6235 * Inherit flags from the parent inode.
6236 *
6237 * Currently only the compression flags and the cow flags are inherited.
6238 */
6239static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6240{
6241 unsigned int flags;
6242
6243 if (!dir)
6244 return;
6245
6246 flags = BTRFS_I(dir)->flags;
6247
6248 if (flags & BTRFS_INODE_NOCOMPRESS) {
6249 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6250 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6251 } else if (flags & BTRFS_INODE_COMPRESS) {
6252 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6253 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6254 }
6255
6256 if (flags & BTRFS_INODE_NODATACOW) {
6257 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6258 if (S_ISREG(inode->i_mode))
6259 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6260 }
6261
6262 btrfs_update_iflags(inode);
6263}
6264
Chris Mason39279cc2007-06-12 06:35:45 -04006265static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6266 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006267 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006268 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006269 u64 ref_objectid, u64 objectid,
6270 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006271{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006272 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006273 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006274 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006275 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006276 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006277 struct btrfs_inode_ref *ref;
6278 struct btrfs_key key[2];
6279 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006281 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006282 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006283
Chris Mason5f39d392007-10-15 16:14:19 -04006284 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006285 if (!path)
6286 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006287
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006288 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006289 if (!inode) {
6290 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006291 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006292 }
Chris Mason39279cc2007-06-12 06:35:45 -04006293
Li Zefan581bb052011-04-20 10:06:11 +08006294 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006295 * O_TMPFILE, set link count to 0, so that after this point,
6296 * we fill in an inode item with the correct link count.
6297 */
6298 if (!name)
6299 set_nlink(inode, 0);
6300
6301 /*
Li Zefan581bb052011-04-20 10:06:11 +08006302 * we have to initialize this early, so we can reclaim the inode
6303 * number if we fail afterwards in this function.
6304 */
6305 inode->i_ino = objectid;
6306
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006307 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006308 trace_btrfs_inode_request(dir);
6309
Nikolay Borisov877574e2017-02-20 13:50:33 +02006310 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006311 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006312 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006313 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006314 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006315 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006316 } else if (dir) {
6317 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006318 }
6319 /*
6320 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006321 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006322 * number
6323 */
6324 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006325 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006326 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006327 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006328 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006329
Josef Bacik5dc562c2012-08-17 13:14:17 -04006330 /*
6331 * We could have gotten an inode number from somebody who was fsynced
6332 * and then removed in this same transaction, so let's just set full
6333 * sync since it will be a full sync anyway and this will blow away the
6334 * old info in the log.
6335 */
6336 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6337
Chris Mason9c583092008-01-29 15:15:18 -05006338 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006339 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006340 key[0].offset = 0;
6341
Chris Mason9c583092008-01-29 15:15:18 -05006342 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006343
6344 if (name) {
6345 /*
6346 * Start new inodes with an inode_ref. This is slightly more
6347 * efficient for small numbers of hard links since they will
6348 * be packed into one item. Extended refs will kick in if we
6349 * add more hard links than can fit in the ref item.
6350 */
6351 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006352 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006353 key[1].offset = ref_objectid;
6354
6355 sizes[1] = name_len + sizeof(*ref);
6356 }
Chris Mason9c583092008-01-29 15:15:18 -05006357
Chris Masonb0d5d102014-09-08 13:08:51 -07006358 location = &BTRFS_I(inode)->location;
6359 location->objectid = objectid;
6360 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006361 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006362
6363 ret = btrfs_insert_inode_locked(inode);
6364 if (ret < 0)
6365 goto fail;
6366
Chris Masonb9473432009-03-13 11:00:37 -04006367 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006368 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006369 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006370 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006371
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006372 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006373 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306374
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006375 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306376 inode->i_atime = inode->i_mtime;
6377 inode->i_ctime = inode->i_mtime;
6378 BTRFS_I(inode)->i_otime = inode->i_mtime;
6379
Chris Mason5f39d392007-10-15 16:14:19 -04006380 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6381 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006382 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006383 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006384 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006385
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006386 if (name) {
6387 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6388 struct btrfs_inode_ref);
6389 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6390 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6391 ptr = (unsigned long)(ref + 1);
6392 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6393 }
Chris Mason9c583092008-01-29 15:15:18 -05006394
Chris Mason5f39d392007-10-15 16:14:19 -04006395 btrfs_mark_buffer_dirty(path->nodes[0]);
6396 btrfs_free_path(path);
6397
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006398 btrfs_inherit_iflags(inode, dir);
6399
Al Viro569254b2011-07-24 17:08:40 -04006400 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006401 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006402 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006403 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006404 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6405 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006406 }
6407
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006408 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006409
6410 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006411 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006412
Alexander Block8ea05e32012-07-25 17:35:53 +02006413 btrfs_update_root_times(trans, root);
6414
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006415 ret = btrfs_inode_inherit_props(trans, inode, dir);
6416 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006417 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006418 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006419 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006420
Chris Mason39279cc2007-06-12 06:35:45 -04006421 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006422
6423fail_unlock:
6424 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006425fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006426 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006427 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006428 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006429 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006430 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006431}
6432
6433static inline u8 btrfs_inode_type(struct inode *inode)
6434{
6435 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6436}
6437
Chris Masond352ac62008-09-29 15:18:18 -04006438/*
6439 * utility function to add 'inode' into 'parent_inode' with
6440 * a give name and a given sequence number.
6441 * if 'add_backref' is true, also insert a backref from the
6442 * inode to the parent directory.
6443 */
Chris Masone02119d2008-09-05 16:13:11 -04006444int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006445 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006446 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006447{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006449 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006450 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006451 struct btrfs_root *root = parent_inode->root;
6452 u64 ino = btrfs_ino(inode);
6453 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006454
Li Zefan33345d012011-04-20 10:31:50 +08006455 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006456 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006457 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006458 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006459 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006460 key.offset = 0;
6461 }
Chris Mason39279cc2007-06-12 06:35:45 -04006462
Li Zefan33345d012011-04-20 10:31:50 +08006463 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006464 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6465 root->root_key.objectid, parent_ino,
6466 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006467 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006468 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6469 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006470 }
6471
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006472 /* Nothing to clean up yet */
6473 if (ret)
6474 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006475
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006476 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6477 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006478 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006479 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006480 goto fail_dir_item;
6481 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006482 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006483 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006484 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006485
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006486 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006487 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006488 inode_inc_iversion(&parent_inode->vfs_inode);
6489 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6490 current_time(&parent_inode->vfs_inode);
6491 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006492 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006493 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006494 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006495
6496fail_dir_item:
6497 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6498 u64 local_index;
6499 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006500 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6501 root->root_key.objectid, parent_ino,
6502 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006503
6504 } else if (add_backref) {
6505 u64 local_index;
6506 int err;
6507
6508 err = btrfs_del_inode_ref(trans, root, name, name_len,
6509 ino, parent_ino, &local_index);
6510 }
6511 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006512}
6513
6514static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006515 struct btrfs_inode *dir, struct dentry *dentry,
6516 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006517{
Josef Bacika1b075d2010-11-19 20:36:11 +00006518 int err = btrfs_add_link(trans, dir, inode,
6519 dentry->d_name.name, dentry->d_name.len,
6520 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006521 if (err > 0)
6522 err = -EEXIST;
6523 return err;
6524}
6525
Josef Bacik618e21d2007-07-11 10:18:17 -04006526static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006527 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006528{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006529 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006530 struct btrfs_trans_handle *trans;
6531 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006532 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006533 int err;
6534 int drop_inode = 0;
6535 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006536 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006537
Josef Bacik9ed74f22009-09-11 16:12:44 -04006538 /*
6539 * 2 for inode item and ref
6540 * 2 for dir items
6541 * 1 for xattr if selinux is on
6542 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006543 trans = btrfs_start_transaction(root, 5);
6544 if (IS_ERR(trans))
6545 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006546
Li Zefan581bb052011-04-20 10:06:11 +08006547 err = btrfs_find_free_ino(root, &objectid);
6548 if (err)
6549 goto out_unlock;
6550
Josef Bacikaec74772008-07-24 12:12:38 -04006551 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006552 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6553 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006554 if (IS_ERR(inode)) {
6555 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006556 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006557 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006558
Casey Schauflerad19db72011-12-15 10:09:07 -05006559 /*
6560 * If the active LSM wants to access the inode during
6561 * d_instantiate it needs these. Smack checks to see
6562 * if the filesystem supports xattrs by looking at the
6563 * ops vector.
6564 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006565 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006566 init_special_inode(inode, inode->i_mode, rdev);
6567
6568 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006570 goto out_unlock_inode;
6571
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006572 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6573 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006574 if (err) {
6575 goto out_unlock_inode;
6576 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006577 btrfs_update_inode(trans, root, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04006578 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006579 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006580
Josef Bacik618e21d2007-07-11 10:18:17 -04006581out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006582 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006583 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 if (drop_inode) {
6585 inode_dec_link_count(inode);
6586 iput(inode);
6587 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006588 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006589
6590out_unlock_inode:
6591 drop_inode = 1;
6592 unlock_new_inode(inode);
6593 goto out_unlock;
6594
Josef Bacik618e21d2007-07-11 10:18:17 -04006595}
6596
Chris Mason39279cc2007-06-12 06:35:45 -04006597static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006598 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006599{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006600 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006601 struct btrfs_trans_handle *trans;
6602 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006603 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006604 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006605 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006606 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006607 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006608
Josef Bacik9ed74f22009-09-11 16:12:44 -04006609 /*
6610 * 2 for inode item and ref
6611 * 2 for dir items
6612 * 1 for xattr if selinux is on
6613 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006614 trans = btrfs_start_transaction(root, 5);
6615 if (IS_ERR(trans))
6616 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006617
Li Zefan581bb052011-04-20 10:06:11 +08006618 err = btrfs_find_free_ino(root, &objectid);
6619 if (err)
6620 goto out_unlock;
6621
Josef Bacikaec74772008-07-24 12:12:38 -04006622 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006623 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6624 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006625 if (IS_ERR(inode)) {
6626 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006627 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006628 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006629 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006630 /*
6631 * If the active LSM wants to access the inode during
6632 * d_instantiate it needs these. Smack checks to see
6633 * if the filesystem supports xattrs by looking at the
6634 * ops vector.
6635 */
6636 inode->i_fop = &btrfs_file_operations;
6637 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006638 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006639
6640 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6641 if (err)
6642 goto out_unlock_inode;
6643
6644 err = btrfs_update_inode(trans, root, inode);
6645 if (err)
6646 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006647
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006648 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6649 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006651 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006652
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006653 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006654 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006655
Chris Mason39279cc2007-06-12 06:35:45 -04006656out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006657 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006658 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006659 inode_dec_link_count(inode);
6660 iput(inode);
6661 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006662 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006663 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006664
6665out_unlock_inode:
6666 unlock_new_inode(inode);
6667 goto out_unlock;
6668
Chris Mason39279cc2007-06-12 06:35:45 -04006669}
6670
6671static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6672 struct dentry *dentry)
6673{
Filipe Manana271dba452016-01-05 16:24:05 +00006674 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006675 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006676 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006678 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006679 int err;
6680 int drop_inode = 0;
6681
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006682 /* do not allow sys_link's with other subvols of the same device */
6683 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006684 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006685
Mark Fashehf1863732012-08-08 11:32:27 -07006686 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006687 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006688
Nikolay Borisov877574e2017-02-20 13:50:33 +02006689 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006690 if (err)
6691 goto fail;
6692
Yan, Zhenga22285a2010-05-16 10:48:46 -04006693 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006694 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006695 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006696 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006697 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006698 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006699 if (IS_ERR(trans)) {
6700 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006701 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006702 goto fail;
6703 }
Chris Mason5f39d392007-10-15 16:14:19 -04006704
Miao Xie67de1172013-12-26 13:07:06 +08006705 /* There are several dir indexes for this inode, clear the cache. */
6706 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006707 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006708 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006709 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006710 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006711 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006712
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006713 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6714 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006715
Yan, Zhenga5719522009-09-24 09:17:31 -04006716 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006717 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006718 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006719 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006720 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006721 if (err)
6722 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006723 if (inode->i_nlink == 1) {
6724 /*
6725 * If new hard link count is 1, it's a file created
6726 * with open(2) O_TMPFILE flag.
6727 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006728 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006729 if (err)
6730 goto fail;
6731 }
Al Viro08c422c2011-12-23 07:58:13 -05006732 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006733 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006734 }
Chris Mason39279cc2007-06-12 06:35:45 -04006735
Chris Mason1832a6d2007-12-21 16:27:21 -05006736fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006737 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006738 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006739 if (drop_inode) {
6740 inode_dec_link_count(inode);
6741 iput(inode);
6742 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006743 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006744 return err;
6745}
6746
Al Viro18bb1db2011-07-26 01:41:39 -04006747static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006748{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006749 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006750 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006751 struct btrfs_trans_handle *trans;
6752 struct btrfs_root *root = BTRFS_I(dir)->root;
6753 int err = 0;
6754 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006755 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006756 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006757
Josef Bacik9ed74f22009-09-11 16:12:44 -04006758 /*
6759 * 2 items for inode and ref
6760 * 2 items for dir items
6761 * 1 for xattr if selinux is on
6762 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006763 trans = btrfs_start_transaction(root, 5);
6764 if (IS_ERR(trans))
6765 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006766
Li Zefan581bb052011-04-20 10:06:11 +08006767 err = btrfs_find_free_ino(root, &objectid);
6768 if (err)
6769 goto out_fail;
6770
Josef Bacikaec74772008-07-24 12:12:38 -04006771 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006772 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6773 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006774 if (IS_ERR(inode)) {
6775 err = PTR_ERR(inode);
6776 goto out_fail;
6777 }
Chris Mason5f39d392007-10-15 16:14:19 -04006778
Chris Mason39279cc2007-06-12 06:35:45 -04006779 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006780 /* these must be set before we unlock the inode */
6781 inode->i_op = &btrfs_dir_inode_operations;
6782 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006783
Eric Paris2a7dba32011-02-01 11:05:39 -05006784 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006785 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006786 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006788 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006789 err = btrfs_update_inode(trans, root, inode);
6790 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006791 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006792
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006793 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6794 dentry->d_name.name,
6795 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006796 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006797 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006798
Al Viro1e2e5472018-05-04 08:23:01 -04006799 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006800 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006801
6802out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006803 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006804 if (drop_on_err) {
6805 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006806 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006807 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006808 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006809 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006810
6811out_fail_inode:
6812 unlock_new_inode(inode);
6813 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006814}
6815
Chris Masonc8b97812008-10-29 14:49:59 -04006816static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006817 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006818 size_t pg_offset, u64 extent_offset,
6819 struct btrfs_file_extent_item *item)
6820{
6821 int ret;
6822 struct extent_buffer *leaf = path->nodes[0];
6823 char *tmp;
6824 size_t max_size;
6825 unsigned long inline_size;
6826 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006827 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006828
6829 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006830 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006831 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6832 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006833 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006834 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006835 if (!tmp)
6836 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006837 ptr = btrfs_file_extent_inline_start(item);
6838
6839 read_extent_buffer(leaf, tmp, ptr, inline_size);
6840
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006841 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006842 ret = btrfs_decompress(compress_type, tmp, page,
6843 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006844
6845 /*
6846 * decompression code contains a memset to fill in any space between the end
6847 * of the uncompressed data and the end of max_size in case the decompressed
6848 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6849 * the end of an inline extent and the beginning of the next block, so we
6850 * cover that region here.
6851 */
6852
6853 if (max_size + pg_offset < PAGE_SIZE) {
6854 char *map = kmap(page);
6855 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6856 kunmap(page);
6857 }
Chris Masonc8b97812008-10-29 14:49:59 -04006858 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006859 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006860}
6861
Chris Masond352ac62008-09-29 15:18:18 -04006862/*
6863 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006864 * the ugly parts come from merging extents from the disk with the in-ram
6865 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006866 * where the in-ram extents might be locked pending data=ordered completion.
6867 *
6868 * This also copies inline extents directly into the page.
6869 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006870struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6871 struct page *page,
6872 size_t pg_offset, u64 start, u64 len,
6873 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006874{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006876 int ret;
6877 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006878 u64 extent_start = 0;
6879 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006880 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006881 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006882 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006883 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006885 struct extent_buffer *leaf;
6886 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006887 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006888 struct extent_map_tree *em_tree = &inode->extent_tree;
6889 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006890 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006891
Chris Mason890871b2009-09-02 16:24:52 -04006892 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006893 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006894 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006895 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006896 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006897
Chris Masona52d9a82007-08-27 16:49:44 -04006898 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006899 if (em->start > start || em->start + em->len <= start)
6900 free_extent_map(em);
6901 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006902 free_extent_map(em);
6903 else
6904 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006905 }
David Sterba172ddd62011-04-21 00:48:27 +02006906 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006907 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006908 err = -ENOMEM;
6909 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006910 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006911 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006912 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006913 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006914 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006915 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006916
6917 if (!path) {
6918 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006919 if (!path) {
6920 err = -ENOMEM;
6921 goto out;
6922 }
6923 /*
6924 * Chances are we'll be called again, so go ahead and do
6925 * readahead
6926 */
David Sterbae4058b52015-11-27 16:31:35 +01006927 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006928 }
6929
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006930 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006931 if (ret < 0) {
6932 err = ret;
6933 goto out;
6934 }
6935
6936 if (ret != 0) {
6937 if (path->slots[0] == 0)
6938 goto not_found;
6939 path->slots[0]--;
6940 }
6941
Chris Mason5f39d392007-10-15 16:14:19 -04006942 leaf = path->nodes[0];
6943 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006944 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006945 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006947 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006948 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006949 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006950 /*
6951 * If we backup past the first extent we want to move forward
6952 * and see if there is an extent in front of us, otherwise we'll
6953 * say there is a hole for our whole search range which can
6954 * cause problems.
6955 */
6956 extent_end = start;
6957 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006958 }
6959
Chris Mason5f39d392007-10-15 16:14:19 -04006960 found_type = btrfs_file_extent_type(leaf, item);
6961 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006962 if (found_type == BTRFS_FILE_EXTENT_REG ||
6963 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006964 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006965 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006966
6967 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6968 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006969 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6970 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006971 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006972 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006973 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006974
6975 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6976 path->slots[0],
6977 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006978 }
Josef Bacik25a50342013-10-14 12:08:38 -04006979next:
Yan Zheng9036c102008-10-30 14:19:41 -04006980 if (start >= extent_end) {
6981 path->slots[0]++;
6982 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6983 ret = btrfs_next_leaf(root, path);
6984 if (ret < 0) {
6985 err = ret;
6986 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006987 }
Yan Zheng9036c102008-10-30 14:19:41 -04006988 if (ret > 0)
6989 goto not_found;
6990 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
Yan Zheng9036c102008-10-30 14:19:41 -04006992 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6993 if (found_key.objectid != objectid ||
6994 found_key.type != BTRFS_EXTENT_DATA_KEY)
6995 goto not_found;
6996 if (start + len <= found_key.offset)
6997 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006998 if (start > found_key.offset)
6999 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007000 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007001 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007002 em->len = found_key.offset - start;
7003 goto not_found_em;
7004 }
7005
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007006 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007007 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007008
Yan Zhengd899e052008-10-30 14:25:28 -04007009 if (found_type == BTRFS_FILE_EXTENT_REG ||
7010 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007011 goto insert;
7012 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007013 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007014 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007015 size_t size;
7016 size_t extent_offset;
7017 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007018
Filipe Manana7ffbb592014-06-09 03:48:05 +01007019 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007020 goto out;
Yan689f9342007-10-29 11:41:07 -04007021
Chris Mason514ac8a2014-01-03 21:07:00 -08007022 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007023 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007024 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7025 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007026 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007027 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007028 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007029 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007030 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007031 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007032 if (btrfs_file_extent_compression(leaf, item) !=
7033 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007034 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007035 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007036 if (ret) {
7037 err = ret;
7038 goto out;
7039 }
Chris Masonc8b97812008-10-29 14:49:59 -04007040 } else {
7041 map = kmap(page);
7042 read_extent_buffer(leaf, map + pg_offset, ptr,
7043 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007044 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007045 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007046 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007047 copy_size);
7048 }
Chris Masonc8b97812008-10-29 14:49:59 -04007049 kunmap(page);
7050 }
Chris Mason179e29e2007-11-01 11:28:41 -04007051 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007052 }
Chris Masond1310b22008-01-24 16:13:08 -05007053 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007054 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007055 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007056 }
7057not_found:
7058 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007059 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007060 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007061not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007062 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007063insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007064 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007065 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007066 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007067 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7068 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007069 err = -EIO;
7070 goto out;
7071 }
Chris Masond1310b22008-01-24 16:13:08 -05007072
7073 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007074 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007075 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007076 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007077out:
liubo1abe9b82011-03-24 11:18:59 +00007078
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007079 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007080
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007081 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007082 if (err) {
7083 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007084 return ERR_PTR(err);
7085 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007086 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007087 return em;
7088}
7089
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007090struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7091 struct page *page,
7092 size_t pg_offset, u64 start, u64 len,
7093 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007094{
7095 struct extent_map *em;
7096 struct extent_map *hole_em = NULL;
7097 u64 range_start = start;
7098 u64 end;
7099 u64 found;
7100 u64 found_end;
7101 int err = 0;
7102
7103 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7104 if (IS_ERR(em))
7105 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007106 /*
7107 * If our em maps to:
7108 * - a hole or
7109 * - a pre-alloc extent,
7110 * there might actually be delalloc bytes behind it.
7111 */
7112 if (em->block_start != EXTENT_MAP_HOLE &&
7113 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7114 return em;
7115 else
7116 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007117
7118 /* check to see if we've wrapped (len == -1 or similar) */
7119 end = start + len;
7120 if (end < start)
7121 end = (u64)-1;
7122 else
7123 end -= 1;
7124
7125 em = NULL;
7126
7127 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007128 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007129 end, len, EXTENT_DELALLOC, 1);
7130 found_end = range_start + found;
7131 if (found_end < range_start)
7132 found_end = (u64)-1;
7133
7134 /*
7135 * we didn't find anything useful, return
7136 * the original results from get_extent()
7137 */
7138 if (range_start > end || found_end <= start) {
7139 em = hole_em;
7140 hole_em = NULL;
7141 goto out;
7142 }
7143
7144 /* adjust the range_start to make sure it doesn't
7145 * go backwards from the start they passed in
7146 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307147 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007148 found = found_end - range_start;
7149
7150 if (found > 0) {
7151 u64 hole_start = start;
7152 u64 hole_len = len;
7153
David Sterba172ddd62011-04-21 00:48:27 +02007154 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007155 if (!em) {
7156 err = -ENOMEM;
7157 goto out;
7158 }
7159 /*
7160 * when btrfs_get_extent can't find anything it
7161 * returns one huge hole
7162 *
7163 * make sure what it found really fits our range, and
7164 * adjust to make sure it is based on the start from
7165 * the caller
7166 */
7167 if (hole_em) {
7168 u64 calc_end = extent_map_end(hole_em);
7169
7170 if (calc_end <= start || (hole_em->start > end)) {
7171 free_extent_map(hole_em);
7172 hole_em = NULL;
7173 } else {
7174 hole_start = max(hole_em->start, start);
7175 hole_len = calc_end - hole_start;
7176 }
7177 }
7178 em->bdev = NULL;
7179 if (hole_em && range_start > hole_start) {
7180 /* our hole starts before our delalloc, so we
7181 * have to return just the parts of the hole
7182 * that go until the delalloc starts
7183 */
7184 em->len = min(hole_len,
7185 range_start - hole_start);
7186 em->start = hole_start;
7187 em->orig_start = hole_start;
7188 /*
7189 * don't adjust block start at all,
7190 * it is fixed at EXTENT_MAP_HOLE
7191 */
7192 em->block_start = hole_em->block_start;
7193 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007194 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7195 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007196 } else {
7197 em->start = range_start;
7198 em->len = found;
7199 em->orig_start = range_start;
7200 em->block_start = EXTENT_MAP_DELALLOC;
7201 em->block_len = found;
7202 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007203 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007204 return hole_em;
7205 }
7206out:
7207
7208 free_extent_map(hole_em);
7209 if (err) {
7210 free_extent_map(em);
7211 return ERR_PTR(err);
7212 }
7213 return em;
7214}
7215
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007216static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7217 const u64 start,
7218 const u64 len,
7219 const u64 orig_start,
7220 const u64 block_start,
7221 const u64 block_len,
7222 const u64 orig_block_len,
7223 const u64 ram_bytes,
7224 const int type)
7225{
7226 struct extent_map *em = NULL;
7227 int ret;
7228
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007229 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007230 em = create_io_em(inode, start, len, orig_start,
7231 block_start, block_len, orig_block_len,
7232 ram_bytes,
7233 BTRFS_COMPRESS_NONE, /* compress_type */
7234 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007235 if (IS_ERR(em))
7236 goto out;
7237 }
7238 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7239 len, block_len, type);
7240 if (ret) {
7241 if (em) {
7242 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007243 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007244 start + len - 1, 0);
7245 }
7246 em = ERR_PTR(ret);
7247 }
7248 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007249
7250 return em;
7251}
7252
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7254 u64 start, u64 len)
7255{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007258 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259 struct btrfs_key ins;
7260 u64 alloc_hint;
7261 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007264 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007265 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007266 if (ret)
7267 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007268
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007269 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7270 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007271 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007272 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007273 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007274 btrfs_free_reserved_extent(fs_info, ins.objectid,
7275 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007276
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277 return em;
7278}
7279
Chris Mason46bfbb52010-05-26 11:04:10 -04007280/*
7281 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7282 * block must be cow'd
7283 */
Josef Bacik00361582013-08-14 14:02:47 -04007284noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007285 u64 *orig_start, u64 *orig_block_len,
7286 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007287{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007288 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007289 struct btrfs_path *path;
7290 int ret;
7291 struct extent_buffer *leaf;
7292 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007293 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007294 struct btrfs_file_extent_item *fi;
7295 struct btrfs_key key;
7296 u64 disk_bytenr;
7297 u64 backref_offset;
7298 u64 extent_end;
7299 u64 num_bytes;
7300 int slot;
7301 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007302 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007303
Chris Mason46bfbb52010-05-26 11:04:10 -04007304 path = btrfs_alloc_path();
7305 if (!path)
7306 return -ENOMEM;
7307
David Sterbaf85b7372017-01-20 14:54:07 +01007308 ret = btrfs_lookup_file_extent(NULL, root, path,
7309 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007310 if (ret < 0)
7311 goto out;
7312
7313 slot = path->slots[0];
7314 if (ret == 1) {
7315 if (slot == 0) {
7316 /* can't find the item, must cow */
7317 ret = 0;
7318 goto out;
7319 }
7320 slot--;
7321 }
7322 ret = 0;
7323 leaf = path->nodes[0];
7324 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007325 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007326 key.type != BTRFS_EXTENT_DATA_KEY) {
7327 /* not our file or wrong item type, must cow */
7328 goto out;
7329 }
7330
7331 if (key.offset > offset) {
7332 /* Wrong offset, must cow */
7333 goto out;
7334 }
7335
7336 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7337 found_type = btrfs_file_extent_type(leaf, fi);
7338 if (found_type != BTRFS_FILE_EXTENT_REG &&
7339 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7340 /* not a regular extent, must cow */
7341 goto out;
7342 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007343
7344 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7345 goto out;
7346
Miao Xiee77751a2013-12-27 21:11:50 +08007347 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7348 if (extent_end <= offset)
7349 goto out;
7350
Chris Mason46bfbb52010-05-26 11:04:10 -04007351 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007352 if (disk_bytenr == 0)
7353 goto out;
7354
7355 if (btrfs_file_extent_compression(leaf, fi) ||
7356 btrfs_file_extent_encryption(leaf, fi) ||
7357 btrfs_file_extent_other_encoding(leaf, fi))
7358 goto out;
7359
Chris Mason46bfbb52010-05-26 11:04:10 -04007360 backref_offset = btrfs_file_extent_offset(leaf, fi);
7361
Josef Bacik7ee9e442013-06-21 16:37:03 -04007362 if (orig_start) {
7363 *orig_start = key.offset - backref_offset;
7364 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7365 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7366 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007368 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007369 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007370
7371 num_bytes = min(offset + *len, extent_end) - offset;
7372 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7373 u64 range_end;
7374
Jeff Mahoneyda170662016-06-15 09:22:56 -04007375 range_end = round_up(offset + num_bytes,
7376 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007377 ret = test_range_bit(io_tree, offset, range_end,
7378 EXTENT_DELALLOC, 0, NULL);
7379 if (ret) {
7380 ret = -EAGAIN;
7381 goto out;
7382 }
7383 }
7384
Josef Bacik1bda19e2013-10-18 12:10:36 -04007385 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007386
7387 /*
7388 * look for other files referencing this extent, if we
7389 * find any we must cow
7390 */
Josef Bacik00361582013-08-14 14:02:47 -04007391
Liu Boe4c3b2d2017-01-30 12:25:28 -08007392 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007393 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007394 if (ret) {
7395 ret = 0;
7396 goto out;
7397 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007398
7399 /*
7400 * adjust disk_bytenr and num_bytes to cover just the bytes
7401 * in this extent we are about to write. If there
7402 * are any csums in that range we have to cow in order
7403 * to keep the csums correct
7404 */
7405 disk_bytenr += backref_offset;
7406 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007407 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7408 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007409 /*
7410 * all of the above have passed, it is safe to overwrite this extent
7411 * without cow
7412 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007413 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007414 ret = 1;
7415out:
7416 btrfs_free_path(path);
7417 return ret;
7418}
7419
Josef Bacikeb838e72012-07-31 16:28:48 -04007420static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7421 struct extent_state **cached_state, int writing)
7422{
7423 struct btrfs_ordered_extent *ordered;
7424 int ret = 0;
7425
7426 while (1) {
7427 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007428 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007429 /*
7430 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007431 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007432 * extents in this range.
7433 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007434 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007435 lockend - lockstart + 1);
7436
7437 /*
7438 * We need to make sure there are no buffered pages in this
7439 * range either, we could have raced between the invalidate in
7440 * generic_file_direct_write and locking the extent. The
7441 * invalidate needs to happen so that reads after a write do not
7442 * get stale data.
7443 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007444 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007445 (!writing || !filemap_range_has_page(inode->i_mapping,
7446 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007447 break;
7448
7449 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007450 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007451
7452 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007453 /*
7454 * If we are doing a DIO read and the ordered extent we
7455 * found is for a buffered write, we can not wait for it
7456 * to complete and retry, because if we do so we can
7457 * deadlock with concurrent buffered writes on page
7458 * locks. This happens only if our DIO read covers more
7459 * than one extent map, if at this point has already
7460 * created an ordered extent for a previous extent map
7461 * and locked its range in the inode's io tree, and a
7462 * concurrent write against that previous extent map's
7463 * range and this range started (we unlock the ranges
7464 * in the io tree only when the bios complete and
7465 * buffered writes always lock pages before attempting
7466 * to lock range in the io tree).
7467 */
7468 if (writing ||
7469 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7470 btrfs_start_ordered_extent(inode, ordered, 1);
7471 else
7472 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007473 btrfs_put_ordered_extent(ordered);
7474 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007475 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007476 * We could trigger writeback for this range (and wait
7477 * for it to complete) and then invalidate the pages for
7478 * this range (through invalidate_inode_pages2_range()),
7479 * but that can lead us to a deadlock with a concurrent
7480 * call to readpages() (a buffered read or a defrag call
7481 * triggered a readahead) on a page lock due to an
7482 * ordered dio extent we created before but did not have
7483 * yet a corresponding bio submitted (whence it can not
7484 * complete), which makes readpages() wait for that
7485 * ordered extent to complete while holding a lock on
7486 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007487 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007488 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007489 }
7490
Filipe Mananaade77022016-02-18 14:28:55 +00007491 if (ret)
7492 break;
7493
Josef Bacikeb838e72012-07-31 16:28:48 -04007494 cond_resched();
7495 }
7496
7497 return ret;
7498}
7499
Liu Bo6f9994d2017-01-31 07:50:22 -08007500/* The callers of this must take lock_extent() */
7501static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7502 u64 orig_start, u64 block_start,
7503 u64 block_len, u64 orig_block_len,
7504 u64 ram_bytes, int compress_type,
7505 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007506{
7507 struct extent_map_tree *em_tree;
7508 struct extent_map *em;
7509 struct btrfs_root *root = BTRFS_I(inode)->root;
7510 int ret;
7511
Liu Bo6f9994d2017-01-31 07:50:22 -08007512 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7513 type == BTRFS_ORDERED_COMPRESSED ||
7514 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007515 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007516
Josef Bacik69ffb542012-09-11 15:40:07 -04007517 em_tree = &BTRFS_I(inode)->extent_tree;
7518 em = alloc_extent_map();
7519 if (!em)
7520 return ERR_PTR(-ENOMEM);
7521
7522 em->start = start;
7523 em->orig_start = orig_start;
7524 em->len = len;
7525 em->block_len = block_len;
7526 em->block_start = block_start;
7527 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007528 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007529 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007530 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007531 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007532 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007533 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007534 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007535 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7536 em->compress_type = compress_type;
7537 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007538
7539 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007540 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007541 em->start + em->len - 1, 0);
7542 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007543 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007544 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007545 /*
7546 * The caller has taken lock_extent(), who could race with us
7547 * to add em?
7548 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007549 } while (ret == -EEXIST);
7550
7551 if (ret) {
7552 free_extent_map(em);
7553 return ERR_PTR(ret);
7554 }
7555
Liu Bo6f9994d2017-01-31 07:50:22 -08007556 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007557 return em;
7558}
7559
Josef Bacik4b46fce2010-05-23 11:00:55 -04007560static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7561 struct buffer_head *bh_result, int create)
7562{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007563 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007564 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307566 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007567 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007569 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007570 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007571 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007572
Miao Xie172a5042013-02-21 02:48:22 -07007573 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007574 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007575 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007576 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007577
Josef Bacikc3298612012-08-03 16:49:19 -04007578 lockstart = start;
7579 lockend = start + len - 1;
7580
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007581 if (current->journal_info) {
7582 /*
7583 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007584 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007585 * confused.
7586 */
chandan50745b02015-08-28 21:10:13 +05307587 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007588 current->journal_info = NULL;
7589 }
7590
Josef Bacikeb838e72012-07-31 16:28:48 -04007591 /*
7592 * If this errors out it's because we couldn't invalidate pagecache for
7593 * this range and we need to fallback to buffered.
7594 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007595 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7596 create)) {
7597 ret = -ENOTBLK;
7598 goto err;
7599 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007600
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007601 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007602 if (IS_ERR(em)) {
7603 ret = PTR_ERR(em);
7604 goto unlock_err;
7605 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007606
7607 /*
7608 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7609 * io. INLINE is special, and we could probably kludge it in here, but
7610 * it's still buffered so for safety lets just fall back to the generic
7611 * buffered path.
7612 *
7613 * For COMPRESSED we _have_ to read the entire extent in so we can
7614 * decompress it, so there will be buffering required no matter what we
7615 * do, so go ahead and fallback to buffered.
7616 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007617 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007618 * to buffered IO. Don't blame me, this is the price we pay for using
7619 * the generic code.
7620 */
7621 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7622 em->block_start == EXTENT_MAP_INLINE) {
7623 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 ret = -ENOTBLK;
7625 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626 }
7627
7628 /* Just a good old fashioned hole, return */
7629 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7630 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7631 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007632 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007633 }
7634
7635 /*
7636 * We don't allocate a new extent in the following cases
7637 *
7638 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7639 * existing extent.
7640 * 2) The extent is marked as PREALLOC. We're good to go here and can
7641 * just use the extent.
7642 *
7643 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007644 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 len = min(len, em->len - (start - em->start));
7646 lockstart = start + len;
7647 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007648 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649
7650 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7651 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7652 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007653 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007654 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007655
7656 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7657 type = BTRFS_ORDERED_PREALLOC;
7658 else
7659 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007660 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007661 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007662
Josef Bacik00361582013-08-14 14:02:47 -04007663 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007664 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007665 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007666 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007667
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007668 em2 = btrfs_create_dio_extent(inode, start, len,
7669 orig_start, block_start,
7670 len, orig_block_len,
7671 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007672 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007673 if (type == BTRFS_ORDERED_PREALLOC) {
7674 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007675 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007676 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007677 if (em2 && IS_ERR(em2)) {
7678 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007679 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007680 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007681 /*
7682 * For inode marked NODATACOW or extent marked PREALLOC,
7683 * use the existing or preallocated extent, so does not
7684 * need to adjust btrfs_space_info's bytes_may_use.
7685 */
7686 btrfs_free_reserved_data_space_noquota(inode,
7687 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007688 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007690 }
Josef Bacik00361582013-08-14 14:02:47 -04007691
Chris Mason46bfbb52010-05-26 11:04:10 -04007692 /*
7693 * this will cow the extent, reset the len in case we changed
7694 * it above
7695 */
7696 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007697 free_extent_map(em);
7698 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007699 if (IS_ERR(em)) {
7700 ret = PTR_ERR(em);
7701 goto unlock_err;
7702 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007703 len = min(len, em->len - (start - em->start));
7704unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007705 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7706 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007707 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 bh_result->b_bdev = em->bdev;
7709 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007710 if (create) {
7711 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7712 set_buffer_new(bh_result);
7713
7714 /*
7715 * Need to update the i_size under the extent lock so buffered
7716 * readers will get the updated i_size when we unlock.
7717 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007718 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007719 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007720
chandan50745b02015-08-28 21:10:13 +05307721 WARN_ON(dio_data->reserve < len);
7722 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007723 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307724 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007725 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 /*
7728 * In the case of write we need to clear and unlock the entire range,
7729 * in the case of read we need to unlock only the end area that we
7730 * aren't using if there is any left over space.
7731 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007732 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007733 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7734 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007735 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007736 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007737 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007738 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007739
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740 free_extent_map(em);
7741
7742 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007743
7744unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007745 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007746 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007747err:
chandan50745b02015-08-28 21:10:13 +05307748 if (dio_data)
7749 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007750 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751}
7752
Omar Sandoval58efbc92017-08-22 23:45:59 -07007753static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7754 struct bio *bio,
7755 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007756{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007758 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007759
Mike Christie37226b22016-06-05 14:31:52 -05007760 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007761
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007762 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007763 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007764 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007765
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007766 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007767
Miao Xie8b110e32014-09-12 18:44:03 +08007768 return ret;
7769}
7770
7771static int btrfs_check_dio_repairable(struct inode *inode,
7772 struct bio *failed_bio,
7773 struct io_failure_record *failrec,
7774 int failed_mirror)
7775{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007776 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007777 int num_copies;
7778
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007779 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007780 if (num_copies == 1) {
7781 /*
7782 * we only have a single copy of the data, so don't bother with
7783 * all the retry and error correction code that follows. no
7784 * matter what the error is, it is very likely to persist.
7785 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007786 btrfs_debug(fs_info,
7787 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7788 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007789 return 0;
7790 }
7791
7792 failrec->failed_mirror = failed_mirror;
7793 failrec->this_mirror++;
7794 if (failrec->this_mirror == failed_mirror)
7795 failrec->this_mirror++;
7796
7797 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007798 btrfs_debug(fs_info,
7799 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7800 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007801 return 0;
7802 }
7803
7804 return 1;
7805}
7806
Omar Sandoval58efbc92017-08-22 23:45:59 -07007807static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7808 struct page *page, unsigned int pgoff,
7809 u64 start, u64 end, int failed_mirror,
7810 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007811{
7812 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007813 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7814 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007815 struct bio *bio;
7816 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007817 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007818 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007819 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007820 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007821 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007822
Mike Christie37226b22016-06-05 14:31:52 -05007823 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007824
7825 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7826 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007827 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007828
7829 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7830 failed_mirror);
7831 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007832 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007833 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007834 }
7835
Liu Bo17347ce2017-05-15 15:33:27 -07007836 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007837 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007838 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007839 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007840 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007841
7842 isector = start - btrfs_io_bio(failed_bio)->logical;
7843 isector >>= inode->i_sb->s_blocksize_bits;
7844 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307845 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007846 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007847
7848 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007849 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007850 read_mode, failrec->this_mirror, failrec->in_validation);
7851
Omar Sandoval58efbc92017-08-22 23:45:59 -07007852 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7853 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007854 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007855 bio_put(bio);
7856 }
7857
Omar Sandoval58efbc92017-08-22 23:45:59 -07007858 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007859}
7860
7861struct btrfs_retry_complete {
7862 struct completion done;
7863 struct inode *inode;
7864 u64 start;
7865 int uptodate;
7866};
7867
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007868static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007869{
7870 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007871 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007872 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007873 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007874 int i;
7875
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007876 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007877 goto end;
7878
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307879 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007880 io_tree = &BTRFS_I(inode)->io_tree;
7881 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007882 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883
Miao Xie8b110e32014-09-12 18:44:03 +08007884 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007885 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007886 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007887 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7888 io_tree, done->start, bvec->bv_page,
7889 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007890end:
7891 complete(&done->done);
7892 bio_put(bio);
7893}
7894
Omar Sandoval58efbc92017-08-22 23:45:59 -07007895static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7896 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007897{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307898 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007899 struct bio_vec bvec;
7900 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007901 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007902 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307903 unsigned int pgoff;
7904 u32 sectorsize;
7905 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007906 blk_status_t ret;
7907 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007908
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307909 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007910 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307911
Miao Xiec1dc0892014-09-12 18:43:56 +08007912 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007913 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007914 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007915
Liu Bo17347ce2017-05-15 15:33:27 -07007916 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7917 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7918 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307919
7920next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007921 done.uptodate = 0;
7922 done.start = start;
7923 init_completion(&done.done);
7924
Liu Bo17347ce2017-05-15 15:33:27 -07007925 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307926 pgoff, start, start + sectorsize - 1,
7927 io_bio->mirror_num,
7928 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007929 if (ret) {
7930 err = ret;
7931 goto next;
7932 }
Miao Xie8b110e32014-09-12 18:44:03 +08007933
David Sterba9c17f6c2017-07-19 19:26:45 +02007934 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007935
7936 if (!done.uptodate) {
7937 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307938 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007939 }
7940
Liu Bo629ebf42017-05-15 17:20:07 -07007941next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307942 start += sectorsize;
7943
Liu Bo97bf5a52017-04-07 13:11:10 -07007944 nr_sectors--;
7945 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307946 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007947 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307948 goto next_block_or_try_again;
7949 }
Miao Xie8b110e32014-09-12 18:44:03 +08007950 }
7951
Liu Bo629ebf42017-05-15 17:20:07 -07007952 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007953}
7954
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007955static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007956{
7957 struct btrfs_retry_complete *done = bio->bi_private;
7958 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007959 struct extent_io_tree *io_tree, *failure_tree;
7960 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007961 struct bio_vec *bvec;
7962 int uptodate;
7963 int ret;
7964 int i;
7965
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007966 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007967 goto end;
7968
7969 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307971 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007972 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307973
Josef Bacik7870d082017-05-05 11:57:15 -04007974 io_tree = &BTRFS_I(inode)->io_tree;
7975 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7976
David Sterbac09abff2017-07-13 18:10:07 +02007977 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007978 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007979 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7980 bvec->bv_offset, done->start,
7981 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007982 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007983 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7984 failure_tree, io_tree, done->start,
7985 bvec->bv_page,
7986 btrfs_ino(BTRFS_I(inode)),
7987 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007988 else
7989 uptodate = 0;
7990 }
7991
7992 done->uptodate = uptodate;
7993end:
7994 complete(&done->done);
7995 bio_put(bio);
7996}
7997
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007998static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7999 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008000{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008002 struct bio_vec bvec;
8003 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008004 struct btrfs_retry_complete done;
8005 u64 start;
8006 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007 u32 sectorsize;
8008 int nr_sectors;
8009 unsigned int pgoff;
8010 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008011 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008012 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008013 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008014
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008016 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017
Omar Sandoval58efbc92017-08-22 23:45:59 -07008018 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008019 start = io_bio->logical;
8020 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008021 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008022
Liu Bo17347ce2017-05-15 15:33:27 -07008023 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8024 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308025
Liu Bo17347ce2017-05-15 15:33:27 -07008026 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008028 if (uptodate) {
8029 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8030 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8031 bvec.bv_page, pgoff, start, sectorsize);
8032 if (likely(!ret))
8033 goto next;
8034 }
Miao Xie8b110e32014-09-12 18:44:03 +08008035try_again:
8036 done.uptodate = 0;
8037 done.start = start;
8038 init_completion(&done.done);
8039
Omar Sandoval58efbc92017-08-22 23:45:59 -07008040 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8041 pgoff, start, start + sectorsize - 1,
8042 io_bio->mirror_num, btrfs_retry_endio,
8043 &done);
8044 if (status) {
8045 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008046 goto next;
8047 }
8048
David Sterba9c17f6c2017-07-19 19:26:45 +02008049 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008050
8051 if (!done.uptodate) {
8052 /* We might have another mirror, so try again */
8053 goto try_again;
8054 }
8055next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056 offset += sectorsize;
8057 start += sectorsize;
8058
8059 ASSERT(nr_sectors);
8060
Liu Bo97bf5a52017-04-07 13:11:10 -07008061 nr_sectors--;
8062 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008064 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 goto next_block;
8066 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008067 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008068
8069 return err;
8070}
8071
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008072static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8073 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008074{
8075 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8076
8077 if (skip_csum) {
8078 if (unlikely(err))
8079 return __btrfs_correct_data_nocsum(inode, io_bio);
8080 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008081 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008082 } else {
8083 return __btrfs_subio_endio_read(inode, io_bio, err);
8084 }
8085}
8086
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008087static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008088{
8089 struct btrfs_dio_private *dip = bio->bi_private;
8090 struct inode *inode = dip->inode;
8091 struct bio *dio_bio;
8092 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008093 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008094
Liu Bo99c4e3b2017-09-15 15:06:51 -06008095 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008096 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008097
Josef Bacik4b46fce2010-05-23 11:00:55 -04008098 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008099 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008100 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101
Josef Bacik4b46fce2010-05-23 11:00:55 -04008102 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008103
Liu Bo99c4e3b2017-09-15 15:06:51 -06008104 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008105 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008106
8107 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008108 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008109 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008110}
8111
Qu Wenruo524272602017-03-08 10:25:52 +08008112static void __endio_write_update_ordered(struct inode *inode,
8113 const u64 offset, const u64 bytes,
8114 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008117 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008118 struct btrfs_workqueue *wq;
8119 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008120 u64 ordered_offset = offset;
8121 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008122 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008123 int ret;
8124
Qu Wenruo524272602017-03-08 10:25:52 +08008125 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8126 wq = fs_info->endio_freespace_worker;
8127 func = btrfs_freespace_write_helper;
8128 } else {
8129 wq = fs_info->endio_write_workers;
8130 func = btrfs_endio_write_helper;
8131 }
8132
Chris Mason163cf092010-11-28 19:56:33 -05008133again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008134 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008135 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8136 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008137 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008138 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008139 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008140 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008141
Qu Wenruo524272602017-03-08 10:25:52 +08008142 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8143 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008144out_test:
8145 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008146 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8147 * in the range, we can exit.
8148 */
8149 if (ordered_offset == last_offset)
8150 return;
8151 /*
Chris Mason163cf092010-11-28 19:56:33 -05008152 * our bio might span multiple ordered extents. If we haven't
8153 * completed the accounting for the whole dio, go back and try again
8154 */
Filipe Manana14543772015-11-24 16:23:54 +00008155 if (ordered_offset < offset + bytes) {
8156 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008157 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008158 goto again;
8159 }
Filipe Manana14543772015-11-24 16:23:54 +00008160}
8161
8162static void btrfs_endio_direct_write(struct bio *bio)
8163{
8164 struct btrfs_dio_private *dip = bio->bi_private;
8165 struct bio *dio_bio = dip->dio_bio;
8166
Qu Wenruo524272602017-03-08 10:25:52 +08008167 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008168 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008169
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008171
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008172 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008173 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008174 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008175}
8176
David Sterbad0ee3932018-03-08 14:35:48 +01008177static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008178 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008179{
Josef Bacikc6100a42017-05-05 11:57:13 -04008180 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008181 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008182 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008183 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008184 return 0;
8185}
8186
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008187static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008188{
8189 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008190 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008191
Miao Xie8b110e32014-09-12 18:44:03 +08008192 if (err)
8193 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008194 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008195 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8196 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008197 (unsigned long long)bio->bi_iter.bi_sector,
8198 bio->bi_iter.bi_size, err);
8199
8200 if (dip->subio_endio)
8201 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008202
8203 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008204 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008205 * We want to perceive the errors flag being set before
8206 * decrementing the reference count. We don't need a barrier
8207 * since atomic operations with a return value are fully
8208 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008209 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008210 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008211 }
8212
8213 /* if there are more bios still pending for this dio, just exit */
8214 if (!atomic_dec_and_test(&dip->pending_bios))
8215 goto out;
8216
Chris Mason9be33952013-05-17 18:30:14 -04008217 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008218 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008219 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008220 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008221 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008222 }
8223out:
8224 bio_put(bio);
8225}
8226
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008227static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008228 struct btrfs_dio_private *dip,
8229 struct bio *bio,
8230 u64 file_offset)
8231{
8232 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8233 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008234 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008235
8236 /*
8237 * We load all the csum data we need when we submit
8238 * the first bio to reduce the csum tree search and
8239 * contention.
8240 */
8241 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008242 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008243 file_offset);
8244 if (ret)
8245 return ret;
8246 }
8247
8248 if (bio == dip->orig_bio)
8249 return 0;
8250
8251 file_offset -= dip->logical_offset;
8252 file_offset >>= inode->i_sb->s_blocksize_bits;
8253 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8254
8255 return 0;
8256}
8257
David Sterbad0ee3932018-03-08 14:35:48 +01008258static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8259 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008260{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008261 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008262 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008263 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008264 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008265
Liu Bo4c274bc2017-11-01 17:19:27 -06008266 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008267 if (async_submit)
8268 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8269
Josef Bacik5fd02042012-05-02 14:00:54 -04008270 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008271 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008272 if (ret)
8273 goto err;
8274 }
Miao Xiee65e1532010-11-22 03:04:43 +00008275
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008276 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008277 goto map;
8278
8279 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008280 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8281 file_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01008282 btrfs_submit_bio_start_direct_io,
8283 btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008284 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008285 } else if (write) {
8286 /*
8287 * If we aren't doing async submit, calculate the csum of the
8288 * bio now.
8289 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008290 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008291 if (ret)
8292 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008293 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008294 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008295 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008296 if (ret)
8297 goto err;
8298 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008299map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008300 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008301err:
Miao Xiee65e1532010-11-22 03:04:43 +00008302 return ret;
8303}
8304
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008305static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008306{
8307 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008309 struct bio *bio;
8310 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008311 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008312 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008313 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008314 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008315 u64 submit_len;
8316 int clone_offset = 0;
8317 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308318 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008319 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008320
Kent Overstreet4f024f32013-10-11 15:44:27 -07008321 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008322 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008323 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8324 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008325 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008326 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008327
Liu Bo725130b2017-05-16 09:51:39 -07008328 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008329 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008330 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008331 goto submit;
8332 }
8333
David Woodhouse53b381b2013-01-29 18:40:14 -05008334 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008335 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008336 async_submit = 0;
8337 else
8338 async_submit = 1;
8339
Liu Bo725130b2017-05-16 09:51:39 -07008340 /* bio split */
8341 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008342 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008343 do {
Liu Bo725130b2017-05-16 09:51:39 -07008344 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008345
Liu Bo725130b2017-05-16 09:51:39 -07008346 /*
8347 * This will never fail as it's passing GPF_NOFS and
8348 * the allocation is backed by btrfs_bioset.
8349 */
Liu Boe4770942017-05-16 10:57:14 -07008350 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008351 clone_len);
8352 bio->bi_private = dip;
8353 bio->bi_end_io = btrfs_end_dio_bio;
8354 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008355
Liu Bo725130b2017-05-16 09:51:39 -07008356 ASSERT(submit_len >= clone_len);
8357 submit_len -= clone_len;
8358 if (submit_len == 0)
8359 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008360
Liu Bo725130b2017-05-16 09:51:39 -07008361 /*
8362 * Increase the count before we submit the bio so we know
8363 * the end IO handler won't happen before we increase the
8364 * count. Otherwise, the dip might get freed before we're
8365 * done setting it up.
8366 */
8367 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008368
David Sterbad0ee3932018-03-08 14:35:48 +01008369 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008370 async_submit);
8371 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008372 bio_put(bio);
8373 atomic_dec(&dip->pending_bios);
8374 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008375 }
Liu Bo725130b2017-05-16 09:51:39 -07008376
8377 clone_offset += clone_len;
8378 start_sector += clone_len >> 9;
8379 file_offset += clone_len;
8380
8381 map_length = submit_len;
8382 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8383 start_sector << 9, &map_length, NULL, 0);
8384 if (ret)
8385 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008386 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008387
Josef Bacik02f57c72011-04-06 14:25:44 -04008388submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008389 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008390 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008391 return 0;
8392
8393 bio_put(bio);
8394out_err:
8395 dip->errors = 1;
8396 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008397 * Before atomic variable goto zero, we must make sure dip->errors is
8398 * perceived to be set. This ordering is ensured by the fact that an
8399 * atomic operations with a return value are fully ordered as per
8400 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008401 */
Miao Xiee65e1532010-11-22 03:04:43 +00008402 if (atomic_dec_and_test(&dip->pending_bios))
8403 bio_io_error(dip->orig_bio);
8404
8405 /* bio_end_io() will handle error, so we needn't return it */
8406 return 0;
8407}
8408
Mike Christie8a4c1e42016-06-05 14:31:50 -05008409static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8410 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008411{
Filipe Manana61de7182015-07-01 12:13:10 +01008412 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008413 struct bio *bio = NULL;
8414 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008415 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008416 int ret = 0;
8417
David Sterba8b6c1d52017-06-02 17:48:13 +02008418 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008419
Miao Xiec1dc0892014-09-12 18:43:56 +08008420 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008421 if (!dip) {
8422 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008423 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008424 }
8425
8426 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008427 dip->inode = inode;
8428 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008429 dip->bytes = dio_bio->bi_iter.bi_size;
8430 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008431 bio->bi_private = dip;
8432 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008433 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008434 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008435 io_bio = btrfs_io_bio(bio);
8436 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008437
Miao Xiec1dc0892014-09-12 18:43:56 +08008438 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008439 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008440 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008441 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008442 dip->subio_endio = btrfs_subio_endio_read;
8443 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008444
Filipe Mananaf28a4922015-12-08 19:23:20 +00008445 /*
8446 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8447 * even if we fail to submit a bio, because in such case we do the
8448 * corresponding error handling below and it must not be done a second
8449 * time by btrfs_direct_IO().
8450 */
8451 if (write) {
8452 struct btrfs_dio_data *dio_data = current->journal_info;
8453
8454 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8455 dip->bytes;
8456 dio_data->unsubmitted_oe_range_start =
8457 dio_data->unsubmitted_oe_range_end;
8458 }
8459
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008460 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008461 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008462 return;
Chris Mason9be33952013-05-17 18:30:14 -04008463
Liu Bo3892ac92017-04-17 15:00:28 -07008464 if (io_bio->end_io)
8465 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008466
Josef Bacik4b46fce2010-05-23 11:00:55 -04008467free_ordered:
8468 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008469 * If we arrived here it means either we failed to submit the dip
8470 * or we either failed to clone the dio_bio or failed to allocate the
8471 * dip. If we cloned the dio_bio and allocated the dip, we can just
8472 * call bio_endio against our io_bio so that we get proper resource
8473 * cleanup if we fail to submit the dip, otherwise, we must do the
8474 * same as btrfs_endio_direct_[write|read] because we can't call these
8475 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008476 */
Liu Bo3892ac92017-04-17 15:00:28 -07008477 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008478 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008479 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008480 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008481 * and all the cleanup and final put for dio_bio (through
8482 * dio_end_io()).
8483 */
8484 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008485 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008486 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008487 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008488 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008489 file_offset,
8490 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008491 false);
Filipe Manana14543772015-11-24 16:23:54 +00008492 else
Filipe Manana61de7182015-07-01 12:13:10 +01008493 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8494 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008495
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008496 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008497 /*
8498 * Releases and cleans up our dio_bio, no need to bio_put()
8499 * nor bio_endio()/bio_io_error() against dio_bio.
8500 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008501 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008502 }
Liu Bo3892ac92017-04-17 15:00:28 -07008503 if (bio)
8504 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008505 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008506}
8507
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008508static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008509 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008510{
8511 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008512 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008513 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008514 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008515
8516 if (offset & blocksize_mask)
8517 goto out;
8518
Al Viro28060d52014-03-22 05:15:17 -04008519 if (iov_iter_alignment(iter) & blocksize_mask)
8520 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008521
Al Viro28060d52014-03-22 05:15:17 -04008522 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008523 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008524 return 0;
8525 /*
8526 * Check to make sure we don't have duplicate iov_base's in this
8527 * iovec, if so return EINVAL, otherwise we'll get csum errors
8528 * when reading back.
8529 */
8530 for (seg = 0; seg < iter->nr_segs; seg++) {
8531 for (i = seg + 1; i < iter->nr_segs; i++) {
8532 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008533 goto out;
8534 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008535 }
8536 retval = 0;
8537out:
8538 return retval;
8539}
Josef Bacikeb838e72012-07-31 16:28:48 -04008540
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008541static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008542{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008543 struct file *file = iocb->ki_filp;
8544 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008545 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308546 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008547 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008548 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008549 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008550 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008551 bool wakeup = true;
8552 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008553 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008554
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008555 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008556 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008557
Jens Axboefe0f07d2015-04-15 17:05:48 -06008558 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008559
Josef Bacik0e267c42013-07-02 10:38:02 -04008560 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008561 * The generic stuff only does filemap_write_and_wait_range, which
8562 * isn't enough if we've written compressed pages to this area, so
8563 * we need to flush the dirty pages again to make absolutely sure
8564 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008565 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008566 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008567 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8568 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008569 filemap_fdatawrite_range(inode->i_mapping, offset,
8570 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008571
Omar Sandoval6f673762015-03-16 04:33:52 -07008572 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008573 /*
8574 * If the write DIO is beyond the EOF, we need update
8575 * the isize, but it is protected by i_mutex. So we can
8576 * not unlock the i_mutex at this case.
8577 */
8578 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008579 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008580 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008581 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008582 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8583 ret = -EAGAIN;
8584 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008585 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008586 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8587 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008588 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008589 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008590
8591 /*
8592 * We need to know how many extents we reserved so that we can
8593 * do the accounting properly if we go over the number we
8594 * originally calculated. Abuse current->journal_info for this.
8595 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008596 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008597 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008598 dio_data.unsubmitted_oe_range_start = (u64)offset;
8599 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308600 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308601 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008602 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8603 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008604 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008605 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8606 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008607 }
8608
Omar Sandoval17f8c842015-03-16 04:33:50 -07008609 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008610 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008611 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008612 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008613 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308614 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008615 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008616 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308617 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008618 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008619 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008620 /*
8621 * On error we might have left some ordered extents
8622 * without submitting corresponding bios for them, so
8623 * cleanup them up to avoid other tasks getting them
8624 * and waiting for them to complete forever.
8625 */
8626 if (dio_data.unsubmitted_oe_range_start <
8627 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008628 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008629 dio_data.unsubmitted_oe_range_start,
8630 dio_data.unsubmitted_oe_range_end -
8631 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008632 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008633 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008634 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008635 offset, count - (size_t)ret, true);
8636 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008637 }
Miao Xie38851cc2013-02-08 07:04:11 +00008638out:
Miao Xie2e60a512013-02-08 07:01:08 +00008639 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008640 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008641 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008642 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008643
Qu Wenruo364ecf32017-02-27 15:10:38 +08008644 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008645 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008646}
8647
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008648#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8649
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008650static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8651 __u64 start, __u64 len)
8652{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008653 int ret;
8654
8655 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8656 if (ret)
8657 return ret;
8658
David Sterba2135fb92017-06-23 04:09:57 +02008659 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008660}
8661
Chris Mason9ebefb182007-06-15 13:50:00 -04008662int btrfs_readpage(struct file *file, struct page *page)
8663{
Chris Masond1310b22008-01-24 16:13:08 -05008664 struct extent_io_tree *tree;
8665 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008666 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008667}
Chris Mason1832a6d2007-12-21 16:27:21 -05008668
Chris Mason39279cc2007-06-12 06:35:45 -04008669static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8670{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008671 struct inode *inode = page->mapping->host;
8672 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008673
8674 if (current->flags & PF_MEMALLOC) {
8675 redirty_page_for_writepage(wbc, page);
8676 unlock_page(page);
8677 return 0;
8678 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008679
8680 /*
8681 * If we are under memory pressure we will call this directly from the
8682 * VM, we need to make sure we have the inode referenced for the ordered
8683 * extent. If not just return like we didn't do anything.
8684 */
8685 if (!igrab(inode)) {
8686 redirty_page_for_writepage(wbc, page);
8687 return AOP_WRITEPAGE_ACTIVATE;
8688 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008689 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008690 btrfs_add_delayed_iput(inode);
8691 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008692}
Chris Mason39279cc2007-06-12 06:35:45 -04008693
Eric Sandeen48a3b632013-04-25 20:41:01 +00008694static int btrfs_writepages(struct address_space *mapping,
8695 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008696{
Chris Masond1310b22008-01-24 16:13:08 -05008697 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008698
Chris Masond1310b22008-01-24 16:13:08 -05008699 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008700 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008701}
8702
Chris Mason3ab2fb52007-11-08 10:59:22 -05008703static int
8704btrfs_readpages(struct file *file, struct address_space *mapping,
8705 struct list_head *pages, unsigned nr_pages)
8706{
Chris Masond1310b22008-01-24 16:13:08 -05008707 struct extent_io_tree *tree;
8708 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008709 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008710}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008711static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008712{
Chris Masond1310b22008-01-24 16:13:08 -05008713 struct extent_io_tree *tree;
8714 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008715 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008716
Chris Masond1310b22008-01-24 16:13:08 -05008717 tree = &BTRFS_I(page->mapping->host)->io_tree;
8718 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008719 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008720 if (ret == 1) {
8721 ClearPagePrivate(page);
8722 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008723 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008724 }
8725 return ret;
8726}
Chris Mason39279cc2007-06-12 06:35:45 -04008727
Chris Masone6dcd2d2008-07-17 12:53:50 -04008728static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8729{
Chris Mason98509cf2008-09-11 15:51:43 -04008730 if (PageWriteback(page) || PageDirty(page))
8731 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008732 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008733}
8734
Lukas Czernerd47992f2013-05-21 23:17:23 -04008735static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8736 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008737{
Josef Bacik5fd02042012-05-02 14:00:54 -04008738 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008739 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008740 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008741 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008742 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008743 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308744 u64 start;
8745 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008746 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008747
Chris Mason8b62b722009-09-02 16:53:46 -04008748 /*
8749 * we have the page locked, so new writeback can't start,
8750 * and the dirty bit won't be cleared while we are here.
8751 *
8752 * Wait for IO on this page so that we can safely clear
8753 * the PagePrivate2 bit and do ordered accounting
8754 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008755 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008756
Josef Bacik5fd02042012-05-02 14:00:54 -04008757 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008758 if (offset) {
8759 btrfs_releasepage(page, GFP_NOFS);
8760 return;
8761 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008762
8763 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008764 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308765again:
8766 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008767 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308768 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008769 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308770 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008771 /*
8772 * IO on this page will never be started, so we need
8773 * to account for any ordered extents now
8774 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008775 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308776 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008777 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008778 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008779 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008780 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008781 /*
8782 * whoever cleared the private bit is responsible
8783 * for the finish_ordered_io
8784 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008785 if (TestClearPagePrivate2(page)) {
8786 struct btrfs_ordered_inode_tree *tree;
8787 u64 new_len;
8788
8789 tree = &BTRFS_I(inode)->ordered_tree;
8790
8791 spin_lock_irq(&tree->lock);
8792 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308793 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008794 if (new_len < ordered->truncated_len)
8795 ordered->truncated_len = new_len;
8796 spin_unlock_irq(&tree->lock);
8797
8798 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308799 start,
8800 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008801 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008802 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008803 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008804 if (!inode_evicting) {
8805 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308806 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008807 &cached_state);
8808 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308809
8810 start = end + 1;
8811 if (start < page_end)
8812 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008814
Qu Wenruob9d0b382015-09-29 10:35:16 +08008815 /*
8816 * Qgroup reserved space handler
8817 * Page here will be either
8818 * 1) Already written to disk
8819 * In this case, its reserved space is released from data rsv map
8820 * and will be freed by delayed_ref handler finally.
8821 * So even we call qgroup_free_data(), it won't decrease reserved
8822 * space.
8823 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008824 * This means the reserved space should be freed here. However,
8825 * if a truncate invalidates the page (by clearing PageDirty)
8826 * and the page is accounted for while allocating extent
8827 * in btrfs_check_data_free_space() we let delayed_ref to
8828 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008829 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008830 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008831 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008832 if (!inode_evicting) {
8833 clear_extent_bit(tree, page_start, page_end,
8834 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008835 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8836 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008837 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008838
8839 __btrfs_releasepage(page, GFP_NOFS);
8840 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008841
Chris Mason4a096752008-07-21 10:29:44 -04008842 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008843 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008844 ClearPagePrivate(page);
8845 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008846 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008847 }
Chris Mason39279cc2007-06-12 06:35:45 -04008848}
8849
Chris Mason9ebefb182007-06-15 13:50:00 -04008850/*
8851 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8852 * called from a page fault handler when a page is first dirtied. Hence we must
8853 * be careful to check for EOF conditions here. We set the page up correctly
8854 * for a written page which means we get ENOSPC checking when writing into
8855 * holes and correct delalloc and unwritten extent mapping on filesystems that
8856 * support these features.
8857 *
8858 * We are not allowed to take the i_mutex here so we have to play games to
8859 * protect against truncate races as the page could now be beyond EOF. Because
8860 * vmtruncate() writes the inode size before removing pages, once we have the
8861 * page lock we can determine safely if the page is beyond EOF. If it is not
8862 * beyond EOF, then the page is guaranteed safe against truncation until we
8863 * unlock the page.
8864 */
Dave Jiang11bac802017-02-24 14:56:41 -08008865int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008866{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008867 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008868 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008869 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008870 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8871 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008872 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008873 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008874 char *kaddr;
8875 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008876 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008877 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008878 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308879 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008880 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008881 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308882 u64 end;
8883
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008884 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008885
Jan Karab2b5ef52012-06-12 16:20:45 +02008886 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008887 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008888 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308889 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008890
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308891 /*
8892 * Reserving delalloc space after obtaining the page lock can lead to
8893 * deadlock. For example, if a dirty page is locked by this function
8894 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8895 * dirty page write out, then the btrfs_writepage() function could
8896 * end up waiting indefinitely to get a lock on the page currently
8897 * being processed by btrfs_page_mkwrite() function.
8898 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008899 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308900 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008901 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008902 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008903 reserved = 1;
8904 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008905 if (ret) {
8906 if (ret == -ENOMEM)
8907 ret = VM_FAULT_OOM;
8908 else /* -ENOSPC, -EIO, etc */
8909 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008910 if (reserved)
8911 goto out;
8912 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008913 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008914
Nick Piggin56a76f82009-03-31 15:23:23 -07008915 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008917 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008918 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008919
Chris Mason9ebefb182007-06-15 13:50:00 -04008920 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008921 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008922 /* page got truncated out from underneath us */
8923 goto out_unlock;
8924 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008925 wait_on_page_writeback(page);
8926
David Sterbaff13db42015-12-03 14:30:40 +01008927 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008928 set_page_extent_mapped(page);
8929
Chris Masoneb84ae02008-07-17 13:53:27 -04008930 /*
8931 * we can't set the delalloc bits if there are pending ordered
8932 * extents. Drop our locks and wait for them to finish
8933 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008934 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8935 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008936 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008937 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008938 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008940 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008941 btrfs_put_ordered_extent(ordered);
8942 goto again;
8943 }
8944
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008945 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008946 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008947 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008948 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308949 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008950 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008951 page_start, PAGE_SIZE - reserved_space,
8952 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308953 }
8954 }
8955
Josef Bacikfbf19082009-10-01 17:10:23 -04008956 /*
Liu Bo54160342016-12-13 12:15:19 -08008957 * page_mkwrite gets called when the page is firstly dirtied after it's
8958 * faulted in, but write(2) could also dirty a page and set delalloc
8959 * bits, thus in this case for space account reason, we still need to
8960 * clear any delalloc bits within this page range since we have to
8961 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008962 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308963 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008964 EXTENT_DIRTY | EXTENT_DELALLOC |
8965 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008966 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008967
Filipe Mananae3b8a482017-11-04 00:16:59 +00008968 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008969 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008970 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008971 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008972 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008973 ret = VM_FAULT_SIGBUS;
8974 goto out_unlock;
8975 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008977
8978 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 if (page_start + PAGE_SIZE > size)
8980 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008981 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008982 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008983
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008984 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008985 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008986 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008987 flush_dcache_page(page);
8988 kunmap(page);
8989 }
Chris Mason247e7432008-07-17 12:53:51 -04008990 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008992 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008993
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008994 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008995 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008996 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008997
David Sterbae43bbe52017-12-12 21:43:52 +01008998 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008999
9000out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009001 if (!ret) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009002 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009003 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009004 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009005 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009006 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009007 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009008out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009009 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009010 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009011 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009012out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009013 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009014 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 return ret;
9016}
9017
Filipe Manana213e8c52018-02-06 20:40:31 +00009018static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009019{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009020 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009021 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009022 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009023 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009024 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009025 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009026 u64 mask = fs_info->sectorsize - 1;
9027 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009028
Filipe Manana213e8c52018-02-06 20:40:31 +00009029 if (!skip_writeback) {
9030 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9031 (u64)-1);
9032 if (ret)
9033 return ret;
9034 }
Chris Mason39279cc2007-06-12 06:35:45 -04009035
Josef Bacikfcb80c22011-05-03 10:40:22 -04009036 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009037 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009038 * 3 things going on here
9039 *
9040 * 1) We need to reserve space for our orphan item and the space to
9041 * delete our orphan item. Lord knows we don't want to have a dangling
9042 * orphan item because we didn't reserve space to remove it.
9043 *
9044 * 2) We need to reserve space to update our inode.
9045 *
9046 * 3) We need to have something to cache all the space that is going to
9047 * be free'd up by the truncate operation, but also have some slack
9048 * space reserved in case it uses space during the truncate (thank you
9049 * very much snapshotting).
9050 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009051 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009052 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009053 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009054 * doesn't end up using space reserved for updating the inode or
9055 * removing the orphan item. We also need to be able to stop the
9056 * transaction and start a new one, which means we need to be able to
9057 * update the inode several times, and we have no idea of knowing how
9058 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009059 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009060 * Then there is the orphan item, which does indeed need to be held on
9061 * to for the whole operation, and we need nobody to touch this reserved
9062 * space except the orphan code.
9063 *
9064 * So that leaves us with
9065 *
9066 * 1) root->orphan_block_rsv - for the orphan deletion.
9067 * 2) rsv - for the truncate reservation, which we will steal from the
9068 * transaction reservation.
9069 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9070 * updating the inode.
9071 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009072 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009073 if (!rsv)
9074 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009075 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009076 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009077
Josef Bacik907cbce2011-08-08 13:46:15 -04009078 /*
Josef Bacik07127182011-08-19 10:29:59 -04009079 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009080 * 1 for updating the inode.
9081 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009082 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009083 if (IS_ERR(trans)) {
9084 err = PTR_ERR(trans);
9085 goto out;
9086 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009087
Josef Bacik907cbce2011-08-08 13:46:15 -04009088 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009089 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009090 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009091 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009092
Chris Mason5a3f23d2009-03-31 13:27:11 -04009093 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009094 * So if we truncate and then write and fsync we normally would just
9095 * write the extents that changed, which is a problem if we need to
9096 * first truncate that entire inode. So set this flag so we write out
9097 * all of the extents in the inode to the sync log so we're completely
9098 * safe.
9099 */
9100 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009101 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009102
Yan, Zheng80825102009-11-12 09:35:36 +00009103 while (1) {
9104 ret = btrfs_truncate_inode_items(trans, root, inode,
9105 inode->i_size,
9106 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009107 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009108 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009109 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009110 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009111 }
Chris Mason39279cc2007-06-12 06:35:45 -04009112
Yan, Zheng80825102009-11-12 09:35:36 +00009113 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009114 if (ret) {
9115 err = ret;
9116 break;
9117 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009118
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009119 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009120 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009121
9122 trans = btrfs_start_transaction(root, 2);
9123 if (IS_ERR(trans)) {
9124 ret = err = PTR_ERR(trans);
9125 trans = NULL;
9126 break;
9127 }
9128
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009129 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009130 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009131 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009132 BUG_ON(ret); /* shouldn't happen */
9133 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009134 }
9135
Josef Bacikddfae632017-10-19 14:16:02 -04009136 /*
9137 * We can't call btrfs_truncate_block inside a trans handle as we could
9138 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9139 * we've truncated everything except the last little bit, and can do
9140 * btrfs_truncate_block and then update the disk_i_size.
9141 */
9142 if (ret == NEED_TRUNCATE_BLOCK) {
9143 btrfs_end_transaction(trans);
9144 btrfs_btree_balance_dirty(fs_info);
9145
9146 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9147 if (ret)
9148 goto out;
9149 trans = btrfs_start_transaction(root, 1);
9150 if (IS_ERR(trans)) {
9151 ret = PTR_ERR(trans);
9152 goto out;
9153 }
9154 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9155 }
9156
Yan, Zheng80825102009-11-12 09:35:36 +00009157 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009158 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009159 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009160 if (ret)
9161 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009162 }
9163
Chris Mason917c16b2011-11-08 14:49:59 -05009164 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009165 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009166 ret = btrfs_update_inode(trans, root, inode);
9167 if (ret && !err)
9168 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009169
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009170 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009171 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009172 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009173out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009174 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009175
Josef Bacik3893e332011-01-31 16:03:11 -05009176 if (ret && !err)
9177 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009178
Josef Bacik3893e332011-01-31 16:03:11 -05009179 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009180}
9181
Sven Wegener3b963622008-06-09 21:57:42 -04009182/*
Chris Masond352ac62008-09-29 15:18:18 -04009183 * create a new subvolume directory/inode (helper for the ioctl).
9184 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009185int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009186 struct btrfs_root *new_root,
9187 struct btrfs_root *parent_root,
9188 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009189{
Chris Mason39279cc2007-06-12 06:35:45 -04009190 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009191 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009192 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009193
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009194 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9195 new_dirid, new_dirid,
9196 S_IFDIR | (~current_umask() & S_IRWXUGO),
9197 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009198 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009199 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009200 inode->i_op = &btrfs_dir_inode_operations;
9201 inode->i_fop = &btrfs_dir_file_operations;
9202
Miklos Szeredibfe86842011-10-28 14:13:29 +02009203 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009204 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009205 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009206
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009207 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9208 if (err)
9209 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009210 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009211 new_root->root_key.objectid, err);
9212
Yan, Zheng76dda932009-09-21 16:00:26 -04009213 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009214
Yan, Zheng76dda932009-09-21 16:00:26 -04009215 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009216 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009217}
9218
Chris Mason39279cc2007-06-12 06:35:45 -04009219struct inode *btrfs_alloc_inode(struct super_block *sb)
9220{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009221 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009222 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009223 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009224
David Sterba712e36c2017-10-31 17:08:27 +01009225 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009226 if (!ei)
9227 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009228
9229 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009230 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009231 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009232 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009233 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009234 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009235 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009236 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009237 ei->disk_i_size = 0;
9238 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009239 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009240 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009241 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009242 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009243 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244
Josef Bacik9e0baf62011-07-15 15:16:44 +00009245 spin_lock_init(&ei->lock);
9246 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009247 if (sb->s_magic != BTRFS_TEST_MAGIC)
9248 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9249 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009250 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009251 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009252 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009253
Miao Xie16cdcec2011-04-22 18:12:22 +08009254 ei->delayed_node = NULL;
9255
chandan r9cc97d62012-07-04 12:48:07 +05309256 ei->i_otime.tv_sec = 0;
9257 ei->i_otime.tv_nsec = 0;
9258
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009259 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009260 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009261 extent_io_tree_init(&ei->io_tree, inode);
9262 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009263 ei->io_tree.track_uptodate = 1;
9264 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009265 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009266 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009267 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009268 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009270 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009271 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009272 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009273
9274 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009275}
9276
Josef Bacikaaedb552013-10-11 14:44:09 -04009277#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9278void btrfs_test_destroy_inode(struct inode *inode)
9279{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009280 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009281 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9282}
9283#endif
9284
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009285static void btrfs_i_callback(struct rcu_head *head)
9286{
9287 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009288 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9289}
9290
Chris Mason39279cc2007-06-12 06:35:45 -04009291void btrfs_destroy_inode(struct inode *inode)
9292{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009294 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009295 struct btrfs_root *root = BTRFS_I(inode)->root;
9296
Al Virob3d9b7a2012-06-09 13:51:19 -04009297 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009298 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009299 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9300 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009301 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009302 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009303 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009304 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009305 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009306
Chris Mason5a3f23d2009-03-31 13:27:11 -04009307 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009308 * This can happen where we create an inode, but somebody else also
9309 * created the same inode and we need to destroy the one we already
9310 * created.
9311 */
9312 if (!root)
9313 goto free;
9314
Josef Bacik8a35d952012-05-23 14:26:42 -04009315 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9316 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009317 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009318 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009319 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009320 }
Josef Bacik7b128762008-07-24 12:17:14 -04009321
Chris Masond3977122009-01-05 21:25:51 -05009322 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009323 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9324 if (!ordered)
9325 break;
9326 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009327 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009328 "found ordered extent %llu %llu on inode cleanup",
9329 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009330 btrfs_remove_ordered_extent(inode, ordered);
9331 btrfs_put_ordered_extent(ordered);
9332 btrfs_put_ordered_extent(ordered);
9333 }
9334 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009335 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009336 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009337 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009338free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009339 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009340}
9341
Al Viro45321ac2010-06-07 13:43:19 -04009342int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009343{
9344 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009345
Naohiro Aota6379ef92013-06-06 09:56:34 +00009346 if (root == NULL)
9347 return 1;
9348
Liu Bofa6ac872013-02-20 14:10:23 +00009349 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009350 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009351 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009352 else
Al Viro45321ac2010-06-07 13:43:19 -04009353 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009354}
9355
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009356static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009357{
9358 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9359
9360 inode_init_once(&ei->vfs_inode);
9361}
9362
David Sterbae67c7182018-02-19 17:24:18 +01009363void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009364{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009365 /*
9366 * Make sure all delayed rcu free inodes are flushed before we
9367 * destroy cache.
9368 */
9369 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009370 kmem_cache_destroy(btrfs_inode_cachep);
9371 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009372 kmem_cache_destroy(btrfs_path_cachep);
9373 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009374}
9375
Liu Bof5c29bd2017-11-02 17:21:50 -06009376int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009377{
David Sterba837e1972012-09-07 03:00:48 -06009378 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009379 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009380 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9381 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009382 if (!btrfs_inode_cachep)
9383 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009384
David Sterba837e1972012-09-07 03:00:48 -06009385 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009386 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009387 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009388 if (!btrfs_trans_handle_cachep)
9389 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009390
David Sterba837e1972012-09-07 03:00:48 -06009391 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009392 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009393 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009394 if (!btrfs_path_cachep)
9395 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009396
David Sterba837e1972012-09-07 03:00:48 -06009397 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009398 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009399 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009400 if (!btrfs_free_space_cachep)
9401 goto fail;
9402
Chris Mason39279cc2007-06-12 06:35:45 -04009403 return 0;
9404fail:
9405 btrfs_destroy_cachep();
9406 return -ENOMEM;
9407}
9408
David Howellsa528d352017-01-31 16:46:22 +00009409static int btrfs_getattr(const struct path *path, struct kstat *stat,
9410 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009411{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009412 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009413 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009414 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009415 u32 bi_flags = BTRFS_I(inode)->flags;
9416
9417 stat->result_mask |= STATX_BTIME;
9418 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9419 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9420 if (bi_flags & BTRFS_INODE_APPEND)
9421 stat->attributes |= STATX_ATTR_APPEND;
9422 if (bi_flags & BTRFS_INODE_COMPRESS)
9423 stat->attributes |= STATX_ATTR_COMPRESSED;
9424 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9425 stat->attributes |= STATX_ATTR_IMMUTABLE;
9426 if (bi_flags & BTRFS_INODE_NODUMP)
9427 stat->attributes |= STATX_ATTR_NODUMP;
9428
9429 stat->attributes_mask |= (STATX_ATTR_APPEND |
9430 STATX_ATTR_COMPRESSED |
9431 STATX_ATTR_IMMUTABLE |
9432 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009433
Chris Mason39279cc2007-06-12 06:35:45 -04009434 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009435 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009436
9437 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009438 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009439 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009440 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009441 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009442 return 0;
9443}
9444
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009445static int btrfs_rename_exchange(struct inode *old_dir,
9446 struct dentry *old_dentry,
9447 struct inode *new_dir,
9448 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009449{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009450 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009451 struct btrfs_trans_handle *trans;
9452 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009453 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 struct inode *new_inode = new_dentry->d_inode;
9455 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009456 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009457 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009458 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9459 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460 u64 old_idx = 0;
9461 u64 new_idx = 0;
9462 u64 root_objectid;
9463 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009464 bool root_log_pinned = false;
9465 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466
9467 /* we only allow rename subvolume link between subvolumes */
9468 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9469 return -EXDEV;
9470
9471 /* close the race window with snapshot create/destroy ioctl */
9472 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009473 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009474 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009475 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009476
9477 /*
9478 * We want to reserve the absolute worst case amount of items. So if
9479 * both inodes are subvols and we need to unlink them then that would
9480 * require 4 item modifications, but if they are both normal inodes it
9481 * would require 5 item modifications, so we'll assume their normal
9482 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9483 * should cover the worst case number of items we'll modify.
9484 */
9485 trans = btrfs_start_transaction(root, 12);
9486 if (IS_ERR(trans)) {
9487 ret = PTR_ERR(trans);
9488 goto out_notrans;
9489 }
9490
9491 /*
9492 * We need to find a free sequence number both in the source and
9493 * in the destination directory for the exchange.
9494 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009495 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009496 if (ret)
9497 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009498 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499 if (ret)
9500 goto out_fail;
9501
9502 BTRFS_I(old_inode)->dir_index = 0ULL;
9503 BTRFS_I(new_inode)->dir_index = 0ULL;
9504
9505 /* Reference for the source. */
9506 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9507 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009508 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009510 btrfs_pin_log_trans(root);
9511 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 ret = btrfs_insert_inode_ref(trans, dest,
9513 new_dentry->d_name.name,
9514 new_dentry->d_name.len,
9515 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009516 btrfs_ino(BTRFS_I(new_dir)),
9517 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 if (ret)
9519 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520 }
9521
9522 /* And now for the dest. */
9523 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9524 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009525 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009527 btrfs_pin_log_trans(dest);
9528 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009529 ret = btrfs_insert_inode_ref(trans, root,
9530 old_dentry->d_name.name,
9531 old_dentry->d_name.len,
9532 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009533 btrfs_ino(BTRFS_I(old_dir)),
9534 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009535 if (ret)
9536 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009537 }
9538
9539 /* Update inode version and ctime/mtime. */
9540 inode_inc_iversion(old_dir);
9541 inode_inc_iversion(new_dir);
9542 inode_inc_iversion(old_inode);
9543 inode_inc_iversion(new_inode);
9544 old_dir->i_ctime = old_dir->i_mtime = ctime;
9545 new_dir->i_ctime = new_dir->i_mtime = ctime;
9546 old_inode->i_ctime = ctime;
9547 new_inode->i_ctime = ctime;
9548
9549 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009550 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9551 BTRFS_I(old_inode), 1);
9552 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9553 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554 }
9555
9556 /* src is a subvolume */
9557 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9558 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9559 ret = btrfs_unlink_subvol(trans, root, old_dir,
9560 root_objectid,
9561 old_dentry->d_name.name,
9562 old_dentry->d_name.len);
9563 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009564 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9565 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 old_dentry->d_name.name,
9567 old_dentry->d_name.len);
9568 if (!ret)
9569 ret = btrfs_update_inode(trans, root, old_inode);
9570 }
9571 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009572 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 goto out_fail;
9574 }
9575
9576 /* dest is a subvolume */
9577 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9578 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9579 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9580 root_objectid,
9581 new_dentry->d_name.name,
9582 new_dentry->d_name.len);
9583 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009584 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9585 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 new_dentry->d_name.name,
9587 new_dentry->d_name.len);
9588 if (!ret)
9589 ret = btrfs_update_inode(trans, dest, new_inode);
9590 }
9591 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009592 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593 goto out_fail;
9594 }
9595
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009596 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 new_dentry->d_name.name,
9598 new_dentry->d_name.len, 0, old_idx);
9599 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009600 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601 goto out_fail;
9602 }
9603
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009604 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 old_dentry->d_name.name,
9606 old_dentry->d_name.len, 0, new_idx);
9607 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009608 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 goto out_fail;
9610 }
9611
9612 if (old_inode->i_nlink == 1)
9613 BTRFS_I(old_inode)->dir_index = old_idx;
9614 if (new_inode->i_nlink == 1)
9615 BTRFS_I(new_inode)->dir_index = new_idx;
9616
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009619 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9620 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009622 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009624 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009626 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9627 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009629 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 }
9631out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009632 /*
9633 * If we have pinned a log and an error happened, we unpin tasks
9634 * trying to sync the log and force them to fallback to a transaction
9635 * commit if the log currently contains any of the inodes involved in
9636 * this rename operation (to ensure we do not persist a log with an
9637 * inconsistent state for any of these inodes or leading to any
9638 * inconsistencies when replayed). If the transaction was aborted, the
9639 * abortion reason is propagated to userspace when attempting to commit
9640 * the transaction. If the log does not contain any of these inodes, we
9641 * allow the tasks to sync it.
9642 */
9643 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009644 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9645 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9646 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009647 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009648 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009649 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009650
9651 if (root_log_pinned) {
9652 btrfs_end_log_trans(root);
9653 root_log_pinned = false;
9654 }
9655 if (dest_log_pinned) {
9656 btrfs_end_log_trans(dest);
9657 dest_log_pinned = false;
9658 }
9659 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009660 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661out_notrans:
9662 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009663 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009665 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666
9667 return ret;
9668}
9669
9670static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9671 struct btrfs_root *root,
9672 struct inode *dir,
9673 struct dentry *dentry)
9674{
9675 int ret;
9676 struct inode *inode;
9677 u64 objectid;
9678 u64 index;
9679
9680 ret = btrfs_find_free_ino(root, &objectid);
9681 if (ret)
9682 return ret;
9683
9684 inode = btrfs_new_inode(trans, root, dir,
9685 dentry->d_name.name,
9686 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009687 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009688 objectid,
9689 S_IFCHR | WHITEOUT_MODE,
9690 &index);
9691
9692 if (IS_ERR(inode)) {
9693 ret = PTR_ERR(inode);
9694 return ret;
9695 }
9696
9697 inode->i_op = &btrfs_special_inode_operations;
9698 init_special_inode(inode, inode->i_mode,
9699 WHITEOUT_DEV);
9700
9701 ret = btrfs_init_inode_security(trans, inode, dir,
9702 &dentry->d_name);
9703 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009704 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009706 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9707 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009709 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710
9711 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009712out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009714 if (ret)
9715 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 iput(inode);
9717
Filipe Mananac9901612016-05-05 01:41:57 +01009718 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719}
9720
Chris Mason39279cc2007-06-12 06:35:45 -04009721static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722 struct inode *new_dir, struct dentry *new_dentry,
9723 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009725 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009726 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009727 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009728 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9729 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009730 struct inode *new_inode = d_inode(new_dentry);
9731 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009732 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009733 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009734 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009735 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009736 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009737
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009738 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009739 return -EPERM;
9740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009741 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009742 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009743 return -EXDEV;
9744
Li Zefan33345d012011-04-20 10:31:50 +08009745 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009746 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009747 return -ENOTEMPTY;
9748
Chris Mason39279cc2007-06-12 06:35:45 -04009749 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009750 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009751 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009752
9753
9754 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009755 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009756 new_dentry->d_name.name,
9757 new_dentry->d_name.len);
9758
9759 if (ret) {
9760 if (ret == -EEXIST) {
9761 /* we shouldn't get
9762 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309763 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009764 return ret;
9765 }
9766 } else {
9767 /* maybe -EOVERFLOW */
9768 return ret;
9769 }
9770 }
9771 ret = 0;
9772
Chris Mason5a3f23d2009-03-31 13:27:11 -04009773 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009774 * we're using rename to replace one file with another. Start IO on it
9775 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009776 */
Chris Mason8d875f92014-08-12 10:47:42 -07009777 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009778 filemap_flush(old_inode->i_mapping);
9779
Yan, Zheng76dda932009-09-21 16:00:26 -04009780 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009781 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009782 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009783 /*
9784 * We want to reserve the absolute worst case amount of items. So if
9785 * both inodes are subvols and we need to unlink them then that would
9786 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009787 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009788 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9789 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009790 * If our rename has the whiteout flag, we need more 5 units for the
9791 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9792 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009793 */
Filipe Manana5062af32016-05-05 10:26:26 +01009794 trans_num_items = 11;
9795 if (flags & RENAME_WHITEOUT)
9796 trans_num_items += 5;
9797 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009798 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799 ret = PTR_ERR(trans);
9800 goto out_notrans;
9801 }
Chris Mason5f39d392007-10-15 16:14:19 -04009802
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009803 if (dest != root)
9804 btrfs_record_root_in_trans(trans, dest);
9805
Nikolay Borisov877574e2017-02-20 13:50:33 +02009806 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009807 if (ret)
9808 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009809
Miao Xie67de1172013-12-26 13:07:06 +08009810 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009811 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009812 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009813 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009814 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009815 btrfs_pin_log_trans(root);
9816 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009817 ret = btrfs_insert_inode_ref(trans, dest,
9818 new_dentry->d_name.name,
9819 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009820 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009821 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009822 if (ret)
9823 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009824 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009825
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009826 inode_inc_iversion(old_dir);
9827 inode_inc_iversion(new_dir);
9828 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009829 old_dir->i_ctime = old_dir->i_mtime =
9830 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009831 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009832
Chris Mason12fcfd22009-03-24 10:24:20 -04009833 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009834 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9835 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009836
Li Zefan33345d012011-04-20 10:31:50 +08009837 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9839 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9840 old_dentry->d_name.name,
9841 old_dentry->d_name.len);
9842 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009843 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9844 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009845 old_dentry->d_name.name,
9846 old_dentry->d_name.len);
9847 if (!ret)
9848 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009849 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009850 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009851 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009852 goto out_fail;
9853 }
Chris Mason39279cc2007-06-12 06:35:45 -04009854
9855 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009856 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009857 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009858 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009859 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9860 root_objectid = BTRFS_I(new_inode)->location.objectid;
9861 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9862 root_objectid,
9863 new_dentry->d_name.name,
9864 new_dentry->d_name.len);
9865 BUG_ON(new_inode->i_nlink == 0);
9866 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009867 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9868 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 new_dentry->d_name.name,
9870 new_dentry->d_name.len);
9871 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009872 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009873 ret = btrfs_orphan_add(trans,
9874 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009875 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009876 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009877 goto out_fail;
9878 }
Chris Mason39279cc2007-06-12 06:35:45 -04009879 }
Josef Bacikaec74772008-07-24 12:12:38 -04009880
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009881 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009882 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009883 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009884 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009885 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009886 goto out_fail;
9887 }
Chris Mason39279cc2007-06-12 06:35:45 -04009888
Miao Xie67de1172013-12-26 13:07:06 +08009889 if (old_inode->i_nlink == 1)
9890 BTRFS_I(old_inode)->dir_index = index;
9891
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009892 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009893 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009894
David Sterbaf85b7372017-01-20 14:54:07 +01009895 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9896 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009897 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009898 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009899 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009900
9901 if (flags & RENAME_WHITEOUT) {
9902 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9903 old_dentry);
9904
9905 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009906 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009907 goto out_fail;
9908 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009909 }
Chris Mason39279cc2007-06-12 06:35:45 -04009910out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009911 /*
9912 * If we have pinned the log and an error happened, we unpin tasks
9913 * trying to sync the log and force them to fallback to a transaction
9914 * commit if the log currently contains any of the inodes involved in
9915 * this rename operation (to ensure we do not persist a log with an
9916 * inconsistent state for any of these inodes or leading to any
9917 * inconsistencies when replayed). If the transaction was aborted, the
9918 * abortion reason is propagated to userspace when attempting to commit
9919 * the transaction. If the log does not contain any of these inodes, we
9920 * allow the tasks to sync it.
9921 */
9922 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009923 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9924 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9925 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009926 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009927 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009928 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009929
9930 btrfs_end_log_trans(root);
9931 log_pinned = false;
9932 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009933 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009934out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009935 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009936 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009937
Chris Mason39279cc2007-06-12 06:35:45 -04009938 return ret;
9939}
9940
Miklos Szeredi80ace852014-07-23 15:15:32 +02009941static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9942 struct inode *new_dir, struct dentry *new_dentry,
9943 unsigned int flags)
9944{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009945 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009946 return -EINVAL;
9947
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009948 if (flags & RENAME_EXCHANGE)
9949 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9950 new_dentry);
9951
9952 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009953}
9954
Miao Xie8ccf6f192012-10-25 09:28:04 +00009955static void btrfs_run_delalloc_work(struct btrfs_work *work)
9956{
9957 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009958 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009959
9960 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9961 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009962 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009963 filemap_flush(inode->i_mapping);
9964 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9965 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009966 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009967
9968 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009969 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009970 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009971 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009972 complete(&delalloc_work->completion);
9973}
9974
9975struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009976 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009977{
9978 struct btrfs_delalloc_work *work;
9979
David Sterba100d5702015-12-08 14:39:32 +01009980 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981 if (!work)
9982 return NULL;
9983
9984 init_completion(&work->completion);
9985 INIT_LIST_HEAD(&work->list);
9986 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009987 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009988 WARN_ON_ONCE(!inode);
9989 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9990 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009991
9992 return work;
9993}
9994
9995void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9996{
9997 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009998 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999}
10000
Chris Masond352ac62008-09-29 15:18:18 -040010001/*
10002 * some fairly slow code that needs optimization. This walks the list
10003 * of all the inodes with pending delalloc and forces them to disk.
10004 */
Miao Xie6c255e62014-03-06 13:55:01 +080010005static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10006 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010007{
Chris Masonea8c2812008-08-04 23:17:27 -040010008 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010009 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010010 struct btrfs_delalloc_work *work, *next;
10011 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010012 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010013 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010014
Miao Xie8ccf6f192012-10-25 09:28:04 +000010015 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010016 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010017
Miao Xie573bfb72014-03-06 13:55:03 +080010018 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 spin_lock(&root->delalloc_lock);
10020 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010021 while (!list_empty(&splice)) {
10022 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010023 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010024
Miao Xieeb73c1b2013-05-15 07:48:22 +000010025 list_move_tail(&binode->delalloc_inodes,
10026 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010027 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010028 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010029 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010030 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010031 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010032 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033
David Sterba651d4942015-11-27 19:24:16 +010010034 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010035 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010036 if (delay_iput)
10037 btrfs_add_delayed_iput(inode);
10038 else
10039 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010040 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010041 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010042 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010043 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010044 btrfs_queue_work(root->fs_info->flush_workers,
10045 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010046 ret++;
10047 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010048 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010049 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010051 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010052 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010053
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010054out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010055 list_for_each_entry_safe(work, next, &works, list) {
10056 list_del_init(&work->list);
10057 btrfs_wait_and_free_delalloc_work(work);
10058 }
10059
Miao Xieeb73c1b2013-05-15 07:48:22 +000010060 if (!list_empty_careful(&splice)) {
10061 spin_lock(&root->delalloc_lock);
10062 list_splice_tail(&splice, &root->delalloc_inodes);
10063 spin_unlock(&root->delalloc_lock);
10064 }
Miao Xie573bfb72014-03-06 13:55:03 +080010065 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010066 return ret;
10067}
10068
10069int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10070{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010071 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010072 int ret;
10073
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010075 return -EROFS;
10076
Miao Xie6c255e62014-03-06 13:55:01 +080010077 ret = __start_delalloc_inodes(root, delay_iput, -1);
10078 if (ret > 0)
10079 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010080 return ret;
10081}
10082
Miao Xie6c255e62014-03-06 13:55:01 +080010083int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10084 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010085{
10086 struct btrfs_root *root;
10087 struct list_head splice;
10088 int ret;
10089
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010090 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010091 return -EROFS;
10092
10093 INIT_LIST_HEAD(&splice);
10094
Miao Xie573bfb72014-03-06 13:55:03 +080010095 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096 spin_lock(&fs_info->delalloc_root_lock);
10097 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010098 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010099 root = list_first_entry(&splice, struct btrfs_root,
10100 delalloc_root);
10101 root = btrfs_grab_fs_root(root);
10102 BUG_ON(!root);
10103 list_move_tail(&root->delalloc_root,
10104 &fs_info->delalloc_roots);
10105 spin_unlock(&fs_info->delalloc_root_lock);
10106
Miao Xie6c255e62014-03-06 13:55:01 +080010107 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010108 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010109 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 goto out;
10111
Miao Xie6c255e62014-03-06 13:55:01 +080010112 if (nr != -1) {
10113 nr -= ret;
10114 WARN_ON(nr < 0);
10115 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 spin_lock(&fs_info->delalloc_root_lock);
10117 }
10118 spin_unlock(&fs_info->delalloc_root_lock);
10119
Miao Xie6c255e62014-03-06 13:55:01 +080010120 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010121out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010122 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 spin_lock(&fs_info->delalloc_root_lock);
10124 list_splice_tail(&splice, &fs_info->delalloc_roots);
10125 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010126 }
Miao Xie573bfb72014-03-06 13:55:03 +080010127 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010128 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010129}
10130
Chris Mason39279cc2007-06-12 06:35:45 -040010131static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10132 const char *symname)
10133{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010134 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010135 struct btrfs_trans_handle *trans;
10136 struct btrfs_root *root = BTRFS_I(dir)->root;
10137 struct btrfs_path *path;
10138 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010139 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010140 int err;
10141 int drop_inode = 0;
10142 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010143 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010144 int name_len;
10145 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010146 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010147 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010148 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010149
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010150 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010151 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010152 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010153
Josef Bacik9ed74f22009-09-11 16:12:44 -040010154 /*
10155 * 2 items for inode item and ref
10156 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010157 * 1 item for updating parent inode item
10158 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010159 * 1 item for xattr if selinux is on
10160 */
Filipe Manana9269d122015-12-31 18:16:29 +000010161 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010162 if (IS_ERR(trans))
10163 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010164
Li Zefan581bb052011-04-20 10:06:11 +080010165 err = btrfs_find_free_ino(root, &objectid);
10166 if (err)
10167 goto out_unlock;
10168
Josef Bacikaec74772008-07-24 12:12:38 -040010169 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010170 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10171 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010172 if (IS_ERR(inode)) {
10173 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010174 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010175 }
Chris Mason39279cc2007-06-12 06:35:45 -040010176
Casey Schauflerad19db72011-12-15 10:09:07 -050010177 /*
10178 * If the active LSM wants to access the inode during
10179 * d_instantiate it needs these. Smack checks to see
10180 * if the filesystem supports xattrs by looking at the
10181 * ops vector.
10182 */
10183 inode->i_fop = &btrfs_file_operations;
10184 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010185 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010186 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10187
10188 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10189 if (err)
10190 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010191
Chris Mason39279cc2007-06-12 06:35:45 -040010192 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010193 if (!path) {
10194 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010195 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010196 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010197 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010198 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010199 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010200 datasize = btrfs_file_extent_calc_inline_size(name_len);
10201 err = btrfs_insert_empty_item(trans, root, path, &key,
10202 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010203 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010204 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010205 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010206 }
Chris Mason5f39d392007-10-15 16:14:19 -040010207 leaf = path->nodes[0];
10208 ei = btrfs_item_ptr(leaf, path->slots[0],
10209 struct btrfs_file_extent_item);
10210 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10211 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010212 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010213 btrfs_set_file_extent_encryption(leaf, ei, 0);
10214 btrfs_set_file_extent_compression(leaf, ei, 0);
10215 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10216 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10217
Chris Mason39279cc2007-06-12 06:35:45 -040010218 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010219 write_extent_buffer(leaf, symname, ptr, name_len);
10220 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010221 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010222
Chris Mason39279cc2007-06-12 06:35:45 -040010223 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010224 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010225 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010226 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010227 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010228 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010229 /*
10230 * Last step, add directory indexes for our symlink inode. This is the
10231 * last step to avoid extra cleanup of these indexes if an error happens
10232 * elsewhere above.
10233 */
10234 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010235 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10236 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010237 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010238 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010239 goto out_unlock_inode;
10240 }
10241
Al Viro1e2e5472018-05-04 08:23:01 -040010242 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010243
10244out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010245 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010246 if (drop_inode) {
10247 inode_dec_link_count(inode);
10248 iput(inode);
10249 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010250 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010251 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010252
10253out_unlock_inode:
10254 drop_inode = 1;
10255 unlock_new_inode(inode);
10256 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010257}
Chris Mason16432982008-04-10 10:23:21 -040010258
Josef Bacik0af3d002010-06-21 14:48:16 -040010259static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10260 u64 start, u64 num_bytes, u64 min_size,
10261 loff_t actual_len, u64 *alloc_hint,
10262 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010265 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10266 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010267 struct btrfs_root *root = BTRFS_I(inode)->root;
10268 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010269 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010270 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010271 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010272 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010273 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010274 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010275 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010276
Josef Bacik0af3d002010-06-21 14:48:16 -040010277 if (trans)
10278 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010279 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010280 if (own_trans) {
10281 trans = btrfs_start_transaction(root, 3);
10282 if (IS_ERR(trans)) {
10283 ret = PTR_ERR(trans);
10284 break;
10285 }
Yan Zhengd899e052008-10-30 14:25:28 -040010286 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010287
Byongho Leeee221842015-12-15 01:42:10 +090010288 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010289 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010290 /*
10291 * If we are severely fragmented we could end up with really
10292 * small allocations, so if the allocator is returning small
10293 * chunks lets make its job easier by only searching for those
10294 * sized chunks.
10295 */
10296 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010297 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10298 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010299 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010300 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010301 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010302 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010303 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010304 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010305
Josef Bacik0b670dc2015-09-23 17:11:16 -040010306 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010307 ret = insert_reserved_file_extent(trans, inode,
10308 cur_offset, ins.objectid,
10309 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010310 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010311 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010312 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010313 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010314 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010315 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010316 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010317 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010318 break;
10319 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010320
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010321 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010322 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010323
Josef Bacik5dc562c2012-08-17 13:14:17 -040010324 em = alloc_extent_map();
10325 if (!em) {
10326 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10327 &BTRFS_I(inode)->runtime_flags);
10328 goto next;
10329 }
10330
10331 em->start = cur_offset;
10332 em->orig_start = cur_offset;
10333 em->len = ins.offset;
10334 em->block_start = ins.objectid;
10335 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010336 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010337 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010338 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010339 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10340 em->generation = trans->transid;
10341
10342 while (1) {
10343 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010344 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010345 write_unlock(&em_tree->lock);
10346 if (ret != -EEXIST)
10347 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010348 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010349 cur_offset + ins.offset - 1,
10350 0);
10351 }
10352 free_extent_map(em);
10353next:
Yan Zhengd899e052008-10-30 14:25:28 -040010354 num_bytes -= ins.offset;
10355 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010356 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010357
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010358 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010359 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010360 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010361 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010362 (actual_len > inode->i_size) &&
10363 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010364 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010365 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010366 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010367 i_size = cur_offset;
10368 i_size_write(inode, i_size);
10369 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010370 }
10371
Yan Zhengd899e052008-10-30 14:25:28 -040010372 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010373
10374 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010375 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010376 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010377 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010378 break;
10379 }
Yan Zhengd899e052008-10-30 14:25:28 -040010380
Josef Bacik0af3d002010-06-21 14:48:16 -040010381 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010382 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010383 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010384 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010385 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010386 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010387 return ret;
10388}
10389
Josef Bacik0af3d002010-06-21 14:48:16 -040010390int btrfs_prealloc_file_range(struct inode *inode, int mode,
10391 u64 start, u64 num_bytes, u64 min_size,
10392 loff_t actual_len, u64 *alloc_hint)
10393{
10394 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10395 min_size, actual_len, alloc_hint,
10396 NULL);
10397}
10398
10399int btrfs_prealloc_file_range_trans(struct inode *inode,
10400 struct btrfs_trans_handle *trans, int mode,
10401 u64 start, u64 num_bytes, u64 min_size,
10402 loff_t actual_len, u64 *alloc_hint)
10403{
10404 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10405 min_size, actual_len, alloc_hint, trans);
10406}
10407
Chris Masone6dcd2d2008-07-17 12:53:50 -040010408static int btrfs_set_page_dirty(struct page *page)
10409{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010410 return __set_page_dirty_nobuffers(page);
10411}
10412
Al Viro10556cb2011-06-20 19:28:19 -040010413static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010414{
Li Zefanb83cc962010-12-20 16:04:08 +080010415 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010416 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010417
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010418 if (mask & MAY_WRITE &&
10419 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10420 if (btrfs_root_readonly(root))
10421 return -EROFS;
10422 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10423 return -EACCES;
10424 }
Al Viro2830ba72011-06-20 19:16:29 -040010425 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010426}
Chris Mason39279cc2007-06-12 06:35:45 -040010427
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010428static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10429{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010430 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010431 struct btrfs_trans_handle *trans;
10432 struct btrfs_root *root = BTRFS_I(dir)->root;
10433 struct inode *inode = NULL;
10434 u64 objectid;
10435 u64 index;
10436 int ret = 0;
10437
10438 /*
10439 * 5 units required for adding orphan entry
10440 */
10441 trans = btrfs_start_transaction(root, 5);
10442 if (IS_ERR(trans))
10443 return PTR_ERR(trans);
10444
10445 ret = btrfs_find_free_ino(root, &objectid);
10446 if (ret)
10447 goto out;
10448
10449 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010450 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010451 if (IS_ERR(inode)) {
10452 ret = PTR_ERR(inode);
10453 inode = NULL;
10454 goto out;
10455 }
10456
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010457 inode->i_fop = &btrfs_file_operations;
10458 inode->i_op = &btrfs_file_inode_operations;
10459
10460 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010461 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10462
Chris Masonb0d5d102014-09-08 13:08:51 -070010463 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10464 if (ret)
10465 goto out_inode;
10466
10467 ret = btrfs_update_inode(trans, root, inode);
10468 if (ret)
10469 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010470 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010471 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010472 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010473
Filipe Manana5762b5c2014-08-01 00:10:32 +010010474 /*
10475 * We set number of links to 0 in btrfs_new_inode(), and here we set
10476 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10477 * through:
10478 *
10479 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10480 */
10481 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010482 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010483 d_tmpfile(dentry, inode);
10484 mark_inode_dirty(inode);
10485
10486out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010487 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010488 if (ret)
10489 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010490 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010491 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010492
10493out_inode:
10494 unlock_new_inode(inode);
10495 goto out;
10496
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010497}
10498
David Sterba20a7db82017-02-17 16:24:29 +010010499__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010500static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010501{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010502 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010503}
10504
Josef Bacikc6100a42017-05-05 11:57:13 -040010505static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10506{
10507 struct inode *inode = private_data;
10508 return btrfs_sb(inode->i_sb);
10509}
10510
10511static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10512 u64 start, u64 end)
10513{
10514 struct inode *inode = private_data;
10515 u64 isize;
10516
10517 isize = i_size_read(inode);
10518 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10519 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10520 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10521 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10522 }
10523}
10524
10525void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10526{
10527 struct inode *inode = private_data;
10528 unsigned long index = start >> PAGE_SHIFT;
10529 unsigned long end_index = end >> PAGE_SHIFT;
10530 struct page *page;
10531
10532 while (index <= end_index) {
10533 page = find_get_page(inode->i_mapping, index);
10534 ASSERT(page); /* Pages should be in the extent_io_tree */
10535 set_page_writeback(page);
10536 put_page(page);
10537 index++;
10538 }
10539}
10540
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010541static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010542 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010543 .lookup = btrfs_lookup,
10544 .create = btrfs_create,
10545 .unlink = btrfs_unlink,
10546 .link = btrfs_link,
10547 .mkdir = btrfs_mkdir,
10548 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010549 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010550 .symlink = btrfs_symlink,
10551 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010552 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010553 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010554 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010555 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010556 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010557 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010558 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010559};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010560static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010561 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010562 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010563 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010564};
Yan, Zheng76dda932009-09-21 16:00:26 -040010565
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010566static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010567 .llseek = generic_file_llseek,
10568 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010569 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010570 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010571 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010572#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010573 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010574#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010575 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010576 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010577};
10578
David Sterba20e55062015-11-19 11:42:28 +010010579static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010580 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010581 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010582 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010583 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010584 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010585 .tree_fs_info = iotree_fs_info,
10586 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010587
10588 /* optional callbacks */
10589 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010590 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010591 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010592 .set_bit_hook = btrfs_set_bit_hook,
10593 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010594 .merge_extent_hook = btrfs_merge_extent_hook,
10595 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010596 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010597};
10598
Chris Mason35054392009-01-21 13:11:13 -050010599/*
10600 * btrfs doesn't support the bmap operation because swapfiles
10601 * use bmap to make a mapping of extents in the file. They assume
10602 * these extents won't change over the life of the file and they
10603 * use the bmap result to do IO directly to the drive.
10604 *
10605 * the btrfs bmap call would return logical addresses that aren't
10606 * suitable for IO and they also will change frequently as COW
10607 * operations happen. So, swapfile + btrfs == corruption.
10608 *
10609 * For now we're avoiding this by dropping bmap.
10610 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010611static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010612 .readpage = btrfs_readpage,
10613 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010614 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010615 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010616 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010617 .invalidatepage = btrfs_invalidatepage,
10618 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010619 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010620 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010621};
10622
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010623static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010624 .readpage = btrfs_readpage,
10625 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010626 .invalidatepage = btrfs_invalidatepage,
10627 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010628};
10629
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010630static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010631 .getattr = btrfs_getattr,
10632 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010633 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010634 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010635 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010636 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010637 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010638 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010639};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010640static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010641 .getattr = btrfs_getattr,
10642 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010643 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010644 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010645 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010646 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010647 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010648};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010649static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010650 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010651 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010652 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010653 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010654 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010655 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010656};
Yan, Zheng76dda932009-09-21 16:00:26 -040010657
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010658const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010659 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010660 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010661};