blob: 7148abaf1da32a985e7ee3dbb12d1186249c7f98 [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>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040040#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040041#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040042#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020043#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040044#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050045#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050046#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080047#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000048#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080051#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080054 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040055 struct btrfs_root *root;
56};
57
Filipe Mananaf28a4922015-12-08 19:23:20 +000058struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000059 u64 reserve;
60 u64 unsubmitted_oe_range_start;
61 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080062 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000063};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010073static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
76struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010081static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040082 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Eric Sandeen3972f262013-01-12 02:57:22 +000091static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000092static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080096 u64 start, u64 end, u64 delalloc_end,
97 int *page_started, unsigned long *nr_written,
98 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -080099static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
100 u64 orig_start, u64 block_start,
101 u64 block_len, u64 orig_block_len,
102 u64 ram_bytes, int compress_type,
103 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400104
Qu Wenruo524272602017-03-08 10:25:52 +0800105static void __endio_write_update_ordered(struct inode *inode,
106 const u64 offset, const u64 bytes,
107 const bool uptodate);
108
109/*
110 * Cleanup all submitted ordered extents in specified range to handle errors
111 * from the fill_dellaloc() callback.
112 *
113 * NOTE: caller must ensure that when an error happens, it can not call
114 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
115 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
116 * to be released, which we want to happen only when finishing the ordered
117 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
118 * fill_delalloc() callback already does proper cleanup for the first page of
119 * the range, that is, it invokes the callback writepage_end_io_hook() for the
120 * range of the first page.
121 */
122static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
123 const u64 offset,
124 const u64 bytes)
125{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900126 unsigned long index = offset >> PAGE_SHIFT;
127 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
128 struct page *page;
129
130 while (index <= end_index) {
131 page = find_get_page(inode->i_mapping, index);
132 index++;
133 if (!page)
134 continue;
135 ClearPagePrivate2(page);
136 put_page(page);
137 }
Qu Wenruo524272602017-03-08 10:25:52 +0800138 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
139 bytes - PAGE_SIZE, false);
140}
141
Eric Sandeen48a3b632013-04-25 20:41:01 +0000142static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400143
Josef Bacik6a3891c2015-03-16 17:38:52 -0400144#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145void btrfs_test_inode_set_ops(struct inode *inode)
146{
147 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
148}
149#endif
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500152 struct inode *inode, struct inode *dir,
153 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500154{
155 int err;
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500159 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500160 return err;
161}
162
Chris Masond352ac62008-09-29 15:18:18 -0400163/*
Chris Masonc8b97812008-10-29 14:49:59 -0400164 * this does all the hard work for inserting an inline extent into
165 * the btree. The caller should have done a btrfs_drop_extents so that
166 * no overlapping inline items exist in the btree
167 */
Chris Mason40f76582014-05-21 13:35:51 -0700168static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000169 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct btrfs_root *root, struct inode *inode,
171 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000172 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page **compressed_pages)
174{
Chris Masonc8b97812008-10-29 14:49:59 -0400175 struct extent_buffer *leaf;
176 struct page *page = NULL;
177 char *kaddr;
178 unsigned long ptr;
179 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 int ret;
181 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400183
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400185 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400186
Chris Masonc8b97812008-10-29 14:49:59 -0400187 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000188
189 if (!extent_inserted) {
190 struct btrfs_key key;
191 size_t datasize;
192
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200193 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000194 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200195 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196
197 datasize = btrfs_file_extent_calc_inline_size(cur_size);
198 path->leave_spinning = 1;
199 ret = btrfs_insert_empty_item(trans, root, path, &key,
200 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200201 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000202 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400203 }
204 leaf = path->nodes[0];
205 ei = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
208 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
209 btrfs_set_file_extent_encryption(leaf, ei, 0);
210 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
211 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
212 ptr = btrfs_file_extent_inline_start(ei);
213
Li Zefan261507a02010-12-17 14:21:50 +0800214 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400215 struct page *cpage;
216 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500217 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500219 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300220 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
Cong Wang7ac687d2011-11-25 23:14:28 +0800222 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400223 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
226 i++;
227 ptr += cur_size;
228 compressed_size -= cur_size;
229 }
230 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800231 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400232 } else {
233 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 }
242 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400244
Yan, Zhengc2167752009-11-12 09:34:21 +0000245 /*
246 * we're an inline extent, so nobody can
247 * extend the file past i_size without locking
248 * a page we already have locked.
249 *
250 * We must do any isize and inode updates
251 * before we unlock the pages. Otherwise we
252 * could end up racing with unlink.
253 */
Chris Masonc8b97812008-10-29 14:49:59 -0400254 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100255 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000256
Chris Masonc8b97812008-10-29 14:49:59 -0400257fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259}
260
261
262/*
263 * conditionally insert an inline extent into the file. This
264 * does the checks required to make sure the data is small enough
265 * to fit as an inline extent.
266 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200267static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400268 u64 end, size_t compressed_size,
269 int compress_type,
270 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400271{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200272 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400274 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400275 u64 isize = i_size_read(inode);
276 u64 actual_end = min(end + 1, isize);
277 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400278 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400279 u64 data_len = inline_len;
280 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 struct btrfs_path *path;
282 int extent_inserted = 0;
283 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400284
285 if (compressed_size)
286 data_len = compressed_size;
287
288 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400289 actual_end > fs_info->sectorsize ||
290 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400291 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400292 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400293 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400294 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400295 return 1;
296 }
297
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000298 path = btrfs_alloc_path();
299 if (!path)
300 return -ENOMEM;
301
Josef Bacik00361582013-08-14 14:02:47 -0400302 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000303 if (IS_ERR(trans)) {
304 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400305 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400307 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400308
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 if (compressed_size && compressed_pages)
310 extent_item_size = btrfs_file_extent_calc_inline_size(
311 compressed_size);
312 else
313 extent_item_size = btrfs_file_extent_calc_inline_size(
314 inline_len);
315
316 ret = __btrfs_drop_extents(trans, root, inode, path,
317 start, aligned_end, NULL,
318 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400319 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400320 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400321 goto out;
322 }
Chris Masonc8b97812008-10-29 14:49:59 -0400323
324 if (isize > actual_end)
325 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000326 ret = insert_inline_extent(trans, path, extent_inserted,
327 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400328 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000329 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400330 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400331 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400332 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400333 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400334 ret = 1;
335 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100336 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400337
Josef Bacikbdc20e62013-02-28 13:23:38 -0500338 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200339 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400340out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800341 /*
342 * Don't forget to free the reserved space, as for inlined extent
343 * it won't count as data extent, free them directly here.
344 * And at reserve time, it's always aligned to page size, so
345 * just free one page here.
346 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800347 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000348 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400349 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400350 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400351}
352
Chris Mason771ed682008-11-06 22:02:51 -0500353struct async_extent {
354 u64 start;
355 u64 ram_size;
356 u64 compressed_size;
357 struct page **pages;
358 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800359 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500360 struct list_head list;
361};
362
363struct async_cow {
364 struct inode *inode;
365 struct btrfs_root *root;
366 struct page *locked_page;
367 u64 start;
368 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600369 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500370 struct list_head extents;
371 struct btrfs_work work;
372};
373
374static noinline int add_async_extent(struct async_cow *cow,
375 u64 start, u64 ram_size,
376 u64 compressed_size,
377 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800378 unsigned long nr_pages,
379 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500380{
381 struct async_extent *async_extent;
382
383 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100384 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500385 async_extent->start = start;
386 async_extent->ram_size = ram_size;
387 async_extent->compressed_size = compressed_size;
388 async_extent->pages = pages;
389 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800390 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500391 list_add_tail(&async_extent->list, &cow->extents);
392 return 0;
393}
394
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300395static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800398
399 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400400 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800401 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200402 /* defrag ioctl */
403 if (BTRFS_I(inode)->defrag_compress)
404 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800405 /* bad compression ratios */
406 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
407 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400408 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800409 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200410 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300411 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800412 return 0;
413}
414
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200415static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800416 u64 start, u64 end, u64 num_bytes, u64 small_write)
417{
418 /* If this is a small write inside eof, kick off a defrag */
419 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200420 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800421 btrfs_add_inode_defrag(NULL, inode);
422}
423
Chris Masonc8b97812008-10-29 14:49:59 -0400424/*
Chris Mason771ed682008-11-06 22:02:51 -0500425 * we create compressed extents in two phases. The first
426 * phase compresses a range of pages that have already been
427 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400428 *
Chris Mason771ed682008-11-06 22:02:51 -0500429 * This is done inside an ordered work queue, and the compression
430 * is spread across many cpus. The actual IO submission is step
431 * two, and the ordered work queue takes care of making sure that
432 * happens in the same order things were put onto the queue by
433 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400434 *
Chris Mason771ed682008-11-06 22:02:51 -0500435 * If this code finds it can't get good compression, it puts an
436 * entry onto the work queue to write the uncompressed bytes. This
437 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300438 * are written in the same order that the flusher thread sent them
439 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400440 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100441static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500442 struct page *locked_page,
443 u64 start, u64 end,
444 struct async_cow *async_cow,
445 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400448 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400449 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500450 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400451 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400452 struct page **pages = NULL;
453 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400454 unsigned long total_compressed = 0;
455 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400456 int i;
457 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400458 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400459 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400460
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200461 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
462 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400463
Chris Mason42dc7ba2008-12-15 11:44:56 -0500464 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400465again:
466 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300467 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100468 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
469 nr_pages = min_t(unsigned long, nr_pages,
470 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400471
Chris Masonf03d9301f2009-02-04 09:31:06 -0500472 /*
473 * we don't want to send crud past the end of i_size through
474 * compression, that's just a waste of CPU time. So, if the
475 * end of the file is before the start of our current
476 * requested range of bytes, we bail out to the uncompressed
477 * cleanup code that can deal with all of this.
478 *
479 * It isn't really the fastest way to fix things, but this is a
480 * very uncommon corner.
481 */
482 if (actual_end <= start)
483 goto cleanup_and_bail_uncompressed;
484
Chris Masonc8b97812008-10-29 14:49:59 -0400485 total_compressed = actual_end - start;
486
Shilong Wang4bcbb332014-10-07 18:44:35 -0400487 /*
488 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400489 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400490 */
491 if (total_compressed <= blocksize &&
492 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
493 goto cleanup_and_bail_uncompressed;
494
David Sterba069eac72017-02-14 19:36:54 +0100495 total_compressed = min_t(unsigned long, total_compressed,
496 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 total_in = 0;
498 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400499
Chris Mason771ed682008-11-06 22:02:51 -0500500 /*
501 * we do compression for mount -o compress and when the
502 * inode has not been flagged as nocompress. This flag can
503 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400504 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300505 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400506 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100507 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800508 if (!pages) {
509 /* just bail out to the uncompressed code */
510 goto cont;
511 }
Chris Mason179e29e2007-11-01 11:28:41 -0400512
David Sterbaeec63c62017-07-17 19:41:31 +0200513 if (BTRFS_I(inode)->defrag_compress)
514 compress_type = BTRFS_I(inode)->defrag_compress;
515 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200516 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800517
Chris Mason4adaa612013-03-26 13:07:00 -0400518 /*
519 * we need to call clear_page_dirty_for_io on each
520 * page in the range. Otherwise applications with the file
521 * mmap'd can wander in and change the page contents while
522 * we are compressing them.
523 *
524 * If the compression fails for any reason, we set the pages
525 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300526 *
527 * Note that the remaining part is redirtied, the start pointer
528 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400529 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300530 if (!redirty) {
531 extent_range_clear_dirty_for_io(inode, start, end);
532 redirty = 1;
533 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200534
535 /* Compression level is applied here and only here */
536 ret = btrfs_compress_pages(
537 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800538 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100539 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100540 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800541 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100542 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400543
544 if (!ret) {
545 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300546 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100547 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400548 char *kaddr;
549
550 /* zero the tail end of the last page, we might be
551 * sending it down to disk
552 */
553 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800554 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400555 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300556 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800557 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 }
559 will_compress = 1;
560 }
561 }
Li Zefan560f7d72011-09-08 10:22:01 +0800562cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400563 if (start == 0) {
564 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300565 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400566 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500567 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400568 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200569 ret = cow_file_range_inline(inode, start, end, 0,
570 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500572 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200573 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000574 total_compressed,
575 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400576 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100577 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400578 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400579 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
580 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100581 unsigned long page_error_op;
582
Filipe Mananae6eb4312014-10-10 10:45:12 +0100583 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400584
Chris Mason771ed682008-11-06 22:02:51 -0500585 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100586 * inline extent creation worked or returned error,
587 * we don't need to create any more async work items.
588 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400589 *
590 * We use DO_ACCOUNTING here because we need the
591 * delalloc_release_metadata to be done _after_ we drop
592 * our outstanding extent for clearing delalloc for this
593 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500594 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800595 extent_clear_unlock_delalloc(inode, start, end, end,
596 NULL, clear_flags,
597 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400598 PAGE_CLEAR_DIRTY |
599 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100600 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400601 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400602 goto free_pages_out;
603 }
604 }
605
606 if (will_compress) {
607 /*
608 * we aren't doing an inline extent round the compressed size
609 * up to a block size boundary so the allocator does sane
610 * things
611 */
Qu Wenruofda28322013-02-26 08:10:22 +0000612 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400613
614 /*
615 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300616 * win, compare the page count read with the blocks on disk,
617 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400618 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300620 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700621 *num_added += 1;
622
623 /*
624 * The async work queues will take care of doing actual
625 * allocation on disk for these compressed pages, and
626 * will submit them to the elevator.
627 */
Timofey Titovets11708622017-10-03 18:06:01 +0300628 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100629 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700630 compress_type);
631
Timofey Titovets11708622017-10-03 18:06:01 +0300632 if (start + total_in < end) {
633 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 pages = NULL;
635 cond_resched();
636 goto again;
637 }
638 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400639 }
640 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700641 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400642 /*
643 * the compression code ran but failed to make things smaller,
644 * free any pages it allocated and our page pointer array
645 */
David Sterba4d3a8002017-02-14 19:04:07 +0100646 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400647 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300648 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400649 }
650 kfree(pages);
651 pages = NULL;
652 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100653 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400654
655 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400656 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200657 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500658 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500659 }
Chris Masonc8b97812008-10-29 14:49:59 -0400660 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500661cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700662 /*
663 * No compression, but we still need to write the pages in the file
664 * we've been given so far. redirty the locked page if it corresponds
665 * to our extent and set things up for the async work queue to run
666 * cow_file_range to do the normal delalloc dance.
667 */
668 if (page_offset(locked_page) >= start &&
669 page_offset(locked_page) <= end)
670 __set_page_dirty_nobuffers(locked_page);
671 /* unlocked later on in the async handlers */
672
673 if (redirty)
674 extent_range_redirty_for_io(inode, start, end);
675 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
676 BTRFS_COMPRESS_NONE);
677 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500678
Filipe Mananac44f6492014-10-09 21:15:44 +0100679 return;
Chris Mason771ed682008-11-06 22:02:51 -0500680
681free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100682 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500683 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300684 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500685 }
Chris Masond3977122009-01-05 21:25:51 -0500686 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500687}
Chris Mason771ed682008-11-06 22:02:51 -0500688
Filipe Manana40ae8372014-10-06 22:14:24 +0100689static void free_async_extent_pages(struct async_extent *async_extent)
690{
691 int i;
692
693 if (!async_extent->pages)
694 return;
695
696 for (i = 0; i < async_extent->nr_pages; i++) {
697 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300698 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100699 }
700 kfree(async_extent->pages);
701 async_extent->nr_pages = 0;
702 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500703}
704
705/*
706 * phase two of compressed writeback. This is the ordered portion
707 * of the code, which only gets called in the order the work was
708 * queued. We walk all the async extents created by compress_file_range
709 * and send them down to the disk.
710 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100711static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500712 struct async_cow *async_cow)
713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500715 struct async_extent *async_extent;
716 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500717 struct btrfs_key ins;
718 struct extent_map *em;
719 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500721 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500722
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500723again:
Chris Masond3977122009-01-05 21:25:51 -0500724 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500725 async_extent = list_entry(async_cow->extents.next,
726 struct async_extent, list);
727 list_del(&async_extent->list);
728
729 io_tree = &BTRFS_I(inode)->io_tree;
730
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500731retry:
Chris Mason771ed682008-11-06 22:02:51 -0500732 /* did the compression code fall back to uncompressed IO? */
733 if (!async_extent->pages) {
734 int page_started = 0;
735 unsigned long nr_written = 0;
736
737 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000738 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100739 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500740
741 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500742 ret = cow_file_range(inode, async_cow->locked_page,
743 async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800746 async_extent->start +
747 async_extent->ram_size - 1,
748 &page_started, &nr_written, 0,
749 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500750
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100751 /* JDM XXX */
752
Chris Mason771ed682008-11-06 22:02:51 -0500753 /*
754 * if page_started, cow_file_range inserted an
755 * inline extent and took care of all the unlocking
756 * and IO for us. Otherwise, we need to submit
757 * all those pages down to the drive.
758 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500759 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200760 extent_write_locked_range(inode,
761 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500762 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500763 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500764 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500765 else if (ret)
766 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500767 kfree(async_extent);
768 cond_resched();
769 continue;
770 }
771
772 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100773 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500774
Wang Xiaoguang18513092016-07-25 15:51:40 +0800775 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500776 async_extent->compressed_size,
777 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800778 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500779 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100780 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400782 if (ret == -ENOSPC) {
783 unlock_extent(io_tree, async_extent->start,
784 async_extent->start +
785 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800786
787 /*
788 * we need to redirty the pages if we decide to
789 * fallback to uncompressed IO, otherwise we
790 * will not submit these pages down to lower
791 * layers.
792 */
793 extent_range_redirty_for_io(inode,
794 async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1);
797
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400799 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500800 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500801 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000802 /*
803 * here we're doing allocation and writeback of the
804 * compressed pages
805 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800806 em = create_io_em(inode, async_extent->start,
807 async_extent->ram_size, /* len */
808 async_extent->start, /* orig_start */
809 ins.objectid, /* block_start */
810 ins.offset, /* block_len */
811 ins.offset, /* orig_block_len */
812 async_extent->ram_size, /* ram_bytes */
813 async_extent->compress_type,
814 BTRFS_ORDERED_COMPRESSED);
815 if (IS_ERR(em))
816 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500817 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800818 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819
Li Zefan261507a02010-12-17 14:21:50 +0800820 ret = btrfs_add_ordered_extent_compress(inode,
821 async_extent->start,
822 ins.objectid,
823 async_extent->ram_size,
824 ins.offset,
825 BTRFS_ORDERED_COMPRESSED,
826 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200828 btrfs_drop_extent_cache(BTRFS_I(inode),
829 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 async_extent->start +
831 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100833 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400834 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500835
Chris Mason771ed682008-11-06 22:02:51 -0500836 /*
837 * clear dirty, set writeback and unlock the pages.
838 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400839 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400840 async_extent->start +
841 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800842 async_extent->start +
843 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400844 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
845 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200847 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500848 async_extent->start,
849 async_extent->ram_size,
850 ins.objectid,
851 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600852 async_extent->nr_pages,
853 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100854 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
855 struct page *p = async_extent->pages[0];
856 const u64 start = async_extent->start;
857 const u64 end = start + async_extent->ram_size - 1;
858
859 p->mapping = inode->i_mapping;
860 tree->ops->writepage_end_io_hook(p, start, end,
861 NULL, 0);
862 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800863 extent_clear_unlock_delalloc(inode, start, end, end,
864 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100865 PAGE_END_WRITEBACK |
866 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100867 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100868 }
Chris Mason771ed682008-11-06 22:02:51 -0500869 alloc_hint = ins.objectid + ins.offset;
870 kfree(async_extent);
871 cond_resched();
872 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100873 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500874out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400875 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400876 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100877out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400878 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500879 async_extent->start +
880 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800881 async_extent->start +
882 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400883 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100884 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400885 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
886 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100887 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
888 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100889 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500892}
893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
895 u64 num_bytes)
896{
897 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
898 struct extent_map *em;
899 u64 alloc_hint = 0;
900
901 read_lock(&em_tree->lock);
902 em = search_extent_mapping(em_tree, start, num_bytes);
903 if (em) {
904 /*
905 * if block start isn't an actual block number then find the
906 * first block in this inode and use that as a hint. If that
907 * block is also bogus then just don't worry about it.
908 */
909 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
910 free_extent_map(em);
911 em = search_extent_mapping(em_tree, 0, 0);
912 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
913 alloc_hint = em->block_start;
914 if (em)
915 free_extent_map(em);
916 } else {
917 alloc_hint = em->block_start;
918 free_extent_map(em);
919 }
920 }
921 read_unlock(&em_tree->lock);
922
923 return alloc_hint;
924}
925
Chris Mason771ed682008-11-06 22:02:51 -0500926/*
927 * when extent_io.c finds a delayed allocation range in the file,
928 * the call backs end up in this code. The basic idea is to
929 * allocate extents on disk for the range, and create ordered data structs
930 * in ram to track those extents.
931 *
932 * locked_page is the page that writepage had locked already. We use
933 * it to make sure we don't do extra locks or unlocks.
934 *
935 * *page_started is set to one if we unlock locked_page and do everything
936 * required to start IO on it. It may be clean and already done with
937 * IO when we return.
938 */
Josef Bacik00361582013-08-14 14:02:47 -0400939static noinline int cow_file_range(struct inode *inode,
940 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800941 u64 start, u64 end, u64 delalloc_end,
942 int *page_started, unsigned long *nr_written,
943 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500944{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400946 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500947 u64 alloc_hint = 0;
948 u64 num_bytes;
949 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000950 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400951 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500952 struct btrfs_key ins;
953 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000954 unsigned clear_bits;
955 unsigned long page_ops;
956 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500957 int ret = 0;
958
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200959 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400960 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500961 ret = -EINVAL;
962 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400963 }
Chris Mason771ed682008-11-06 22:02:51 -0500964
Qu Wenruofda28322013-02-26 08:10:22 +0000965 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500966 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800967 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500968
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200969 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400970
Chris Mason771ed682008-11-06 22:02:51 -0500971 if (start == 0) {
972 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200973 ret = cow_file_range_inline(inode, start, end, 0,
974 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500975 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400976 /*
977 * We use DO_ACCOUNTING here because we need the
978 * delalloc_release_metadata to be run _after_ we drop
979 * our outstanding extent for clearing delalloc for this
980 * range.
981 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800982 extent_clear_unlock_delalloc(inode, start, end,
983 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400984 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400985 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
986 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400987 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
988 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500989 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300990 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500991 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500992 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100993 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500995 }
996 }
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Josef Bacik4b46fce2010-05-23 11:00:55 -0400998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200999 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1000 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001001
Anand Jain3752d222018-02-15 12:29:38 +08001002 while (num_bytes > 0) {
1003 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001004 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001005 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001006 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001007 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001009 cur_alloc_size = ins.offset;
1010 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001011
Chris Mason771ed682008-11-06 22:02:51 -05001012 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001013 em = create_io_em(inode, start, ins.offset, /* len */
1014 start, /* orig_start */
1015 ins.objectid, /* block_start */
1016 ins.offset, /* block_len */
1017 ins.offset, /* orig_block_len */
1018 ram_size, /* ram_bytes */
1019 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001020 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001021 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001022 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001023 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001024
Chris Masone6dcd2d2008-07-17 12:53:50 -04001025 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001026 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001027 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001028 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001029
Yan Zheng17d217f2008-12-12 10:03:38 -05001030 if (root->root_key.objectid ==
1031 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1032 ret = btrfs_reloc_clone_csums(inode, start,
1033 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001034 /*
1035 * Only drop cache here, and process as normal.
1036 *
1037 * We must not allow extent_clear_unlock_delalloc()
1038 * at out_unlock label to free meta of this ordered
1039 * extent, as its meta should be freed by
1040 * btrfs_finish_ordered_io().
1041 *
1042 * So we must continue until @start is increased to
1043 * skip current ordered extent.
1044 */
Josef Bacik00361582013-08-14 14:02:47 -04001045 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001046 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1047 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001048 }
1049
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001050 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001051
Chris Masonc8b97812008-10-29 14:49:59 -04001052 /* we're not doing compressed IO, don't unlock the first
1053 * page (which the caller expects to stay locked), don't
1054 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001055 *
1056 * Do set the Private2 bit so we know this page was properly
1057 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001058 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001059 page_ops = unlock ? PAGE_UNLOCK : 0;
1060 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001061
Josef Bacikc2790a22013-07-29 11:20:47 -04001062 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001063 start + ram_size - 1,
1064 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001065 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001066 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001067 if (num_bytes < cur_alloc_size)
1068 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001069 else
Anand Jain3752d222018-02-15 12:29:38 +08001070 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001071 alloc_hint = ins.objectid + ins.offset;
1072 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001073 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001074
1075 /*
1076 * btrfs_reloc_clone_csums() error, since start is increased
1077 * extent_clear_unlock_delalloc() at out_unlock label won't
1078 * free metadata of current ordered extent, we're OK to exit.
1079 */
1080 if (ret)
1081 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001082 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001083out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001084 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001085
Filipe Mananad9f85962014-08-25 10:43:00 +01001086out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001087 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001088out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001089 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001090 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001091out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001092 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1093 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001094 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1095 PAGE_END_WRITEBACK;
1096 /*
1097 * If we reserved an extent for our delalloc range (or a subrange) and
1098 * failed to create the respective ordered extent, then it means that
1099 * when we reserved the extent we decremented the extent's size from
1100 * the data space_info's bytes_may_use counter and incremented the
1101 * space_info's bytes_reserved counter by the same amount. We must make
1102 * sure extent_clear_unlock_delalloc() does not try to decrement again
1103 * the data space_info's bytes_may_use counter, therefore we do not pass
1104 * it the flag EXTENT_CLEAR_DATA_RESV.
1105 */
1106 if (extent_reserved) {
1107 extent_clear_unlock_delalloc(inode, start,
1108 start + cur_alloc_size,
1109 start + cur_alloc_size,
1110 locked_page,
1111 clear_bits,
1112 page_ops);
1113 start += cur_alloc_size;
1114 if (start >= end)
1115 goto out;
1116 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001117 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1118 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001119 clear_bits | EXTENT_CLEAR_DATA_RESV,
1120 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001121 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001122}
Chris Masonc8b97812008-10-29 14:49:59 -04001123
Chris Mason771ed682008-11-06 22:02:51 -05001124/*
1125 * work queue call back to started compression on a file and pages
1126 */
1127static noinline void async_cow_start(struct btrfs_work *work)
1128{
1129 struct async_cow *async_cow;
1130 int num_added = 0;
1131 async_cow = container_of(work, struct async_cow, work);
1132
1133 compress_file_range(async_cow->inode, async_cow->locked_page,
1134 async_cow->start, async_cow->end, async_cow,
1135 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001137 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001138 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001139 }
Chris Mason771ed682008-11-06 22:02:51 -05001140}
1141
1142/*
1143 * work queue call back to submit previously compressed pages
1144 */
1145static noinline void async_cow_submit(struct btrfs_work *work)
1146{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001147 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001148 struct async_cow *async_cow;
1149 struct btrfs_root *root;
1150 unsigned long nr_pages;
1151
1152 async_cow = container_of(work, struct async_cow, work);
1153
1154 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001155 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001156 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1157 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001158
David Sterba093258e2018-02-26 16:15:17 +01001159 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001160 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001161 5 * SZ_1M)
1162 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001163
Chris Masond3977122009-01-05 21:25:51 -05001164 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001165 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001166}
Chris Masonc8b97812008-10-29 14:49:59 -04001167
Chris Mason771ed682008-11-06 22:02:51 -05001168static noinline void async_cow_free(struct btrfs_work *work)
1169{
1170 struct async_cow *async_cow;
1171 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001172 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001173 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001174 kfree(async_cow);
1175}
1176
1177static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1178 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001179 unsigned long *nr_written,
1180 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001181{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001183 struct async_cow *async_cow;
1184 struct btrfs_root *root = BTRFS_I(inode)->root;
1185 unsigned long nr_pages;
1186 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001187
Chris Masona3429ab2009-10-08 12:30:20 -04001188 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001189 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001190 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001191 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001192 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001193 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001194 async_cow->root = root;
1195 async_cow->locked_page = locked_page;
1196 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001197 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001198
Wang Shilongf79707b2014-07-17 11:44:09 +08001199 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001200 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001201 cur_end = end;
1202 else
Byongho Leeee221842015-12-15 01:42:10 +09001203 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001204
1205 async_cow->end = cur_end;
1206 INIT_LIST_HEAD(&async_cow->extents);
1207
Liu Bo9e0af232014-08-15 23:36:53 +08001208 btrfs_init_work(&async_cow->work,
1209 btrfs_delalloc_helper,
1210 async_cow_start, async_cow_submit,
1211 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001212
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001213 nr_pages = (cur_end - start + PAGE_SIZE) >>
1214 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001218
Chris Mason771ed682008-11-06 22:02:51 -05001219 *nr_written += nr_pages;
1220 start = cur_end + 1;
1221 }
1222 *page_started = 1;
1223 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001224}
1225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001226static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001227 u64 bytenr, u64 num_bytes)
1228{
1229 int ret;
1230 struct btrfs_ordered_sum *sums;
1231 LIST_HEAD(list);
1232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001233 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001234 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001235 if (ret == 0 && list_empty(&list))
1236 return 0;
1237
1238 while (!list_empty(&list)) {
1239 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1240 list_del(&sums->list);
1241 kfree(sums);
1242 }
Liu Bo58113752018-01-31 17:09:13 -07001243 if (ret < 0)
1244 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001245 return 1;
1246}
1247
Chris Masond352ac62008-09-29 15:18:18 -04001248/*
1249 * when nowcow writeback call back. This checks for snapshots or COW copies
1250 * of the extents that exist in the file, and COWs the file as required.
1251 *
1252 * If no cow copies or snapshots exist, we write directly to the existing
1253 * blocks on disk
1254 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001255static noinline int run_delalloc_nocow(struct inode *inode,
1256 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001257 u64 start, u64 end, int *page_started, int force,
1258 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001260 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001261 struct btrfs_root *root = BTRFS_I(inode)->root;
1262 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001263 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001265 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001266 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 u64 cow_start;
1268 u64 cur_offset;
1269 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001270 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 u64 disk_bytenr;
1272 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001273 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001274 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001276 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001277 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 int nocow;
1279 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001280 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001281 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001282
1283 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001284 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001285 extent_clear_unlock_delalloc(inode, start, end, end,
1286 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001287 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001288 EXTENT_DO_ACCOUNTING |
1289 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 PAGE_CLEAR_DIRTY |
1291 PAGE_SET_WRITEBACK |
1292 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001293 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001294 }
Li Zefan82d59022011-04-20 10:33:24 +08001295
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001296 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001297
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 cow_start = (u64)-1;
1299 cur_offset = start;
1300 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001301 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001303 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1306 leaf = path->nodes[0];
1307 btrfs_item_key_to_cpu(leaf, &found_key,
1308 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001309 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 found_key.type == BTRFS_EXTENT_DATA_KEY)
1311 path->slots[0]--;
1312 }
1313 check_prev = 0;
1314next_slot:
1315 leaf = path->nodes[0];
1316 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1317 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001318 if (ret < 0) {
1319 if (cow_start != (u64)-1)
1320 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001322 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 if (ret > 0)
1324 break;
1325 leaf = path->nodes[0];
1326 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 nocow = 0;
1329 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001332
Filipe Manana1d512cb2015-11-09 00:33:58 +00001333 if (found_key.objectid > ino)
1334 break;
1335 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1336 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1337 path->slots[0]++;
1338 goto next_slot;
1339 }
1340 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 found_key.offset > end)
1342 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (found_key.offset > cur_offset) {
1345 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001346 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 goto out_check;
1348 }
Chris Masonc31f8832008-01-08 15:46:31 -05001349
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 fi = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_file_extent_item);
1352 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001353
Josef Bacikcc95bef2013-04-04 14:31:27 -04001354 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001355 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1356 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001358 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 extent_end = found_key.offset +
1360 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001361 disk_num_bytes =
1362 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 if (extent_end <= start) {
1364 path->slots[0]++;
1365 goto next_slot;
1366 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001367 if (disk_bytenr == 0)
1368 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 if (btrfs_file_extent_compression(leaf, fi) ||
1370 btrfs_file_extent_encryption(leaf, fi) ||
1371 btrfs_file_extent_other_encoding(leaf, fi))
1372 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001373 /*
1374 * Do the same check as in btrfs_cross_ref_exist but
1375 * without the unnecessary search.
1376 */
1377 if (btrfs_file_extent_generation(leaf, fi) <=
1378 btrfs_root_last_snapshot(&root->root_item))
1379 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001380 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1381 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001382 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001384 ret = btrfs_cross_ref_exist(root, ino,
1385 found_key.offset -
1386 extent_offset, disk_bytenr);
1387 if (ret) {
1388 /*
1389 * ret could be -EIO if the above fails to read
1390 * metadata.
1391 */
1392 if (ret < 0) {
1393 if (cow_start != (u64)-1)
1394 cur_offset = cow_start;
1395 goto error;
1396 }
1397
1398 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001399 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001400 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 disk_bytenr += cur_offset - found_key.offset;
1403 num_bytes = min(end + 1, extent_end) - cur_offset;
1404 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 * if there are pending snapshots for this root,
1406 * we fall into common COW way.
1407 */
1408 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001409 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001410 if (!err)
1411 goto out_check;
1412 }
1413 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001414 * force cow if csum exists in the range.
1415 * this ensure that csum for a given extent are
1416 * either valid or do not exist.
1417 */
Liu Bo58113752018-01-31 17:09:13 -07001418 ret = csum_exist_in_range(fs_info, disk_bytenr,
1419 num_bytes);
1420 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001421 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001422 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001423
1424 /*
1425 * ret could be -EIO if the above fails to read
1426 * metadata.
1427 */
1428 if (ret < 0) {
1429 if (cow_start != (u64)-1)
1430 cur_offset = cow_start;
1431 goto error;
1432 }
1433 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001434 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001435 }
1436 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1437 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001438 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001439 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001440 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 nocow = 1;
1442 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1443 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001444 btrfs_file_extent_inline_len(leaf,
1445 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001446 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001447 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 } else {
1449 BUG_ON(1);
1450 }
1451out_check:
1452 if (extent_end <= start) {
1453 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001454 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001455 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001456 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001457 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 goto next_slot;
1459 }
1460 if (!nocow) {
1461 if (cow_start == (u64)-1)
1462 cow_start = cur_offset;
1463 cur_offset = extent_end;
1464 if (cur_offset > end)
1465 break;
1466 path->slots[0]++;
1467 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001468 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001469
David Sterbab3b4aa72011-04-21 01:20:15 +02001470 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001472 ret = cow_file_range(inode, locked_page,
1473 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001474 end, page_started, nr_written, 1,
1475 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001476 if (ret) {
1477 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001478 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001479 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001480 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001481 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001482 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001483 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001484 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001485 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001486
Yan Zhengd899e052008-10-30 14:25:28 -04001487 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001488 u64 orig_start = found_key.offset - extent_offset;
1489
1490 em = create_io_em(inode, cur_offset, num_bytes,
1491 orig_start,
1492 disk_bytenr, /* block_start */
1493 num_bytes, /* block_len */
1494 disk_num_bytes, /* orig_block_len */
1495 ram_bytes, BTRFS_COMPRESS_NONE,
1496 BTRFS_ORDERED_PREALLOC);
1497 if (IS_ERR(em)) {
1498 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001499 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001500 if (nocow)
1501 btrfs_dec_nocow_writers(fs_info,
1502 disk_bytenr);
1503 ret = PTR_ERR(em);
1504 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001505 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001506 free_extent_map(em);
1507 }
1508
1509 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001510 type = BTRFS_ORDERED_PREALLOC;
1511 } else {
1512 type = BTRFS_ORDERED_NOCOW;
1513 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001514
1515 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001516 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001517 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001518 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001519 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001520
Yan, Zhengefa56462010-05-16 10:49:59 -04001521 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001522 BTRFS_DATA_RELOC_TREE_OBJECTID)
1523 /*
1524 * Error handled later, as we must prevent
1525 * extent_clear_unlock_delalloc() in error handler
1526 * from freeing metadata of created ordered extent.
1527 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001528 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1529 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001530
Josef Bacikc2790a22013-07-29 11:20:47 -04001531 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001532 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001533 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001534 EXTENT_DELALLOC |
1535 EXTENT_CLEAR_DATA_RESV,
1536 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1537
Wang Shilonge9894fd2014-03-27 11:12:25 +08001538 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001539 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001540 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001541
1542 /*
1543 * btrfs_reloc_clone_csums() error, now we're OK to call error
1544 * handler, as metadata for created ordered extent will only
1545 * be freed by btrfs_finish_ordered_io().
1546 */
1547 if (ret)
1548 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001549 if (cur_offset > end)
1550 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001551 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001552 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001553
Josef Bacik17ca04a2012-05-31 15:58:55 -04001554 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001555 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001556 cur_offset = end;
1557 }
1558
Yan Zheng80ff3852008-10-30 14:20:02 -04001559 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001560 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1561 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001562 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001563 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001564 }
1565
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001566error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001567 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001568 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001569 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001570 EXTENT_DELALLOC | EXTENT_DEFRAG |
1571 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1572 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001573 PAGE_SET_WRITEBACK |
1574 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001575 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001576 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001577}
1578
Wang Shilong47059d92014-07-03 18:22:07 +08001579static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1580{
1581
1582 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1583 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1584 return 0;
1585
1586 /*
1587 * @defrag_bytes is a hint value, no spinlock held here,
1588 * if is not zero, it means the file is defragging.
1589 * Force cow if given extent needs to be defragged.
1590 */
1591 if (BTRFS_I(inode)->defrag_bytes &&
1592 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1593 EXTENT_DEFRAG, 0, NULL))
1594 return 1;
1595
1596 return 0;
1597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
1600 * extent_io.c call back to do delayed allocation processing
1601 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001602static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001603 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001604 unsigned long *nr_written,
1605 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001606{
Josef Bacikc6100a42017-05-05 11:57:13 -04001607 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001608 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001609 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001610 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001611
Wang Shilong47059d92014-07-03 18:22:07 +08001612 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001613 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001614 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001615 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001616 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001617 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001618 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001619 ret = cow_file_range(inode, locked_page, start, end, end,
1620 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001621 } else {
1622 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1623 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001624 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001625 page_started, nr_written,
1626 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001627 }
Qu Wenruo524272602017-03-08 10:25:52 +08001628 if (ret)
1629 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001630 return ret;
1631}
1632
Josef Bacikc6100a42017-05-05 11:57:13 -04001633static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001634 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635{
Josef Bacikc6100a42017-05-05 11:57:13 -04001636 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001637 u64 size;
1638
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001639 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001640 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001641 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001642
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643 size = orig->end - orig->start + 1;
1644 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001645 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001646 u64 new_size;
1647
1648 /*
Josef Bacikba117212015-03-13 15:01:24 -04001649 * See the explanation in btrfs_merge_extent_hook, the same
1650 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 */
1652 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001653 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001654 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001655 num_extents += count_max_extents(new_size);
1656 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001657 return;
1658 }
1659
Josef Bacik9e0baf62011-07-15 15:16:44 +00001660 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001661 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001662 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663}
1664
1665/*
1666 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1667 * extents so we can keep track of new extents that are just merged onto old
1668 * extents, such as when we are doing sequential writes, so we can properly
1669 * account for the metadata space we'll need.
1670 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001671static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001672 struct extent_state *new,
1673 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674{
Josef Bacikc6100a42017-05-05 11:57:13 -04001675 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001677 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678
Josef Bacik9ed74f22009-09-11 16:12:44 -04001679 /* not delalloc, ignore it */
1680 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001681 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001682
Josef Bacik8461a3d2015-03-13 15:12:08 -04001683 if (new->start > other->start)
1684 new_size = new->end - other->start + 1;
1685 else
1686 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001687
1688 /* we're not bigger than the max, unreserve the space and go */
1689 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1690 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001691 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001692 spin_unlock(&BTRFS_I(inode)->lock);
1693 return;
1694 }
1695
1696 /*
Josef Bacikba117212015-03-13 15:01:24 -04001697 * We have to add up either side to figure out how many extents were
1698 * accounted for before we merged into one big extent. If the number of
1699 * extents we accounted for is <= the amount we need for the new range
1700 * then we can return, otherwise drop. Think of it like this
1701 *
1702 * [ 4k][MAX_SIZE]
1703 *
1704 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1705 * need 2 outstanding extents, on one side we have 1 and the other side
1706 * we have 1 so they are == and we can return. But in this case
1707 *
1708 * [MAX_SIZE+4k][MAX_SIZE+4k]
1709 *
1710 * Each range on their own accounts for 2 extents, but merged together
1711 * they are only 3 extents worth of accounting, so we need to drop in
1712 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001713 */
Josef Bacikba117212015-03-13 15:01:24 -04001714 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001715 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001716 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001717 num_extents += count_max_extents(old_size);
1718 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001719 return;
1720
Josef Bacik9e0baf62011-07-15 15:16:44 +00001721 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001722 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001723 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001724}
1725
Miao Xieeb73c1b2013-05-15 07:48:22 +00001726static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1727 struct inode *inode)
1728{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001729 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1730
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 spin_lock(&root->delalloc_lock);
1732 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1733 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1734 &root->delalloc_inodes);
1735 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1736 &BTRFS_I(inode)->runtime_flags);
1737 root->nr_delalloc_inodes++;
1738 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740 BUG_ON(!list_empty(&root->delalloc_root));
1741 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001742 &fs_info->delalloc_roots);
1743 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001744 }
1745 }
1746 spin_unlock(&root->delalloc_lock);
1747}
1748
Nikolay Borisov2b877332018-04-27 12:21:51 +03001749
1750void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1751 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001754
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001755 if (!list_empty(&inode->delalloc_inodes)) {
1756 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001757 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001758 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 root->nr_delalloc_inodes--;
1760 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001761 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001762 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001763 BUG_ON(list_empty(&root->delalloc_root));
1764 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001765 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001766 }
1767 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001768}
1769
1770static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1771 struct btrfs_inode *inode)
1772{
1773 spin_lock(&root->delalloc_lock);
1774 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001775 spin_unlock(&root->delalloc_lock);
1776}
1777
Chris Masond352ac62008-09-29 15:18:18 -04001778/*
1779 * extent_io.c set_bit_hook, used to track delayed allocation
1780 * bytes in this file, and to maintain the list of inodes that
1781 * have pending delalloc work to be done.
1782 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001783static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001784 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001785{
Josef Bacikc6100a42017-05-05 11:57:13 -04001786 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001787
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1789
Wang Shilong47059d92014-07-03 18:22:07 +08001790 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1791 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001792 /*
1793 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001794 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001795 * bit, which is only set or cleared with irqs on
1796 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001797 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001798 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001799 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001800 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001801 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001802
Josef Bacik8b62f872017-10-19 14:15:55 -04001803 spin_lock(&BTRFS_I(inode)->lock);
1804 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1805 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001806
Josef Bacik6a3891c2015-03-16 17:38:52 -04001807 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001808 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001809 return;
1810
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001811 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1812 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001813 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001814 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001815 if (*bits & EXTENT_DEFRAG)
1816 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001817 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001818 &BTRFS_I(inode)->runtime_flags))
1819 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001820 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001821 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001822
1823 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1824 (*bits & EXTENT_DELALLOC_NEW)) {
1825 spin_lock(&BTRFS_I(inode)->lock);
1826 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1827 state->start;
1828 spin_unlock(&BTRFS_I(inode)->lock);
1829 }
Chris Mason291d6732008-01-29 15:55:23 -05001830}
1831
Chris Masond352ac62008-09-29 15:18:18 -04001832/*
1833 * extent_io.c clear_bit_hook, see set_bit_hook for why
1834 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001835static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001836 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001837 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001838{
Josef Bacikc6100a42017-05-05 11:57:13 -04001839 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001841 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001842 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001843
Filipe Manana4a4b9642017-07-27 19:52:55 +01001844 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1845 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001846 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001847 spin_unlock(&inode->lock);
1848 }
Wang Shilong47059d92014-07-03 18:22:07 +08001849
Chris Mason75eff682008-12-15 15:54:40 -05001850 /*
1851 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001852 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001853 * bit, which is only set or cleared with irqs on
1854 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001855 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001856 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001857 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001858
Josef Bacik8b62f872017-10-19 14:15:55 -04001859 spin_lock(&inode->lock);
1860 btrfs_mod_outstanding_extents(inode, -num_extents);
1861 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001862
Josef Bacikb6d08f02013-09-27 14:57:43 -04001863 /*
1864 * We don't reserve metadata space for space cache inodes so we
1865 * don't need to call dellalloc_release_metadata if there is an
1866 * error.
1867 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001868 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001870 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001871
Josef Bacik6a3891c2015-03-16 17:38:52 -04001872 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001874 return;
1875
Filipe Mananaa315e682017-03-06 23:04:20 +00001876 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1877 do_list && !(state->state & EXTENT_NORESERVE) &&
1878 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001879 btrfs_free_reserved_data_space_noquota(
1880 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001881 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001882
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001883 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1884 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001885 spin_lock(&inode->lock);
1886 inode->delalloc_bytes -= len;
1887 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001888 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001889 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001890 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001891 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001892 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001893
1894 if ((state->state & EXTENT_DELALLOC_NEW) &&
1895 (*bits & EXTENT_DELALLOC_NEW)) {
1896 spin_lock(&inode->lock);
1897 ASSERT(inode->new_delalloc_bytes >= len);
1898 inode->new_delalloc_bytes -= len;
1899 spin_unlock(&inode->lock);
1900 }
Chris Mason291d6732008-01-29 15:55:23 -05001901}
1902
Chris Masond352ac62008-09-29 15:18:18 -04001903/*
1904 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1905 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001906 *
1907 * return 1 if page cannot be merged to bio
1908 * return 0 if page can be merged to bio
1909 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001910 */
Mike Christie81a75f672016-06-05 14:31:54 -05001911int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001912 size_t size, struct bio *bio,
1913 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001915 struct inode *inode = page->mapping->host;
1916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001917 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001918 u64 length = 0;
1919 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001920 int ret;
1921
Chris Mason771ed682008-11-06 22:02:51 -05001922 if (bio_flags & EXTENT_BIO_COMPRESSED)
1923 return 0;
1924
Kent Overstreet4f024f32013-10-11 15:44:27 -07001925 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001926 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001927 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1928 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001929 if (ret < 0)
1930 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001931 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001932 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001933 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001934}
1935
Chris Masond352ac62008-09-29 15:18:18 -04001936/*
1937 * in order to insert checksums into the metadata in large chunks,
1938 * we wait until bio submission time. All the pages in the bio are
1939 * checksummed and sums are attached onto the ordered extent record.
1940 *
1941 * At IO completion time the cums attached on the ordered extent record
1942 * are inserted into the btree
1943 */
David Sterbad0ee3932018-03-08 14:35:48 +01001944static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001945 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001946{
Josef Bacikc6100a42017-05-05 11:57:13 -04001947 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001948 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001950 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001952 return 0;
1953}
Chris Masone0156402008-04-16 11:15:20 -04001954
Chris Mason4a69a412008-11-06 22:03:00 -05001955/*
1956 * in order to insert checksums into the metadata in large chunks,
1957 * we wait until bio submission time. All the pages in the bio are
1958 * checksummed and sums are attached onto the ordered extent record.
1959 *
1960 * At IO completion time the cums attached on the ordered extent record
1961 * are inserted into the btree
1962 */
David Sterbad0ee3932018-03-08 14:35:48 +01001963static blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001964 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001965{
Josef Bacikc6100a42017-05-05 11:57:13 -04001966 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001967 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001968 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001969
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001970 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001971 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001972 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001973 bio_endio(bio);
1974 }
Stefan Behrens61891922012-11-05 18:51:52 +01001975 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001976}
1977
Chris Masond352ac62008-09-29 15:18:18 -04001978/*
Chris Masoncad321a2008-12-17 14:51:42 -05001979 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001980 * on write, or reading the csums from the tree before a read.
1981 *
1982 * Rules about async/sync submit,
1983 * a) read: sync submit
1984 *
1985 * b) write without checksum: sync submit
1986 *
1987 * c) write with checksum:
1988 * c-1) if bio is issued by fsync: sync submit
1989 * (sync_writers != 0)
1990 *
1991 * c-2) if root is reloc root: sync submit
1992 * (only in case of buffered IO)
1993 *
1994 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001995 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001996static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001997 int mirror_num, unsigned long bio_flags,
1998 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001999{
Josef Bacikc6100a42017-05-05 11:57:13 -04002000 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002001 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002002 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302003 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002004 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002005 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002006 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002007
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002008 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002009
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002010 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302011 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002012
Mike Christie37226b22016-06-05 14:31:52 -05002013 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002015 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002016 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002017
Chris Masond20f7042008-12-08 16:58:54 -05002018 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002019 ret = btrfs_submit_compressed_read(inode, bio,
2020 mirror_num,
2021 bio_flags);
2022 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002023 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002024 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002025 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002026 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002027 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002028 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002029 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002030 /* csum items have already been cloned */
2031 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2032 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002033 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002034 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2035 bio_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01002036 btrfs_submit_bio_start,
2037 btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002038 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002039 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002040 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002041 if (ret)
2042 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002043 }
2044
Chris Mason0b86a832008-03-24 15:01:56 -04002045mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002046 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002047
2048out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002049 if (ret) {
2050 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002051 bio_endio(bio);
2052 }
Stefan Behrens61891922012-11-05 18:51:52 +01002053 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002054}
Chris Mason6885f302008-02-20 16:11:05 -05002055
Chris Masond352ac62008-09-29 15:18:18 -04002056/*
2057 * given a list of ordered sums record them in the inode. This happens
2058 * at IO completion time based on sums calculated at bio submission time.
2059 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002060static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002061 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002062{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002063 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002064 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002065
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002066 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002067 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002068 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002069 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002070 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002071 if (ret)
2072 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002073 }
2074 return 0;
2075}
2076
Josef Bacik2ac55d42010-02-03 19:33:23 +00002077int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002078 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002079 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002080{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002082 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002083 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002084}
2085
Chris Masond352ac62008-09-29 15:18:18 -04002086/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002087struct btrfs_writepage_fixup {
2088 struct page *page;
2089 struct btrfs_work work;
2090};
2091
Christoph Hellwigb2950862008-12-02 09:54:17 -05002092static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002093{
2094 struct btrfs_writepage_fixup *fixup;
2095 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002096 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002097 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002098 struct page *page;
2099 struct inode *inode;
2100 u64 page_start;
2101 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002102 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002103
2104 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2105 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002106again:
Chris Mason247e7432008-07-17 12:53:51 -04002107 lock_page(page);
2108 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2109 ClearPageChecked(page);
2110 goto out_page;
2111 }
2112
2113 inode = page->mapping->host;
2114 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002115 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002116
David Sterbaff13db42015-12-03 14:30:40 +01002117 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002118 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002119
2120 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002121 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002122 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002123
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002124 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002126 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002127 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002128 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002129 unlock_page(page);
2130 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002131 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002132 goto again;
2133 }
Chris Mason247e7432008-07-17 12:53:51 -04002134
Qu Wenruo364ecf32017-02-27 15:10:38 +08002135 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002137 if (ret) {
2138 mapping_set_error(page->mapping, ret);
2139 end_extent_writepage(page, ret, page_start, page_end);
2140 ClearPageChecked(page);
2141 goto out;
2142 }
2143
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002144 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2145 &cached_state, 0);
2146 if (ret) {
2147 mapping_set_error(page->mapping, ret);
2148 end_extent_writepage(page, ret, page_start, page_end);
2149 ClearPageChecked(page);
2150 goto out;
2151 }
2152
Chris Mason247e7432008-07-17 12:53:51 -04002153 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002154 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002155 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002156out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002157 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002158 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002159out_page:
2160 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002161 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002162 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002163 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002164}
2165
2166/*
2167 * There are a few paths in the higher layers of the kernel that directly
2168 * set the page dirty bit without asking the filesystem if it is a
2169 * good idea. This causes problems because we want to make sure COW
2170 * properly happens and the data=ordered rules are followed.
2171 *
Chris Masonc8b97812008-10-29 14:49:59 -04002172 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002173 * hasn't been properly setup for IO. We kick off an async process
2174 * to fix it up. The async helper will wait for ordered extents, set
2175 * the delalloc bit and make it safe to write the page.
2176 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002177static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002178{
2179 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002181 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002182
Chris Mason8b62b722009-09-02 16:53:46 -04002183 /* this page is properly in the ordered list */
2184 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002185 return 0;
2186
2187 if (PageChecked(page))
2188 return -EAGAIN;
2189
2190 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2191 if (!fixup)
2192 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002193
Chris Mason247e7432008-07-17 12:53:51 -04002194 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002196 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2197 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002198 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002199 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002200 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002201}
2202
Yan Zhengd899e052008-10-30 14:25:28 -04002203static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2204 struct inode *inode, u64 file_pos,
2205 u64 disk_bytenr, u64 disk_num_bytes,
2206 u64 num_bytes, u64 ram_bytes,
2207 u8 compression, u8 encryption,
2208 u16 other_encoding, int extent_type)
2209{
2210 struct btrfs_root *root = BTRFS_I(inode)->root;
2211 struct btrfs_file_extent_item *fi;
2212 struct btrfs_path *path;
2213 struct extent_buffer *leaf;
2214 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002215 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002216 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002217 int ret;
2218
2219 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002220 if (!path)
2221 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002222
Chris Masona1ed8352009-09-11 12:27:37 -04002223 /*
2224 * we may be replacing one extent in the tree with another.
2225 * The new extent is pinned in the extent map, and we don't want
2226 * to drop it from the cache until it is completely in the btree.
2227 *
2228 * So, tell btrfs_drop_extents to leave this extent in the cache.
2229 * the caller is expected to unpin it and allow it to be merged
2230 * with the others.
2231 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002232 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2233 file_pos + num_bytes, NULL, 0,
2234 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002235 if (ret)
2236 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002237
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002238 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002239 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002240 ins.offset = file_pos;
2241 ins.type = BTRFS_EXTENT_DATA_KEY;
2242
2243 path->leave_spinning = 1;
2244 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2245 sizeof(*fi));
2246 if (ret)
2247 goto out;
2248 }
Yan Zhengd899e052008-10-30 14:25:28 -04002249 leaf = path->nodes[0];
2250 fi = btrfs_item_ptr(leaf, path->slots[0],
2251 struct btrfs_file_extent_item);
2252 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2253 btrfs_set_file_extent_type(leaf, fi, extent_type);
2254 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2255 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2256 btrfs_set_file_extent_offset(leaf, fi, 0);
2257 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2258 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2259 btrfs_set_file_extent_compression(leaf, fi, compression);
2260 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2261 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002262
Yan Zhengd899e052008-10-30 14:25:28 -04002263 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002264 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002265
2266 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002267
2268 ins.objectid = disk_bytenr;
2269 ins.offset = disk_num_bytes;
2270 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002271
Qu Wenruo297d7502015-09-08 17:08:37 +08002272 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002273 * Release the reserved range from inode dirty range map, as it is
2274 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002275 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002276 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2277 if (ret < 0)
2278 goto out;
2279 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002280 ret = btrfs_alloc_reserved_file_extent(trans, root,
2281 btrfs_ino(BTRFS_I(inode)),
2282 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283out:
Yan Zhengd899e052008-10-30 14:25:28 -04002284 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002285
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002286 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002287}
2288
Liu Bo38c227d2013-01-29 03:18:40 +00002289/* snapshot-aware defrag */
2290struct sa_defrag_extent_backref {
2291 struct rb_node node;
2292 struct old_sa_defrag_extent *old;
2293 u64 root_id;
2294 u64 inum;
2295 u64 file_pos;
2296 u64 extent_offset;
2297 u64 num_bytes;
2298 u64 generation;
2299};
2300
2301struct old_sa_defrag_extent {
2302 struct list_head list;
2303 struct new_sa_defrag_extent *new;
2304
2305 u64 extent_offset;
2306 u64 bytenr;
2307 u64 offset;
2308 u64 len;
2309 int count;
2310};
2311
2312struct new_sa_defrag_extent {
2313 struct rb_root root;
2314 struct list_head head;
2315 struct btrfs_path *path;
2316 struct inode *inode;
2317 u64 file_pos;
2318 u64 len;
2319 u64 bytenr;
2320 u64 disk_len;
2321 u8 compress_type;
2322};
2323
2324static int backref_comp(struct sa_defrag_extent_backref *b1,
2325 struct sa_defrag_extent_backref *b2)
2326{
2327 if (b1->root_id < b2->root_id)
2328 return -1;
2329 else if (b1->root_id > b2->root_id)
2330 return 1;
2331
2332 if (b1->inum < b2->inum)
2333 return -1;
2334 else if (b1->inum > b2->inum)
2335 return 1;
2336
2337 if (b1->file_pos < b2->file_pos)
2338 return -1;
2339 else if (b1->file_pos > b2->file_pos)
2340 return 1;
2341
2342 /*
2343 * [------------------------------] ===> (a range of space)
2344 * |<--->| |<---->| =============> (fs/file tree A)
2345 * |<---------------------------->| ===> (fs/file tree B)
2346 *
2347 * A range of space can refer to two file extents in one tree while
2348 * refer to only one file extent in another tree.
2349 *
2350 * So we may process a disk offset more than one time(two extents in A)
2351 * and locate at the same extent(one extent in B), then insert two same
2352 * backrefs(both refer to the extent in B).
2353 */
2354 return 0;
2355}
2356
2357static void backref_insert(struct rb_root *root,
2358 struct sa_defrag_extent_backref *backref)
2359{
2360 struct rb_node **p = &root->rb_node;
2361 struct rb_node *parent = NULL;
2362 struct sa_defrag_extent_backref *entry;
2363 int ret;
2364
2365 while (*p) {
2366 parent = *p;
2367 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2368
2369 ret = backref_comp(backref, entry);
2370 if (ret < 0)
2371 p = &(*p)->rb_left;
2372 else
2373 p = &(*p)->rb_right;
2374 }
2375
2376 rb_link_node(&backref->node, parent, p);
2377 rb_insert_color(&backref->node, root);
2378}
2379
2380/*
2381 * Note the backref might has changed, and in this case we just return 0.
2382 */
2383static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2384 void *ctx)
2385{
2386 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002387 struct old_sa_defrag_extent *old = ctx;
2388 struct new_sa_defrag_extent *new = old->new;
2389 struct btrfs_path *path = new->path;
2390 struct btrfs_key key;
2391 struct btrfs_root *root;
2392 struct sa_defrag_extent_backref *backref;
2393 struct extent_buffer *leaf;
2394 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002395 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002396 int slot;
2397 int ret;
2398 u64 extent_offset;
2399 u64 num_bytes;
2400
2401 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002402 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002403 return 0;
2404
2405 key.objectid = root_id;
2406 key.type = BTRFS_ROOT_ITEM_KEY;
2407 key.offset = (u64)-1;
2408
Liu Bo38c227d2013-01-29 03:18:40 +00002409 root = btrfs_read_fs_root_no_name(fs_info, &key);
2410 if (IS_ERR(root)) {
2411 if (PTR_ERR(root) == -ENOENT)
2412 return 0;
2413 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002414 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002415 inum, offset, root_id);
2416 return PTR_ERR(root);
2417 }
2418
2419 key.objectid = inum;
2420 key.type = BTRFS_EXTENT_DATA_KEY;
2421 if (offset > (u64)-1 << 32)
2422 key.offset = 0;
2423 else
2424 key.offset = offset;
2425
2426 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302427 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002428 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002429 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002430
2431 while (1) {
2432 cond_resched();
2433
2434 leaf = path->nodes[0];
2435 slot = path->slots[0];
2436
2437 if (slot >= btrfs_header_nritems(leaf)) {
2438 ret = btrfs_next_leaf(root, path);
2439 if (ret < 0) {
2440 goto out;
2441 } else if (ret > 0) {
2442 ret = 0;
2443 goto out;
2444 }
2445 continue;
2446 }
2447
2448 path->slots[0]++;
2449
2450 btrfs_item_key_to_cpu(leaf, &key, slot);
2451
2452 if (key.objectid > inum)
2453 goto out;
2454
2455 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2456 continue;
2457
2458 extent = btrfs_item_ptr(leaf, slot,
2459 struct btrfs_file_extent_item);
2460
2461 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2462 continue;
2463
Liu Boe68afa42013-07-01 22:13:26 +08002464 /*
2465 * 'offset' refers to the exact key.offset,
2466 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2467 * (key.offset - extent_offset).
2468 */
2469 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002470 continue;
2471
Liu Boe68afa42013-07-01 22:13:26 +08002472 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002473 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002474
Liu Bo38c227d2013-01-29 03:18:40 +00002475 if (extent_offset >= old->extent_offset + old->offset +
2476 old->len || extent_offset + num_bytes <=
2477 old->extent_offset + old->offset)
2478 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002479 break;
2480 }
2481
2482 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2483 if (!backref) {
2484 ret = -ENOENT;
2485 goto out;
2486 }
2487
2488 backref->root_id = root_id;
2489 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002490 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002491 backref->num_bytes = num_bytes;
2492 backref->extent_offset = extent_offset;
2493 backref->generation = btrfs_file_extent_generation(leaf, extent);
2494 backref->old = old;
2495 backref_insert(&new->root, backref);
2496 old->count++;
2497out:
2498 btrfs_release_path(path);
2499 WARN_ON(ret);
2500 return ret;
2501}
2502
2503static noinline bool record_extent_backrefs(struct btrfs_path *path,
2504 struct new_sa_defrag_extent *new)
2505{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002507 struct old_sa_defrag_extent *old, *tmp;
2508 int ret;
2509
2510 new->path = path;
2511
2512 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002513 ret = iterate_inodes_from_logical(old->bytenr +
2514 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002515 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002516 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002517 if (ret < 0 && ret != -ENOENT)
2518 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002519
2520 /* no backref to be processed for this extent */
2521 if (!old->count) {
2522 list_del(&old->list);
2523 kfree(old);
2524 }
2525 }
2526
2527 if (list_empty(&new->head))
2528 return false;
2529
2530 return true;
2531}
2532
2533static int relink_is_mergable(struct extent_buffer *leaf,
2534 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002535 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002536{
Liu Bo116e0022013-08-02 16:30:40 +08002537 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002538 return 0;
2539
2540 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2541 return 0;
2542
Liu Bo116e0022013-08-02 16:30:40 +08002543 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2544 return 0;
2545
2546 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002547 btrfs_file_extent_other_encoding(leaf, fi))
2548 return 0;
2549
2550 return 1;
2551}
2552
2553/*
2554 * Note the backref might has changed, and in this case we just return 0.
2555 */
2556static noinline int relink_extent_backref(struct btrfs_path *path,
2557 struct sa_defrag_extent_backref *prev,
2558 struct sa_defrag_extent_backref *backref)
2559{
2560 struct btrfs_file_extent_item *extent;
2561 struct btrfs_file_extent_item *item;
2562 struct btrfs_ordered_extent *ordered;
2563 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002564 struct btrfs_root *root;
2565 struct btrfs_key key;
2566 struct extent_buffer *leaf;
2567 struct old_sa_defrag_extent *old = backref->old;
2568 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002569 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002570 struct inode *inode;
2571 struct extent_state *cached = NULL;
2572 int ret = 0;
2573 u64 start;
2574 u64 len;
2575 u64 lock_start;
2576 u64 lock_end;
2577 bool merge = false;
2578 int index;
2579
2580 if (prev && prev->root_id == backref->root_id &&
2581 prev->inum == backref->inum &&
2582 prev->file_pos + prev->num_bytes == backref->file_pos)
2583 merge = true;
2584
2585 /* step 1: get root */
2586 key.objectid = backref->root_id;
2587 key.type = BTRFS_ROOT_ITEM_KEY;
2588 key.offset = (u64)-1;
2589
Liu Bo38c227d2013-01-29 03:18:40 +00002590 index = srcu_read_lock(&fs_info->subvol_srcu);
2591
2592 root = btrfs_read_fs_root_no_name(fs_info, &key);
2593 if (IS_ERR(root)) {
2594 srcu_read_unlock(&fs_info->subvol_srcu, index);
2595 if (PTR_ERR(root) == -ENOENT)
2596 return 0;
2597 return PTR_ERR(root);
2598 }
Liu Bo38c227d2013-01-29 03:18:40 +00002599
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002600 if (btrfs_root_readonly(root)) {
2601 srcu_read_unlock(&fs_info->subvol_srcu, index);
2602 return 0;
2603 }
2604
Liu Bo38c227d2013-01-29 03:18:40 +00002605 /* step 2: get inode */
2606 key.objectid = backref->inum;
2607 key.type = BTRFS_INODE_ITEM_KEY;
2608 key.offset = 0;
2609
2610 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2611 if (IS_ERR(inode)) {
2612 srcu_read_unlock(&fs_info->subvol_srcu, index);
2613 return 0;
2614 }
2615
2616 srcu_read_unlock(&fs_info->subvol_srcu, index);
2617
2618 /* step 3: relink backref */
2619 lock_start = backref->file_pos;
2620 lock_end = backref->file_pos + backref->num_bytes - 1;
2621 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002622 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002623
2624 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2625 if (ordered) {
2626 btrfs_put_ordered_extent(ordered);
2627 goto out_unlock;
2628 }
2629
2630 trans = btrfs_join_transaction(root);
2631 if (IS_ERR(trans)) {
2632 ret = PTR_ERR(trans);
2633 goto out_unlock;
2634 }
2635
2636 key.objectid = backref->inum;
2637 key.type = BTRFS_EXTENT_DATA_KEY;
2638 key.offset = backref->file_pos;
2639
2640 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2641 if (ret < 0) {
2642 goto out_free_path;
2643 } else if (ret > 0) {
2644 ret = 0;
2645 goto out_free_path;
2646 }
2647
2648 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2649 struct btrfs_file_extent_item);
2650
2651 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2652 backref->generation)
2653 goto out_free_path;
2654
2655 btrfs_release_path(path);
2656
2657 start = backref->file_pos;
2658 if (backref->extent_offset < old->extent_offset + old->offset)
2659 start += old->extent_offset + old->offset -
2660 backref->extent_offset;
2661
2662 len = min(backref->extent_offset + backref->num_bytes,
2663 old->extent_offset + old->offset + old->len);
2664 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2665
2666 ret = btrfs_drop_extents(trans, root, inode, start,
2667 start + len, 1);
2668 if (ret)
2669 goto out_free_path;
2670again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002671 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002672 key.type = BTRFS_EXTENT_DATA_KEY;
2673 key.offset = start;
2674
Liu Boa09a0a72013-03-11 09:20:58 +00002675 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002676 if (merge) {
2677 struct btrfs_file_extent_item *fi;
2678 u64 extent_len;
2679 struct btrfs_key found_key;
2680
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002681 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002682 if (ret < 0)
2683 goto out_free_path;
2684
2685 path->slots[0]--;
2686 leaf = path->nodes[0];
2687 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2688
2689 fi = btrfs_item_ptr(leaf, path->slots[0],
2690 struct btrfs_file_extent_item);
2691 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2692
Liu Bo116e0022013-08-02 16:30:40 +08002693 if (extent_len + found_key.offset == start &&
2694 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002695 btrfs_set_file_extent_num_bytes(leaf, fi,
2696 extent_len + len);
2697 btrfs_mark_buffer_dirty(leaf);
2698 inode_add_bytes(inode, len);
2699
2700 ret = 1;
2701 goto out_free_path;
2702 } else {
2703 merge = false;
2704 btrfs_release_path(path);
2705 goto again;
2706 }
2707 }
2708
2709 ret = btrfs_insert_empty_item(trans, root, path, &key,
2710 sizeof(*extent));
2711 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002712 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002713 goto out_free_path;
2714 }
2715
2716 leaf = path->nodes[0];
2717 item = btrfs_item_ptr(leaf, path->slots[0],
2718 struct btrfs_file_extent_item);
2719 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2720 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2721 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2722 btrfs_set_file_extent_num_bytes(leaf, item, len);
2723 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2724 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2725 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2726 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2727 btrfs_set_file_extent_encryption(leaf, item, 0);
2728 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2729
2730 btrfs_mark_buffer_dirty(leaf);
2731 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002732 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002733
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002734 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002735 new->disk_len, 0,
2736 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002737 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002738 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002739 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002740 goto out_free_path;
2741 }
2742
2743 ret = 1;
2744out_free_path:
2745 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002746 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002747 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002748out_unlock:
2749 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002750 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002751 iput(inode);
2752 return ret;
2753}
2754
Liu Bo6f519562013-10-29 10:45:05 +08002755static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2756{
2757 struct old_sa_defrag_extent *old, *tmp;
2758
2759 if (!new)
2760 return;
2761
2762 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002763 kfree(old);
2764 }
2765 kfree(new);
2766}
2767
Liu Bo38c227d2013-01-29 03:18:40 +00002768static void relink_file_extents(struct new_sa_defrag_extent *new)
2769{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002770 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002771 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002772 struct sa_defrag_extent_backref *backref;
2773 struct sa_defrag_extent_backref *prev = NULL;
2774 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002775 struct rb_node *node;
2776 int ret;
2777
2778 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002779
2780 path = btrfs_alloc_path();
2781 if (!path)
2782 return;
2783
2784 if (!record_extent_backrefs(path, new)) {
2785 btrfs_free_path(path);
2786 goto out;
2787 }
2788 btrfs_release_path(path);
2789
2790 while (1) {
2791 node = rb_first(&new->root);
2792 if (!node)
2793 break;
2794 rb_erase(node, &new->root);
2795
2796 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2797
2798 ret = relink_extent_backref(path, prev, backref);
2799 WARN_ON(ret < 0);
2800
2801 kfree(prev);
2802
2803 if (ret == 1)
2804 prev = backref;
2805 else
2806 prev = NULL;
2807 cond_resched();
2808 }
2809 kfree(prev);
2810
2811 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002812out:
Liu Bo6f519562013-10-29 10:45:05 +08002813 free_sa_defrag_extent(new);
2814
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002815 atomic_dec(&fs_info->defrag_running);
2816 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002817}
2818
2819static struct new_sa_defrag_extent *
2820record_old_file_extents(struct inode *inode,
2821 struct btrfs_ordered_extent *ordered)
2822{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002823 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002824 struct btrfs_root *root = BTRFS_I(inode)->root;
2825 struct btrfs_path *path;
2826 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002827 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002828 struct new_sa_defrag_extent *new;
2829 int ret;
2830
2831 new = kmalloc(sizeof(*new), GFP_NOFS);
2832 if (!new)
2833 return NULL;
2834
2835 new->inode = inode;
2836 new->file_pos = ordered->file_offset;
2837 new->len = ordered->len;
2838 new->bytenr = ordered->start;
2839 new->disk_len = ordered->disk_len;
2840 new->compress_type = ordered->compress_type;
2841 new->root = RB_ROOT;
2842 INIT_LIST_HEAD(&new->head);
2843
2844 path = btrfs_alloc_path();
2845 if (!path)
2846 goto out_kfree;
2847
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002848 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002849 key.type = BTRFS_EXTENT_DATA_KEY;
2850 key.offset = new->file_pos;
2851
2852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2853 if (ret < 0)
2854 goto out_free_path;
2855 if (ret > 0 && path->slots[0] > 0)
2856 path->slots[0]--;
2857
2858 /* find out all the old extents for the file range */
2859 while (1) {
2860 struct btrfs_file_extent_item *extent;
2861 struct extent_buffer *l;
2862 int slot;
2863 u64 num_bytes;
2864 u64 offset;
2865 u64 end;
2866 u64 disk_bytenr;
2867 u64 extent_offset;
2868
2869 l = path->nodes[0];
2870 slot = path->slots[0];
2871
2872 if (slot >= btrfs_header_nritems(l)) {
2873 ret = btrfs_next_leaf(root, path);
2874 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002875 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002876 else if (ret > 0)
2877 break;
2878 continue;
2879 }
2880
2881 btrfs_item_key_to_cpu(l, &key, slot);
2882
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002883 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002884 break;
2885 if (key.type != BTRFS_EXTENT_DATA_KEY)
2886 break;
2887 if (key.offset >= new->file_pos + new->len)
2888 break;
2889
2890 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2891
2892 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2893 if (key.offset + num_bytes < new->file_pos)
2894 goto next;
2895
2896 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2897 if (!disk_bytenr)
2898 goto next;
2899
2900 extent_offset = btrfs_file_extent_offset(l, extent);
2901
2902 old = kmalloc(sizeof(*old), GFP_NOFS);
2903 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002904 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002905
2906 offset = max(new->file_pos, key.offset);
2907 end = min(new->file_pos + new->len, key.offset + num_bytes);
2908
2909 old->bytenr = disk_bytenr;
2910 old->extent_offset = extent_offset;
2911 old->offset = offset - key.offset;
2912 old->len = end - offset;
2913 old->new = new;
2914 old->count = 0;
2915 list_add_tail(&old->list, &new->head);
2916next:
2917 path->slots[0]++;
2918 cond_resched();
2919 }
2920
2921 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002923
2924 return new;
2925
Liu Bo38c227d2013-01-29 03:18:40 +00002926out_free_path:
2927 btrfs_free_path(path);
2928out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002929 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002930 return NULL;
2931}
2932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002933static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002934 u64 start, u64 len)
2935{
2936 struct btrfs_block_group_cache *cache;
2937
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002939 ASSERT(cache);
2940
2941 spin_lock(&cache->lock);
2942 cache->delalloc_bytes -= len;
2943 spin_unlock(&cache->lock);
2944
2945 btrfs_put_block_group(cache);
2946}
2947
Chris Masond352ac62008-09-29 15:18:18 -04002948/* as ordered data IO finishes, this gets called so we can finish
2949 * an ordered extent if the range of bytes in the file it covers are
2950 * fully written.
2951 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002952static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953{
Josef Bacik5fd02042012-05-02 14:00:54 -04002954 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002956 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002957 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002958 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002959 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002960 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002961 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002962 int ret = 0;
2963 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002964 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002965 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002966 bool range_locked = false;
2967 bool clear_new_delalloc_bytes = false;
2968
2969 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2970 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2971 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2972 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002974 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975
Josef Bacik5fd02042012-05-02 14:00:54 -04002976 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2977 ret = -EIO;
2978 goto out;
2979 }
2980
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002981 btrfs_free_io_failure_record(BTRFS_I(inode),
2982 ordered_extent->file_offset,
2983 ordered_extent->file_offset +
2984 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002985
Josef Bacik77cef2e2013-08-29 13:57:21 -04002986 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2987 truncated = true;
2988 logical_len = ordered_extent->truncated_len;
2989 /* Truncated the entire extent, don't bother adding */
2990 if (!logical_len)
2991 goto out;
2992 }
2993
Yan, Zhengc2167752009-11-12 09:34:21 +00002994 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002995 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002996
2997 /*
2998 * For mwrite(mmap + memset to write) case, we still reserve
2999 * space for NOCOW range.
3000 * As NOCOW won't cause a new delayed ref, just free the space
3001 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003002 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003003 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003004 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3005 if (nolock)
3006 trans = btrfs_join_transaction_nolock(root);
3007 else
3008 trans = btrfs_join_transaction(root);
3009 if (IS_ERR(trans)) {
3010 ret = PTR_ERR(trans);
3011 trans = NULL;
3012 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003013 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003014 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003015 ret = btrfs_update_inode_fallback(trans, root, inode);
3016 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003017 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003018 goto out;
3019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003020
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003021 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003022 lock_extent_bits(io_tree, ordered_extent->file_offset,
3023 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003024 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003025
Liu Bo38c227d2013-01-29 03:18:40 +00003026 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3027 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003028 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003029 if (ret) {
3030 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003031 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003032 /* the inode is shared */
3033 new = record_old_file_extents(inode, ordered_extent);
3034
3035 clear_extent_bit(io_tree, ordered_extent->file_offset,
3036 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003037 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003038 }
3039
Josef Bacik0cb59c92010-07-02 12:14:14 -04003040 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003041 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003042 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003043 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003044 if (IS_ERR(trans)) {
3045 ret = PTR_ERR(trans);
3046 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003047 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003048 }
Chris Masona79b7d42014-05-22 16:18:52 -07003049
Josef Bacik69fe2d72017-10-19 14:15:57 -04003050 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003051
Chris Masonc8b97812008-10-29 14:49:59 -04003052 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003053 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003054 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003055 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003056 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3057 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003058 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003059 ordered_extent->file_offset,
3060 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003061 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003062 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003063 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003064 ret = insert_reserved_file_extent(trans, inode,
3065 ordered_extent->file_offset,
3066 ordered_extent->start,
3067 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003068 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003069 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003070 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003071 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003072 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003073 ordered_extent->start,
3074 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003075 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003076 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3077 ordered_extent->file_offset, ordered_extent->len,
3078 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003079 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003080 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003081 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003083
Nikolay Borisovac01f262018-01-08 10:59:43 +02003084 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3085 if (ret) {
3086 btrfs_abort_transaction(trans, ret);
3087 goto out;
3088 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089
Josef Bacik6c760c02012-11-09 10:53:21 -05003090 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3091 ret = btrfs_update_inode_fallback(trans, root, inode);
3092 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003093 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003094 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003095 }
3096 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003097out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003098 if (range_locked || clear_new_delalloc_bytes) {
3099 unsigned int clear_bits = 0;
3100
3101 if (range_locked)
3102 clear_bits |= EXTENT_LOCKED;
3103 if (clear_new_delalloc_bytes)
3104 clear_bits |= EXTENT_DELALLOC_NEW;
3105 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3106 ordered_extent->file_offset,
3107 ordered_extent->file_offset +
3108 ordered_extent->len - 1,
3109 clear_bits,
3110 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003111 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003112 }
3113
Miao Xiea698d0752012-09-20 01:51:59 -06003114 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003115 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003116
Josef Bacik77cef2e2013-08-29 13:57:21 -04003117 if (ret || truncated) {
3118 u64 start, end;
3119
3120 if (truncated)
3121 start = ordered_extent->file_offset + logical_len;
3122 else
3123 start = ordered_extent->file_offset;
3124 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003125 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003126
3127 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003128 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003129
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003130 /*
3131 * If the ordered extent had an IOERR or something else went
3132 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003133 * back to the allocator. We only free the extent in the
3134 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003135 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003136 if ((ret || !logical_len) &&
3137 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003138 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003139 btrfs_free_reserved_extent(fs_info,
3140 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003141 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003142 }
3143
3144
Josef Bacik5fd02042012-05-02 14:00:54 -04003145 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003146 * This needs to be done to make sure anybody waiting knows we are done
3147 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003148 */
3149 btrfs_remove_ordered_extent(inode, ordered_extent);
3150
Liu Bo38c227d2013-01-29 03:18:40 +00003151 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003152 if (new) {
3153 if (ret) {
3154 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003155 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003156 } else {
3157 relink_file_extents(new);
3158 }
3159 }
Liu Bo38c227d2013-01-29 03:18:40 +00003160
Chris Masone6dcd2d2008-07-17 12:53:50 -04003161 /* once for us */
3162 btrfs_put_ordered_extent(ordered_extent);
3163 /* once for the tree */
3164 btrfs_put_ordered_extent(ordered_extent);
3165
Ethan Liene73e81b2018-05-28 13:48:20 +08003166 /* Try to release some metadata so we don't get an OOM but don't wait */
3167 btrfs_btree_balance_dirty_nodelay(fs_info);
3168
Josef Bacik5fd02042012-05-02 14:00:54 -04003169 return ret;
3170}
3171
3172static void finish_ordered_fn(struct btrfs_work *work)
3173{
3174 struct btrfs_ordered_extent *ordered_extent;
3175 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3176 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003177}
3178
David Sterbac3988d62017-02-17 15:18:32 +01003179static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003180 struct extent_state *state, int uptodate)
3181{
Josef Bacik5fd02042012-05-02 14:00:54 -04003182 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003183 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003184 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003185 struct btrfs_workqueue *wq;
3186 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003187
liubo1abe9b82011-03-24 11:18:59 +00003188 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3189
Chris Mason8b62b722009-09-02 16:53:46 -04003190 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003191 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3192 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003193 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003194
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003195 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003196 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003197 func = btrfs_freespace_write_helper;
3198 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003199 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003200 func = btrfs_endio_write_helper;
3201 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003202
Liu Bo9e0af232014-08-15 23:36:53 +08003203 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3204 NULL);
3205 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003206}
3207
Miao Xiedc380ae2014-09-12 18:43:55 +08003208static int __readpage_endio_check(struct inode *inode,
3209 struct btrfs_io_bio *io_bio,
3210 int icsum, struct page *page,
3211 int pgoff, u64 start, size_t len)
3212{
3213 char *kaddr;
3214 u32 csum_expected;
3215 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003216
3217 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3218
3219 kaddr = kmap_atomic(page);
3220 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003221 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003222 if (csum != csum_expected)
3223 goto zeroit;
3224
3225 kunmap_atomic(kaddr);
3226 return 0;
3227zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003228 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003229 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003230 memset(kaddr + pgoff, 1, len);
3231 flush_dcache_page(page);
3232 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003233 return -EIO;
3234}
3235
Chris Masond352ac62008-09-29 15:18:18 -04003236/*
Chris Masond352ac62008-09-29 15:18:18 -04003237 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003238 * if there's a match, we allow the bio to finish. If not, the code in
3239 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003240 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003241static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3242 u64 phy_offset, struct page *page,
3243 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003244{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003245 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003246 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003247 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003248 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003249
Chris Masond20f7042008-12-08 16:58:54 -05003250 if (PageChecked(page)) {
3251 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003252 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003253 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003254
3255 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003256 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003257
Yan Zheng17d217f2008-12-12 10:03:38 -05003258 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003259 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003260 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003261 return 0;
3262 }
3263
Miao Xiefacc8a222013-07-25 19:22:34 +08003264 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003265 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3266 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003267}
Chris Masonb888db22007-08-27 16:49:44 -04003268
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003269/*
3270 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3271 *
3272 * @inode: The inode we want to perform iput on
3273 *
3274 * This function uses the generic vfs_inode::i_count to track whether we should
3275 * just decrement it (in case it's > 1) or if this is the last iput then link
3276 * the inode to the delayed iput machinery. Delayed iputs are processed at
3277 * transaction commit time/superblock commit/cleaner kthread.
3278 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003279void btrfs_add_delayed_iput(struct inode *inode)
3280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003282 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283
3284 if (atomic_add_unless(&inode->i_count, -1, 1))
3285 return;
3286
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003287 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003288 ASSERT(list_empty(&binode->delayed_iput));
3289 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003290 spin_unlock(&fs_info->delayed_iput_lock);
3291}
3292
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003293void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003294{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003295
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003296 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003297 while (!list_empty(&fs_info->delayed_iputs)) {
3298 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003299
David Sterba8089fe62015-11-19 14:15:51 +01003300 inode = list_first_entry(&fs_info->delayed_iputs,
3301 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003302 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003303 spin_unlock(&fs_info->delayed_iput_lock);
3304 iput(&inode->vfs_inode);
3305 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003306 }
David Sterba8089fe62015-11-19 14:15:51 +01003307 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003308}
3309
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003311 * This creates an orphan entry for the given inode in case something goes wrong
3312 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003313 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003314int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003315 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003316{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003318
Omar Sandoval27919062018-05-11 13:13:37 -07003319 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3320 if (ret && ret != -EEXIST) {
3321 btrfs_abort_transaction(trans, ret);
3322 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003323 }
3324
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003326}
3327
3328/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003329 * We have done the delete so we can go ahead and remove the orphan item for
3330 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003331 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003332static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003333 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003334{
Omar Sandoval27919062018-05-11 13:13:37 -07003335 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003336}
3337
3338/*
3339 * this cleans up any orphans that may be left on the list from the last use
3340 * of this root.
3341 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003342int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003344 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003345 struct btrfs_path *path;
3346 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003347 struct btrfs_key key, found_key;
3348 struct btrfs_trans_handle *trans;
3349 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003350 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003351 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003352
Yan, Zhengd68fc572010-05-16 10:49:58 -04003353 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003354 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003355
3356 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003357 if (!path) {
3358 ret = -ENOMEM;
3359 goto out;
3360 }
David Sterbae4058b52015-11-27 16:31:35 +01003361 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003362
3363 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003364 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003365 key.offset = (u64)-1;
3366
Josef Bacik7b128762008-07-24 12:17:14 -04003367 while (1) {
3368 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003369 if (ret < 0)
3370 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003371
3372 /*
3373 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003374 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003375 * find the key and see if we have stuff that matches
3376 */
3377 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003378 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003379 if (path->slots[0] == 0)
3380 break;
3381 path->slots[0]--;
3382 }
3383
3384 /* pull out the item */
3385 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003386 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3387
3388 /* make sure the item matches what we want */
3389 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3390 break;
David Sterba962a2982014-06-04 18:41:45 +02003391 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003392 break;
3393
3394 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003395 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003396
3397 /*
3398 * this is where we are basically btrfs_lookup, without the
3399 * crossing root thing. we store the inode number in the
3400 * offset of the orphan item.
3401 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003402
3403 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003404 btrfs_err(fs_info,
3405 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003406 ret = -EINVAL;
3407 goto out;
3408 }
3409
3410 last_objectid = found_key.offset;
3411
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003412 found_key.objectid = found_key.offset;
3413 found_key.type = BTRFS_INODE_ITEM_KEY;
3414 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003415 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303416 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003417 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003418 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003419
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003420 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003421 struct btrfs_root *dead_root;
3422 struct btrfs_fs_info *fs_info = root->fs_info;
3423 int is_dead_root = 0;
3424
3425 /*
3426 * this is an orphan in the tree root. Currently these
3427 * could come from 2 sources:
3428 * a) a snapshot deletion in progress
3429 * b) a free space cache inode
3430 * We need to distinguish those two, as the snapshot
3431 * orphan must not get deleted.
3432 * find_dead_roots already ran before us, so if this
3433 * is a snapshot deletion, we should find the root
3434 * in the dead_roots list
3435 */
3436 spin_lock(&fs_info->trans_lock);
3437 list_for_each_entry(dead_root, &fs_info->dead_roots,
3438 root_list) {
3439 if (dead_root->root_key.objectid ==
3440 found_key.objectid) {
3441 is_dead_root = 1;
3442 break;
3443 }
3444 }
3445 spin_unlock(&fs_info->trans_lock);
3446 if (is_dead_root) {
3447 /* prevent this orphan from being found again */
3448 key.offset = found_key.objectid - 1;
3449 continue;
3450 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003451
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003452 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003453
Josef Bacika8c9e572011-09-21 16:55:59 -04003454 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003455 * If we have an inode with links, there are a couple of
3456 * possibilities. Old kernels (before v3.12) used to create an
3457 * orphan item for truncate indicating that there were possibly
3458 * extent items past i_size that needed to be deleted. In v3.12,
3459 * truncate was changed to update i_size in sync with the extent
3460 * items, but the (useless) orphan item was still created. Since
3461 * v4.18, we don't create the orphan item for truncate at all.
3462 *
3463 * So, this item could mean that we need to do a truncate, but
3464 * only if this filesystem was last used on a pre-v3.12 kernel
3465 * and was not cleanly unmounted. The odds of that are quite
3466 * slim, and it's a pain to do the truncate now, so just delete
3467 * the orphan item.
3468 *
3469 * It's also possible that this orphan item was supposed to be
3470 * deleted but wasn't. The inode number may have been reused,
3471 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003472 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003473 if (ret == -ENOENT || inode->i_nlink) {
3474 if (!ret)
3475 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003476 trans = btrfs_start_transaction(root, 1);
3477 if (IS_ERR(trans)) {
3478 ret = PTR_ERR(trans);
3479 goto out;
3480 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003481 btrfs_debug(fs_info, "auto deleting %Lu",
3482 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003483 ret = btrfs_del_orphan_item(trans, root,
3484 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003485 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003486 if (ret)
3487 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003488 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 }
Josef Bacik7b128762008-07-24 12:17:14 -04003490
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003491 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003492
3493 /* this will do delete_inode and everything for us */
3494 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 if (ret)
3496 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003497 }
Miao Xie3254c872011-11-10 20:45:05 -05003498 /* release the path since we're done with it */
3499 btrfs_release_path(path);
3500
Yan, Zhengd68fc572010-05-16 10:49:58 -04003501 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3502
Omar Sandovala575cee2018-05-11 13:13:38 -07003503 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003504 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003505 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003506 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003507 }
Josef Bacik7b128762008-07-24 12:17:14 -04003508
3509 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003510 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003511
3512out:
3513 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003514 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003515 btrfs_free_path(path);
3516 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003517}
3518
Chris Masond352ac62008-09-29 15:18:18 -04003519/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003520 * very simple check to peek ahead in the leaf looking for xattrs. If we
3521 * don't find any xattrs, we know there can't be any acls.
3522 *
3523 * slot is the slot the inode is in, objectid is the objectid of the inode
3524 */
3525static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003526 int slot, u64 objectid,
3527 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003528{
3529 u32 nritems = btrfs_header_nritems(leaf);
3530 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003531 static u64 xattr_access = 0;
3532 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003533 int scanned = 0;
3534
Josef Bacikf23b5a52013-06-19 10:16:26 -04003535 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003536 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3537 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3538 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3539 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003540 }
3541
Chris Mason46a53cc2009-04-27 11:47:50 -04003542 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003543 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003544 while (slot < nritems) {
3545 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3546
3547 /* we found a different objectid, there must not be acls */
3548 if (found_key.objectid != objectid)
3549 return 0;
3550
3551 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003552 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003553 if (*first_xattr_slot == -1)
3554 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003555 if (found_key.offset == xattr_access ||
3556 found_key.offset == xattr_default)
3557 return 1;
3558 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003559
3560 /*
3561 * we found a key greater than an xattr key, there can't
3562 * be any acls later on
3563 */
3564 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3565 return 0;
3566
3567 slot++;
3568 scanned++;
3569
3570 /*
3571 * it goes inode, inode backrefs, xattrs, extents,
3572 * so if there are a ton of hard links to an inode there can
3573 * be a lot of backrefs. Don't waste time searching too hard,
3574 * this is just an optimization
3575 */
3576 if (scanned >= 8)
3577 break;
3578 }
3579 /* we hit the end of the leaf before we found an xattr or
3580 * something larger than an xattr. We have to assume the inode
3581 * has acls
3582 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003583 if (*first_xattr_slot == -1)
3584 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 return 1;
3586}
3587
3588/*
Chris Masond352ac62008-09-29 15:18:18 -04003589 * read an inode from the btree into the in-memory inode
3590 */
Filipe Manana67710892016-06-06 11:51:25 +01003591static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003592{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003593 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003594 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003595 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003596 struct btrfs_inode_item *inode_item;
3597 struct btrfs_root *root = BTRFS_I(inode)->root;
3598 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003599 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003600 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003601 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003602 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003603 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003604 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003605
3606 ret = btrfs_fill_inode(inode, &rdev);
3607 if (!ret)
3608 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003609
3610 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003611 if (!path) {
3612 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003613 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003614 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003615
Chris Mason39279cc2007-06-12 06:35:45 -04003616 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003617
Chris Mason39279cc2007-06-12 06:35:45 -04003618 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003619 if (ret) {
3620 if (ret > 0)
3621 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003622 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003623 }
Chris Mason39279cc2007-06-12 06:35:45 -04003624
Chris Mason5f39d392007-10-15 16:14:19 -04003625 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003626
3627 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003628 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003629
Chris Mason5f39d392007-10-15 16:14:19 -04003630 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3631 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003632 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003633 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003634 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3635 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003636 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003637
David Sterbaa937b972014-12-12 17:39:12 +01003638 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3639 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003640
David Sterbaa937b972014-12-12 17:39:12 +01003641 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3642 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003643
David Sterbaa937b972014-12-12 17:39:12 +01003644 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3645 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003646
chandan r9cc97d62012-07-04 12:48:07 +05303647 BTRFS_I(inode)->i_otime.tv_sec =
3648 btrfs_timespec_sec(leaf, &inode_item->otime);
3649 BTRFS_I(inode)->i_otime.tv_nsec =
3650 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003651
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003652 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003653 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003654 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3655
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003656 inode_set_iversion_queried(inode,
3657 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003658 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003659 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003660 rdev = btrfs_inode_rdev(leaf, inode_item);
3661
Josef Bacikaec74772008-07-24 12:12:38 -04003662 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003663 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003664
3665cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003666 /*
3667 * If we were modified in the current generation and evicted from memory
3668 * and then re-read we need to do a full sync since we don't have any
3669 * idea about which extents were modified before we were evicted from
3670 * cache.
3671 *
3672 * This is required for both inode re-read from disk and delayed inode
3673 * in delayed_nodes_tree.
3674 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003675 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003676 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3677 &BTRFS_I(inode)->runtime_flags);
3678
Filipe Mananabde6c242015-07-24 00:00:19 +01003679 /*
3680 * We don't persist the id of the transaction where an unlink operation
3681 * against the inode was last made. So here we assume the inode might
3682 * have been evicted, and therefore the exact value of last_unlink_trans
3683 * lost, and set it to last_trans to avoid metadata inconsistencies
3684 * between the inode and its parent if the inode is fsync'ed and the log
3685 * replayed. For example, in the scenario:
3686 *
3687 * touch mydir/foo
3688 * ln mydir/foo mydir/bar
3689 * sync
3690 * unlink mydir/bar
3691 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3692 * xfs_io -c fsync mydir/foo
3693 * <power failure>
3694 * mount fs, triggers fsync log replay
3695 *
3696 * We must make sure that when we fsync our inode foo we also log its
3697 * parent inode, otherwise after log replay the parent still has the
3698 * dentry with the "bar" name but our inode foo has a link count of 1
3699 * and doesn't have an inode ref with the name "bar" anymore.
3700 *
3701 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003702 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003703 * transaction commits on fsync if our inode is a directory, or if our
3704 * inode is not a directory, logging its parent unnecessarily.
3705 */
3706 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3707
Miao Xie67de1172013-12-26 13:07:06 +08003708 path->slots[0]++;
3709 if (inode->i_nlink != 1 ||
3710 path->slots[0] >= btrfs_header_nritems(leaf))
3711 goto cache_acl;
3712
3713 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003714 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003715 goto cache_acl;
3716
3717 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3718 if (location.type == BTRFS_INODE_REF_KEY) {
3719 struct btrfs_inode_ref *ref;
3720
3721 ref = (struct btrfs_inode_ref *)ptr;
3722 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3723 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3724 struct btrfs_inode_extref *extref;
3725
3726 extref = (struct btrfs_inode_extref *)ptr;
3727 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3728 extref);
3729 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003730cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003731 /*
3732 * try to precache a NULL acl entry for files that don't have
3733 * any xattrs or acls
3734 */
Li Zefan33345d012011-04-20 10:31:50 +08003735 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003736 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003737 if (first_xattr_slot != -1) {
3738 path->slots[0] = first_xattr_slot;
3739 ret = btrfs_load_inode_props(inode, path);
3740 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003741 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003742 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003743 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003744 root->root_key.objectid, ret);
3745 }
3746 btrfs_free_path(path);
3747
Al Viro72c04902009-06-24 16:58:48 -04003748 if (!maybe_acls)
3749 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003750
Chris Mason39279cc2007-06-12 06:35:45 -04003751 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003752 case S_IFREG:
3753 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003754 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 inode->i_fop = &btrfs_file_operations;
3756 inode->i_op = &btrfs_file_inode_operations;
3757 break;
3758 case S_IFDIR:
3759 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003760 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003761 break;
3762 case S_IFLNK:
3763 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003764 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003765 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3766 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003767 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003768 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003769 init_special_inode(inode, inode->i_mode, rdev);
3770 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003771 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003772
David Sterba7b6a2212018-03-26 18:40:21 +02003773 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003774 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003775
3776make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003777 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003778 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003779 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003780}
3781
Chris Masond352ac62008-09-29 15:18:18 -04003782/*
3783 * given a leaf and an inode, copy the inode fields into the leaf
3784 */
Chris Masone02119d2008-09-05 16:13:11 -04003785static void fill_inode_item(struct btrfs_trans_handle *trans,
3786 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003787 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003788 struct inode *inode)
3789{
Liu Bo51fab692012-12-27 09:01:21 +00003790 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003791
Liu Bo51fab692012-12-27 09:01:21 +00003792 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Liu Bo51fab692012-12-27 09:01:21 +00003794 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3795 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3796 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3797 &token);
3798 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3799 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003800
David Sterbaa937b972014-12-12 17:39:12 +01003801 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003802 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003803 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003804 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
David Sterbaa937b972014-12-12 17:39:12 +01003806 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003807 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003808 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003809 inode->i_mtime.tv_nsec, &token);
3810
David Sterbaa937b972014-12-12 17:39:12 +01003811 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003812 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003813 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003814 inode->i_ctime.tv_nsec, &token);
3815
chandan r9cc97d62012-07-04 12:48:07 +05303816 btrfs_set_token_timespec_sec(leaf, &item->otime,
3817 BTRFS_I(inode)->i_otime.tv_sec, &token);
3818 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3819 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3820
Liu Bo51fab692012-12-27 09:01:21 +00003821 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3822 &token);
3823 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3824 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003825 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3826 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003827 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3828 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3829 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3830 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003831}
3832
Chris Masond352ac62008-09-29 15:18:18 -04003833/*
3834 * copy everything in the in-memory inode into the btree.
3835 */
Chris Mason21151332011-11-10 20:39:08 -05003836static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003837 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003838{
3839 struct btrfs_inode_item *inode_item;
3840 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003841 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003842 int ret;
3843
3844 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003845 if (!path)
3846 return -ENOMEM;
3847
Chris Masonb9473432009-03-13 11:00:37 -04003848 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003849 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3850 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003851 if (ret) {
3852 if (ret > 0)
3853 ret = -ENOENT;
3854 goto failed;
3855 }
3856
Chris Mason5f39d392007-10-15 16:14:19 -04003857 leaf = path->nodes[0];
3858 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003859 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003860
Chris Masone02119d2008-09-05 16:13:11 -04003861 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003862 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003863 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003864 ret = 0;
3865failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003866 btrfs_free_path(path);
3867 return ret;
3868}
3869
Chris Masond352ac62008-09-29 15:18:18 -04003870/*
Chris Mason21151332011-11-10 20:39:08 -05003871 * copy everything in the in-memory inode into the btree.
3872 */
3873noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3874 struct btrfs_root *root, struct inode *inode)
3875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003876 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003877 int ret;
3878
3879 /*
3880 * If the inode is a free space inode, we can deadlock during commit
3881 * if we put it into the delayed code.
3882 *
3883 * The data relocation inode should also be directly updated
3884 * without delay
3885 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003886 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003887 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003888 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003889 btrfs_update_root_times(trans, root);
3890
Chris Mason21151332011-11-10 20:39:08 -05003891 ret = btrfs_delayed_update_inode(trans, root, inode);
3892 if (!ret)
3893 btrfs_set_inode_last_trans(trans, inode);
3894 return ret;
3895 }
3896
3897 return btrfs_update_inode_item(trans, root, inode);
3898}
3899
Josef Bacikbe6aef62012-10-22 15:43:12 -04003900noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3901 struct btrfs_root *root,
3902 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003903{
3904 int ret;
3905
3906 ret = btrfs_update_inode(trans, root, inode);
3907 if (ret == -ENOSPC)
3908 return btrfs_update_inode_item(trans, root, inode);
3909 return ret;
3910}
3911
3912/*
Chris Masond352ac62008-09-29 15:18:18 -04003913 * unlink helper that gets used here in inode.c and in the tree logging
3914 * recovery code. It remove a link in a directory with a given name, and
3915 * also drops the back refs in the inode to the directory
3916 */
Al Viro92986792011-03-04 17:14:37 +00003917static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3918 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003919 struct btrfs_inode *dir,
3920 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003921 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003922{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003923 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003925 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003926 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003927 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003928 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003929 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003930 u64 ino = btrfs_ino(inode);
3931 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003932
3933 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003934 if (!path) {
3935 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003936 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003937 }
3938
Chris Masonb9473432009-03-13 11:00:37 -04003939 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003940 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003941 name, name_len, -1);
3942 if (IS_ERR(di)) {
3943 ret = PTR_ERR(di);
3944 goto err;
3945 }
3946 if (!di) {
3947 ret = -ENOENT;
3948 goto err;
3949 }
Chris Mason5f39d392007-10-15 16:14:19 -04003950 leaf = path->nodes[0];
3951 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003952 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003953 if (ret)
3954 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003955 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003956
Miao Xie67de1172013-12-26 13:07:06 +08003957 /*
3958 * If we don't have dir index, we have to get it by looking up
3959 * the inode ref, since we get the inode ref, remove it directly,
3960 * it is unnecessary to do delayed deletion.
3961 *
3962 * But if we have dir index, needn't search inode ref to get it.
3963 * Since the inode ref is close to the inode item, it is better
3964 * that we delay to delete it, and just do this deletion when
3965 * we update the inode item.
3966 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003967 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003968 ret = btrfs_delayed_delete_inode_ref(inode);
3969 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003970 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003971 goto skip_backref;
3972 }
3973 }
3974
Li Zefan33345d012011-04-20 10:31:50 +08003975 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3976 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003977 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003978 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003979 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003980 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003981 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003982 goto err;
3983 }
Miao Xie67de1172013-12-26 13:07:06 +08003984skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003985 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003986 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003987 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003988 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003989 }
Chris Mason39279cc2007-06-12 06:35:45 -04003990
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003991 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3992 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003993 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003994 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003995 goto err;
3996 }
Chris Masone02119d2008-09-05 16:13:11 -04003997
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003998 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3999 index);
Chris Mason6418c962010-10-30 07:34:24 -04004000 if (ret == -ENOENT)
4001 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004002 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004003 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004004err:
4005 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004006 if (ret)
4007 goto out;
4008
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004009 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004010 inode_inc_iversion(&inode->vfs_inode);
4011 inode_inc_iversion(&dir->vfs_inode);
4012 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4013 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4014 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004015out:
Chris Mason39279cc2007-06-12 06:35:45 -04004016 return ret;
4017}
4018
Al Viro92986792011-03-04 17:14:37 +00004019int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4020 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004021 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004022 const char *name, int name_len)
4023{
4024 int ret;
4025 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4026 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004027 drop_nlink(&inode->vfs_inode);
4028 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004029 }
4030 return ret;
4031}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032
4033/*
4034 * helper to start transaction for unlink and rmdir.
4035 *
Josef Bacikd52be812013-05-29 14:54:47 -04004036 * unlink and rmdir are special in btrfs, they do not always free space, so
4037 * if we cannot make our reservations the normal way try and see if there is
4038 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4039 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040 */
Josef Bacikd52be812013-05-29 14:54:47 -04004041static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004043 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044
Josef Bacike70bea52011-10-11 14:18:24 -04004045 /*
4046 * 1 for the possible orphan item
4047 * 1 for the dir item
4048 * 1 for the dir index
4049 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004050 * 1 for the inode
4051 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004052 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053}
4054
Chris Mason39279cc2007-06-12 06:35:45 -04004055static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4056{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004058 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004059 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004060 int ret;
4061
Josef Bacikd52be812013-05-29 14:54:47 -04004062 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004063 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004064 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004065
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004066 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4067 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004068
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004069 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4070 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4071 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004072 if (ret)
4073 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004074
Yan, Zhenga22285a2010-05-16 10:48:46 -04004075 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004076 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004077 if (ret)
4078 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004079 }
Josef Bacik7b128762008-07-24 12:17:14 -04004080
Tsutomu Itohb5324022011-07-19 07:27:20 +00004081out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004082 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004083 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004084 return ret;
4085}
4086
Misono Tomohirof60a2362018-04-18 11:34:52 +09004087static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 struct btrfs_root *root,
4089 struct inode *dir, u64 objectid,
4090 const char *name, int name_len)
4091{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004092 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 struct btrfs_path *path;
4094 struct extent_buffer *leaf;
4095 struct btrfs_dir_item *di;
4096 struct btrfs_key key;
4097 u64 index;
4098 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004099 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100
4101 path = btrfs_alloc_path();
4102 if (!path)
4103 return -ENOMEM;
4104
Li Zefan33345d012011-04-20 10:31:50 +08004105 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (IS_ERR_OR_NULL(di)) {
4108 if (!di)
4109 ret = -ENOENT;
4110 else
4111 ret = PTR_ERR(di);
4112 goto out;
4113 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114
4115 leaf = path->nodes[0];
4116 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4117 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4118 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004120 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004121 goto out;
4122 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004123 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004125 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4126 root->root_key.objectid, dir_ino,
4127 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004130 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004131 goto out;
4132 }
Li Zefan33345d012011-04-20 10:31:50 +08004133 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (IS_ERR_OR_NULL(di)) {
4136 if (!di)
4137 ret = -ENOENT;
4138 else
4139 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004140 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141 goto out;
4142 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143
4144 leaf = path->nodes[0];
4145 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004146 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147 index = key.offset;
4148 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004149 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004151 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004153 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 goto out;
4155 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004156
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004157 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004158 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004159 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004160 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004162 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004164 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166}
4167
Misono Tomohiroec42f162018-04-18 11:34:13 +09004168/*
4169 * Helper to check if the subvolume references other subvolumes or if it's
4170 * default.
4171 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004172static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004173{
4174 struct btrfs_fs_info *fs_info = root->fs_info;
4175 struct btrfs_path *path;
4176 struct btrfs_dir_item *di;
4177 struct btrfs_key key;
4178 u64 dir_id;
4179 int ret;
4180
4181 path = btrfs_alloc_path();
4182 if (!path)
4183 return -ENOMEM;
4184
4185 /* Make sure this root isn't set as the default subvol */
4186 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4187 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4188 dir_id, "default", 7, 0);
4189 if (di && !IS_ERR(di)) {
4190 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4191 if (key.objectid == root->root_key.objectid) {
4192 ret = -EPERM;
4193 btrfs_err(fs_info,
4194 "deleting default subvolume %llu is not allowed",
4195 key.objectid);
4196 goto out;
4197 }
4198 btrfs_release_path(path);
4199 }
4200
4201 key.objectid = root->root_key.objectid;
4202 key.type = BTRFS_ROOT_REF_KEY;
4203 key.offset = (u64)-1;
4204
4205 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4206 if (ret < 0)
4207 goto out;
4208 BUG_ON(ret == 0);
4209
4210 ret = 0;
4211 if (path->slots[0] > 0) {
4212 path->slots[0]--;
4213 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4214 if (key.objectid == root->root_key.objectid &&
4215 key.type == BTRFS_ROOT_REF_KEY)
4216 ret = -ENOTEMPTY;
4217 }
4218out:
4219 btrfs_free_path(path);
4220 return ret;
4221}
4222
Nikolay Borisov20a68002018-04-27 14:36:24 +03004223/* Delete all dentries for inodes belonging to the root */
4224static void btrfs_prune_dentries(struct btrfs_root *root)
4225{
4226 struct btrfs_fs_info *fs_info = root->fs_info;
4227 struct rb_node *node;
4228 struct rb_node *prev;
4229 struct btrfs_inode *entry;
4230 struct inode *inode;
4231 u64 objectid = 0;
4232
4233 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4234 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4235
4236 spin_lock(&root->inode_lock);
4237again:
4238 node = root->inode_tree.rb_node;
4239 prev = NULL;
4240 while (node) {
4241 prev = node;
4242 entry = rb_entry(node, struct btrfs_inode, rb_node);
4243
4244 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
4245 node = node->rb_left;
4246 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
4247 node = node->rb_right;
4248 else
4249 break;
4250 }
4251 if (!node) {
4252 while (prev) {
4253 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4254 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
4255 node = prev;
4256 break;
4257 }
4258 prev = rb_next(prev);
4259 }
4260 }
4261 while (node) {
4262 entry = rb_entry(node, struct btrfs_inode, rb_node);
4263 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
4264 inode = igrab(&entry->vfs_inode);
4265 if (inode) {
4266 spin_unlock(&root->inode_lock);
4267 if (atomic_read(&inode->i_count) > 1)
4268 d_prune_aliases(inode);
4269 /*
4270 * btrfs_drop_inode will have it removed from the inode
4271 * cache when its usage count hits zero.
4272 */
4273 iput(inode);
4274 cond_resched();
4275 spin_lock(&root->inode_lock);
4276 goto again;
4277 }
4278
4279 if (cond_resched_lock(&root->inode_lock))
4280 goto again;
4281
4282 node = rb_next(node);
4283 }
4284 spin_unlock(&root->inode_lock);
4285}
4286
Misono Tomohirof60a2362018-04-18 11:34:52 +09004287int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4288{
4289 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4290 struct btrfs_root *root = BTRFS_I(dir)->root;
4291 struct inode *inode = d_inode(dentry);
4292 struct btrfs_root *dest = BTRFS_I(inode)->root;
4293 struct btrfs_trans_handle *trans;
4294 struct btrfs_block_rsv block_rsv;
4295 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004296 int ret;
4297 int err;
4298
4299 /*
4300 * Don't allow to delete a subvolume with send in progress. This is
4301 * inside the inode lock so the error handling that has to drop the bit
4302 * again is not run concurrently.
4303 */
4304 spin_lock(&dest->root_item_lock);
4305 root_flags = btrfs_root_flags(&dest->root_item);
4306 if (dest->send_in_progress == 0) {
4307 btrfs_set_root_flags(&dest->root_item,
4308 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4309 spin_unlock(&dest->root_item_lock);
4310 } else {
4311 spin_unlock(&dest->root_item_lock);
4312 btrfs_warn(fs_info,
4313 "attempt to delete subvolume %llu during send",
4314 dest->root_key.objectid);
4315 return -EPERM;
4316 }
4317
4318 down_write(&fs_info->subvol_sem);
4319
4320 err = may_destroy_subvol(dest);
4321 if (err)
4322 goto out_up_write;
4323
4324 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4325 /*
4326 * One for dir inode,
4327 * two for dir entries,
4328 * two for root ref/backref.
4329 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004330 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004331 if (err)
4332 goto out_up_write;
4333
4334 trans = btrfs_start_transaction(root, 0);
4335 if (IS_ERR(trans)) {
4336 err = PTR_ERR(trans);
4337 goto out_release;
4338 }
4339 trans->block_rsv = &block_rsv;
4340 trans->bytes_reserved = block_rsv.size;
4341
4342 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4343
4344 ret = btrfs_unlink_subvol(trans, root, dir,
4345 dest->root_key.objectid,
4346 dentry->d_name.name,
4347 dentry->d_name.len);
4348 if (ret) {
4349 err = ret;
4350 btrfs_abort_transaction(trans, ret);
4351 goto out_end_trans;
4352 }
4353
4354 btrfs_record_root_in_trans(trans, dest);
4355
4356 memset(&dest->root_item.drop_progress, 0,
4357 sizeof(dest->root_item.drop_progress));
4358 dest->root_item.drop_level = 0;
4359 btrfs_set_root_refs(&dest->root_item, 0);
4360
4361 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4362 ret = btrfs_insert_orphan_item(trans,
4363 fs_info->tree_root,
4364 dest->root_key.objectid);
4365 if (ret) {
4366 btrfs_abort_transaction(trans, ret);
4367 err = ret;
4368 goto out_end_trans;
4369 }
4370 }
4371
Lu Fengqid1957792018-05-29 15:01:54 +08004372 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004373 BTRFS_UUID_KEY_SUBVOL,
4374 dest->root_key.objectid);
4375 if (ret && ret != -ENOENT) {
4376 btrfs_abort_transaction(trans, ret);
4377 err = ret;
4378 goto out_end_trans;
4379 }
4380 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004381 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004382 dest->root_item.received_uuid,
4383 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4384 dest->root_key.objectid);
4385 if (ret && ret != -ENOENT) {
4386 btrfs_abort_transaction(trans, ret);
4387 err = ret;
4388 goto out_end_trans;
4389 }
4390 }
4391
4392out_end_trans:
4393 trans->block_rsv = NULL;
4394 trans->bytes_reserved = 0;
4395 ret = btrfs_end_transaction(trans);
4396 if (ret && !err)
4397 err = ret;
4398 inode->i_flags |= S_DEAD;
4399out_release:
4400 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4401out_up_write:
4402 up_write(&fs_info->subvol_sem);
4403 if (err) {
4404 spin_lock(&dest->root_item_lock);
4405 root_flags = btrfs_root_flags(&dest->root_item);
4406 btrfs_set_root_flags(&dest->root_item,
4407 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4408 spin_unlock(&dest->root_item_lock);
4409 } else {
4410 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004411 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004412 ASSERT(dest->send_in_progress == 0);
4413
4414 /* the last ref */
4415 if (dest->ino_cache_inode) {
4416 iput(dest->ino_cache_inode);
4417 dest->ino_cache_inode = NULL;
4418 }
4419 }
4420
4421 return err;
4422}
4423
Chris Mason39279cc2007-06-12 06:35:45 -04004424static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4425{
David Howells2b0143b2015-03-17 22:25:59 +00004426 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004427 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004428 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004430 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004431
David Sterbab3ae2442012-09-13 16:04:34 -06004432 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004433 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004434 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004435 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004436
Josef Bacikd52be812013-05-29 14:54:47 -04004437 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004438 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004439 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004440
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004441 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004442 err = btrfs_unlink_subvol(trans, root, dir,
4443 BTRFS_I(inode)->location.objectid,
4444 dentry->d_name.name,
4445 dentry->d_name.len);
4446 goto out;
4447 }
4448
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004449 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004450 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004451 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004452
Filipe Manana44f714d2016-06-06 16:11:13 +01004453 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4454
Chris Mason39279cc2007-06-12 06:35:45 -04004455 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004456 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4457 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4458 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004459 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004460 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004461 /*
4462 * Propagate the last_unlink_trans value of the deleted dir to
4463 * its parent directory. This is to prevent an unrecoverable
4464 * log tree in the case we do something like this:
4465 * 1) create dir foo
4466 * 2) create snapshot under dir foo
4467 * 3) delete the snapshot
4468 * 4) rmdir foo
4469 * 5) mkdir foo
4470 * 6) fsync foo or some file inside foo
4471 */
4472 if (last_unlink_trans >= trans->transid)
4473 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4474 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004475out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004476 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004477 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004478
Chris Mason39279cc2007-06-12 06:35:45 -04004479 return err;
4480}
4481
Chris Mason28f75a02015-02-04 06:59:29 -08004482static int truncate_space_check(struct btrfs_trans_handle *trans,
4483 struct btrfs_root *root,
4484 u64 bytes_deleted)
4485{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004486 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004487 int ret;
4488
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004489 /*
4490 * This is only used to apply pressure to the enospc system, we don't
4491 * intend to use this reservation at all.
4492 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004493 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004494 bytes_deleted *= fs_info->nodesize;
4495 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004496 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004497 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004498 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004499 trans->transid,
4500 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004501 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004502 }
Chris Mason28f75a02015-02-04 06:59:29 -08004503 return ret;
4504
4505}
4506
Josef Bacikddfae632017-10-19 14:16:02 -04004507/*
4508 * Return this if we need to call truncate_block for the last bit of the
4509 * truncate.
4510 */
4511#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004512
Chris Mason323ac952008-10-01 19:05:46 -04004513/*
Chris Mason39279cc2007-06-12 06:35:45 -04004514 * this can truncate away extent items, csum items and directory items.
4515 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004516 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004517 *
4518 * csum items that cross the new i_size are truncated to the new size
4519 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004520 *
4521 * min_type is the minimum key type to truncate down to. If set to 0, this
4522 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004523 */
Yan, Zheng80825102009-11-12 09:35:36 +00004524int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4525 struct btrfs_root *root,
4526 struct inode *inode,
4527 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004528{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004529 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004530 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004531 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004532 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004533 struct btrfs_key key;
4534 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004535 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004536 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004537 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004538 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004539 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004540 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004541 int found_extent;
4542 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004543 int pending_del_nr = 0;
4544 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004545 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004546 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004547 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004548 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004549 bool be_nice = false;
4550 bool should_throttle = false;
4551 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004552
4553 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004554
Chris Mason28ed1342014-12-17 09:41:04 -08004555 /*
4556 * for non-free space inodes and ref cows, we want to back off from
4557 * time to time
4558 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004559 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004560 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004561 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004562
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004563 path = btrfs_alloc_path();
4564 if (!path)
4565 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004566 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004567
Josef Bacik5dc562c2012-08-17 13:14:17 -04004568 /*
4569 * We want to drop from the next block forward in case this new size is
4570 * not block aligned since we will be keeping the last block of the
4571 * extent just the way it is.
4572 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004573 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004574 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004575 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004576 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004577 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004578
Miao Xie16cdcec2011-04-22 18:12:22 +08004579 /*
4580 * This function is also used to drop the items in the log tree before
4581 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4582 * it is used to drop the loged items. So we shouldn't kill the delayed
4583 * items.
4584 */
4585 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004586 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004587
Li Zefan33345d012011-04-20 10:31:50 +08004588 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004589 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004590 key.type = (u8)-1;
4591
Chris Mason85e21ba2008-01-29 15:11:36 -05004592search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004593 /*
4594 * with a 16K leaf size and 128MB extents, you can actually queue
4595 * up a huge file in a single leaf. Most of the time that
4596 * bytes_deleted is > 0, it will be huge by the time we get here
4597 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004598 if (be_nice && bytes_deleted > SZ_32M &&
4599 btrfs_should_end_transaction(trans)) {
4600 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004601 goto out;
4602 }
Chris Masond3977122009-01-05 21:25:51 -05004603
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004604 path->leave_spinning = 1;
4605 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4606 if (ret < 0)
4607 goto out;
4608
Chris Mason85e21ba2008-01-29 15:11:36 -05004609 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004610 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004611 /* there are no items in the tree for us to truncate, we're
4612 * done
4613 */
Yan, Zheng80825102009-11-12 09:35:36 +00004614 if (path->slots[0] == 0)
4615 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004616 path->slots[0]--;
4617 }
4618
Chris Masond3977122009-01-05 21:25:51 -05004619 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004620 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004621 leaf = path->nodes[0];
4622 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004623 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004624
Li Zefan33345d012011-04-20 10:31:50 +08004625 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004626 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004627
Chris Mason85e21ba2008-01-29 15:11:36 -05004628 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004629 break;
4630
Chris Mason5f39d392007-10-15 16:14:19 -04004631 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004632 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004633 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004634 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004635 extent_type = btrfs_file_extent_type(leaf, fi);
4636 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004637 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004638 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004639
4640 trace_btrfs_truncate_show_fi_regular(
4641 BTRFS_I(inode), leaf, fi,
4642 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004643 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004644 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004645 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004646
4647 trace_btrfs_truncate_show_fi_inline(
4648 BTRFS_I(inode), leaf, fi, path->slots[0],
4649 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004650 }
Yan008630c2007-11-07 13:31:09 -05004651 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004652 }
Yan, Zheng80825102009-11-12 09:35:36 +00004653 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004654 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004655 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004656 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004657 break;
4658 if (found_key.offset >= new_size)
4659 del_item = 1;
4660 else
4661 del_item = 0;
4662 }
Chris Mason39279cc2007-06-12 06:35:45 -04004663 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004664 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004665 if (found_type != BTRFS_EXTENT_DATA_KEY)
4666 goto delete;
4667
4668 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004669 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004670 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004671 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004672 u64 orig_num_bytes =
4673 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004674 extent_num_bytes = ALIGN(new_size -
4675 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004676 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004677 btrfs_set_file_extent_num_bytes(leaf, fi,
4678 extent_num_bytes);
4679 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004680 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004681 if (test_bit(BTRFS_ROOT_REF_COWS,
4682 &root->state) &&
4683 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004684 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004685 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004686 } else {
Chris Masondb945352007-10-15 16:15:53 -04004687 extent_num_bytes =
4688 btrfs_file_extent_disk_num_bytes(leaf,
4689 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004690 extent_offset = found_key.offset -
4691 btrfs_file_extent_offset(leaf, fi);
4692
Chris Mason39279cc2007-06-12 06:35:45 -04004693 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004694 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004695 if (extent_start != 0) {
4696 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004697 if (test_bit(BTRFS_ROOT_REF_COWS,
4698 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004699 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004700 }
4701 }
Chris Mason90692182008-02-08 13:49:28 -05004702 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004703 /*
4704 * we can't truncate inline items that have had
4705 * special encodings
4706 */
4707 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004708 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004709 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4710 btrfs_file_extent_compression(leaf, fi) == 0) {
4711 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004712
Josef Bacikddfae632017-10-19 14:16:02 -04004713 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4714 size = btrfs_file_extent_calc_inline_size(size);
4715 btrfs_truncate_item(root->fs_info, path, size, 1);
4716 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004717 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004718 * We have to bail so the last_size is set to
4719 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004720 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004721 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004722 break;
Chris Mason90692182008-02-08 13:49:28 -05004723 }
Josef Bacikddfae632017-10-19 14:16:02 -04004724
4725 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4726 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004727 }
Chris Mason179e29e2007-11-01 11:28:41 -04004728delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004729 if (del_item)
4730 last_size = found_key.offset;
4731 else
4732 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004733 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004734 if (!pending_del_nr) {
4735 /* no pending yet, add ourselves */
4736 pending_del_slot = path->slots[0];
4737 pending_del_nr = 1;
4738 } else if (pending_del_nr &&
4739 path->slots[0] + 1 == pending_del_slot) {
4740 /* hop on the pending chunk */
4741 pending_del_nr++;
4742 pending_del_slot = path->slots[0];
4743 } else {
Chris Masond3977122009-01-05 21:25:51 -05004744 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004745 }
Chris Mason39279cc2007-06-12 06:35:45 -04004746 } else {
4747 break;
4748 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004749 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004750
Miao Xie27cdeb72014-04-02 19:51:05 +08004751 if (found_extent &&
4752 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004753 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004754 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004755 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004756 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004757 extent_num_bytes, 0,
4758 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004759 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004760 if (ret) {
4761 btrfs_abort_transaction(trans, ret);
4762 break;
4763 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004764 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4765 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004766 trans->delayed_ref_updates * 2,
4767 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004768 if (be_nice) {
4769 if (truncate_space_check(trans, root,
4770 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004771 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004772 }
4773 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004774 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004775 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004776 }
Chris Mason39279cc2007-06-12 06:35:45 -04004777 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004778
Yan, Zheng80825102009-11-12 09:35:36 +00004779 if (found_type == BTRFS_INODE_ITEM_KEY)
4780 break;
4781
4782 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004783 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004784 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004785 if (pending_del_nr) {
4786 ret = btrfs_del_items(trans, root, path,
4787 pending_del_slot,
4788 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004789 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004790 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004791 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004792 }
Yan, Zheng80825102009-11-12 09:35:36 +00004793 pending_del_nr = 0;
4794 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004795 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004796 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004797 unsigned long updates = trans->delayed_ref_updates;
4798 if (updates) {
4799 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004800 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004801 updates * 2);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004802 if (ret)
4803 break;
Josef Bacik12621332015-02-03 07:50:16 -08004804 }
4805 }
Chris Mason28f75a02015-02-04 06:59:29 -08004806 /*
4807 * if we failed to refill our space rsv, bail out
4808 * and let the transaction restart
4809 */
4810 if (should_end) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004811 ret = -EAGAIN;
4812 break;
Chris Mason28f75a02015-02-04 06:59:29 -08004813 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004814 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004815 } else {
4816 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004817 }
Chris Mason39279cc2007-06-12 06:35:45 -04004818 }
Yan, Zheng80825102009-11-12 09:35:36 +00004819out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004820 if (ret >= 0 && pending_del_nr) {
4821 int err;
4822
4823 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004824 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004825 if (err) {
4826 btrfs_abort_transaction(trans, err);
4827 ret = err;
4828 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004829 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004830 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4831 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004832 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004833 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004834 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004835 }
Chris Mason28ed1342014-12-17 09:41:04 -08004836
Chris Mason39279cc2007-06-12 06:35:45 -04004837 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004838
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004839 if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004840 unsigned long updates = trans->delayed_ref_updates;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004841 int err;
4842
Chris Mason28ed1342014-12-17 09:41:04 -08004843 if (updates) {
4844 trans->delayed_ref_updates = 0;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004845 err = btrfs_run_delayed_refs(trans, updates * 2);
4846 if (err)
4847 ret = err;
Chris Mason28ed1342014-12-17 09:41:04 -08004848 }
4849 }
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004850 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004851}
4852
Chris Masona52d9a82007-08-27 16:49:44 -04004853/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004855 * @inode - inode that we're zeroing
4856 * @from - the offset to start zeroing
4857 * @len - the length to zero, 0 to zero the entire range respective to the
4858 * offset
4859 * @front - zero up to the offset instead of from the offset on
4860 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304861 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004862 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004863 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304864int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004865 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004866{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004867 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004868 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004869 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4870 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004871 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004872 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004873 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004874 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004875 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304876 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004877 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004878 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004879 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304880 u64 block_start;
4881 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004882
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004883 if (IS_ALIGNED(offset, blocksize) &&
4884 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004885 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304886
Josef Bacik8b62f872017-10-19 14:15:55 -04004887 block_start = round_down(from, blocksize);
4888 block_end = block_start + blocksize - 1;
4889
Qu Wenruo364ecf32017-02-27 15:10:38 +08004890 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004891 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004892 if (ret)
4893 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004894
Chris Mason211c17f2008-05-15 09:13:45 -04004895again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004896 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004897 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004898 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004899 block_start, blocksize, true);
4900 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004901 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004902 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004903 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004904
Chris Masona52d9a82007-08-27 16:49:44 -04004905 if (!PageUptodate(page)) {
4906 ret = btrfs_readpage(NULL, page);
4907 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004908 if (page->mapping != mapping) {
4909 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004910 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004911 goto again;
4912 }
Chris Masona52d9a82007-08-27 16:49:44 -04004913 if (!PageUptodate(page)) {
4914 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004915 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004916 }
4917 }
Chris Mason211c17f2008-05-15 09:13:45 -04004918 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004919
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304920 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004921 set_page_extent_mapped(page);
4922
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304923 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004924 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304925 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004926 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004927 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004928 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004929 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004930 btrfs_put_ordered_extent(ordered);
4931 goto again;
4932 }
4933
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304934 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004935 EXTENT_DIRTY | EXTENT_DELALLOC |
4936 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004937 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004938
Filipe Mananae3b8a482017-11-04 00:16:59 +00004939 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004940 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004941 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304942 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004943 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004944 goto out_unlock;
4945 }
4946
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304947 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004948 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304949 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004950 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004951 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304952 memset(kaddr + (block_start - page_offset(page)),
4953 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004954 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304955 memset(kaddr + (block_start - page_offset(page)) + offset,
4956 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004957 flush_dcache_page(page);
4958 kunmap(page);
4959 }
Chris Mason247e7432008-07-17 12:53:51 -04004960 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004961 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004962 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004963
Chris Mason89642222008-07-24 09:41:53 -04004964out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004965 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004966 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004967 blocksize, true);
4968 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004969 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004970 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004971out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004972 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004973 return ret;
4974}
4975
Josef Bacik16e75492013-10-22 12:18:51 -04004976static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4977 u64 offset, u64 len)
4978{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004980 struct btrfs_trans_handle *trans;
4981 int ret;
4982
4983 /*
4984 * Still need to make sure the inode looks like it's been updated so
4985 * that any holes get logged if we fsync.
4986 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004987 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4988 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004989 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4990 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4991 return 0;
4992 }
4993
4994 /*
4995 * 1 - for the one we're dropping
4996 * 1 - for the one we're adding
4997 * 1 - for updating the inode.
4998 */
4999 trans = btrfs_start_transaction(root, 3);
5000 if (IS_ERR(trans))
5001 return PTR_ERR(trans);
5002
5003 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5004 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005005 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005006 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005007 return ret;
5008 }
5009
David Sterbaf85b7372017-01-20 14:54:07 +01005010 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5011 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005012 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005013 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005014 else
5015 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005016 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005017 return ret;
5018}
5019
Josef Bacik695a0d02011-03-04 15:46:53 -05005020/*
5021 * This function puts in dummy file extents for the area we're creating a hole
5022 * for. So if we are truncating this file to a larger size we need to insert
5023 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5024 * the range between oldsize and size
5025 */
Josef Bacika41ad392011-01-31 15:30:16 -05005026int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005027{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005029 struct btrfs_root *root = BTRFS_I(inode)->root;
5030 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005031 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005032 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005033 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005034 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5035 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005036 u64 last_byte;
5037 u64 cur_offset;
5038 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005039 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005040
Josef Bacika71754f2013-06-17 17:14:39 -04005041 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305042 * If our size started in the middle of a block we need to zero out the
5043 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005044 * expose stale data.
5045 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305046 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005047 if (err)
5048 return err;
5049
Yan Zheng9036c102008-10-30 14:19:41 -04005050 if (size <= hole_start)
5051 return 0;
5052
Yan Zheng9036c102008-10-30 14:19:41 -04005053 while (1) {
5054 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005055
David Sterbaff13db42015-12-03 14:30:40 +01005056 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005057 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005058 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005059 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005060 if (!ordered)
5061 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005062 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005063 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005064 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005065 btrfs_put_ordered_extent(ordered);
5066 }
5067
Yan Zheng9036c102008-10-30 14:19:41 -04005068 cur_offset = hole_start;
5069 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02005070 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005071 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005072 if (IS_ERR(em)) {
5073 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005074 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005075 break;
5076 }
Yan Zheng9036c102008-10-30 14:19:41 -04005077 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005078 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005079 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005080 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005081 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005082
Josef Bacik16e75492013-10-22 12:18:51 -04005083 err = maybe_insert_hole(root, inode, cur_offset,
5084 hole_size);
5085 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005086 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005087 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005088 cur_offset + hole_size - 1, 0);
5089 hole_em = alloc_extent_map();
5090 if (!hole_em) {
5091 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5092 &BTRFS_I(inode)->runtime_flags);
5093 goto next;
5094 }
5095 hole_em->start = cur_offset;
5096 hole_em->len = hole_size;
5097 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005098
Josef Bacik5dc562c2012-08-17 13:14:17 -04005099 hole_em->block_start = EXTENT_MAP_HOLE;
5100 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005101 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005102 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005103 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005104 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005105 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005106
5107 while (1) {
5108 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005109 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005110 write_unlock(&em_tree->lock);
5111 if (err != -EEXIST)
5112 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005113 btrfs_drop_extent_cache(BTRFS_I(inode),
5114 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005115 cur_offset +
5116 hole_size - 1, 0);
5117 }
5118 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005119 }
Josef Bacik16e75492013-10-22 12:18:51 -04005120next:
Yan Zheng9036c102008-10-30 14:19:41 -04005121 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005122 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005123 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005124 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005125 break;
5126 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005127 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005128 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005129 return err;
5130}
5131
Eric Sandeen3972f262013-01-12 02:57:22 +00005132static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005133{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005134 struct btrfs_root *root = BTRFS_I(inode)->root;
5135 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005136 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005137 loff_t newsize = attr->ia_size;
5138 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005139 int ret;
5140
Eric Sandeen3972f262013-01-12 02:57:22 +00005141 /*
5142 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5143 * special case where we need to update the times despite not having
5144 * these flags set. For all other operations the VFS set these flags
5145 * explicitly if it wants a timestamp update.
5146 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005147 if (newsize != oldsize) {
5148 inode_inc_iversion(inode);
5149 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5150 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005151 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005152 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005153
Josef Bacika41ad392011-01-31 15:30:16 -05005154 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005155 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005156 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005157 * This is to ensure the snapshot captures a fully consistent
5158 * state of this file - if the snapshot captures this expanding
5159 * truncation, it must capture all writes that happened before
5160 * this truncation.
5161 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005162 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005163 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005164 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005165 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005166 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005167 }
Yan, Zheng80825102009-11-12 09:35:36 +00005168
Miao Xief4a2f4c2011-12-14 20:12:01 -05005169 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005170 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005171 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005172 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005173 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005174
5175 i_size_write(inode, newsize);
5176 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305177 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005178 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005179 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005180 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005181 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005182
Josef Bacika41ad392011-01-31 15:30:16 -05005183 /*
5184 * We're truncating a file that used to have good data down to
5185 * zero. Make sure it gets into the ordered flush list so that
5186 * any new writes get down to disk quickly.
5187 */
5188 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005189 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5190 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005191
Josef Bacika41ad392011-01-31 15:30:16 -05005192 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005193
5194 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005195 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005196 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005197 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005198
Filipe Manana213e8c52018-02-06 20:40:31 +00005199 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005200 if (ret && inode->i_nlink) {
5201 int err;
5202
5203 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005204 * Truncate failed, so fix up the in-memory size. We
5205 * adjusted disk_i_size down as we removed extents, so
5206 * wait for disk_i_size to be stable and then update the
5207 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005208 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005209 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005210 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005211 return err;
5212 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005213 }
Yan, Zheng80825102009-11-12 09:35:36 +00005214 }
5215
Josef Bacika41ad392011-01-31 15:30:16 -05005216 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005217}
5218
Chris Mason39279cc2007-06-12 06:35:45 -04005219static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5220{
David Howells2b0143b2015-03-17 22:25:59 +00005221 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005222 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005223 int err;
5224
Li Zefanb83cc962010-12-20 16:04:08 +08005225 if (btrfs_root_readonly(root))
5226 return -EROFS;
5227
Jan Kara31051c82016-05-26 16:55:18 +02005228 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005229 if (err)
5230 return err;
5231
Chris Mason5a3f23d2009-03-31 13:27:11 -04005232 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005233 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005234 if (err)
5235 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005236 }
Yan Zheng9036c102008-10-30 14:19:41 -04005237
Christoph Hellwig10257742010-06-04 11:30:02 +02005238 if (attr->ia_valid) {
5239 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005240 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005241 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005242
Josef Bacik22c44fe2011-11-30 10:45:38 -05005243 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005244 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005245 }
5246
Chris Mason39279cc2007-06-12 06:35:45 -04005247 return err;
5248}
Chris Mason61295eb2008-01-14 16:24:38 -05005249
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005250/*
5251 * While truncating the inode pages during eviction, we get the VFS calling
5252 * btrfs_invalidatepage() against each page of the inode. This is slow because
5253 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5254 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5255 * extent_state structures over and over, wasting lots of time.
5256 *
5257 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5258 * those expensive operations on a per page basis and do only the ordered io
5259 * finishing, while we release here the extent_map and extent_state structures,
5260 * without the excessive merging and splitting.
5261 */
5262static void evict_inode_truncate_pages(struct inode *inode)
5263{
5264 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5265 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5266 struct rb_node *node;
5267
5268 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005269 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005270
5271 write_lock(&map_tree->lock);
5272 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5273 struct extent_map *em;
5274
5275 node = rb_first(&map_tree->map);
5276 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005277 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5278 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279 remove_extent_mapping(map_tree, em);
5280 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005281 if (need_resched()) {
5282 write_unlock(&map_tree->lock);
5283 cond_resched();
5284 write_lock(&map_tree->lock);
5285 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005286 }
5287 write_unlock(&map_tree->lock);
5288
Filipe Manana6ca07092015-05-26 00:55:42 +01005289 /*
5290 * Keep looping until we have no more ranges in the io tree.
5291 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005292 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5293 * still in progress (unlocked the pages in the bio but did not yet
5294 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005295 * ranges can still be locked and eviction started because before
5296 * submitting those bios, which are executed by a separate task (work
5297 * queue kthread), inode references (inode->i_count) were not taken
5298 * (which would be dropped in the end io callback of each bio).
5299 * Therefore here we effectively end up waiting for those bios and
5300 * anyone else holding locked ranges without having bumped the inode's
5301 * reference count - if we don't do it, when they access the inode's
5302 * io_tree to unlock a range it may be too late, leading to an
5303 * use-after-free issue.
5304 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005305 spin_lock(&io_tree->lock);
5306 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5307 struct extent_state *state;
5308 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005309 u64 start;
5310 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005311
5312 node = rb_first(&io_tree->state);
5313 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005314 start = state->start;
5315 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005316 spin_unlock(&io_tree->lock);
5317
David Sterbaff13db42015-12-03 14:30:40 +01005318 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005319
5320 /*
5321 * If still has DELALLOC flag, the extent didn't reach disk,
5322 * and its reserved space won't be freed by delayed_ref.
5323 * So we need to free its reserved space here.
5324 * (Refer to comment in btrfs_invalidatepage, case 2)
5325 *
5326 * Note, end is the bytenr of last byte, so we need + 1 here.
5327 */
5328 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005329 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005330
Filipe Manana6ca07092015-05-26 00:55:42 +01005331 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005332 EXTENT_LOCKED | EXTENT_DIRTY |
5333 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005334 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005335
Filipe Manana7064dd52014-08-08 02:47:05 +01005336 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005337 spin_lock(&io_tree->lock);
5338 }
5339 spin_unlock(&io_tree->lock);
5340}
5341
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005342static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5343 struct btrfs_block_rsv *rsv,
5344 u64 min_size)
5345{
5346 struct btrfs_fs_info *fs_info = root->fs_info;
5347 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5348 int failures = 0;
5349
5350 for (;;) {
5351 struct btrfs_trans_handle *trans;
5352 int ret;
5353
5354 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5355 BTRFS_RESERVE_FLUSH_LIMIT);
5356
5357 if (ret && ++failures > 2) {
5358 btrfs_warn(fs_info,
5359 "could not allocate space for a delete; will truncate on mount");
5360 return ERR_PTR(-ENOSPC);
5361 }
5362
5363 trans = btrfs_join_transaction(root);
5364 if (IS_ERR(trans) || !ret)
5365 return trans;
5366
5367 /*
5368 * Try to steal from the global reserve if there is space for
5369 * it.
5370 */
5371 if (!btrfs_check_space_for_delayed_refs(trans, fs_info) &&
5372 !btrfs_block_rsv_migrate(global_rsv, rsv, min_size, 0))
5373 return trans;
5374
5375 /* If not, commit and try again. */
5376 ret = btrfs_commit_transaction(trans);
5377 if (ret)
5378 return ERR_PTR(ret);
5379 }
5380}
5381
Al Virobd555972010-06-07 11:35:40 -04005382void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005385 struct btrfs_trans_handle *trans;
5386 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005387 struct btrfs_block_rsv *rsv;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005388 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005389 int ret;
5390
liubo1abe9b82011-03-24 11:18:59 +00005391 trace_btrfs_inode_evict(inode);
5392
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005393 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005394 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005395 return;
5396 }
5397
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005398 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005399
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005400 evict_inode_truncate_pages(inode);
5401
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005402 if (inode->i_nlink &&
5403 ((btrfs_root_refs(&root->root_item) != 0 &&
5404 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005405 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005406 goto no_delete;
5407
Omar Sandoval27919062018-05-11 13:13:37 -07005408 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005409 goto no_delete;
Al Virobd555972010-06-07 11:35:40 -04005410 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005411 if (!special_file(inode->i_mode))
5412 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005413
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005414 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005415
Omar Sandoval7b40b692018-05-11 13:13:33 -07005416 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005417 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005418
Yan, Zheng76dda932009-09-21 16:00:26 -04005419 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005420 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5421 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005422 goto no_delete;
5423 }
5424
Nikolay Borisovaa790212017-01-10 20:35:40 +02005425 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005426 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005427 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005428
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005429 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005430 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005431 goto no_delete;
Josef Bacik4a338542011-08-29 11:01:31 -04005432 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005433 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005434
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005435 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005436
Yan, Zheng80825102009-11-12 09:35:36 +00005437 while (1) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005438 trans = evict_refill_and_join(root, rsv, min_size);
Omar Sandoval27919062018-05-11 13:13:37 -07005439 if (IS_ERR(trans))
5440 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005441
5442 trans->block_rsv = rsv;
5443
Yan, Zhengd68fc572010-05-16 10:49:58 -04005444 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005445 trans->block_rsv = &fs_info->trans_block_rsv;
5446 btrfs_end_transaction(trans);
5447 btrfs_btree_balance_dirty(fs_info);
5448 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5449 goto free_rsv;
5450 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005451 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005452 }
5453
Josef Bacik4ef31a42013-08-13 14:10:08 -04005454 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005455 * Errors here aren't a big deal, it just means we leave orphan items in
5456 * the tree. They will be cleaned up on the next mount. If the inode
5457 * number gets reused, cleanup deletes the orphan item without doing
5458 * anything, and unlink reuses the existing orphan item.
5459 *
5460 * If it turns out that we are dropping too many of these, we might want
5461 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005462 */
Omar Sandoval27919062018-05-11 13:13:37 -07005463 trans = evict_refill_and_join(root, rsv, min_size);
5464 if (!IS_ERR(trans)) {
5465 trans->block_rsv = rsv;
5466 btrfs_orphan_del(trans, BTRFS_I(inode));
5467 trans->block_rsv = &fs_info->trans_block_rsv;
5468 btrfs_end_transaction(trans);
5469 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005470
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005471 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005472 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005473 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005474
Omar Sandoval27919062018-05-11 13:13:37 -07005475free_rsv:
5476 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005477no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005478 /*
5479 * If we didn't successfully delete, the orphan item will still be in
5480 * the tree and we'll retry on the next mount. Again, we might also want
5481 * to retry these periodically in the future.
5482 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005483 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005484 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005485}
5486
5487/*
5488 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005489 * If no dir entries were found, returns -ENOENT.
5490 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005491 */
5492static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5493 struct btrfs_key *location)
5494{
5495 const char *name = dentry->d_name.name;
5496 int namelen = dentry->d_name.len;
5497 struct btrfs_dir_item *di;
5498 struct btrfs_path *path;
5499 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005500 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005501
5502 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005503 if (!path)
5504 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005505
David Sterbaf85b7372017-01-20 14:54:07 +01005506 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5507 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005508 if (!di) {
5509 ret = -ENOENT;
5510 goto out;
5511 }
5512 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005513 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005514 goto out;
5515 }
Chris Masond3977122009-01-05 21:25:51 -05005516
Chris Mason5f39d392007-10-15 16:14:19 -04005517 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005518 if (location->type != BTRFS_INODE_ITEM_KEY &&
5519 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005520 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005521 btrfs_warn(root->fs_info,
5522"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5523 __func__, name, btrfs_ino(BTRFS_I(dir)),
5524 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005525 }
Chris Mason39279cc2007-06-12 06:35:45 -04005526out:
Chris Mason39279cc2007-06-12 06:35:45 -04005527 btrfs_free_path(path);
5528 return ret;
5529}
5530
5531/*
5532 * when we hit a tree root in a directory, the btrfs part of the inode
5533 * needs to be changed to reflect the root directory of the tree root. This
5534 * is kind of like crossing a mount point.
5535 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005536static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005537 struct inode *dir,
5538 struct dentry *dentry,
5539 struct btrfs_key *location,
5540 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005541{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005542 struct btrfs_path *path;
5543 struct btrfs_root *new_root;
5544 struct btrfs_root_ref *ref;
5545 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005546 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 int ret;
5548 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005549
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 path = btrfs_alloc_path();
5551 if (!path) {
5552 err = -ENOMEM;
5553 goto out;
5554 }
Chris Mason39279cc2007-06-12 06:35:45 -04005555
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005556 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005557 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5558 key.type = BTRFS_ROOT_REF_KEY;
5559 key.offset = location->objectid;
5560
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005561 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005562 if (ret) {
5563 if (ret < 0)
5564 err = ret;
5565 goto out;
5566 }
Chris Mason39279cc2007-06-12 06:35:45 -04005567
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 leaf = path->nodes[0];
5569 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005570 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005571 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5572 goto out;
5573
5574 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5575 (unsigned long)(ref + 1),
5576 dentry->d_name.len);
5577 if (ret)
5578 goto out;
5579
David Sterbab3b4aa72011-04-21 01:20:15 +02005580 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005581
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005582 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005583 if (IS_ERR(new_root)) {
5584 err = PTR_ERR(new_root);
5585 goto out;
5586 }
5587
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005588 *sub_root = new_root;
5589 location->objectid = btrfs_root_dirid(&new_root->root_item);
5590 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005591 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005592 err = 0;
5593out:
5594 btrfs_free_path(path);
5595 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005596}
5597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598static void inode_tree_add(struct inode *inode)
5599{
5600 struct btrfs_root *root = BTRFS_I(inode)->root;
5601 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005602 struct rb_node **p;
5603 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005604 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005605 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606
Al Viro1d3382cb2010-10-23 15:19:20 -04005607 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005608 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005609 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005611 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 while (*p) {
5613 parent = *p;
5614 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5615
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005616 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005617 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005618 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005619 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 else {
5621 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005622 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005623 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005624 RB_CLEAR_NODE(parent);
5625 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005626 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005627 }
5628 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005629 rb_link_node(new, parent, p);
5630 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005631 spin_unlock(&root->inode_lock);
5632}
5633
5634static void inode_tree_del(struct inode *inode)
5635{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005636 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005637 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005638 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005639
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005640 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005641 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005643 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005644 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005645 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005646 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005647
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005648 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005649 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005650 spin_lock(&root->inode_lock);
5651 empty = RB_EMPTY_ROOT(&root->inode_tree);
5652 spin_unlock(&root->inode_lock);
5653 if (empty)
5654 btrfs_add_dead_root(root);
5655 }
5656}
5657
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005658
Chris Masone02119d2008-09-05 16:13:11 -04005659static int btrfs_init_locked_inode(struct inode *inode, void *p)
5660{
5661 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005662 inode->i_ino = args->location->objectid;
5663 memcpy(&BTRFS_I(inode)->location, args->location,
5664 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005665 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005666 return 0;
5667}
5668
5669static int btrfs_find_actor(struct inode *inode, void *opaque)
5670{
5671 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005672 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005673 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005674}
5675
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005676static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005677 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005679{
5680 struct inode *inode;
5681 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005682 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005683
Chris Mason90d3e592014-01-09 17:28:00 -08005684 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005685 args.root = root;
5686
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005687 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005688 btrfs_init_locked_inode,
5689 (void *)&args);
5690 return inode;
5691}
5692
Balaji Rao1a54ef82008-07-21 02:01:04 +05305693/* Get an inode object given its location and corresponding root.
5694 * Returns in *is_new if the inode was read from disk
5695 */
5696struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005697 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305698{
5699 struct inode *inode;
5700
Chris Mason90d3e592014-01-09 17:28:00 -08005701 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305702 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005703 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305704
5705 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005706 int ret;
5707
5708 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005709 if (!is_bad_inode(inode)) {
5710 inode_tree_add(inode);
5711 unlock_new_inode(inode);
5712 if (new)
5713 *new = 1;
5714 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005715 unlock_new_inode(inode);
5716 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005717 ASSERT(ret < 0);
5718 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005719 }
5720 }
5721
Balaji Rao1a54ef82008-07-21 02:01:04 +05305722 return inode;
5723}
5724
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725static struct inode *new_simple_dir(struct super_block *s,
5726 struct btrfs_key *key,
5727 struct btrfs_root *root)
5728{
5729 struct inode *inode = new_inode(s);
5730
5731 if (!inode)
5732 return ERR_PTR(-ENOMEM);
5733
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005734 BTRFS_I(inode)->root = root;
5735 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005736 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737
5738 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005739 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005740 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005741 inode->i_fop = &simple_dir_operations;
5742 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005743 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305744 inode->i_atime = inode->i_mtime;
5745 inode->i_ctime = inode->i_mtime;
5746 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747
5748 return inode;
5749}
5750
Chris Mason3de45862008-11-17 21:02:50 -05005751struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005752{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005753 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005754 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005756 struct btrfs_root *sub_root = root;
5757 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005758 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005759 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005760
5761 if (dentry->d_name.len > BTRFS_NAME_LEN)
5762 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005763
Jeff Layton39e3c952012-11-28 11:30:53 -05005764 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005765 if (ret < 0)
5766 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005767
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005768 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005769 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005770 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005771 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005772
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005773 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005774 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005775 &location, &sub_root);
5776 if (ret < 0) {
5777 if (ret != -ENOENT)
5778 inode = ERR_PTR(ret);
5779 else
5780 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5781 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005782 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005783 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005784 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005785
Julia Lawall34d19ba2011-01-24 19:55:19 +00005786 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005787 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005788 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005789 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005790 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005791 if (ret) {
5792 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005793 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005794 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005795 }
5796
Chris Mason3de45862008-11-17 21:02:50 -05005797 return inode;
5798}
5799
Nick Pigginfe15ce42011-01-07 17:49:23 +11005800static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005801{
5802 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005803 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005804
Li Zefan848cce02012-02-21 17:04:28 +08005805 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005806 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005807
Li Zefan848cce02012-02-21 17:04:28 +08005808 if (inode) {
5809 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005810 if (btrfs_root_refs(&root->root_item) == 0)
5811 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005812
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005813 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005814 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005815 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005816 return 0;
5817}
5818
Chris Mason3de45862008-11-17 21:02:50 -05005819static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005820 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005821{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005822 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005823
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005824 inode = btrfs_lookup_dentry(dir, dentry);
5825 if (IS_ERR(inode)) {
5826 if (PTR_ERR(inode) == -ENOENT)
5827 inode = NULL;
5828 else
5829 return ERR_CAST(inode);
5830 }
5831
Al Viro41d28bc2014-10-12 22:24:21 -04005832 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005833}
5834
Miao Xie16cdcec2011-04-22 18:12:22 +08005835unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005836 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5837};
5838
Josef Bacik23b5ec72017-07-24 15:14:25 -04005839/*
5840 * All this infrastructure exists because dir_emit can fault, and we are holding
5841 * the tree lock when doing readdir. For now just allocate a buffer and copy
5842 * our information into that, and then dir_emit from the buffer. This is
5843 * similar to what NFS does, only we don't keep the buffer around in pagecache
5844 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5845 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5846 * tree lock.
5847 */
5848static int btrfs_opendir(struct inode *inode, struct file *file)
5849{
5850 struct btrfs_file_private *private;
5851
5852 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5853 if (!private)
5854 return -ENOMEM;
5855 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5856 if (!private->filldir_buf) {
5857 kfree(private);
5858 return -ENOMEM;
5859 }
5860 file->private_data = private;
5861 return 0;
5862}
5863
5864struct dir_entry {
5865 u64 ino;
5866 u64 offset;
5867 unsigned type;
5868 int name_len;
5869};
5870
5871static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5872{
5873 while (entries--) {
5874 struct dir_entry *entry = addr;
5875 char *name = (char *)(entry + 1);
5876
David Sterba92d32172018-04-16 21:10:14 +02005877 ctx->pos = get_unaligned(&entry->offset);
5878 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5879 get_unaligned(&entry->ino),
5880 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005881 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005882 addr += sizeof(struct dir_entry) +
5883 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005884 ctx->pos++;
5885 }
5886 return 0;
5887}
5888
Al Viro9cdda8d2013-05-22 16:48:09 -04005889static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005890{
Al Viro9cdda8d2013-05-22 16:48:09 -04005891 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005892 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005893 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005894 struct btrfs_dir_item *di;
5895 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005896 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005897 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005898 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005899 struct list_head ins_list;
5900 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005901 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005902 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005903 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005904 char *name_ptr;
5905 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005906 int entries = 0;
5907 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005908 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005909 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005910
Al Viro9cdda8d2013-05-22 16:48:09 -04005911 if (!dir_emit_dots(file, ctx))
5912 return 0;
5913
David Woodhouse49593bf2008-08-17 17:08:36 +01005914 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005915 if (!path)
5916 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005917
Josef Bacik23b5ec72017-07-24 15:14:25 -04005918 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005919 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005920
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005921 INIT_LIST_HEAD(&ins_list);
5922 INIT_LIST_HEAD(&del_list);
5923 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005924
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005926 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005927 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005928 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005929
Chris Mason39279cc2007-06-12 06:35:45 -04005930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5931 if (ret < 0)
5932 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005933
5934 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005935 struct dir_entry *entry;
5936
Chris Mason5f39d392007-10-15 16:14:19 -04005937 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005938 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005939 if (slot >= btrfs_header_nritems(leaf)) {
5940 ret = btrfs_next_leaf(root, path);
5941 if (ret < 0)
5942 goto err;
5943 else if (ret > 0)
5944 break;
5945 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 }
Chris Mason3de45862008-11-17 21:02:50 -05005947
Chris Mason5f39d392007-10-15 16:14:19 -04005948 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5949
5950 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005951 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005952 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005953 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005954 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005955 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005956 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005957 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005958 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005959 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005960 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5961 PAGE_SIZE) {
5962 btrfs_release_path(path);
5963 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5964 if (ret)
5965 goto nopos;
5966 addr = private->filldir_buf;
5967 entries = 0;
5968 total_len = 0;
5969 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005970 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005971
5972 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005973 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005974 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005975 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5976 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005977 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5978 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005979 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005980 put_unaligned(location.objectid, &entry->ino);
5981 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005982 entries++;
5983 addr += sizeof(struct dir_entry) + name_len;
5984 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005985next:
5986 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005987 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005988 btrfs_release_path(path);
5989
5990 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5991 if (ret)
5992 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005993
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005994 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005995 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005996 goto nopos;
5997
Zach Browndb62efb2013-07-11 16:19:42 -07005998 /*
5999 * Stop new entries from being returned after we return the last
6000 * entry.
6001 *
6002 * New directory entries are assigned a strictly increasing
6003 * offset. This means that new entries created during readdir
6004 * are *guaranteed* to be seen in the future by that readdir.
6005 * This has broken buggy programs which operate on names as
6006 * they're returned by readdir. Until we re-use freed offsets
6007 * we have this hack to stop new entries from being returned
6008 * under the assumption that they'll never reach this huge
6009 * offset.
6010 *
6011 * This is being careful not to overflow 32bit loff_t unless the
6012 * last entry requires it because doing so has broken 32bit apps
6013 * in the past.
6014 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006015 if (ctx->pos >= INT_MAX)
6016 ctx->pos = LLONG_MAX;
6017 else
6018 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006019nopos:
6020 ret = 0;
6021err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006022 if (put)
6023 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006024 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006025 return ret;
6026}
6027
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006028int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006029{
6030 struct btrfs_root *root = BTRFS_I(inode)->root;
6031 struct btrfs_trans_handle *trans;
6032 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006033 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006034
Josef Bacik72ac3c02012-05-23 14:13:11 -04006035 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006036 return 0;
6037
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006038 if (btrfs_fs_closing(root->fs_info) &&
6039 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006040 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006041
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006042 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006043 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006044 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006045 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006046 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006047 if (IS_ERR(trans))
6048 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006049 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006050 }
6051 return ret;
6052}
6053
6054/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006055 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006056 * inode changes. But, it is most likely to find the inode in cache.
6057 * FIXME, needs more benchmarking...there are no reasons other than performance
6058 * to keep or drop this code.
6059 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006060static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006061{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006062 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006063 struct btrfs_root *root = BTRFS_I(inode)->root;
6064 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006065 int ret;
6066
Josef Bacik72ac3c02012-05-23 14:13:11 -04006067 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006068 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006069
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006070 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006071 if (IS_ERR(trans))
6072 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006073
6074 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006075 if (ret && ret == -ENOSPC) {
6076 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006077 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006078 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006079 if (IS_ERR(trans))
6080 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006081
Chris Mason94b60442010-05-26 11:02:00 -04006082 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006083 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006084 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006085 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006086 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006087
6088 return ret;
6089}
6090
6091/*
6092 * This is a copy of file_update_time. We need this so we can return error on
6093 * ENOSPC for updating the inode in the case of file write and mmap writes.
6094 */
Josef Bacike41f9412012-03-26 09:46:47 -04006095static int btrfs_update_time(struct inode *inode, struct timespec *now,
6096 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006097{
Alexander Block2bc5565282012-06-15 09:49:33 +02006098 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006099 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006100
6101 if (btrfs_root_readonly(root))
6102 return -EROFS;
6103
Josef Bacike41f9412012-03-26 09:46:47 -04006104 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006105 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006106 if (flags & S_CTIME)
6107 inode->i_ctime = *now;
6108 if (flags & S_MTIME)
6109 inode->i_mtime = *now;
6110 if (flags & S_ATIME)
6111 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006112 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006113}
6114
Chris Masond352ac62008-09-29 15:18:18 -04006115/*
6116 * find the highest existing sequence number in a directory
6117 * and then set the in-memory index_cnt variable to reflect
6118 * free sequence numbers
6119 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006120static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006121{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006122 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006123 struct btrfs_key key, found_key;
6124 struct btrfs_path *path;
6125 struct extent_buffer *leaf;
6126 int ret;
6127
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006128 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006129 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006130 key.offset = (u64)-1;
6131
6132 path = btrfs_alloc_path();
6133 if (!path)
6134 return -ENOMEM;
6135
6136 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6137 if (ret < 0)
6138 goto out;
6139 /* FIXME: we should be able to handle this */
6140 if (ret == 0)
6141 goto out;
6142 ret = 0;
6143
6144 /*
6145 * MAGIC NUMBER EXPLANATION:
6146 * since we search a directory based on f_pos we have to start at 2
6147 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6148 * else has to start at 2
6149 */
6150 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006151 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006152 goto out;
6153 }
6154
6155 path->slots[0]--;
6156
6157 leaf = path->nodes[0];
6158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6159
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006160 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006161 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006162 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006163 goto out;
6164 }
6165
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006166 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006167out:
6168 btrfs_free_path(path);
6169 return ret;
6170}
6171
Chris Masond352ac62008-09-29 15:18:18 -04006172/*
6173 * helper to find a free sequence number in a given directory. This current
6174 * code is very simple, later versions will do smarter things in the btree
6175 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006176int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006177{
6178 int ret = 0;
6179
Nikolay Borisov877574e2017-02-20 13:50:33 +02006180 if (dir->index_cnt == (u64)-1) {
6181 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006182 if (ret) {
6183 ret = btrfs_set_inode_index_count(dir);
6184 if (ret)
6185 return ret;
6186 }
Josef Bacikaec74772008-07-24 12:12:38 -04006187 }
6188
Nikolay Borisov877574e2017-02-20 13:50:33 +02006189 *index = dir->index_cnt;
6190 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006191
6192 return ret;
6193}
6194
Chris Masonb0d5d102014-09-08 13:08:51 -07006195static int btrfs_insert_inode_locked(struct inode *inode)
6196{
6197 struct btrfs_iget_args args;
6198 args.location = &BTRFS_I(inode)->location;
6199 args.root = BTRFS_I(inode)->root;
6200
6201 return insert_inode_locked4(inode,
6202 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6203 btrfs_find_actor, &args);
6204}
6205
Anand Jain19aee8d2017-07-18 17:37:05 +08006206/*
6207 * Inherit flags from the parent inode.
6208 *
6209 * Currently only the compression flags and the cow flags are inherited.
6210 */
6211static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6212{
6213 unsigned int flags;
6214
6215 if (!dir)
6216 return;
6217
6218 flags = BTRFS_I(dir)->flags;
6219
6220 if (flags & BTRFS_INODE_NOCOMPRESS) {
6221 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6222 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6223 } else if (flags & BTRFS_INODE_COMPRESS) {
6224 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6225 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6226 }
6227
6228 if (flags & BTRFS_INODE_NODATACOW) {
6229 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6230 if (S_ISREG(inode->i_mode))
6231 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6232 }
6233
David Sterba7b6a2212018-03-26 18:40:21 +02006234 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006235}
6236
Chris Mason39279cc2007-06-12 06:35:45 -04006237static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6238 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006239 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006240 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006241 u64 ref_objectid, u64 objectid,
6242 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006243{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006244 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006245 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006246 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006247 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006248 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006249 struct btrfs_inode_ref *ref;
6250 struct btrfs_key key[2];
6251 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006252 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006253 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006254 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006255
Chris Mason5f39d392007-10-15 16:14:19 -04006256 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006257 if (!path)
6258 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006260 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006261 if (!inode) {
6262 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006263 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006264 }
Chris Mason39279cc2007-06-12 06:35:45 -04006265
Li Zefan581bb052011-04-20 10:06:11 +08006266 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006267 * O_TMPFILE, set link count to 0, so that after this point,
6268 * we fill in an inode item with the correct link count.
6269 */
6270 if (!name)
6271 set_nlink(inode, 0);
6272
6273 /*
Li Zefan581bb052011-04-20 10:06:11 +08006274 * we have to initialize this early, so we can reclaim the inode
6275 * number if we fail afterwards in this function.
6276 */
6277 inode->i_ino = objectid;
6278
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006279 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006280 trace_btrfs_inode_request(dir);
6281
Nikolay Borisov877574e2017-02-20 13:50:33 +02006282 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006283 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006284 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006285 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006286 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006287 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006288 } else if (dir) {
6289 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006290 }
6291 /*
6292 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006293 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006294 * number
6295 */
6296 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006297 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006298 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006299 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006300 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006301
Josef Bacik5dc562c2012-08-17 13:14:17 -04006302 /*
6303 * We could have gotten an inode number from somebody who was fsynced
6304 * and then removed in this same transaction, so let's just set full
6305 * sync since it will be a full sync anyway and this will blow away the
6306 * old info in the log.
6307 */
6308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6309
Chris Mason9c583092008-01-29 15:15:18 -05006310 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006311 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006312 key[0].offset = 0;
6313
Chris Mason9c583092008-01-29 15:15:18 -05006314 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006315
6316 if (name) {
6317 /*
6318 * Start new inodes with an inode_ref. This is slightly more
6319 * efficient for small numbers of hard links since they will
6320 * be packed into one item. Extended refs will kick in if we
6321 * add more hard links than can fit in the ref item.
6322 */
6323 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006324 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006325 key[1].offset = ref_objectid;
6326
6327 sizes[1] = name_len + sizeof(*ref);
6328 }
Chris Mason9c583092008-01-29 15:15:18 -05006329
Chris Masonb0d5d102014-09-08 13:08:51 -07006330 location = &BTRFS_I(inode)->location;
6331 location->objectid = objectid;
6332 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006333 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006334
6335 ret = btrfs_insert_inode_locked(inode);
6336 if (ret < 0)
6337 goto fail;
6338
Chris Masonb9473432009-03-13 11:00:37 -04006339 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006340 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006341 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006342 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006343
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006344 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006345 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306346
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006347 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306348 inode->i_atime = inode->i_mtime;
6349 inode->i_ctime = inode->i_mtime;
6350 BTRFS_I(inode)->i_otime = inode->i_mtime;
6351
Chris Mason5f39d392007-10-15 16:14:19 -04006352 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6353 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006354 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006355 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006356 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006357
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006358 if (name) {
6359 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6360 struct btrfs_inode_ref);
6361 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6362 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6363 ptr = (unsigned long)(ref + 1);
6364 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6365 }
Chris Mason9c583092008-01-29 15:15:18 -05006366
Chris Mason5f39d392007-10-15 16:14:19 -04006367 btrfs_mark_buffer_dirty(path->nodes[0]);
6368 btrfs_free_path(path);
6369
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006370 btrfs_inherit_iflags(inode, dir);
6371
Al Viro569254b2011-07-24 17:08:40 -04006372 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006373 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006374 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006375 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006376 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6377 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006378 }
6379
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006380 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006381
6382 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006383 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006384
Alexander Block8ea05e32012-07-25 17:35:53 +02006385 btrfs_update_root_times(trans, root);
6386
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006387 ret = btrfs_inode_inherit_props(trans, inode, dir);
6388 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006389 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006390 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006391 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006392
Chris Mason39279cc2007-06-12 06:35:45 -04006393 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006394
6395fail_unlock:
6396 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006397fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006398 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006399 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006400 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006401 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006402 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006403}
6404
6405static inline u8 btrfs_inode_type(struct inode *inode)
6406{
6407 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6408}
6409
Chris Masond352ac62008-09-29 15:18:18 -04006410/*
6411 * utility function to add 'inode' into 'parent_inode' with
6412 * a give name and a given sequence number.
6413 * if 'add_backref' is true, also insert a backref from the
6414 * inode to the parent directory.
6415 */
Chris Masone02119d2008-09-05 16:13:11 -04006416int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006417 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006418 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006419{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006420 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006421 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006422 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006423 struct btrfs_root *root = parent_inode->root;
6424 u64 ino = btrfs_ino(inode);
6425 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006426
Li Zefan33345d012011-04-20 10:31:50 +08006427 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006428 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006429 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006430 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006431 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006432 key.offset = 0;
6433 }
Chris Mason39279cc2007-06-12 06:35:45 -04006434
Li Zefan33345d012011-04-20 10:31:50 +08006435 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006436 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6437 root->root_key.objectid, parent_ino,
6438 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006439 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006440 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6441 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006442 }
6443
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006444 /* Nothing to clean up yet */
6445 if (ret)
6446 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006447
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006448 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6449 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006450 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006451 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006452 goto fail_dir_item;
6453 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006454 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006455 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006456 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006457
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006458 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006460 inode_inc_iversion(&parent_inode->vfs_inode);
6461 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6462 current_time(&parent_inode->vfs_inode);
6463 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006464 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006465 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006467
6468fail_dir_item:
6469 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6470 u64 local_index;
6471 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006472 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6473 root->root_key.objectid, parent_ino,
6474 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006475
6476 } else if (add_backref) {
6477 u64 local_index;
6478 int err;
6479
6480 err = btrfs_del_inode_ref(trans, root, name, name_len,
6481 ino, parent_ino, &local_index);
6482 }
6483 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006484}
6485
6486static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006487 struct btrfs_inode *dir, struct dentry *dentry,
6488 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006489{
Josef Bacika1b075d2010-11-19 20:36:11 +00006490 int err = btrfs_add_link(trans, dir, inode,
6491 dentry->d_name.name, dentry->d_name.len,
6492 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006493 if (err > 0)
6494 err = -EEXIST;
6495 return err;
6496}
6497
Josef Bacik618e21d2007-07-11 10:18:17 -04006498static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006499 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006500{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006501 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006502 struct btrfs_trans_handle *trans;
6503 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006504 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006505 int err;
6506 int drop_inode = 0;
6507 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006508 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006509
Josef Bacik9ed74f22009-09-11 16:12:44 -04006510 /*
6511 * 2 for inode item and ref
6512 * 2 for dir items
6513 * 1 for xattr if selinux is on
6514 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006515 trans = btrfs_start_transaction(root, 5);
6516 if (IS_ERR(trans))
6517 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006518
Li Zefan581bb052011-04-20 10:06:11 +08006519 err = btrfs_find_free_ino(root, &objectid);
6520 if (err)
6521 goto out_unlock;
6522
Josef Bacikaec74772008-07-24 12:12:38 -04006523 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006524 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6525 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006526 if (IS_ERR(inode)) {
6527 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006528 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006529 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006530
Casey Schauflerad19db72011-12-15 10:09:07 -05006531 /*
6532 * If the active LSM wants to access the inode during
6533 * d_instantiate it needs these. Smack checks to see
6534 * if the filesystem supports xattrs by looking at the
6535 * ops vector.
6536 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006537 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006538 init_special_inode(inode, inode->i_mode, rdev);
6539
6540 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006541 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006542 goto out_unlock_inode;
6543
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006544 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6545 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006546 if (err) {
6547 goto out_unlock_inode;
6548 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006549 btrfs_update_inode(trans, root, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04006550 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006551 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006552
Josef Bacik618e21d2007-07-11 10:18:17 -04006553out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006554 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006555 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006556 if (drop_inode) {
6557 inode_dec_link_count(inode);
6558 iput(inode);
6559 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006560 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006561
6562out_unlock_inode:
6563 drop_inode = 1;
6564 unlock_new_inode(inode);
6565 goto out_unlock;
6566
Josef Bacik618e21d2007-07-11 10:18:17 -04006567}
6568
Chris Mason39279cc2007-06-12 06:35:45 -04006569static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006570 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006571{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006573 struct btrfs_trans_handle *trans;
6574 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006575 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006576 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006577 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006578 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006579 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006580
Josef Bacik9ed74f22009-09-11 16:12:44 -04006581 /*
6582 * 2 for inode item and ref
6583 * 2 for dir items
6584 * 1 for xattr if selinux is on
6585 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006586 trans = btrfs_start_transaction(root, 5);
6587 if (IS_ERR(trans))
6588 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006589
Li Zefan581bb052011-04-20 10:06:11 +08006590 err = btrfs_find_free_ino(root, &objectid);
6591 if (err)
6592 goto out_unlock;
6593
Josef Bacikaec74772008-07-24 12:12:38 -04006594 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006595 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6596 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006597 if (IS_ERR(inode)) {
6598 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006599 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006600 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006601 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006602 /*
6603 * If the active LSM wants to access the inode during
6604 * d_instantiate it needs these. Smack checks to see
6605 * if the filesystem supports xattrs by looking at the
6606 * ops vector.
6607 */
6608 inode->i_fop = &btrfs_file_operations;
6609 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006610 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006611
6612 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6613 if (err)
6614 goto out_unlock_inode;
6615
6616 err = btrfs_update_inode(trans, root, inode);
6617 if (err)
6618 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006619
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006620 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6621 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006622 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006623 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006624
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006625 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006626 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006627
Chris Mason39279cc2007-06-12 06:35:45 -04006628out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006629 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006630 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006631 inode_dec_link_count(inode);
6632 iput(inode);
6633 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006634 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006635 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006636
6637out_unlock_inode:
6638 unlock_new_inode(inode);
6639 goto out_unlock;
6640
Chris Mason39279cc2007-06-12 06:35:45 -04006641}
6642
6643static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6644 struct dentry *dentry)
6645{
Filipe Manana271dba42016-01-05 16:24:05 +00006646 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006647 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006648 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006650 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006651 int err;
6652 int drop_inode = 0;
6653
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006654 /* do not allow sys_link's with other subvols of the same device */
6655 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006656 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006657
Mark Fashehf1863732012-08-08 11:32:27 -07006658 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006659 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006660
Nikolay Borisov877574e2017-02-20 13:50:33 +02006661 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006662 if (err)
6663 goto fail;
6664
Yan, Zhenga22285a2010-05-16 10:48:46 -04006665 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006666 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006667 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006668 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006669 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006670 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006671 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006672 if (IS_ERR(trans)) {
6673 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006674 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006675 goto fail;
6676 }
Chris Mason5f39d392007-10-15 16:14:19 -04006677
Miao Xie67de1172013-12-26 13:07:06 +08006678 /* There are several dir indexes for this inode, clear the cache. */
6679 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006680 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006681 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006682 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006683 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006684 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006685
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006686 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6687 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006688
Yan, Zhenga5719522009-09-24 09:17:31 -04006689 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006690 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006691 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006692 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006693 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006694 if (err)
6695 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006696 if (inode->i_nlink == 1) {
6697 /*
6698 * If new hard link count is 1, it's a file created
6699 * with open(2) O_TMPFILE flag.
6700 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006701 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006702 if (err)
6703 goto fail;
6704 }
Al Viro08c422c2011-12-23 07:58:13 -05006705 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006706 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006707 }
Chris Mason39279cc2007-06-12 06:35:45 -04006708
Chris Mason1832a6d2007-12-21 16:27:21 -05006709fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006710 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006711 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006712 if (drop_inode) {
6713 inode_dec_link_count(inode);
6714 iput(inode);
6715 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006716 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006717 return err;
6718}
6719
Al Viro18bb1db2011-07-26 01:41:39 -04006720static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006721{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006722 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006723 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006724 struct btrfs_trans_handle *trans;
6725 struct btrfs_root *root = BTRFS_I(dir)->root;
6726 int err = 0;
6727 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006728 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006729 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006730
Josef Bacik9ed74f22009-09-11 16:12:44 -04006731 /*
6732 * 2 items for inode and ref
6733 * 2 items for dir items
6734 * 1 for xattr if selinux is on
6735 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006736 trans = btrfs_start_transaction(root, 5);
6737 if (IS_ERR(trans))
6738 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006739
Li Zefan581bb052011-04-20 10:06:11 +08006740 err = btrfs_find_free_ino(root, &objectid);
6741 if (err)
6742 goto out_fail;
6743
Josef Bacikaec74772008-07-24 12:12:38 -04006744 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006745 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6746 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006747 if (IS_ERR(inode)) {
6748 err = PTR_ERR(inode);
6749 goto out_fail;
6750 }
Chris Mason5f39d392007-10-15 16:14:19 -04006751
Chris Mason39279cc2007-06-12 06:35:45 -04006752 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006753 /* these must be set before we unlock the inode */
6754 inode->i_op = &btrfs_dir_inode_operations;
6755 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006756
Eric Paris2a7dba32011-02-01 11:05:39 -05006757 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006758 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006759 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006760
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006761 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006762 err = btrfs_update_inode(trans, root, inode);
6763 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006764 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006765
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006766 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6767 dentry->d_name.name,
6768 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006769 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006770 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006771
Al Viro1e2e5472018-05-04 08:23:01 -04006772 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006773 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006774
6775out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006776 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006777 if (drop_on_err) {
6778 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006779 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006780 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006781 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006782 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006783
6784out_fail_inode:
6785 unlock_new_inode(inode);
6786 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006787}
6788
Chris Masonc8b97812008-10-29 14:49:59 -04006789static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006790 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006791 size_t pg_offset, u64 extent_offset,
6792 struct btrfs_file_extent_item *item)
6793{
6794 int ret;
6795 struct extent_buffer *leaf = path->nodes[0];
6796 char *tmp;
6797 size_t max_size;
6798 unsigned long inline_size;
6799 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006800 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006801
6802 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006803 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006804 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6805 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006806 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006807 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006808 if (!tmp)
6809 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006810 ptr = btrfs_file_extent_inline_start(item);
6811
6812 read_extent_buffer(leaf, tmp, ptr, inline_size);
6813
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006814 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006815 ret = btrfs_decompress(compress_type, tmp, page,
6816 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006817
6818 /*
6819 * decompression code contains a memset to fill in any space between the end
6820 * of the uncompressed data and the end of max_size in case the decompressed
6821 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6822 * the end of an inline extent and the beginning of the next block, so we
6823 * cover that region here.
6824 */
6825
6826 if (max_size + pg_offset < PAGE_SIZE) {
6827 char *map = kmap(page);
6828 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6829 kunmap(page);
6830 }
Chris Masonc8b97812008-10-29 14:49:59 -04006831 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006832 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006833}
6834
Chris Masond352ac62008-09-29 15:18:18 -04006835/*
6836 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006837 * the ugly parts come from merging extents from the disk with the in-ram
6838 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006839 * where the in-ram extents might be locked pending data=ordered completion.
6840 *
6841 * This also copies inline extents directly into the page.
6842 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006843struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6844 struct page *page,
6845 size_t pg_offset, u64 start, u64 len,
6846 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006847{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006849 int ret;
6850 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006851 u64 extent_start = 0;
6852 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006853 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006854 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006855 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006856 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006857 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006858 struct extent_buffer *leaf;
6859 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006860 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006861 struct extent_map_tree *em_tree = &inode->extent_tree;
6862 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006863 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006864
Chris Mason890871b2009-09-02 16:24:52 -04006865 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006866 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006867 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006868 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006869 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006870
Chris Masona52d9a82007-08-27 16:49:44 -04006871 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006872 if (em->start > start || em->start + em->len <= start)
6873 free_extent_map(em);
6874 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006875 free_extent_map(em);
6876 else
6877 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006878 }
David Sterba172ddd62011-04-21 00:48:27 +02006879 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006880 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006881 err = -ENOMEM;
6882 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006883 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006884 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006885 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006886 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006887 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006888 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006889
6890 if (!path) {
6891 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006892 if (!path) {
6893 err = -ENOMEM;
6894 goto out;
6895 }
6896 /*
6897 * Chances are we'll be called again, so go ahead and do
6898 * readahead
6899 */
David Sterbae4058b52015-11-27 16:31:35 +01006900 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006901 }
6902
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006903 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006904 if (ret < 0) {
6905 err = ret;
6906 goto out;
6907 }
6908
6909 if (ret != 0) {
6910 if (path->slots[0] == 0)
6911 goto not_found;
6912 path->slots[0]--;
6913 }
6914
Chris Mason5f39d392007-10-15 16:14:19 -04006915 leaf = path->nodes[0];
6916 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006917 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006918 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006919 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006920 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006921 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006922 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006923 /*
6924 * If we backup past the first extent we want to move forward
6925 * and see if there is an extent in front of us, otherwise we'll
6926 * say there is a hole for our whole search range which can
6927 * cause problems.
6928 */
6929 extent_end = start;
6930 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006931 }
6932
Chris Mason5f39d392007-10-15 16:14:19 -04006933 found_type = btrfs_file_extent_type(leaf, item);
6934 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006935 if (found_type == BTRFS_FILE_EXTENT_REG ||
6936 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006937 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006938 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006939
6940 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6941 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006942 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6943 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006944 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006945 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006946 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006947
6948 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6949 path->slots[0],
6950 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006951 }
Josef Bacik25a50342013-10-14 12:08:38 -04006952next:
Yan Zheng9036c102008-10-30 14:19:41 -04006953 if (start >= extent_end) {
6954 path->slots[0]++;
6955 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6956 ret = btrfs_next_leaf(root, path);
6957 if (ret < 0) {
6958 err = ret;
6959 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006960 }
Yan Zheng9036c102008-10-30 14:19:41 -04006961 if (ret > 0)
6962 goto not_found;
6963 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006964 }
Yan Zheng9036c102008-10-30 14:19:41 -04006965 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6966 if (found_key.objectid != objectid ||
6967 found_key.type != BTRFS_EXTENT_DATA_KEY)
6968 goto not_found;
6969 if (start + len <= found_key.offset)
6970 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006971 if (start > found_key.offset)
6972 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006973 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006974 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006975 em->len = found_key.offset - start;
6976 goto not_found_em;
6977 }
6978
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006979 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006980 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006981
Yan Zhengd899e052008-10-30 14:25:28 -04006982 if (found_type == BTRFS_FILE_EXTENT_REG ||
6983 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006984 goto insert;
6985 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006986 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006987 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006988 size_t size;
6989 size_t extent_offset;
6990 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006991
Filipe Manana7ffbb592014-06-09 03:48:05 +01006992 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006993 goto out;
Yan689f9342007-10-29 11:41:07 -04006994
Chris Mason514ac8a2014-01-03 21:07:00 -08006995 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006996 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006997 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6998 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006999 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007000 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007001 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007002 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007003 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007004 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007005 if (btrfs_file_extent_compression(leaf, item) !=
7006 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007007 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007008 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007009 if (ret) {
7010 err = ret;
7011 goto out;
7012 }
Chris Masonc8b97812008-10-29 14:49:59 -04007013 } else {
7014 map = kmap(page);
7015 read_extent_buffer(leaf, map + pg_offset, ptr,
7016 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007017 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007018 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007019 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007020 copy_size);
7021 }
Chris Masonc8b97812008-10-29 14:49:59 -04007022 kunmap(page);
7023 }
Chris Mason179e29e2007-11-01 11:28:41 -04007024 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007025 }
Chris Masond1310b22008-01-24 16:13:08 -05007026 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007027 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007028 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007029 }
7030not_found:
7031 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007032 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007033 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007034not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007035 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007036insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007037 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007038 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007039 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007040 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7041 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007042 err = -EIO;
7043 goto out;
7044 }
Chris Masond1310b22008-01-24 16:13:08 -05007045
7046 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007047 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007048 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007049 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007050out:
liubo1abe9b82011-03-24 11:18:59 +00007051
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007052 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007053
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007054 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007055 if (err) {
7056 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007057 return ERR_PTR(err);
7058 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007059 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007060 return em;
7061}
7062
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007063struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7064 struct page *page,
7065 size_t pg_offset, u64 start, u64 len,
7066 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007067{
7068 struct extent_map *em;
7069 struct extent_map *hole_em = NULL;
7070 u64 range_start = start;
7071 u64 end;
7072 u64 found;
7073 u64 found_end;
7074 int err = 0;
7075
7076 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7077 if (IS_ERR(em))
7078 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007079 /*
7080 * If our em maps to:
7081 * - a hole or
7082 * - a pre-alloc extent,
7083 * there might actually be delalloc bytes behind it.
7084 */
7085 if (em->block_start != EXTENT_MAP_HOLE &&
7086 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7087 return em;
7088 else
7089 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007090
7091 /* check to see if we've wrapped (len == -1 or similar) */
7092 end = start + len;
7093 if (end < start)
7094 end = (u64)-1;
7095 else
7096 end -= 1;
7097
7098 em = NULL;
7099
7100 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007101 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007102 end, len, EXTENT_DELALLOC, 1);
7103 found_end = range_start + found;
7104 if (found_end < range_start)
7105 found_end = (u64)-1;
7106
7107 /*
7108 * we didn't find anything useful, return
7109 * the original results from get_extent()
7110 */
7111 if (range_start > end || found_end <= start) {
7112 em = hole_em;
7113 hole_em = NULL;
7114 goto out;
7115 }
7116
7117 /* adjust the range_start to make sure it doesn't
7118 * go backwards from the start they passed in
7119 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307120 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007121 found = found_end - range_start;
7122
7123 if (found > 0) {
7124 u64 hole_start = start;
7125 u64 hole_len = len;
7126
David Sterba172ddd62011-04-21 00:48:27 +02007127 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007128 if (!em) {
7129 err = -ENOMEM;
7130 goto out;
7131 }
7132 /*
7133 * when btrfs_get_extent can't find anything it
7134 * returns one huge hole
7135 *
7136 * make sure what it found really fits our range, and
7137 * adjust to make sure it is based on the start from
7138 * the caller
7139 */
7140 if (hole_em) {
7141 u64 calc_end = extent_map_end(hole_em);
7142
7143 if (calc_end <= start || (hole_em->start > end)) {
7144 free_extent_map(hole_em);
7145 hole_em = NULL;
7146 } else {
7147 hole_start = max(hole_em->start, start);
7148 hole_len = calc_end - hole_start;
7149 }
7150 }
7151 em->bdev = NULL;
7152 if (hole_em && range_start > hole_start) {
7153 /* our hole starts before our delalloc, so we
7154 * have to return just the parts of the hole
7155 * that go until the delalloc starts
7156 */
7157 em->len = min(hole_len,
7158 range_start - hole_start);
7159 em->start = hole_start;
7160 em->orig_start = hole_start;
7161 /*
7162 * don't adjust block start at all,
7163 * it is fixed at EXTENT_MAP_HOLE
7164 */
7165 em->block_start = hole_em->block_start;
7166 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007167 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7168 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007169 } else {
7170 em->start = range_start;
7171 em->len = found;
7172 em->orig_start = range_start;
7173 em->block_start = EXTENT_MAP_DELALLOC;
7174 em->block_len = found;
7175 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007176 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007177 return hole_em;
7178 }
7179out:
7180
7181 free_extent_map(hole_em);
7182 if (err) {
7183 free_extent_map(em);
7184 return ERR_PTR(err);
7185 }
7186 return em;
7187}
7188
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007189static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7190 const u64 start,
7191 const u64 len,
7192 const u64 orig_start,
7193 const u64 block_start,
7194 const u64 block_len,
7195 const u64 orig_block_len,
7196 const u64 ram_bytes,
7197 const int type)
7198{
7199 struct extent_map *em = NULL;
7200 int ret;
7201
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007202 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007203 em = create_io_em(inode, start, len, orig_start,
7204 block_start, block_len, orig_block_len,
7205 ram_bytes,
7206 BTRFS_COMPRESS_NONE, /* compress_type */
7207 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007208 if (IS_ERR(em))
7209 goto out;
7210 }
7211 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7212 len, block_len, type);
7213 if (ret) {
7214 if (em) {
7215 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007216 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007217 start + len - 1, 0);
7218 }
7219 em = ERR_PTR(ret);
7220 }
7221 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007222
7223 return em;
7224}
7225
Josef Bacik4b46fce2010-05-23 11:00:55 -04007226static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7227 u64 start, u64 len)
7228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007230 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007231 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232 struct btrfs_key ins;
7233 u64 alloc_hint;
7234 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007235
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007237 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007238 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007239 if (ret)
7240 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007242 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7243 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007244 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007245 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007246 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007247 btrfs_free_reserved_extent(fs_info, ins.objectid,
7248 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007249
Josef Bacik4b46fce2010-05-23 11:00:55 -04007250 return em;
7251}
7252
Chris Mason46bfbb52010-05-26 11:04:10 -04007253/*
7254 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7255 * block must be cow'd
7256 */
Josef Bacik00361582013-08-14 14:02:47 -04007257noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007258 u64 *orig_start, u64 *orig_block_len,
7259 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007260{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007261 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007262 struct btrfs_path *path;
7263 int ret;
7264 struct extent_buffer *leaf;
7265 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007266 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007267 struct btrfs_file_extent_item *fi;
7268 struct btrfs_key key;
7269 u64 disk_bytenr;
7270 u64 backref_offset;
7271 u64 extent_end;
7272 u64 num_bytes;
7273 int slot;
7274 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007275 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007276
Chris Mason46bfbb52010-05-26 11:04:10 -04007277 path = btrfs_alloc_path();
7278 if (!path)
7279 return -ENOMEM;
7280
David Sterbaf85b7372017-01-20 14:54:07 +01007281 ret = btrfs_lookup_file_extent(NULL, root, path,
7282 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007283 if (ret < 0)
7284 goto out;
7285
7286 slot = path->slots[0];
7287 if (ret == 1) {
7288 if (slot == 0) {
7289 /* can't find the item, must cow */
7290 ret = 0;
7291 goto out;
7292 }
7293 slot--;
7294 }
7295 ret = 0;
7296 leaf = path->nodes[0];
7297 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007298 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007299 key.type != BTRFS_EXTENT_DATA_KEY) {
7300 /* not our file or wrong item type, must cow */
7301 goto out;
7302 }
7303
7304 if (key.offset > offset) {
7305 /* Wrong offset, must cow */
7306 goto out;
7307 }
7308
7309 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7310 found_type = btrfs_file_extent_type(leaf, fi);
7311 if (found_type != BTRFS_FILE_EXTENT_REG &&
7312 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7313 /* not a regular extent, must cow */
7314 goto out;
7315 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007316
7317 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7318 goto out;
7319
Miao Xiee77751a2013-12-27 21:11:50 +08007320 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7321 if (extent_end <= offset)
7322 goto out;
7323
Chris Mason46bfbb52010-05-26 11:04:10 -04007324 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007325 if (disk_bytenr == 0)
7326 goto out;
7327
7328 if (btrfs_file_extent_compression(leaf, fi) ||
7329 btrfs_file_extent_encryption(leaf, fi) ||
7330 btrfs_file_extent_other_encoding(leaf, fi))
7331 goto out;
7332
Ethan Lien78d42952018-05-17 14:58:29 +08007333 /*
7334 * Do the same check as in btrfs_cross_ref_exist but without the
7335 * unnecessary search.
7336 */
7337 if (btrfs_file_extent_generation(leaf, fi) <=
7338 btrfs_root_last_snapshot(&root->root_item))
7339 goto out;
7340
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 backref_offset = btrfs_file_extent_offset(leaf, fi);
7342
Josef Bacik7ee9e442013-06-21 16:37:03 -04007343 if (orig_start) {
7344 *orig_start = key.offset - backref_offset;
7345 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7346 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7347 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007348
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007349 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007351
7352 num_bytes = min(offset + *len, extent_end) - offset;
7353 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7354 u64 range_end;
7355
Jeff Mahoneyda170662016-06-15 09:22:56 -04007356 range_end = round_up(offset + num_bytes,
7357 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007358 ret = test_range_bit(io_tree, offset, range_end,
7359 EXTENT_DELALLOC, 0, NULL);
7360 if (ret) {
7361 ret = -EAGAIN;
7362 goto out;
7363 }
7364 }
7365
Josef Bacik1bda19e2013-10-18 12:10:36 -04007366 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007367
7368 /*
7369 * look for other files referencing this extent, if we
7370 * find any we must cow
7371 */
Josef Bacik00361582013-08-14 14:02:47 -04007372
Liu Boe4c3b2d2017-01-30 12:25:28 -08007373 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007374 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007375 if (ret) {
7376 ret = 0;
7377 goto out;
7378 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007379
7380 /*
7381 * adjust disk_bytenr and num_bytes to cover just the bytes
7382 * in this extent we are about to write. If there
7383 * are any csums in that range we have to cow in order
7384 * to keep the csums correct
7385 */
7386 disk_bytenr += backref_offset;
7387 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007388 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7389 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007390 /*
7391 * all of the above have passed, it is safe to overwrite this extent
7392 * without cow
7393 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007394 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007395 ret = 1;
7396out:
7397 btrfs_free_path(path);
7398 return ret;
7399}
7400
Josef Bacikeb838e72012-07-31 16:28:48 -04007401static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7402 struct extent_state **cached_state, int writing)
7403{
7404 struct btrfs_ordered_extent *ordered;
7405 int ret = 0;
7406
7407 while (1) {
7408 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007409 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007410 /*
7411 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007412 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007413 * extents in this range.
7414 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007415 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007416 lockend - lockstart + 1);
7417
7418 /*
7419 * We need to make sure there are no buffered pages in this
7420 * range either, we could have raced between the invalidate in
7421 * generic_file_direct_write and locking the extent. The
7422 * invalidate needs to happen so that reads after a write do not
7423 * get stale data.
7424 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007425 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007426 (!writing || !filemap_range_has_page(inode->i_mapping,
7427 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007428 break;
7429
7430 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007431 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007432
7433 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007434 /*
7435 * If we are doing a DIO read and the ordered extent we
7436 * found is for a buffered write, we can not wait for it
7437 * to complete and retry, because if we do so we can
7438 * deadlock with concurrent buffered writes on page
7439 * locks. This happens only if our DIO read covers more
7440 * than one extent map, if at this point has already
7441 * created an ordered extent for a previous extent map
7442 * and locked its range in the inode's io tree, and a
7443 * concurrent write against that previous extent map's
7444 * range and this range started (we unlock the ranges
7445 * in the io tree only when the bios complete and
7446 * buffered writes always lock pages before attempting
7447 * to lock range in the io tree).
7448 */
7449 if (writing ||
7450 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7451 btrfs_start_ordered_extent(inode, ordered, 1);
7452 else
7453 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007454 btrfs_put_ordered_extent(ordered);
7455 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007456 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007457 * We could trigger writeback for this range (and wait
7458 * for it to complete) and then invalidate the pages for
7459 * this range (through invalidate_inode_pages2_range()),
7460 * but that can lead us to a deadlock with a concurrent
7461 * call to readpages() (a buffered read or a defrag call
7462 * triggered a readahead) on a page lock due to an
7463 * ordered dio extent we created before but did not have
7464 * yet a corresponding bio submitted (whence it can not
7465 * complete), which makes readpages() wait for that
7466 * ordered extent to complete while holding a lock on
7467 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007469 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007470 }
7471
Filipe Mananaade77022016-02-18 14:28:55 +00007472 if (ret)
7473 break;
7474
Josef Bacikeb838e72012-07-31 16:28:48 -04007475 cond_resched();
7476 }
7477
7478 return ret;
7479}
7480
Liu Bo6f9994d2017-01-31 07:50:22 -08007481/* The callers of this must take lock_extent() */
7482static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7483 u64 orig_start, u64 block_start,
7484 u64 block_len, u64 orig_block_len,
7485 u64 ram_bytes, int compress_type,
7486 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007487{
7488 struct extent_map_tree *em_tree;
7489 struct extent_map *em;
7490 struct btrfs_root *root = BTRFS_I(inode)->root;
7491 int ret;
7492
Liu Bo6f9994d2017-01-31 07:50:22 -08007493 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7494 type == BTRFS_ORDERED_COMPRESSED ||
7495 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007496 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007497
Josef Bacik69ffb542012-09-11 15:40:07 -04007498 em_tree = &BTRFS_I(inode)->extent_tree;
7499 em = alloc_extent_map();
7500 if (!em)
7501 return ERR_PTR(-ENOMEM);
7502
7503 em->start = start;
7504 em->orig_start = orig_start;
7505 em->len = len;
7506 em->block_len = block_len;
7507 em->block_start = block_start;
7508 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007509 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007510 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007511 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007512 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007513 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007514 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007515 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007516 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7517 em->compress_type = compress_type;
7518 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007519
7520 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007521 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007522 em->start + em->len - 1, 0);
7523 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007524 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007525 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007526 /*
7527 * The caller has taken lock_extent(), who could race with us
7528 * to add em?
7529 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007530 } while (ret == -EEXIST);
7531
7532 if (ret) {
7533 free_extent_map(em);
7534 return ERR_PTR(ret);
7535 }
7536
Liu Bo6f9994d2017-01-31 07:50:22 -08007537 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007538 return em;
7539}
7540
Josef Bacik4b46fce2010-05-23 11:00:55 -04007541static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7542 struct buffer_head *bh_result, int create)
7543{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007544 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007545 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007546 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307547 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007548 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007549 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007550 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007551 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007552 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007553
Miao Xie172a5042013-02-21 02:48:22 -07007554 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007555 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007556 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007557 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007558
Josef Bacikc3298612012-08-03 16:49:19 -04007559 lockstart = start;
7560 lockend = start + len - 1;
7561
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007562 if (current->journal_info) {
7563 /*
7564 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007565 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007566 * confused.
7567 */
chandan50745b02015-08-28 21:10:13 +05307568 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007569 current->journal_info = NULL;
7570 }
7571
Josef Bacikeb838e72012-07-31 16:28:48 -04007572 /*
7573 * If this errors out it's because we couldn't invalidate pagecache for
7574 * this range and we need to fallback to buffered.
7575 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007576 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7577 create)) {
7578 ret = -ENOTBLK;
7579 goto err;
7580 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007581
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007582 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 if (IS_ERR(em)) {
7584 ret = PTR_ERR(em);
7585 goto unlock_err;
7586 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587
7588 /*
7589 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7590 * io. INLINE is special, and we could probably kludge it in here, but
7591 * it's still buffered so for safety lets just fall back to the generic
7592 * buffered path.
7593 *
7594 * For COMPRESSED we _have_ to read the entire extent in so we can
7595 * decompress it, so there will be buffering required no matter what we
7596 * do, so go ahead and fallback to buffered.
7597 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007598 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007599 * to buffered IO. Don't blame me, this is the price we pay for using
7600 * the generic code.
7601 */
7602 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7603 em->block_start == EXTENT_MAP_INLINE) {
7604 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007605 ret = -ENOTBLK;
7606 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007607 }
7608
7609 /* Just a good old fashioned hole, return */
7610 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7611 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7612 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007613 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007614 }
7615
7616 /*
7617 * We don't allocate a new extent in the following cases
7618 *
7619 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7620 * existing extent.
7621 * 2) The extent is marked as PREALLOC. We're good to go here and can
7622 * just use the extent.
7623 *
7624 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007625 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007626 len = min(len, em->len - (start - em->start));
7627 lockstart = start + len;
7628 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007629 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007630
7631 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7632 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7633 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007634 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007635 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007636
7637 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7638 type = BTRFS_ORDERED_PREALLOC;
7639 else
7640 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007641 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007642 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007643
Josef Bacik00361582013-08-14 14:02:47 -04007644 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007645 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007646 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007647 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007648
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007649 em2 = btrfs_create_dio_extent(inode, start, len,
7650 orig_start, block_start,
7651 len, orig_block_len,
7652 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007653 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007654 if (type == BTRFS_ORDERED_PREALLOC) {
7655 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007656 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007657 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007658 if (em2 && IS_ERR(em2)) {
7659 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007660 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007661 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007662 /*
7663 * For inode marked NODATACOW or extent marked PREALLOC,
7664 * use the existing or preallocated extent, so does not
7665 * need to adjust btrfs_space_info's bytes_may_use.
7666 */
7667 btrfs_free_reserved_data_space_noquota(inode,
7668 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007669 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007670 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671 }
Josef Bacik00361582013-08-14 14:02:47 -04007672
Chris Mason46bfbb52010-05-26 11:04:10 -04007673 /*
7674 * this will cow the extent, reset the len in case we changed
7675 * it above
7676 */
7677 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007678 free_extent_map(em);
7679 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007680 if (IS_ERR(em)) {
7681 ret = PTR_ERR(em);
7682 goto unlock_err;
7683 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007684 len = min(len, em->len - (start - em->start));
7685unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7687 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007688 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689 bh_result->b_bdev = em->bdev;
7690 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007691 if (create) {
7692 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7693 set_buffer_new(bh_result);
7694
7695 /*
7696 * Need to update the i_size under the extent lock so buffered
7697 * readers will get the updated i_size when we unlock.
7698 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007699 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007700 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007701
chandan50745b02015-08-28 21:10:13 +05307702 WARN_ON(dio_data->reserve < len);
7703 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007704 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307705 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007706 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707
Josef Bacikeb838e72012-07-31 16:28:48 -04007708 /*
7709 * In the case of write we need to clear and unlock the entire range,
7710 * in the case of read we need to unlock only the end area that we
7711 * aren't using if there is any left over space.
7712 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007713 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007714 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7715 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007716 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007717 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007718 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007719 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007720
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721 free_extent_map(em);
7722
7723 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007724
7725unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007726 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007727 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007728err:
chandan50745b02015-08-28 21:10:13 +05307729 if (dio_data)
7730 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007731 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007732}
7733
Omar Sandoval58efbc92017-08-22 23:45:59 -07007734static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7735 struct bio *bio,
7736 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007737{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007738 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007739 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007740
Mike Christie37226b22016-06-05 14:31:52 -05007741 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007742
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007743 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007744 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007745 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007746
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007747 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007748
Miao Xie8b110e32014-09-12 18:44:03 +08007749 return ret;
7750}
7751
7752static int btrfs_check_dio_repairable(struct inode *inode,
7753 struct bio *failed_bio,
7754 struct io_failure_record *failrec,
7755 int failed_mirror)
7756{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007758 int num_copies;
7759
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007760 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007761 if (num_copies == 1) {
7762 /*
7763 * we only have a single copy of the data, so don't bother with
7764 * all the retry and error correction code that follows. no
7765 * matter what the error is, it is very likely to persist.
7766 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007767 btrfs_debug(fs_info,
7768 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7769 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007770 return 0;
7771 }
7772
7773 failrec->failed_mirror = failed_mirror;
7774 failrec->this_mirror++;
7775 if (failrec->this_mirror == failed_mirror)
7776 failrec->this_mirror++;
7777
7778 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007779 btrfs_debug(fs_info,
7780 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7781 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007782 return 0;
7783 }
7784
7785 return 1;
7786}
7787
Omar Sandoval58efbc92017-08-22 23:45:59 -07007788static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7789 struct page *page, unsigned int pgoff,
7790 u64 start, u64 end, int failed_mirror,
7791 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007792{
7793 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007794 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7795 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007796 struct bio *bio;
7797 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007798 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007799 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007800 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007801 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007802 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007803
Mike Christie37226b22016-06-05 14:31:52 -05007804 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007805
7806 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7807 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007808 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007809
7810 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7811 failed_mirror);
7812 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007813 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007814 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007815 }
7816
Liu Bo17347ce2017-05-15 15:33:27 -07007817 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007818 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007819 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007820 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007821 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007822
7823 isector = start - btrfs_io_bio(failed_bio)->logical;
7824 isector >>= inode->i_sb->s_blocksize_bits;
7825 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307826 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007827 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007828
7829 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007830 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007831 read_mode, failrec->this_mirror, failrec->in_validation);
7832
Omar Sandoval58efbc92017-08-22 23:45:59 -07007833 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7834 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007835 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007836 bio_put(bio);
7837 }
7838
Omar Sandoval58efbc92017-08-22 23:45:59 -07007839 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007840}
7841
7842struct btrfs_retry_complete {
7843 struct completion done;
7844 struct inode *inode;
7845 u64 start;
7846 int uptodate;
7847};
7848
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007849static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007850{
7851 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007852 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007853 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007854 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007855 int i;
7856
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007857 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007858 goto end;
7859
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307860 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007861 io_tree = &BTRFS_I(inode)->io_tree;
7862 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007863 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307864
Miao Xie8b110e32014-09-12 18:44:03 +08007865 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007866 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007867 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007868 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7869 io_tree, done->start, bvec->bv_page,
7870 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007871end:
7872 complete(&done->done);
7873 bio_put(bio);
7874}
7875
Omar Sandoval58efbc92017-08-22 23:45:59 -07007876static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7877 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007878{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307879 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007880 struct bio_vec bvec;
7881 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007882 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307884 unsigned int pgoff;
7885 u32 sectorsize;
7886 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007887 blk_status_t ret;
7888 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007889
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307890 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007891 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307892
Miao Xiec1dc0892014-09-12 18:43:56 +08007893 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007894 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007895 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007896
Liu Bo17347ce2017-05-15 15:33:27 -07007897 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7898 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7899 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307900
7901next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007902 done.uptodate = 0;
7903 done.start = start;
7904 init_completion(&done.done);
7905
Liu Bo17347ce2017-05-15 15:33:27 -07007906 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307907 pgoff, start, start + sectorsize - 1,
7908 io_bio->mirror_num,
7909 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007910 if (ret) {
7911 err = ret;
7912 goto next;
7913 }
Miao Xie8b110e32014-09-12 18:44:03 +08007914
David Sterba9c17f6c2017-07-19 19:26:45 +02007915 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007916
7917 if (!done.uptodate) {
7918 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307919 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007920 }
7921
Liu Bo629ebf42017-05-15 17:20:07 -07007922next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923 start += sectorsize;
7924
Liu Bo97bf5a52017-04-07 13:11:10 -07007925 nr_sectors--;
7926 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307927 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007928 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929 goto next_block_or_try_again;
7930 }
Miao Xie8b110e32014-09-12 18:44:03 +08007931 }
7932
Liu Bo629ebf42017-05-15 17:20:07 -07007933 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007934}
7935
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007936static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007937{
7938 struct btrfs_retry_complete *done = bio->bi_private;
7939 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007940 struct extent_io_tree *io_tree, *failure_tree;
7941 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007942 struct bio_vec *bvec;
7943 int uptodate;
7944 int ret;
7945 int i;
7946
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007947 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007948 goto end;
7949
7950 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307951
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007953 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307954
Josef Bacik7870d082017-05-05 11:57:15 -04007955 io_tree = &BTRFS_I(inode)->io_tree;
7956 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7957
David Sterbac09abff2017-07-13 18:10:07 +02007958 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007959 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007960 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7961 bvec->bv_offset, done->start,
7962 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007963 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007964 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7965 failure_tree, io_tree, done->start,
7966 bvec->bv_page,
7967 btrfs_ino(BTRFS_I(inode)),
7968 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007969 else
7970 uptodate = 0;
7971 }
7972
7973 done->uptodate = uptodate;
7974end:
7975 complete(&done->done);
7976 bio_put(bio);
7977}
7978
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007979static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7980 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007981{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007983 struct bio_vec bvec;
7984 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007985 struct btrfs_retry_complete done;
7986 u64 start;
7987 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988 u32 sectorsize;
7989 int nr_sectors;
7990 unsigned int pgoff;
7991 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007992 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007993 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007994 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007995
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007997 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998
Omar Sandoval58efbc92017-08-22 23:45:59 -07007999 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008000 start = io_bio->logical;
8001 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008002 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008003
Liu Bo17347ce2017-05-15 15:33:27 -07008004 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8005 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006
Liu Bo17347ce2017-05-15 15:33:27 -07008007 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308008next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008009 if (uptodate) {
8010 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8011 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8012 bvec.bv_page, pgoff, start, sectorsize);
8013 if (likely(!ret))
8014 goto next;
8015 }
Miao Xie8b110e32014-09-12 18:44:03 +08008016try_again:
8017 done.uptodate = 0;
8018 done.start = start;
8019 init_completion(&done.done);
8020
Omar Sandoval58efbc92017-08-22 23:45:59 -07008021 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8022 pgoff, start, start + sectorsize - 1,
8023 io_bio->mirror_num, btrfs_retry_endio,
8024 &done);
8025 if (status) {
8026 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008027 goto next;
8028 }
8029
David Sterba9c17f6c2017-07-19 19:26:45 +02008030 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008031
8032 if (!done.uptodate) {
8033 /* We might have another mirror, so try again */
8034 goto try_again;
8035 }
8036next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 offset += sectorsize;
8038 start += sectorsize;
8039
8040 ASSERT(nr_sectors);
8041
Liu Bo97bf5a52017-04-07 13:11:10 -07008042 nr_sectors--;
8043 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008045 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308046 goto next_block;
8047 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008048 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008049
8050 return err;
8051}
8052
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008053static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8054 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008055{
8056 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8057
8058 if (skip_csum) {
8059 if (unlikely(err))
8060 return __btrfs_correct_data_nocsum(inode, io_bio);
8061 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008062 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008063 } else {
8064 return __btrfs_subio_endio_read(inode, io_bio, err);
8065 }
8066}
8067
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008068static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008069{
8070 struct btrfs_dio_private *dip = bio->bi_private;
8071 struct inode *inode = dip->inode;
8072 struct bio *dio_bio;
8073 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008074 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008075
Liu Bo99c4e3b2017-09-15 15:06:51 -06008076 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008077 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008078
Josef Bacik4b46fce2010-05-23 11:00:55 -04008079 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008080 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008081 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008082
Josef Bacik4b46fce2010-05-23 11:00:55 -04008083 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008084
Liu Bo99c4e3b2017-09-15 15:06:51 -06008085 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008086 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008087
8088 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008089 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008090 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008091}
8092
Qu Wenruo524272602017-03-08 10:25:52 +08008093static void __endio_write_update_ordered(struct inode *inode,
8094 const u64 offset, const u64 bytes,
8095 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008096{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008097 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008098 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008099 struct btrfs_workqueue *wq;
8100 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008101 u64 ordered_offset = offset;
8102 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008103 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008104
Qu Wenruo524272602017-03-08 10:25:52 +08008105 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8106 wq = fs_info->endio_freespace_worker;
8107 func = btrfs_freespace_write_helper;
8108 } else {
8109 wq = fs_info->endio_write_workers;
8110 func = btrfs_endio_write_helper;
8111 }
8112
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008113 while (ordered_offset < offset + bytes) {
8114 last_offset = ordered_offset;
8115 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8116 &ordered_offset,
8117 ordered_bytes,
8118 uptodate)) {
8119 btrfs_init_work(&ordered->work, func,
8120 finish_ordered_fn,
8121 NULL, NULL);
8122 btrfs_queue_work(wq, &ordered->work);
8123 }
8124 /*
8125 * If btrfs_dec_test_ordered_pending does not find any ordered
8126 * extent in the range, we can exit.
8127 */
8128 if (ordered_offset == last_offset)
8129 return;
8130 /*
8131 * Our bio might span multiple ordered extents. In this case
8132 * we keep goin until we have accounted the whole dio.
8133 */
8134 if (ordered_offset < offset + bytes) {
8135 ordered_bytes = offset + bytes - ordered_offset;
8136 ordered = NULL;
8137 }
Chris Mason163cf092010-11-28 19:56:33 -05008138 }
Filipe Manana14543772015-11-24 16:23:54 +00008139}
8140
8141static void btrfs_endio_direct_write(struct bio *bio)
8142{
8143 struct btrfs_dio_private *dip = bio->bi_private;
8144 struct bio *dio_bio = dip->dio_bio;
8145
Qu Wenruo524272602017-03-08 10:25:52 +08008146 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008147 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148
Josef Bacik4b46fce2010-05-23 11:00:55 -04008149 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008150
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008151 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008152 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008153 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008154}
8155
David Sterbad0ee3932018-03-08 14:35:48 +01008156static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008157 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008158{
Josef Bacikc6100a42017-05-05 11:57:13 -04008159 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008160 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008161 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008162 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008163 return 0;
8164}
8165
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008166static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008167{
8168 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008169 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008170
Miao Xie8b110e32014-09-12 18:44:03 +08008171 if (err)
8172 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008173 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008174 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8175 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008176 (unsigned long long)bio->bi_iter.bi_sector,
8177 bio->bi_iter.bi_size, err);
8178
8179 if (dip->subio_endio)
8180 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008181
8182 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008183 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008184 * We want to perceive the errors flag being set before
8185 * decrementing the reference count. We don't need a barrier
8186 * since atomic operations with a return value are fully
8187 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008188 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008189 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008190 }
8191
8192 /* if there are more bios still pending for this dio, just exit */
8193 if (!atomic_dec_and_test(&dip->pending_bios))
8194 goto out;
8195
Chris Mason9be33952013-05-17 18:30:14 -04008196 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008197 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008198 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008199 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008200 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008201 }
8202out:
8203 bio_put(bio);
8204}
8205
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008206static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008207 struct btrfs_dio_private *dip,
8208 struct bio *bio,
8209 u64 file_offset)
8210{
8211 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8212 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008213 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008214
8215 /*
8216 * We load all the csum data we need when we submit
8217 * the first bio to reduce the csum tree search and
8218 * contention.
8219 */
8220 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008221 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008222 file_offset);
8223 if (ret)
8224 return ret;
8225 }
8226
8227 if (bio == dip->orig_bio)
8228 return 0;
8229
8230 file_offset -= dip->logical_offset;
8231 file_offset >>= inode->i_sb->s_blocksize_bits;
8232 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8233
8234 return 0;
8235}
8236
David Sterbad0ee3932018-03-08 14:35:48 +01008237static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8238 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008239{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008240 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008241 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008242 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008243 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008244
Liu Bo4c274bc2017-11-01 17:19:27 -06008245 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008246 if (async_submit)
8247 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8248
Josef Bacik5fd02042012-05-02 14:00:54 -04008249 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008250 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008251 if (ret)
8252 goto err;
8253 }
Miao Xiee65e1532010-11-22 03:04:43 +00008254
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008255 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008256 goto map;
8257
8258 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008259 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8260 file_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01008261 btrfs_submit_bio_start_direct_io,
8262 btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008263 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008264 } else if (write) {
8265 /*
8266 * If we aren't doing async submit, calculate the csum of the
8267 * bio now.
8268 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008269 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008270 if (ret)
8271 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008272 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008273 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008274 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008275 if (ret)
8276 goto err;
8277 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008278map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008279 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008280err:
Miao Xiee65e1532010-11-22 03:04:43 +00008281 return ret;
8282}
8283
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008284static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008285{
8286 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008287 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008288 struct bio *bio;
8289 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008290 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008291 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008292 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008293 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008294 u64 submit_len;
8295 int clone_offset = 0;
8296 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308297 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008298 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008299
Kent Overstreet4f024f32013-10-11 15:44:27 -07008300 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008301 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008302 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8303 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008304 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008305 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008306
Liu Bo725130b2017-05-16 09:51:39 -07008307 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008308 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008309 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008310 goto submit;
8311 }
8312
David Woodhouse53b381b2013-01-29 18:40:14 -05008313 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008314 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008315 async_submit = 0;
8316 else
8317 async_submit = 1;
8318
Liu Bo725130b2017-05-16 09:51:39 -07008319 /* bio split */
8320 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008321 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008322 do {
Liu Bo725130b2017-05-16 09:51:39 -07008323 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008324
Liu Bo725130b2017-05-16 09:51:39 -07008325 /*
8326 * This will never fail as it's passing GPF_NOFS and
8327 * the allocation is backed by btrfs_bioset.
8328 */
Liu Boe4770942017-05-16 10:57:14 -07008329 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008330 clone_len);
8331 bio->bi_private = dip;
8332 bio->bi_end_io = btrfs_end_dio_bio;
8333 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008334
Liu Bo725130b2017-05-16 09:51:39 -07008335 ASSERT(submit_len >= clone_len);
8336 submit_len -= clone_len;
8337 if (submit_len == 0)
8338 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008339
Liu Bo725130b2017-05-16 09:51:39 -07008340 /*
8341 * Increase the count before we submit the bio so we know
8342 * the end IO handler won't happen before we increase the
8343 * count. Otherwise, the dip might get freed before we're
8344 * done setting it up.
8345 */
8346 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008347
David Sterbad0ee3932018-03-08 14:35:48 +01008348 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008349 async_submit);
8350 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008351 bio_put(bio);
8352 atomic_dec(&dip->pending_bios);
8353 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008354 }
Liu Bo725130b2017-05-16 09:51:39 -07008355
8356 clone_offset += clone_len;
8357 start_sector += clone_len >> 9;
8358 file_offset += clone_len;
8359
8360 map_length = submit_len;
8361 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8362 start_sector << 9, &map_length, NULL, 0);
8363 if (ret)
8364 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008365 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008366
Josef Bacik02f57c72011-04-06 14:25:44 -04008367submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008368 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008369 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008370 return 0;
8371
8372 bio_put(bio);
8373out_err:
8374 dip->errors = 1;
8375 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008376 * Before atomic variable goto zero, we must make sure dip->errors is
8377 * perceived to be set. This ordering is ensured by the fact that an
8378 * atomic operations with a return value are fully ordered as per
8379 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008380 */
Miao Xiee65e1532010-11-22 03:04:43 +00008381 if (atomic_dec_and_test(&dip->pending_bios))
8382 bio_io_error(dip->orig_bio);
8383
8384 /* bio_end_io() will handle error, so we needn't return it */
8385 return 0;
8386}
8387
Mike Christie8a4c1e42016-06-05 14:31:50 -05008388static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8389 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008390{
Filipe Manana61de7182015-07-01 12:13:10 +01008391 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008392 struct bio *bio = NULL;
8393 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008394 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008395 int ret = 0;
8396
David Sterba8b6c1d52017-06-02 17:48:13 +02008397 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008398
Miao Xiec1dc0892014-09-12 18:43:56 +08008399 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008400 if (!dip) {
8401 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008402 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008403 }
8404
8405 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008406 dip->inode = inode;
8407 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008408 dip->bytes = dio_bio->bi_iter.bi_size;
8409 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008410 bio->bi_private = dip;
8411 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008412 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008413 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008414 io_bio = btrfs_io_bio(bio);
8415 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008416
Miao Xiec1dc0892014-09-12 18:43:56 +08008417 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008418 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008419 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008420 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008421 dip->subio_endio = btrfs_subio_endio_read;
8422 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008423
Filipe Mananaf28a4922015-12-08 19:23:20 +00008424 /*
8425 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8426 * even if we fail to submit a bio, because in such case we do the
8427 * corresponding error handling below and it must not be done a second
8428 * time by btrfs_direct_IO().
8429 */
8430 if (write) {
8431 struct btrfs_dio_data *dio_data = current->journal_info;
8432
8433 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8434 dip->bytes;
8435 dio_data->unsubmitted_oe_range_start =
8436 dio_data->unsubmitted_oe_range_end;
8437 }
8438
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008439 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008440 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008441 return;
Chris Mason9be33952013-05-17 18:30:14 -04008442
Liu Bo3892ac92017-04-17 15:00:28 -07008443 if (io_bio->end_io)
8444 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008445
Josef Bacik4b46fce2010-05-23 11:00:55 -04008446free_ordered:
8447 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008448 * If we arrived here it means either we failed to submit the dip
8449 * or we either failed to clone the dio_bio or failed to allocate the
8450 * dip. If we cloned the dio_bio and allocated the dip, we can just
8451 * call bio_endio against our io_bio so that we get proper resource
8452 * cleanup if we fail to submit the dip, otherwise, we must do the
8453 * same as btrfs_endio_direct_[write|read] because we can't call these
8454 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008455 */
Liu Bo3892ac92017-04-17 15:00:28 -07008456 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008457 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008458 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008459 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008460 * and all the cleanup and final put for dio_bio (through
8461 * dio_end_io()).
8462 */
8463 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008464 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008465 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008466 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008467 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008468 file_offset,
8469 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008470 false);
Filipe Manana14543772015-11-24 16:23:54 +00008471 else
Filipe Manana61de7182015-07-01 12:13:10 +01008472 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8473 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008474
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008475 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008476 /*
8477 * Releases and cleans up our dio_bio, no need to bio_put()
8478 * nor bio_endio()/bio_io_error() against dio_bio.
8479 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008480 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008481 }
Liu Bo3892ac92017-04-17 15:00:28 -07008482 if (bio)
8483 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008484 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008485}
8486
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008487static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008488 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008489{
8490 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008491 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008492 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008493 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008494
8495 if (offset & blocksize_mask)
8496 goto out;
8497
Al Viro28060d52014-03-22 05:15:17 -04008498 if (iov_iter_alignment(iter) & blocksize_mask)
8499 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008500
Al Viro28060d52014-03-22 05:15:17 -04008501 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008502 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008503 return 0;
8504 /*
8505 * Check to make sure we don't have duplicate iov_base's in this
8506 * iovec, if so return EINVAL, otherwise we'll get csum errors
8507 * when reading back.
8508 */
8509 for (seg = 0; seg < iter->nr_segs; seg++) {
8510 for (i = seg + 1; i < iter->nr_segs; i++) {
8511 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008512 goto out;
8513 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008514 }
8515 retval = 0;
8516out:
8517 return retval;
8518}
Josef Bacikeb838e72012-07-31 16:28:48 -04008519
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008520static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008521{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522 struct file *file = iocb->ki_filp;
8523 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008524 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308525 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008526 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008527 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008528 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008529 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008530 bool wakeup = true;
8531 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008532 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008533
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008534 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008535 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008536
Jens Axboefe0f07d2015-04-15 17:05:48 -06008537 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008538
Josef Bacik0e267c42013-07-02 10:38:02 -04008539 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008540 * The generic stuff only does filemap_write_and_wait_range, which
8541 * isn't enough if we've written compressed pages to this area, so
8542 * we need to flush the dirty pages again to make absolutely sure
8543 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008544 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008545 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008546 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8547 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008548 filemap_fdatawrite_range(inode->i_mapping, offset,
8549 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008550
Omar Sandoval6f673762015-03-16 04:33:52 -07008551 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008552 /*
8553 * If the write DIO is beyond the EOF, we need update
8554 * the isize, but it is protected by i_mutex. So we can
8555 * not unlock the i_mutex at this case.
8556 */
8557 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008558 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008559 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008560 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008561 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8562 ret = -EAGAIN;
8563 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008564 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008565 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8566 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008567 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008568 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008569
8570 /*
8571 * We need to know how many extents we reserved so that we can
8572 * do the accounting properly if we go over the number we
8573 * originally calculated. Abuse current->journal_info for this.
8574 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008575 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008576 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008577 dio_data.unsubmitted_oe_range_start = (u64)offset;
8578 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308579 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308580 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008581 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8582 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008583 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008584 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8585 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008586 }
8587
Omar Sandoval17f8c842015-03-16 04:33:50 -07008588 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008589 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008590 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008591 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008592 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308593 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008594 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008595 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308596 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008597 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008598 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008599 /*
8600 * On error we might have left some ordered extents
8601 * without submitting corresponding bios for them, so
8602 * cleanup them up to avoid other tasks getting them
8603 * and waiting for them to complete forever.
8604 */
8605 if (dio_data.unsubmitted_oe_range_start <
8606 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008607 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008608 dio_data.unsubmitted_oe_range_start,
8609 dio_data.unsubmitted_oe_range_end -
8610 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008611 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008612 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008613 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008614 offset, count - (size_t)ret, true);
8615 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008616 }
Miao Xie38851cc2013-02-08 07:04:11 +00008617out:
Miao Xie2e60a512013-02-08 07:01:08 +00008618 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008619 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008620 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008621 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008622
Qu Wenruo364ecf32017-02-27 15:10:38 +08008623 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008624 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008625}
8626
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008627#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8628
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008629static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8630 __u64 start, __u64 len)
8631{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008632 int ret;
8633
8634 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8635 if (ret)
8636 return ret;
8637
David Sterba2135fb92017-06-23 04:09:57 +02008638 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008639}
8640
Chris Mason9ebefb182007-06-15 13:50:00 -04008641int btrfs_readpage(struct file *file, struct page *page)
8642{
Chris Masond1310b22008-01-24 16:13:08 -05008643 struct extent_io_tree *tree;
8644 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008645 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008646}
Chris Mason1832a6d2007-12-21 16:27:21 -05008647
Chris Mason39279cc2007-06-12 06:35:45 -04008648static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8649{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008650 struct inode *inode = page->mapping->host;
8651 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008652
8653 if (current->flags & PF_MEMALLOC) {
8654 redirty_page_for_writepage(wbc, page);
8655 unlock_page(page);
8656 return 0;
8657 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008658
8659 /*
8660 * If we are under memory pressure we will call this directly from the
8661 * VM, we need to make sure we have the inode referenced for the ordered
8662 * extent. If not just return like we didn't do anything.
8663 */
8664 if (!igrab(inode)) {
8665 redirty_page_for_writepage(wbc, page);
8666 return AOP_WRITEPAGE_ACTIVATE;
8667 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008668 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008669 btrfs_add_delayed_iput(inode);
8670 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008671}
Chris Mason39279cc2007-06-12 06:35:45 -04008672
Eric Sandeen48a3b632013-04-25 20:41:01 +00008673static int btrfs_writepages(struct address_space *mapping,
8674 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008675{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008676 return extent_writepages(mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008677}
8678
Chris Mason3ab2fb52007-11-08 10:59:22 -05008679static int
8680btrfs_readpages(struct file *file, struct address_space *mapping,
8681 struct list_head *pages, unsigned nr_pages)
8682{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008683 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008684}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008685
Chris Masone6dcd2d2008-07-17 12:53:50 -04008686static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008687{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008688 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008689 if (ret == 1) {
8690 ClearPagePrivate(page);
8691 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008692 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008693 }
8694 return ret;
8695}
Chris Mason39279cc2007-06-12 06:35:45 -04008696
Chris Masone6dcd2d2008-07-17 12:53:50 -04008697static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8698{
Chris Mason98509cf2008-09-11 15:51:43 -04008699 if (PageWriteback(page) || PageDirty(page))
8700 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008701 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008702}
8703
Lukas Czernerd47992f2013-05-21 23:17:23 -04008704static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8705 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008706{
Josef Bacik5fd02042012-05-02 14:00:54 -04008707 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008708 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008709 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008710 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008711 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008712 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308713 u64 start;
8714 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008715 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008716
Chris Mason8b62b722009-09-02 16:53:46 -04008717 /*
8718 * we have the page locked, so new writeback can't start,
8719 * and the dirty bit won't be cleared while we are here.
8720 *
8721 * Wait for IO on this page so that we can safely clear
8722 * the PagePrivate2 bit and do ordered accounting
8723 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008724 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008725
Josef Bacik5fd02042012-05-02 14:00:54 -04008726 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008727 if (offset) {
8728 btrfs_releasepage(page, GFP_NOFS);
8729 return;
8730 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008731
8732 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008733 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308734again:
8735 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008736 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308737 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008738 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308739 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008740 /*
8741 * IO on this page will never be started, so we need
8742 * to account for any ordered extents now
8743 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008744 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308745 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008746 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008747 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008748 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008749 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008750 /*
8751 * whoever cleared the private bit is responsible
8752 * for the finish_ordered_io
8753 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008754 if (TestClearPagePrivate2(page)) {
8755 struct btrfs_ordered_inode_tree *tree;
8756 u64 new_len;
8757
8758 tree = &BTRFS_I(inode)->ordered_tree;
8759
8760 spin_lock_irq(&tree->lock);
8761 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308762 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008763 if (new_len < ordered->truncated_len)
8764 ordered->truncated_len = new_len;
8765 spin_unlock_irq(&tree->lock);
8766
8767 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308768 start,
8769 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008770 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008771 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008772 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008773 if (!inode_evicting) {
8774 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308775 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008776 &cached_state);
8777 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308778
8779 start = end + 1;
8780 if (start < page_end)
8781 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008782 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008783
Qu Wenruob9d0b382015-09-29 10:35:16 +08008784 /*
8785 * Qgroup reserved space handler
8786 * Page here will be either
8787 * 1) Already written to disk
8788 * In this case, its reserved space is released from data rsv map
8789 * and will be freed by delayed_ref handler finally.
8790 * So even we call qgroup_free_data(), it won't decrease reserved
8791 * space.
8792 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008793 * This means the reserved space should be freed here. However,
8794 * if a truncate invalidates the page (by clearing PageDirty)
8795 * and the page is accounted for while allocating extent
8796 * in btrfs_check_data_free_space() we let delayed_ref to
8797 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008798 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008799 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008800 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008801 if (!inode_evicting) {
8802 clear_extent_bit(tree, page_start, page_end,
8803 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008804 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8805 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008806 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008807
8808 __btrfs_releasepage(page, GFP_NOFS);
8809 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008810
Chris Mason4a096752008-07-21 10:29:44 -04008811 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008812 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008813 ClearPagePrivate(page);
8814 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008815 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008816 }
Chris Mason39279cc2007-06-12 06:35:45 -04008817}
8818
Chris Mason9ebefb182007-06-15 13:50:00 -04008819/*
8820 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8821 * called from a page fault handler when a page is first dirtied. Hence we must
8822 * be careful to check for EOF conditions here. We set the page up correctly
8823 * for a written page which means we get ENOSPC checking when writing into
8824 * holes and correct delalloc and unwritten extent mapping on filesystems that
8825 * support these features.
8826 *
8827 * We are not allowed to take the i_mutex here so we have to play games to
8828 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008829 * truncate_setsize() writes the inode size before removing pages, once we have
8830 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008831 * beyond EOF, then the page is guaranteed safe against truncation until we
8832 * unlock the page.
8833 */
Dave Jiang11bac802017-02-24 14:56:41 -08008834int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008835{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008836 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008837 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008838 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8840 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008841 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008842 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008843 char *kaddr;
8844 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008845 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008846 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008847 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308848 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008849 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308851 u64 end;
8852
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008853 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008854
Jan Karab2b5ef52012-06-12 16:20:45 +02008855 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008856 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008857 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308858 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008859
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308860 /*
8861 * Reserving delalloc space after obtaining the page lock can lead to
8862 * deadlock. For example, if a dirty page is locked by this function
8863 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8864 * dirty page write out, then the btrfs_writepage() function could
8865 * end up waiting indefinitely to get a lock on the page currently
8866 * being processed by btrfs_page_mkwrite() function.
8867 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008868 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308869 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008870 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008871 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008872 reserved = 1;
8873 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008874 if (ret) {
8875 if (ret == -ENOMEM)
8876 ret = VM_FAULT_OOM;
8877 else /* -ENOSPC, -EIO, etc */
8878 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008879 if (reserved)
8880 goto out;
8881 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008882 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008883
Nick Piggin56a76f82009-03-31 15:23:23 -07008884 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008885again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008886 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008887 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008888
Chris Mason9ebefb182007-06-15 13:50:00 -04008889 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008891 /* page got truncated out from underneath us */
8892 goto out_unlock;
8893 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008894 wait_on_page_writeback(page);
8895
David Sterbaff13db42015-12-03 14:30:40 +01008896 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008897 set_page_extent_mapped(page);
8898
Chris Masoneb84ae02008-07-17 13:53:27 -04008899 /*
8900 * we can't set the delalloc bits if there are pending ordered
8901 * extents. Drop our locks and wait for them to finish
8902 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008903 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8904 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008906 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008907 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008909 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008910 btrfs_put_ordered_extent(ordered);
8911 goto again;
8912 }
8913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008914 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008915 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008916 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008917 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308918 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008919 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008920 page_start, PAGE_SIZE - reserved_space,
8921 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308922 }
8923 }
8924
Josef Bacikfbf19082009-10-01 17:10:23 -04008925 /*
Liu Bo54160342016-12-13 12:15:19 -08008926 * page_mkwrite gets called when the page is firstly dirtied after it's
8927 * faulted in, but write(2) could also dirty a page and set delalloc
8928 * bits, thus in this case for space account reason, we still need to
8929 * clear any delalloc bits within this page range since we have to
8930 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008931 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308932 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008933 EXTENT_DIRTY | EXTENT_DELALLOC |
8934 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008935 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008936
Filipe Mananae3b8a482017-11-04 00:16:59 +00008937 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008938 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008939 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008940 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008941 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008942 ret = VM_FAULT_SIGBUS;
8943 goto out_unlock;
8944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008946
8947 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008948 if (page_start + PAGE_SIZE > size)
8949 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008950 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008951 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008952
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008953 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008954 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008955 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 flush_dcache_page(page);
8957 kunmap(page);
8958 }
Chris Mason247e7432008-07-17 12:53:51 -04008959 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008960 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008961 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008962
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008963 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008964 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008965 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008966
David Sterbae43bbe52017-12-12 21:43:52 +01008967 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008968
8969out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008970 if (!ret) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008971 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008972 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008973 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008974 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008975 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008976 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008977out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008978 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008979 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008980 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008981out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008982 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008983 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008984 return ret;
8985}
8986
Filipe Manana213e8c52018-02-06 20:40:31 +00008987static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008988{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008989 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008990 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008991 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008992 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008993 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008994 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008995 u64 mask = fs_info->sectorsize - 1;
8996 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008997
Filipe Manana213e8c52018-02-06 20:40:31 +00008998 if (!skip_writeback) {
8999 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9000 (u64)-1);
9001 if (ret)
9002 return ret;
9003 }
Chris Mason39279cc2007-06-12 06:35:45 -04009004
Josef Bacikfcb80c22011-05-03 10:40:22 -04009005 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009006 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9007 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009008 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009009 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009010 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009011 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009012 * be free'd up by the truncate operation, but also have some slack
9013 * space reserved in case it uses space during the truncate (thank you
9014 * very much snapshotting).
9015 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009016 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009017 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009018 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009019 * doesn't end up using space reserved for updating the inode. We also
9020 * need to be able to stop the transaction and start a new one, which
9021 * means we need to be able to update the inode several times, and we
9022 * have no idea of knowing how many times that will be, so we can't just
9023 * reserve 1 item for the entirety of the operation, so that has to be
9024 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009025 *
9026 * So that leaves us with
9027 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009028 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009029 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009030 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009031 * updating the inode.
9032 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009033 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009034 if (!rsv)
9035 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009036 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009037 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009038
Josef Bacik907cbce2011-08-08 13:46:15 -04009039 /*
Josef Bacik07127182011-08-19 10:29:59 -04009040 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009041 * 1 for updating the inode.
9042 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009043 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009044 if (IS_ERR(trans)) {
9045 err = PTR_ERR(trans);
9046 goto out;
9047 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009048
Josef Bacik907cbce2011-08-08 13:46:15 -04009049 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009050 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009051 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009052 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009053
Chris Mason5a3f23d2009-03-31 13:27:11 -04009054 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009055 * So if we truncate and then write and fsync we normally would just
9056 * write the extents that changed, which is a problem if we need to
9057 * first truncate that entire inode. So set this flag so we write out
9058 * all of the extents in the inode to the sync log so we're completely
9059 * safe.
9060 */
9061 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009062 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009063
Yan, Zheng80825102009-11-12 09:35:36 +00009064 while (1) {
9065 ret = btrfs_truncate_inode_items(trans, root, inode,
9066 inode->i_size,
9067 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009068 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009069 if (ret != -ENOSPC && ret != -EAGAIN) {
Omar Sandovald5014732018-05-22 09:47:58 -07009070 if (ret < 0)
9071 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009072 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009073 }
Chris Mason39279cc2007-06-12 06:35:45 -04009074
Yan, Zheng80825102009-11-12 09:35:36 +00009075 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009076 if (ret) {
9077 err = ret;
9078 break;
9079 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009080
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009081 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009082 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009083
9084 trans = btrfs_start_transaction(root, 2);
9085 if (IS_ERR(trans)) {
9086 ret = err = PTR_ERR(trans);
9087 trans = NULL;
9088 break;
9089 }
9090
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009091 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009092 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009093 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009094 BUG_ON(ret); /* shouldn't happen */
9095 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009096 }
9097
Josef Bacikddfae632017-10-19 14:16:02 -04009098 /*
9099 * We can't call btrfs_truncate_block inside a trans handle as we could
9100 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9101 * we've truncated everything except the last little bit, and can do
9102 * btrfs_truncate_block and then update the disk_i_size.
9103 */
9104 if (ret == NEED_TRUNCATE_BLOCK) {
9105 btrfs_end_transaction(trans);
9106 btrfs_btree_balance_dirty(fs_info);
9107
9108 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9109 if (ret)
9110 goto out;
9111 trans = btrfs_start_transaction(root, 1);
9112 if (IS_ERR(trans)) {
9113 ret = PTR_ERR(trans);
9114 goto out;
9115 }
9116 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9117 }
9118
Chris Mason917c16b2011-11-08 14:49:59 -05009119 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009120 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009121 ret = btrfs_update_inode(trans, root, inode);
9122 if (ret && !err)
9123 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009124
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009125 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009126 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009127 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009128out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009129 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009130
Josef Bacik3893e332011-01-31 16:03:11 -05009131 if (ret && !err)
9132 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009133
Josef Bacik3893e332011-01-31 16:03:11 -05009134 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009135}
9136
Sven Wegener3b963622008-06-09 21:57:42 -04009137/*
Chris Masond352ac62008-09-29 15:18:18 -04009138 * create a new subvolume directory/inode (helper for the ioctl).
9139 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009140int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009141 struct btrfs_root *new_root,
9142 struct btrfs_root *parent_root,
9143 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009144{
Chris Mason39279cc2007-06-12 06:35:45 -04009145 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009146 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009147 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009148
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009149 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9150 new_dirid, new_dirid,
9151 S_IFDIR | (~current_umask() & S_IRWXUGO),
9152 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009153 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009154 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009155 inode->i_op = &btrfs_dir_inode_operations;
9156 inode->i_fop = &btrfs_dir_file_operations;
9157
Miklos Szeredibfe86842011-10-28 14:13:29 +02009158 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009159 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009160 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009161
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009162 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9163 if (err)
9164 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009165 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009166 new_root->root_key.objectid, err);
9167
Yan, Zheng76dda932009-09-21 16:00:26 -04009168 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009169
Yan, Zheng76dda932009-09-21 16:00:26 -04009170 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009171 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009172}
9173
Chris Mason39279cc2007-06-12 06:35:45 -04009174struct inode *btrfs_alloc_inode(struct super_block *sb)
9175{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009176 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009177 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009178 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009179
David Sterba712e36c2017-10-31 17:08:27 +01009180 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009181 if (!ei)
9182 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009183
9184 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009185 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009186 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009187 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009188 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009189 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009190 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009191 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009192 ei->disk_i_size = 0;
9193 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009194 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009195 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009196 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009197 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009198 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009199
Josef Bacik9e0baf62011-07-15 15:16:44 +00009200 spin_lock_init(&ei->lock);
9201 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009202 if (sb->s_magic != BTRFS_TEST_MAGIC)
9203 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9204 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009205 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009206 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009207 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009208
Miao Xie16cdcec2011-04-22 18:12:22 +08009209 ei->delayed_node = NULL;
9210
chandan r9cc97d62012-07-04 12:48:07 +05309211 ei->i_otime.tv_sec = 0;
9212 ei->i_otime.tv_nsec = 0;
9213
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009214 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009215 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009216 extent_io_tree_init(&ei->io_tree, inode);
9217 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009218 ei->io_tree.track_uptodate = 1;
9219 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009220 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009221 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009222 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009223 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009224 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009225 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009226 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009227 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009228
9229 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009230}
9231
Josef Bacikaaedb552013-10-11 14:44:09 -04009232#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9233void btrfs_test_destroy_inode(struct inode *inode)
9234{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009235 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009236 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9237}
9238#endif
9239
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009240static void btrfs_i_callback(struct rcu_head *head)
9241{
9242 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009243 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9244}
9245
Chris Mason39279cc2007-06-12 06:35:45 -04009246void btrfs_destroy_inode(struct inode *inode)
9247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009249 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009250 struct btrfs_root *root = BTRFS_I(inode)->root;
9251
Al Virob3d9b7a2012-06-09 13:51:19 -04009252 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009253 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009254 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9255 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009256 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009257 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009258 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009259 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009260 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009261
Chris Mason5a3f23d2009-03-31 13:27:11 -04009262 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009263 * This can happen where we create an inode, but somebody else also
9264 * created the same inode and we need to destroy the one we already
9265 * created.
9266 */
9267 if (!root)
9268 goto free;
9269
Chris Masond3977122009-01-05 21:25:51 -05009270 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009271 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9272 if (!ordered)
9273 break;
9274 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009275 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009276 "found ordered extent %llu %llu on inode cleanup",
9277 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009278 btrfs_remove_ordered_extent(inode, ordered);
9279 btrfs_put_ordered_extent(ordered);
9280 btrfs_put_ordered_extent(ordered);
9281 }
9282 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009283 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009284 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009285 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009286free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009287 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009288}
9289
Al Viro45321ac2010-06-07 13:43:19 -04009290int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009291{
9292 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009293
Naohiro Aota6379ef92013-06-06 09:56:34 +00009294 if (root == NULL)
9295 return 1;
9296
Liu Bofa6ac872013-02-20 14:10:23 +00009297 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009298 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009299 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009300 else
Al Viro45321ac2010-06-07 13:43:19 -04009301 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009302}
9303
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009304static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009305{
9306 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9307
9308 inode_init_once(&ei->vfs_inode);
9309}
9310
David Sterbae67c7182018-02-19 17:24:18 +01009311void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009312{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009313 /*
9314 * Make sure all delayed rcu free inodes are flushed before we
9315 * destroy cache.
9316 */
9317 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009318 kmem_cache_destroy(btrfs_inode_cachep);
9319 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009320 kmem_cache_destroy(btrfs_path_cachep);
9321 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009322}
9323
Liu Bof5c29bd2017-11-02 17:21:50 -06009324int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009325{
David Sterba837e1972012-09-07 03:00:48 -06009326 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009327 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009328 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9329 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009330 if (!btrfs_inode_cachep)
9331 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009332
David Sterba837e1972012-09-07 03:00:48 -06009333 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009334 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009335 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009336 if (!btrfs_trans_handle_cachep)
9337 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009338
David Sterba837e1972012-09-07 03:00:48 -06009339 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009340 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009341 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009342 if (!btrfs_path_cachep)
9343 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009344
David Sterba837e1972012-09-07 03:00:48 -06009345 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009346 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009347 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009348 if (!btrfs_free_space_cachep)
9349 goto fail;
9350
Chris Mason39279cc2007-06-12 06:35:45 -04009351 return 0;
9352fail:
9353 btrfs_destroy_cachep();
9354 return -ENOMEM;
9355}
9356
David Howellsa528d352017-01-31 16:46:22 +00009357static int btrfs_getattr(const struct path *path, struct kstat *stat,
9358 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009359{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009360 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009361 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009362 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009363 u32 bi_flags = BTRFS_I(inode)->flags;
9364
9365 stat->result_mask |= STATX_BTIME;
9366 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9367 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9368 if (bi_flags & BTRFS_INODE_APPEND)
9369 stat->attributes |= STATX_ATTR_APPEND;
9370 if (bi_flags & BTRFS_INODE_COMPRESS)
9371 stat->attributes |= STATX_ATTR_COMPRESSED;
9372 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9373 stat->attributes |= STATX_ATTR_IMMUTABLE;
9374 if (bi_flags & BTRFS_INODE_NODUMP)
9375 stat->attributes |= STATX_ATTR_NODUMP;
9376
9377 stat->attributes_mask |= (STATX_ATTR_APPEND |
9378 STATX_ATTR_COMPRESSED |
9379 STATX_ATTR_IMMUTABLE |
9380 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009381
Chris Mason39279cc2007-06-12 06:35:45 -04009382 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009383 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009384
9385 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009386 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009387 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009388 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009389 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009390 return 0;
9391}
9392
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009393static int btrfs_rename_exchange(struct inode *old_dir,
9394 struct dentry *old_dentry,
9395 struct inode *new_dir,
9396 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009397{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009398 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009399 struct btrfs_trans_handle *trans;
9400 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009401 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009402 struct inode *new_inode = new_dentry->d_inode;
9403 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009404 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009405 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009406 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9407 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009408 u64 old_idx = 0;
9409 u64 new_idx = 0;
9410 u64 root_objectid;
9411 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009412 bool root_log_pinned = false;
9413 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009414
9415 /* we only allow rename subvolume link between subvolumes */
9416 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9417 return -EXDEV;
9418
9419 /* close the race window with snapshot create/destroy ioctl */
9420 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009421 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009422 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009423 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009424
9425 /*
9426 * We want to reserve the absolute worst case amount of items. So if
9427 * both inodes are subvols and we need to unlink them then that would
9428 * require 4 item modifications, but if they are both normal inodes it
9429 * would require 5 item modifications, so we'll assume their normal
9430 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9431 * should cover the worst case number of items we'll modify.
9432 */
9433 trans = btrfs_start_transaction(root, 12);
9434 if (IS_ERR(trans)) {
9435 ret = PTR_ERR(trans);
9436 goto out_notrans;
9437 }
9438
9439 /*
9440 * We need to find a free sequence number both in the source and
9441 * in the destination directory for the exchange.
9442 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009443 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009444 if (ret)
9445 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009446 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 if (ret)
9448 goto out_fail;
9449
9450 BTRFS_I(old_inode)->dir_index = 0ULL;
9451 BTRFS_I(new_inode)->dir_index = 0ULL;
9452
9453 /* Reference for the source. */
9454 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9455 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009456 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009457 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009458 btrfs_pin_log_trans(root);
9459 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460 ret = btrfs_insert_inode_ref(trans, dest,
9461 new_dentry->d_name.name,
9462 new_dentry->d_name.len,
9463 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009464 btrfs_ino(BTRFS_I(new_dir)),
9465 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466 if (ret)
9467 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009468 }
9469
9470 /* And now for the dest. */
9471 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9472 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009473 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009474 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009475 btrfs_pin_log_trans(dest);
9476 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477 ret = btrfs_insert_inode_ref(trans, root,
9478 old_dentry->d_name.name,
9479 old_dentry->d_name.len,
9480 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009481 btrfs_ino(BTRFS_I(old_dir)),
9482 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009483 if (ret)
9484 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 }
9486
9487 /* Update inode version and ctime/mtime. */
9488 inode_inc_iversion(old_dir);
9489 inode_inc_iversion(new_dir);
9490 inode_inc_iversion(old_inode);
9491 inode_inc_iversion(new_inode);
9492 old_dir->i_ctime = old_dir->i_mtime = ctime;
9493 new_dir->i_ctime = new_dir->i_mtime = ctime;
9494 old_inode->i_ctime = ctime;
9495 new_inode->i_ctime = ctime;
9496
9497 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009498 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9499 BTRFS_I(old_inode), 1);
9500 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9501 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 }
9503
9504 /* src is a subvolume */
9505 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9506 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9507 ret = btrfs_unlink_subvol(trans, root, old_dir,
9508 root_objectid,
9509 old_dentry->d_name.name,
9510 old_dentry->d_name.len);
9511 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009512 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9513 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 old_dentry->d_name.name,
9515 old_dentry->d_name.len);
9516 if (!ret)
9517 ret = btrfs_update_inode(trans, root, old_inode);
9518 }
9519 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009520 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009521 goto out_fail;
9522 }
9523
9524 /* dest is a subvolume */
9525 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9526 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9527 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9528 root_objectid,
9529 new_dentry->d_name.name,
9530 new_dentry->d_name.len);
9531 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009532 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9533 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534 new_dentry->d_name.name,
9535 new_dentry->d_name.len);
9536 if (!ret)
9537 ret = btrfs_update_inode(trans, dest, new_inode);
9538 }
9539 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009540 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009541 goto out_fail;
9542 }
9543
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009544 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 new_dentry->d_name.name,
9546 new_dentry->d_name.len, 0, old_idx);
9547 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009548 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 goto out_fail;
9550 }
9551
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009552 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 old_dentry->d_name.name,
9554 old_dentry->d_name.len, 0, new_idx);
9555 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009556 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 goto out_fail;
9558 }
9559
9560 if (old_inode->i_nlink == 1)
9561 BTRFS_I(old_inode)->dir_index = old_idx;
9562 if (new_inode->i_nlink == 1)
9563 BTRFS_I(new_inode)->dir_index = new_idx;
9564
Filipe Manana86e8aa02016-05-05 02:02:27 +01009565 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009567 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9568 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009570 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009572 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009574 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9575 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009576 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009577 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 }
9579out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009580 /*
9581 * If we have pinned a log and an error happened, we unpin tasks
9582 * trying to sync the log and force them to fallback to a transaction
9583 * commit if the log currently contains any of the inodes involved in
9584 * this rename operation (to ensure we do not persist a log with an
9585 * inconsistent state for any of these inodes or leading to any
9586 * inconsistencies when replayed). If the transaction was aborted, the
9587 * abortion reason is propagated to userspace when attempting to commit
9588 * the transaction. If the log does not contain any of these inodes, we
9589 * allow the tasks to sync it.
9590 */
9591 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009592 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9593 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9594 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009595 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009596 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009597 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009598
9599 if (root_log_pinned) {
9600 btrfs_end_log_trans(root);
9601 root_log_pinned = false;
9602 }
9603 if (dest_log_pinned) {
9604 btrfs_end_log_trans(dest);
9605 dest_log_pinned = false;
9606 }
9607 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009608 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609out_notrans:
9610 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009611 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009613 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614
9615 return ret;
9616}
9617
9618static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9619 struct btrfs_root *root,
9620 struct inode *dir,
9621 struct dentry *dentry)
9622{
9623 int ret;
9624 struct inode *inode;
9625 u64 objectid;
9626 u64 index;
9627
9628 ret = btrfs_find_free_ino(root, &objectid);
9629 if (ret)
9630 return ret;
9631
9632 inode = btrfs_new_inode(trans, root, dir,
9633 dentry->d_name.name,
9634 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009635 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009636 objectid,
9637 S_IFCHR | WHITEOUT_MODE,
9638 &index);
9639
9640 if (IS_ERR(inode)) {
9641 ret = PTR_ERR(inode);
9642 return ret;
9643 }
9644
9645 inode->i_op = &btrfs_special_inode_operations;
9646 init_special_inode(inode, inode->i_mode,
9647 WHITEOUT_DEV);
9648
9649 ret = btrfs_init_inode_security(trans, inode, dir,
9650 &dentry->d_name);
9651 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009652 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009654 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9655 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009657 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658
9659 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009660out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009662 if (ret)
9663 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 iput(inode);
9665
Filipe Mananac9901612016-05-05 01:41:57 +01009666 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009667}
9668
Chris Mason39279cc2007-06-12 06:35:45 -04009669static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670 struct inode *new_dir, struct dentry *new_dentry,
9671 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009672{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009673 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009674 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009675 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009676 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9677 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009678 struct inode *new_inode = d_inode(new_dentry);
9679 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009680 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009681 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009682 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009683 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009684 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009685
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009686 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009687 return -EPERM;
9688
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009689 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009690 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009691 return -EXDEV;
9692
Li Zefan33345d012011-04-20 10:31:50 +08009693 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009694 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009695 return -ENOTEMPTY;
9696
Chris Mason39279cc2007-06-12 06:35:45 -04009697 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009698 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009699 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009700
9701
9702 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009703 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009704 new_dentry->d_name.name,
9705 new_dentry->d_name.len);
9706
9707 if (ret) {
9708 if (ret == -EEXIST) {
9709 /* we shouldn't get
9710 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309711 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009712 return ret;
9713 }
9714 } else {
9715 /* maybe -EOVERFLOW */
9716 return ret;
9717 }
9718 }
9719 ret = 0;
9720
Chris Mason5a3f23d2009-03-31 13:27:11 -04009721 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009722 * we're using rename to replace one file with another. Start IO on it
9723 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009724 */
Chris Mason8d875f92014-08-12 10:47:42 -07009725 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009726 filemap_flush(old_inode->i_mapping);
9727
Yan, Zheng76dda932009-09-21 16:00:26 -04009728 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009729 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009730 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009731 /*
9732 * We want to reserve the absolute worst case amount of items. So if
9733 * both inodes are subvols and we need to unlink them then that would
9734 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009735 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009736 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9737 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009738 * If our rename has the whiteout flag, we need more 5 units for the
9739 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9740 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009741 */
Filipe Manana5062af32016-05-05 10:26:26 +01009742 trans_num_items = 11;
9743 if (flags & RENAME_WHITEOUT)
9744 trans_num_items += 5;
9745 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009746 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747 ret = PTR_ERR(trans);
9748 goto out_notrans;
9749 }
Chris Mason5f39d392007-10-15 16:14:19 -04009750
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009751 if (dest != root)
9752 btrfs_record_root_in_trans(trans, dest);
9753
Nikolay Borisov877574e2017-02-20 13:50:33 +02009754 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009755 if (ret)
9756 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009757
Miao Xie67de1172013-12-26 13:07:06 +08009758 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009759 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009760 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009761 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009762 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009763 btrfs_pin_log_trans(root);
9764 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009765 ret = btrfs_insert_inode_ref(trans, dest,
9766 new_dentry->d_name.name,
9767 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009768 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009769 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009770 if (ret)
9771 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009772 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009773
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009774 inode_inc_iversion(old_dir);
9775 inode_inc_iversion(new_dir);
9776 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009777 old_dir->i_ctime = old_dir->i_mtime =
9778 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009779 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009780
Chris Mason12fcfd22009-03-24 10:24:20 -04009781 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009782 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9783 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009784
Li Zefan33345d012011-04-20 10:31:50 +08009785 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009786 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9787 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9788 old_dentry->d_name.name,
9789 old_dentry->d_name.len);
9790 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009791 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9792 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009793 old_dentry->d_name.name,
9794 old_dentry->d_name.len);
9795 if (!ret)
9796 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009797 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009798 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009799 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009800 goto out_fail;
9801 }
Chris Mason39279cc2007-06-12 06:35:45 -04009802
9803 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009804 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009805 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009806 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009807 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9808 root_objectid = BTRFS_I(new_inode)->location.objectid;
9809 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9810 root_objectid,
9811 new_dentry->d_name.name,
9812 new_dentry->d_name.len);
9813 BUG_ON(new_inode->i_nlink == 0);
9814 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009815 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9816 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 new_dentry->d_name.name,
9818 new_dentry->d_name.len);
9819 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009820 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009821 ret = btrfs_orphan_add(trans,
9822 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009823 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009824 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009825 goto out_fail;
9826 }
Chris Mason39279cc2007-06-12 06:35:45 -04009827 }
Josef Bacikaec74772008-07-24 12:12:38 -04009828
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009829 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009830 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009831 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009832 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009833 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009834 goto out_fail;
9835 }
Chris Mason39279cc2007-06-12 06:35:45 -04009836
Miao Xie67de1172013-12-26 13:07:06 +08009837 if (old_inode->i_nlink == 1)
9838 BTRFS_I(old_inode)->dir_index = index;
9839
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009840 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009841 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009842
David Sterbaf85b7372017-01-20 14:54:07 +01009843 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9844 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009846 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009847 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848
9849 if (flags & RENAME_WHITEOUT) {
9850 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9851 old_dentry);
9852
9853 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009854 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009855 goto out_fail;
9856 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009857 }
Chris Mason39279cc2007-06-12 06:35:45 -04009858out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009859 /*
9860 * If we have pinned the log and an error happened, we unpin tasks
9861 * trying to sync the log and force them to fallback to a transaction
9862 * commit if the log currently contains any of the inodes involved in
9863 * this rename operation (to ensure we do not persist a log with an
9864 * inconsistent state for any of these inodes or leading to any
9865 * inconsistencies when replayed). If the transaction was aborted, the
9866 * abortion reason is propagated to userspace when attempting to commit
9867 * the transaction. If the log does not contain any of these inodes, we
9868 * allow the tasks to sync it.
9869 */
9870 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009871 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9872 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9873 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009874 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009875 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009876 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009877
9878 btrfs_end_log_trans(root);
9879 log_pinned = false;
9880 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009881 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009882out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009883 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009884 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009885
Chris Mason39279cc2007-06-12 06:35:45 -04009886 return ret;
9887}
9888
Miklos Szeredi80ace852014-07-23 15:15:32 +02009889static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9890 struct inode *new_dir, struct dentry *new_dentry,
9891 unsigned int flags)
9892{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009894 return -EINVAL;
9895
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896 if (flags & RENAME_EXCHANGE)
9897 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9898 new_dentry);
9899
9900 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009901}
9902
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009903struct btrfs_delalloc_work {
9904 struct inode *inode;
9905 struct completion completion;
9906 struct list_head list;
9907 struct btrfs_work work;
9908};
9909
Miao Xie8ccf6f192012-10-25 09:28:04 +00009910static void btrfs_run_delalloc_work(struct btrfs_work *work)
9911{
9912 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009913 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009914
9915 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9916 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009917 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009918 filemap_flush(inode->i_mapping);
9919 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9920 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009921 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009922
Nikolay Borisov076da912018-04-23 10:54:16 +03009923 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009924 complete(&delalloc_work->completion);
9925}
9926
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009927static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009928{
9929 struct btrfs_delalloc_work *work;
9930
David Sterba100d5702015-12-08 14:39:32 +01009931 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009932 if (!work)
9933 return NULL;
9934
9935 init_completion(&work->completion);
9936 INIT_LIST_HEAD(&work->list);
9937 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009938 WARN_ON_ONCE(!inode);
9939 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9940 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009941
9942 return work;
9943}
9944
Chris Masond352ac62008-09-29 15:18:18 -04009945/*
9946 * some fairly slow code that needs optimization. This walks the list
9947 * of all the inodes with pending delalloc and forces them to disk.
9948 */
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009949static int start_delalloc_inodes(struct btrfs_root *root, int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009950{
Chris Masonea8c2812008-08-04 23:17:27 -04009951 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009952 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009953 struct btrfs_delalloc_work *work, *next;
9954 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009955 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009956 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009957
Miao Xie8ccf6f192012-10-25 09:28:04 +00009958 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009959 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009960
Miao Xie573bfb72014-03-06 13:55:03 +08009961 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009962 spin_lock(&root->delalloc_lock);
9963 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009964 while (!list_empty(&splice)) {
9965 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009966 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009967
Miao Xieeb73c1b2013-05-15 07:48:22 +00009968 list_move_tail(&binode->delalloc_inodes,
9969 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009970 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009971 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009972 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009973 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009974 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009975 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009976
Nikolay Borisov076da912018-04-23 10:54:16 +03009977 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009978 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009979 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009980 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009981 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009982 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009983 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009984 btrfs_queue_work(root->fs_info->flush_workers,
9985 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009986 ret++;
9987 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009988 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009989 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009990 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009991 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009992 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009993
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009994out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009995 list_for_each_entry_safe(work, next, &works, list) {
9996 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009997 wait_for_completion(&work->completion);
9998 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009999 }
10000
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010001 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010002 spin_lock(&root->delalloc_lock);
10003 list_splice_tail(&splice, &root->delalloc_inodes);
10004 spin_unlock(&root->delalloc_lock);
10005 }
Miao Xie573bfb72014-03-06 13:55:03 +080010006 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010007 return ret;
10008}
10009
Nikolay Borisov76f32e22018-04-23 10:54:14 +030010010int btrfs_start_delalloc_inodes(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010011{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010012 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010013 int ret;
10014
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010015 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010016 return -EROFS;
10017
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010018 ret = start_delalloc_inodes(root, -1);
Miao Xie6c255e62014-03-06 13:55:01 +080010019 if (ret > 0)
10020 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010021 return ret;
10022}
10023
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010024int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010025{
10026 struct btrfs_root *root;
10027 struct list_head splice;
10028 int ret;
10029
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010030 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010031 return -EROFS;
10032
10033 INIT_LIST_HEAD(&splice);
10034
Miao Xie573bfb72014-03-06 13:55:03 +080010035 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010036 spin_lock(&fs_info->delalloc_root_lock);
10037 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010038 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 root = list_first_entry(&splice, struct btrfs_root,
10040 delalloc_root);
10041 root = btrfs_grab_fs_root(root);
10042 BUG_ON(!root);
10043 list_move_tail(&root->delalloc_root,
10044 &fs_info->delalloc_roots);
10045 spin_unlock(&fs_info->delalloc_root_lock);
10046
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010047 ret = start_delalloc_inodes(root, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010048 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010049 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 goto out;
10051
Miao Xie6c255e62014-03-06 13:55:01 +080010052 if (nr != -1) {
10053 nr -= ret;
10054 WARN_ON(nr < 0);
10055 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 spin_lock(&fs_info->delalloc_root_lock);
10057 }
10058 spin_unlock(&fs_info->delalloc_root_lock);
10059
Miao Xie6c255e62014-03-06 13:55:01 +080010060 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010061out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010062 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010063 spin_lock(&fs_info->delalloc_root_lock);
10064 list_splice_tail(&splice, &fs_info->delalloc_roots);
10065 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010066 }
Miao Xie573bfb72014-03-06 13:55:03 +080010067 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010068 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010069}
10070
Chris Mason39279cc2007-06-12 06:35:45 -040010071static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10072 const char *symname)
10073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010075 struct btrfs_trans_handle *trans;
10076 struct btrfs_root *root = BTRFS_I(dir)->root;
10077 struct btrfs_path *path;
10078 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010079 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010080 int err;
10081 int drop_inode = 0;
10082 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010083 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010084 int name_len;
10085 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010086 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010087 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010088 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010089
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010090 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010091 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010092 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010093
Josef Bacik9ed74f22009-09-11 16:12:44 -040010094 /*
10095 * 2 items for inode item and ref
10096 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010097 * 1 item for updating parent inode item
10098 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010099 * 1 item for xattr if selinux is on
10100 */
Filipe Manana9269d122015-12-31 18:16:29 +000010101 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010102 if (IS_ERR(trans))
10103 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010104
Li Zefan581bb052011-04-20 10:06:11 +080010105 err = btrfs_find_free_ino(root, &objectid);
10106 if (err)
10107 goto out_unlock;
10108
Josef Bacikaec74772008-07-24 12:12:38 -040010109 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010110 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10111 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010112 if (IS_ERR(inode)) {
10113 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010114 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010115 }
Chris Mason39279cc2007-06-12 06:35:45 -040010116
Casey Schauflerad19db72011-12-15 10:09:07 -050010117 /*
10118 * If the active LSM wants to access the inode during
10119 * d_instantiate it needs these. Smack checks to see
10120 * if the filesystem supports xattrs by looking at the
10121 * ops vector.
10122 */
10123 inode->i_fop = &btrfs_file_operations;
10124 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010125 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010126 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10127
10128 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10129 if (err)
10130 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010131
Chris Mason39279cc2007-06-12 06:35:45 -040010132 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010133 if (!path) {
10134 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010135 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010136 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010137 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010138 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010139 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010140 datasize = btrfs_file_extent_calc_inline_size(name_len);
10141 err = btrfs_insert_empty_item(trans, root, path, &key,
10142 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010143 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010144 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010145 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010146 }
Chris Mason5f39d392007-10-15 16:14:19 -040010147 leaf = path->nodes[0];
10148 ei = btrfs_item_ptr(leaf, path->slots[0],
10149 struct btrfs_file_extent_item);
10150 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10151 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010152 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010153 btrfs_set_file_extent_encryption(leaf, ei, 0);
10154 btrfs_set_file_extent_compression(leaf, ei, 0);
10155 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10156 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10157
Chris Mason39279cc2007-06-12 06:35:45 -040010158 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010159 write_extent_buffer(leaf, symname, ptr, name_len);
10160 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010161 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010162
Chris Mason39279cc2007-06-12 06:35:45 -040010163 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010164 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010165 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010166 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010167 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010168 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010169 /*
10170 * Last step, add directory indexes for our symlink inode. This is the
10171 * last step to avoid extra cleanup of these indexes if an error happens
10172 * elsewhere above.
10173 */
10174 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010175 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10176 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010177 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010178 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010179 goto out_unlock_inode;
10180 }
10181
Al Viro1e2e5472018-05-04 08:23:01 -040010182 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010183
10184out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010185 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010186 if (drop_inode) {
10187 inode_dec_link_count(inode);
10188 iput(inode);
10189 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010190 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010191 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010192
10193out_unlock_inode:
10194 drop_inode = 1;
10195 unlock_new_inode(inode);
10196 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010197}
Chris Mason16432982008-04-10 10:23:21 -040010198
Josef Bacik0af3d002010-06-21 14:48:16 -040010199static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10200 u64 start, u64 num_bytes, u64 min_size,
10201 loff_t actual_len, u64 *alloc_hint,
10202 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010204 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010205 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10206 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010207 struct btrfs_root *root = BTRFS_I(inode)->root;
10208 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010209 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010210 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010211 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010212 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010213 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010214 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010215 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010216
Josef Bacik0af3d002010-06-21 14:48:16 -040010217 if (trans)
10218 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010219 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010220 if (own_trans) {
10221 trans = btrfs_start_transaction(root, 3);
10222 if (IS_ERR(trans)) {
10223 ret = PTR_ERR(trans);
10224 break;
10225 }
Yan Zhengd899e052008-10-30 14:25:28 -040010226 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010227
Byongho Leeee221842015-12-15 01:42:10 +090010228 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010229 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010230 /*
10231 * If we are severely fragmented we could end up with really
10232 * small allocations, so if the allocator is returning small
10233 * chunks lets make its job easier by only searching for those
10234 * sized chunks.
10235 */
10236 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010237 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10238 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010239 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010240 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010241 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010242 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010243 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010244 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010245
Josef Bacik0b670dc2015-09-23 17:11:16 -040010246 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010247 ret = insert_reserved_file_extent(trans, inode,
10248 cur_offset, ins.objectid,
10249 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010250 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010251 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010252 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010253 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010254 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010255 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010256 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010257 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010258 break;
10259 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010260
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010261 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010262 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010263
Josef Bacik5dc562c2012-08-17 13:14:17 -040010264 em = alloc_extent_map();
10265 if (!em) {
10266 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10267 &BTRFS_I(inode)->runtime_flags);
10268 goto next;
10269 }
10270
10271 em->start = cur_offset;
10272 em->orig_start = cur_offset;
10273 em->len = ins.offset;
10274 em->block_start = ins.objectid;
10275 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010276 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010277 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010278 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010279 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10280 em->generation = trans->transid;
10281
10282 while (1) {
10283 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010284 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010285 write_unlock(&em_tree->lock);
10286 if (ret != -EEXIST)
10287 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010288 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010289 cur_offset + ins.offset - 1,
10290 0);
10291 }
10292 free_extent_map(em);
10293next:
Yan Zhengd899e052008-10-30 14:25:28 -040010294 num_bytes -= ins.offset;
10295 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010296 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010297
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010298 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010299 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010300 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010301 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010302 (actual_len > inode->i_size) &&
10303 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010304 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010305 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010306 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010307 i_size = cur_offset;
10308 i_size_write(inode, i_size);
10309 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010310 }
10311
Yan Zhengd899e052008-10-30 14:25:28 -040010312 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010313
10314 if (ret) {
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 }
Yan Zhengd899e052008-10-30 14:25:28 -040010320
Josef Bacik0af3d002010-06-21 14:48:16 -040010321 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010322 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010323 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010324 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010325 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010326 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010327 return ret;
10328}
10329
Josef Bacik0af3d002010-06-21 14:48:16 -040010330int btrfs_prealloc_file_range(struct inode *inode, int mode,
10331 u64 start, u64 num_bytes, u64 min_size,
10332 loff_t actual_len, u64 *alloc_hint)
10333{
10334 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10335 min_size, actual_len, alloc_hint,
10336 NULL);
10337}
10338
10339int btrfs_prealloc_file_range_trans(struct inode *inode,
10340 struct btrfs_trans_handle *trans, int mode,
10341 u64 start, u64 num_bytes, u64 min_size,
10342 loff_t actual_len, u64 *alloc_hint)
10343{
10344 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10345 min_size, actual_len, alloc_hint, trans);
10346}
10347
Chris Masone6dcd2d2008-07-17 12:53:50 -040010348static int btrfs_set_page_dirty(struct page *page)
10349{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010350 return __set_page_dirty_nobuffers(page);
10351}
10352
Al Viro10556cb2011-06-20 19:28:19 -040010353static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010354{
Li Zefanb83cc962010-12-20 16:04:08 +080010355 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010356 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010357
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010358 if (mask & MAY_WRITE &&
10359 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10360 if (btrfs_root_readonly(root))
10361 return -EROFS;
10362 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10363 return -EACCES;
10364 }
Al Viro2830ba72011-06-20 19:16:29 -040010365 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010366}
Chris Mason39279cc2007-06-12 06:35:45 -040010367
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010368static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10369{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010370 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010371 struct btrfs_trans_handle *trans;
10372 struct btrfs_root *root = BTRFS_I(dir)->root;
10373 struct inode *inode = NULL;
10374 u64 objectid;
10375 u64 index;
10376 int ret = 0;
10377
10378 /*
10379 * 5 units required for adding orphan entry
10380 */
10381 trans = btrfs_start_transaction(root, 5);
10382 if (IS_ERR(trans))
10383 return PTR_ERR(trans);
10384
10385 ret = btrfs_find_free_ino(root, &objectid);
10386 if (ret)
10387 goto out;
10388
10389 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010390 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010391 if (IS_ERR(inode)) {
10392 ret = PTR_ERR(inode);
10393 inode = NULL;
10394 goto out;
10395 }
10396
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010397 inode->i_fop = &btrfs_file_operations;
10398 inode->i_op = &btrfs_file_inode_operations;
10399
10400 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010401 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10402
Chris Masonb0d5d102014-09-08 13:08:51 -070010403 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10404 if (ret)
10405 goto out_inode;
10406
10407 ret = btrfs_update_inode(trans, root, inode);
10408 if (ret)
10409 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010410 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010411 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010412 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010413
Filipe Manana5762b5c2014-08-01 00:10:32 +010010414 /*
10415 * We set number of links to 0 in btrfs_new_inode(), and here we set
10416 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10417 * through:
10418 *
10419 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10420 */
10421 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010422 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010423 d_tmpfile(dentry, inode);
10424 mark_inode_dirty(inode);
10425
10426out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010427 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010428 if (ret)
10429 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010430 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010431 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010432
10433out_inode:
10434 unlock_new_inode(inode);
10435 goto out;
10436
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010437}
10438
David Sterba20a7db82017-02-17 16:24:29 +010010439__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010440static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010441{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010442 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010443}
10444
Josef Bacikc6100a42017-05-05 11:57:13 -040010445static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10446{
10447 struct inode *inode = private_data;
10448 return btrfs_sb(inode->i_sb);
10449}
10450
10451static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10452 u64 start, u64 end)
10453{
10454 struct inode *inode = private_data;
10455 u64 isize;
10456
10457 isize = i_size_read(inode);
10458 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10459 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10460 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10461 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10462 }
10463}
10464
10465void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10466{
10467 struct inode *inode = private_data;
10468 unsigned long index = start >> PAGE_SHIFT;
10469 unsigned long end_index = end >> PAGE_SHIFT;
10470 struct page *page;
10471
10472 while (index <= end_index) {
10473 page = find_get_page(inode->i_mapping, index);
10474 ASSERT(page); /* Pages should be in the extent_io_tree */
10475 set_page_writeback(page);
10476 put_page(page);
10477 index++;
10478 }
10479}
10480
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010481static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010482 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010483 .lookup = btrfs_lookup,
10484 .create = btrfs_create,
10485 .unlink = btrfs_unlink,
10486 .link = btrfs_link,
10487 .mkdir = btrfs_mkdir,
10488 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010489 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010490 .symlink = btrfs_symlink,
10491 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010492 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010493 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010494 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010495 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010496 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010497 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010499};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010500static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010501 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010502 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010503 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010504};
Yan, Zheng76dda932009-09-21 16:00:26 -040010505
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010506static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010507 .llseek = generic_file_llseek,
10508 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010509 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010510 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010511 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010512#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010513 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010514#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010515 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010516 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010517};
10518
David Sterba20e55062015-11-19 11:42:28 +010010519static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010520 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010521 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010522 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010523 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010524 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010525 .tree_fs_info = iotree_fs_info,
10526 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010527
10528 /* optional callbacks */
10529 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010530 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010531 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010532 .set_bit_hook = btrfs_set_bit_hook,
10533 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010534 .merge_extent_hook = btrfs_merge_extent_hook,
10535 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010536 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010537};
10538
Chris Mason35054392009-01-21 13:11:13 -050010539/*
10540 * btrfs doesn't support the bmap operation because swapfiles
10541 * use bmap to make a mapping of extents in the file. They assume
10542 * these extents won't change over the life of the file and they
10543 * use the bmap result to do IO directly to the drive.
10544 *
10545 * the btrfs bmap call would return logical addresses that aren't
10546 * suitable for IO and they also will change frequently as COW
10547 * operations happen. So, swapfile + btrfs == corruption.
10548 *
10549 * For now we're avoiding this by dropping bmap.
10550 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010551static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010552 .readpage = btrfs_readpage,
10553 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010554 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010555 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010556 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010557 .invalidatepage = btrfs_invalidatepage,
10558 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010559 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010560 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010561};
10562
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010563static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010564 .readpage = btrfs_readpage,
10565 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010566 .invalidatepage = btrfs_invalidatepage,
10567 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010568};
10569
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010570static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010571 .getattr = btrfs_getattr,
10572 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010573 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010574 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010575 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010576 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010577 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010578 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010579};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010580static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010581 .getattr = btrfs_getattr,
10582 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010583 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010584 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010585 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010586 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010587 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010588};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010589static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010590 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010591 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010592 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010593 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010594 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010595 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010596};
Yan, Zheng76dda932009-09-21 16:00:26 -040010597
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010598const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010599 .d_delete = btrfs_dentry_delete,
10600};