blob: cbcda4e296f8b14a2693341b7b8123aa67f709fc [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;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1374 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001375 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001377 ret = btrfs_cross_ref_exist(root, ino,
1378 found_key.offset -
1379 extent_offset, disk_bytenr);
1380 if (ret) {
1381 /*
1382 * ret could be -EIO if the above fails to read
1383 * metadata.
1384 */
1385 if (ret < 0) {
1386 if (cow_start != (u64)-1)
1387 cur_offset = cow_start;
1388 goto error;
1389 }
1390
1391 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001392 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001393 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001395 disk_bytenr += cur_offset - found_key.offset;
1396 num_bytes = min(end + 1, extent_end) - cur_offset;
1397 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001398 * if there are pending snapshots for this root,
1399 * we fall into common COW way.
1400 */
1401 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001402 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 if (!err)
1404 goto out_check;
1405 }
1406 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001407 * force cow if csum exists in the range.
1408 * this ensure that csum for a given extent are
1409 * either valid or do not exist.
1410 */
Liu Bo58113752018-01-31 17:09:13 -07001411 ret = csum_exist_in_range(fs_info, disk_bytenr,
1412 num_bytes);
1413 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001414 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001415 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001416
1417 /*
1418 * ret could be -EIO if the above fails to read
1419 * metadata.
1420 */
1421 if (ret < 0) {
1422 if (cow_start != (u64)-1)
1423 cur_offset = cow_start;
1424 goto error;
1425 }
1426 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001427 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001428 }
1429 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1430 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001431 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001432 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001433 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 nocow = 1;
1435 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1436 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001437 btrfs_file_extent_inline_len(leaf,
1438 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001439 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001440 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 } else {
1442 BUG_ON(1);
1443 }
1444out_check:
1445 if (extent_end <= start) {
1446 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001447 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001448 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001450 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 goto next_slot;
1452 }
1453 if (!nocow) {
1454 if (cow_start == (u64)-1)
1455 cow_start = cur_offset;
1456 cur_offset = extent_end;
1457 if (cur_offset > end)
1458 break;
1459 path->slots[0]++;
1460 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001461 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001462
David Sterbab3b4aa72011-04-21 01:20:15 +02001463 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001465 ret = cow_file_range(inode, locked_page,
1466 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001467 end, page_started, nr_written, 1,
1468 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001469 if (ret) {
1470 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001471 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001472 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001473 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001474 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001476 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001477 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001478 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001479
Yan Zhengd899e052008-10-30 14:25:28 -04001480 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001481 u64 orig_start = found_key.offset - extent_offset;
1482
1483 em = create_io_em(inode, cur_offset, num_bytes,
1484 orig_start,
1485 disk_bytenr, /* block_start */
1486 num_bytes, /* block_len */
1487 disk_num_bytes, /* orig_block_len */
1488 ram_bytes, BTRFS_COMPRESS_NONE,
1489 BTRFS_ORDERED_PREALLOC);
1490 if (IS_ERR(em)) {
1491 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001492 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001493 if (nocow)
1494 btrfs_dec_nocow_writers(fs_info,
1495 disk_bytenr);
1496 ret = PTR_ERR(em);
1497 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001498 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001499 free_extent_map(em);
1500 }
1501
1502 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001503 type = BTRFS_ORDERED_PREALLOC;
1504 } else {
1505 type = BTRFS_ORDERED_NOCOW;
1506 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001507
1508 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001509 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001510 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001511 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001512 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001513
Yan, Zhengefa56462010-05-16 10:49:59 -04001514 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001515 BTRFS_DATA_RELOC_TREE_OBJECTID)
1516 /*
1517 * Error handled later, as we must prevent
1518 * extent_clear_unlock_delalloc() in error handler
1519 * from freeing metadata of created ordered extent.
1520 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001521 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1522 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001523
Josef Bacikc2790a22013-07-29 11:20:47 -04001524 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001525 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001526 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001527 EXTENT_DELALLOC |
1528 EXTENT_CLEAR_DATA_RESV,
1529 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1530
Wang Shilonge9894fd2014-03-27 11:12:25 +08001531 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001532 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001534
1535 /*
1536 * btrfs_reloc_clone_csums() error, now we're OK to call error
1537 * handler, as metadata for created ordered extent will only
1538 * be freed by btrfs_finish_ordered_io().
1539 */
1540 if (ret)
1541 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001542 if (cur_offset > end)
1543 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001544 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001545 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001546
Josef Bacik17ca04a2012-05-31 15:58:55 -04001547 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001549 cur_offset = end;
1550 }
1551
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001553 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1554 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001555 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001557 }
1558
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001560 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001561 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001562 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001563 EXTENT_DELALLOC | EXTENT_DEFRAG |
1564 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1565 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001566 PAGE_SET_WRITEBACK |
1567 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001568 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001569 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001570}
1571
Wang Shilong47059d92014-07-03 18:22:07 +08001572static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1573{
1574
1575 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1576 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1577 return 0;
1578
1579 /*
1580 * @defrag_bytes is a hint value, no spinlock held here,
1581 * if is not zero, it means the file is defragging.
1582 * Force cow if given extent needs to be defragged.
1583 */
1584 if (BTRFS_I(inode)->defrag_bytes &&
1585 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1586 EXTENT_DEFRAG, 0, NULL))
1587 return 1;
1588
1589 return 0;
1590}
1591
Chris Masond352ac62008-09-29 15:18:18 -04001592/*
1593 * extent_io.c call back to do delayed allocation processing
1594 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001595static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001596 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001597 unsigned long *nr_written,
1598 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001599{
Josef Bacikc6100a42017-05-05 11:57:13 -04001600 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001601 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001602 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001603 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001604
Wang Shilong47059d92014-07-03 18:22:07 +08001605 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001606 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001607 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001608 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001609 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001610 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001611 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001612 ret = cow_file_range(inode, locked_page, start, end, end,
1613 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001614 } else {
1615 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1616 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001617 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001618 page_started, nr_written,
1619 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001620 }
Qu Wenruo524272602017-03-08 10:25:52 +08001621 if (ret)
1622 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001623 return ret;
1624}
1625
Josef Bacikc6100a42017-05-05 11:57:13 -04001626static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001627 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001628{
Josef Bacikc6100a42017-05-05 11:57:13 -04001629 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 size;
1631
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001632 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001634 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 size = orig->end - orig->start + 1;
1637 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001638 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639 u64 new_size;
1640
1641 /*
Josef Bacikba117212015-03-13 15:01:24 -04001642 * See the explanation in btrfs_merge_extent_hook, the same
1643 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001644 */
1645 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001646 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001647 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001648 num_extents += count_max_extents(new_size);
1649 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001650 return;
1651 }
1652
Josef Bacik9e0baf62011-07-15 15:16:44 +00001653 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001654 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001655 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001656}
1657
1658/*
1659 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1660 * extents so we can keep track of new extents that are just merged onto old
1661 * extents, such as when we are doing sequential writes, so we can properly
1662 * account for the metadata space we'll need.
1663 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001664static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001665 struct extent_state *new,
1666 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667{
Josef Bacikc6100a42017-05-05 11:57:13 -04001668 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001669 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001670 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671
Josef Bacik9ed74f22009-09-11 16:12:44 -04001672 /* not delalloc, ignore it */
1673 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001674 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001675
Josef Bacik8461a3d2015-03-13 15:12:08 -04001676 if (new->start > other->start)
1677 new_size = new->end - other->start + 1;
1678 else
1679 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001680
1681 /* we're not bigger than the max, unreserve the space and go */
1682 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1683 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001684 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001685 spin_unlock(&BTRFS_I(inode)->lock);
1686 return;
1687 }
1688
1689 /*
Josef Bacikba117212015-03-13 15:01:24 -04001690 * We have to add up either side to figure out how many extents were
1691 * accounted for before we merged into one big extent. If the number of
1692 * extents we accounted for is <= the amount we need for the new range
1693 * then we can return, otherwise drop. Think of it like this
1694 *
1695 * [ 4k][MAX_SIZE]
1696 *
1697 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1698 * need 2 outstanding extents, on one side we have 1 and the other side
1699 * we have 1 so they are == and we can return. But in this case
1700 *
1701 * [MAX_SIZE+4k][MAX_SIZE+4k]
1702 *
1703 * Each range on their own accounts for 2 extents, but merged together
1704 * they are only 3 extents worth of accounting, so we need to drop in
1705 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001706 */
Josef Bacikba117212015-03-13 15:01:24 -04001707 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001708 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001709 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001710 num_extents += count_max_extents(old_size);
1711 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001712 return;
1713
Josef Bacik9e0baf62011-07-15 15:16:44 +00001714 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001715 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001716 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001717}
1718
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1720 struct inode *inode)
1721{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1723
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 spin_lock(&root->delalloc_lock);
1725 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1726 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1727 &root->delalloc_inodes);
1728 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1729 &BTRFS_I(inode)->runtime_flags);
1730 root->nr_delalloc_inodes++;
1731 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001732 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001733 BUG_ON(!list_empty(&root->delalloc_root));
1734 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001735 &fs_info->delalloc_roots);
1736 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 }
1738 }
1739 spin_unlock(&root->delalloc_lock);
1740}
1741
Nikolay Borisov2b877332018-04-27 12:21:51 +03001742
1743void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1744 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001747
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001748 if (!list_empty(&inode->delalloc_inodes)) {
1749 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001750 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001751 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752 root->nr_delalloc_inodes--;
1753 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001754 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001755 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001756 BUG_ON(list_empty(&root->delalloc_root));
1757 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001758 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 }
1760 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001761}
1762
1763static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1764 struct btrfs_inode *inode)
1765{
1766 spin_lock(&root->delalloc_lock);
1767 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001768 spin_unlock(&root->delalloc_lock);
1769}
1770
Chris Masond352ac62008-09-29 15:18:18 -04001771/*
1772 * extent_io.c set_bit_hook, used to track delayed allocation
1773 * bytes in this file, and to maintain the list of inodes that
1774 * have pending delalloc work to be done.
1775 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001776static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001777 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001778{
Josef Bacikc6100a42017-05-05 11:57:13 -04001779 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001780
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001781 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1782
Wang Shilong47059d92014-07-03 18:22:07 +08001783 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1784 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001785 /*
1786 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001787 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001788 * bit, which is only set or cleared with irqs on
1789 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001790 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001791 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001792 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001793 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001794 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001795
Josef Bacik8b62f872017-10-19 14:15:55 -04001796 spin_lock(&BTRFS_I(inode)->lock);
1797 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1798 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001799
Josef Bacik6a3891c2015-03-16 17:38:52 -04001800 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001801 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001802 return;
1803
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001804 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1805 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001806 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001807 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001808 if (*bits & EXTENT_DEFRAG)
1809 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001810 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001811 &BTRFS_I(inode)->runtime_flags))
1812 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001813 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001814 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001815
1816 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1817 (*bits & EXTENT_DELALLOC_NEW)) {
1818 spin_lock(&BTRFS_I(inode)->lock);
1819 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1820 state->start;
1821 spin_unlock(&BTRFS_I(inode)->lock);
1822 }
Chris Mason291d6732008-01-29 15:55:23 -05001823}
1824
Chris Masond352ac62008-09-29 15:18:18 -04001825/*
1826 * extent_io.c clear_bit_hook, see set_bit_hook for why
1827 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001828static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001829 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001830 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001831{
Josef Bacikc6100a42017-05-05 11:57:13 -04001832 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001834 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001835 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001836
Filipe Manana4a4b9642017-07-27 19:52:55 +01001837 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1838 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001839 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001840 spin_unlock(&inode->lock);
1841 }
Wang Shilong47059d92014-07-03 18:22:07 +08001842
Chris Mason75eff682008-12-15 15:54:40 -05001843 /*
1844 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001845 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001846 * bit, which is only set or cleared with irqs on
1847 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001848 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001849 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001850 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001851
Josef Bacik8b62f872017-10-19 14:15:55 -04001852 spin_lock(&inode->lock);
1853 btrfs_mod_outstanding_extents(inode, -num_extents);
1854 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001855
Josef Bacikb6d08f02013-09-27 14:57:43 -04001856 /*
1857 * We don't reserve metadata space for space cache inodes so we
1858 * don't need to call dellalloc_release_metadata if there is an
1859 * error.
1860 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001861 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001862 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001863 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001864
Josef Bacik6a3891c2015-03-16 17:38:52 -04001865 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001866 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001867 return;
1868
Filipe Mananaa315e682017-03-06 23:04:20 +00001869 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1870 do_list && !(state->state & EXTENT_NORESERVE) &&
1871 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001872 btrfs_free_reserved_data_space_noquota(
1873 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001874 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001875
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001876 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1877 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001878 spin_lock(&inode->lock);
1879 inode->delalloc_bytes -= len;
1880 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001881 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001882 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001883 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001884 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001885 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001886
1887 if ((state->state & EXTENT_DELALLOC_NEW) &&
1888 (*bits & EXTENT_DELALLOC_NEW)) {
1889 spin_lock(&inode->lock);
1890 ASSERT(inode->new_delalloc_bytes >= len);
1891 inode->new_delalloc_bytes -= len;
1892 spin_unlock(&inode->lock);
1893 }
Chris Mason291d6732008-01-29 15:55:23 -05001894}
1895
Chris Masond352ac62008-09-29 15:18:18 -04001896/*
1897 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1898 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001899 *
1900 * return 1 if page cannot be merged to bio
1901 * return 0 if page can be merged to bio
1902 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001903 */
Mike Christie81a75f672016-06-05 14:31:54 -05001904int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001905 size_t size, struct bio *bio,
1906 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001907{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001908 struct inode *inode = page->mapping->host;
1909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001910 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001911 u64 length = 0;
1912 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001913 int ret;
1914
Chris Mason771ed682008-11-06 22:02:51 -05001915 if (bio_flags & EXTENT_BIO_COMPRESSED)
1916 return 0;
1917
Kent Overstreet4f024f32013-10-11 15:44:27 -07001918 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001919 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001920 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1921 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001922 if (ret < 0)
1923 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001924 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001925 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001926 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001927}
1928
Chris Masond352ac62008-09-29 15:18:18 -04001929/*
1930 * in order to insert checksums into the metadata in large chunks,
1931 * we wait until bio submission time. All the pages in the bio are
1932 * checksummed and sums are attached onto the ordered extent record.
1933 *
1934 * At IO completion time the cums attached on the ordered extent record
1935 * are inserted into the btree
1936 */
David Sterbad0ee3932018-03-08 14:35:48 +01001937static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001938 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001939{
Josef Bacikc6100a42017-05-05 11:57:13 -04001940 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001941 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001942
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001943 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001944 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001945 return 0;
1946}
Chris Masone0156402008-04-16 11:15:20 -04001947
Chris Mason4a69a412008-11-06 22:03:00 -05001948/*
1949 * in order to insert checksums into the metadata in large chunks,
1950 * we wait until bio submission time. All the pages in the bio are
1951 * checksummed and sums are attached onto the ordered extent record.
1952 *
1953 * At IO completion time the cums attached on the ordered extent record
1954 * are inserted into the btree
1955 */
David Sterbad0ee3932018-03-08 14:35:48 +01001956static blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001957 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001958{
Josef Bacikc6100a42017-05-05 11:57:13 -04001959 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001960 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001961 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001962
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001963 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001964 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001965 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001966 bio_endio(bio);
1967 }
Stefan Behrens61891922012-11-05 18:51:52 +01001968 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001969}
1970
Chris Masond352ac62008-09-29 15:18:18 -04001971/*
Chris Masoncad321a2008-12-17 14:51:42 -05001972 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001973 * on write, or reading the csums from the tree before a read.
1974 *
1975 * Rules about async/sync submit,
1976 * a) read: sync submit
1977 *
1978 * b) write without checksum: sync submit
1979 *
1980 * c) write with checksum:
1981 * c-1) if bio is issued by fsync: sync submit
1982 * (sync_writers != 0)
1983 *
1984 * c-2) if root is reloc root: sync submit
1985 * (only in case of buffered IO)
1986 *
1987 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001988 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001989static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001990 int mirror_num, unsigned long bio_flags,
1991 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001992{
Josef Bacikc6100a42017-05-05 11:57:13 -04001993 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001994 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001995 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301996 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001997 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001998 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001999 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002000
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002001 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002002
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002003 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302004 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002005
Mike Christie37226b22016-06-05 14:31:52 -05002006 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002007 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002008 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002009 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002010
Chris Masond20f7042008-12-08 16:58:54 -05002011 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002012 ret = btrfs_submit_compressed_read(inode, bio,
2013 mirror_num,
2014 bio_flags);
2015 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002016 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002017 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002018 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002019 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002020 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002021 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002022 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002023 /* csum items have already been cloned */
2024 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2025 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002026 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002027 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2028 bio_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01002029 btrfs_submit_bio_start,
2030 btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002031 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002032 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002033 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002034 if (ret)
2035 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002036 }
2037
Chris Mason0b86a832008-03-24 15:01:56 -04002038mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002039 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002040
2041out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002042 if (ret) {
2043 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002044 bio_endio(bio);
2045 }
Stefan Behrens61891922012-11-05 18:51:52 +01002046 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002047}
Chris Mason6885f302008-02-20 16:11:05 -05002048
Chris Masond352ac62008-09-29 15:18:18 -04002049/*
2050 * given a list of ordered sums record them in the inode. This happens
2051 * at IO completion time based on sums calculated at bio submission time.
2052 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002053static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002054 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002055{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002056 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002057 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002058
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002059 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002060 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002061 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002062 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002063 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002064 if (ret)
2065 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066 }
2067 return 0;
2068}
2069
Josef Bacik2ac55d42010-02-03 19:33:23 +00002070int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002071 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002072 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002073{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002074 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002075 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002076 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002077}
2078
Chris Masond352ac62008-09-29 15:18:18 -04002079/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002080struct btrfs_writepage_fixup {
2081 struct page *page;
2082 struct btrfs_work work;
2083};
2084
Christoph Hellwigb2950862008-12-02 09:54:17 -05002085static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002086{
2087 struct btrfs_writepage_fixup *fixup;
2088 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002089 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002090 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002091 struct page *page;
2092 struct inode *inode;
2093 u64 page_start;
2094 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002095 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002096
2097 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2098 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002099again:
Chris Mason247e7432008-07-17 12:53:51 -04002100 lock_page(page);
2101 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2102 ClearPageChecked(page);
2103 goto out_page;
2104 }
2105
2106 inode = page->mapping->host;
2107 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002108 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002109
David Sterbaff13db42015-12-03 14:30:40 +01002110 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002111 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002112
2113 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002114 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002115 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002116
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002117 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002119 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002120 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002121 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002122 unlock_page(page);
2123 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002124 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002125 goto again;
2126 }
Chris Mason247e7432008-07-17 12:53:51 -04002127
Qu Wenruo364ecf32017-02-27 15:10:38 +08002128 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002129 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002130 if (ret) {
2131 mapping_set_error(page->mapping, ret);
2132 end_extent_writepage(page, ret, page_start, page_end);
2133 ClearPageChecked(page);
2134 goto out;
2135 }
2136
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002137 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2138 &cached_state, 0);
2139 if (ret) {
2140 mapping_set_error(page->mapping, ret);
2141 end_extent_writepage(page, ret, page_start, page_end);
2142 ClearPageChecked(page);
2143 goto out;
2144 }
2145
Chris Mason247e7432008-07-17 12:53:51 -04002146 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002147 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002148 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002149out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002150 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002151 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002152out_page:
2153 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002154 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002155 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002156 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002157}
2158
2159/*
2160 * There are a few paths in the higher layers of the kernel that directly
2161 * set the page dirty bit without asking the filesystem if it is a
2162 * good idea. This causes problems because we want to make sure COW
2163 * properly happens and the data=ordered rules are followed.
2164 *
Chris Masonc8b97812008-10-29 14:49:59 -04002165 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002166 * hasn't been properly setup for IO. We kick off an async process
2167 * to fix it up. The async helper will wait for ordered extents, set
2168 * the delalloc bit and make it safe to write the page.
2169 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002170static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002171{
2172 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002174 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002175
Chris Mason8b62b722009-09-02 16:53:46 -04002176 /* this page is properly in the ordered list */
2177 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002178 return 0;
2179
2180 if (PageChecked(page))
2181 return -EAGAIN;
2182
2183 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2184 if (!fixup)
2185 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002186
Chris Mason247e7432008-07-17 12:53:51 -04002187 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002188 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002189 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2190 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002191 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002192 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002193 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002194}
2195
Yan Zhengd899e052008-10-30 14:25:28 -04002196static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2197 struct inode *inode, u64 file_pos,
2198 u64 disk_bytenr, u64 disk_num_bytes,
2199 u64 num_bytes, u64 ram_bytes,
2200 u8 compression, u8 encryption,
2201 u16 other_encoding, int extent_type)
2202{
2203 struct btrfs_root *root = BTRFS_I(inode)->root;
2204 struct btrfs_file_extent_item *fi;
2205 struct btrfs_path *path;
2206 struct extent_buffer *leaf;
2207 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002208 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002209 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002210 int ret;
2211
2212 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002213 if (!path)
2214 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002215
Chris Masona1ed8352009-09-11 12:27:37 -04002216 /*
2217 * we may be replacing one extent in the tree with another.
2218 * The new extent is pinned in the extent map, and we don't want
2219 * to drop it from the cache until it is completely in the btree.
2220 *
2221 * So, tell btrfs_drop_extents to leave this extent in the cache.
2222 * the caller is expected to unpin it and allow it to be merged
2223 * with the others.
2224 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002225 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2226 file_pos + num_bytes, NULL, 0,
2227 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228 if (ret)
2229 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002230
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002231 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002232 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002233 ins.offset = file_pos;
2234 ins.type = BTRFS_EXTENT_DATA_KEY;
2235
2236 path->leave_spinning = 1;
2237 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2238 sizeof(*fi));
2239 if (ret)
2240 goto out;
2241 }
Yan Zhengd899e052008-10-30 14:25:28 -04002242 leaf = path->nodes[0];
2243 fi = btrfs_item_ptr(leaf, path->slots[0],
2244 struct btrfs_file_extent_item);
2245 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2246 btrfs_set_file_extent_type(leaf, fi, extent_type);
2247 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2248 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2249 btrfs_set_file_extent_offset(leaf, fi, 0);
2250 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2251 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2252 btrfs_set_file_extent_compression(leaf, fi, compression);
2253 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2254 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002255
Yan Zhengd899e052008-10-30 14:25:28 -04002256 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002257 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002258
2259 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002260
2261 ins.objectid = disk_bytenr;
2262 ins.offset = disk_num_bytes;
2263 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002264
Qu Wenruo297d7502015-09-08 17:08:37 +08002265 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002266 * Release the reserved range from inode dirty range map, as it is
2267 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002268 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002269 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2270 if (ret < 0)
2271 goto out;
2272 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002273 ret = btrfs_alloc_reserved_file_extent(trans, root,
2274 btrfs_ino(BTRFS_I(inode)),
2275 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002276out:
Yan Zhengd899e052008-10-30 14:25:28 -04002277 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002278
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002280}
2281
Liu Bo38c227d2013-01-29 03:18:40 +00002282/* snapshot-aware defrag */
2283struct sa_defrag_extent_backref {
2284 struct rb_node node;
2285 struct old_sa_defrag_extent *old;
2286 u64 root_id;
2287 u64 inum;
2288 u64 file_pos;
2289 u64 extent_offset;
2290 u64 num_bytes;
2291 u64 generation;
2292};
2293
2294struct old_sa_defrag_extent {
2295 struct list_head list;
2296 struct new_sa_defrag_extent *new;
2297
2298 u64 extent_offset;
2299 u64 bytenr;
2300 u64 offset;
2301 u64 len;
2302 int count;
2303};
2304
2305struct new_sa_defrag_extent {
2306 struct rb_root root;
2307 struct list_head head;
2308 struct btrfs_path *path;
2309 struct inode *inode;
2310 u64 file_pos;
2311 u64 len;
2312 u64 bytenr;
2313 u64 disk_len;
2314 u8 compress_type;
2315};
2316
2317static int backref_comp(struct sa_defrag_extent_backref *b1,
2318 struct sa_defrag_extent_backref *b2)
2319{
2320 if (b1->root_id < b2->root_id)
2321 return -1;
2322 else if (b1->root_id > b2->root_id)
2323 return 1;
2324
2325 if (b1->inum < b2->inum)
2326 return -1;
2327 else if (b1->inum > b2->inum)
2328 return 1;
2329
2330 if (b1->file_pos < b2->file_pos)
2331 return -1;
2332 else if (b1->file_pos > b2->file_pos)
2333 return 1;
2334
2335 /*
2336 * [------------------------------] ===> (a range of space)
2337 * |<--->| |<---->| =============> (fs/file tree A)
2338 * |<---------------------------->| ===> (fs/file tree B)
2339 *
2340 * A range of space can refer to two file extents in one tree while
2341 * refer to only one file extent in another tree.
2342 *
2343 * So we may process a disk offset more than one time(two extents in A)
2344 * and locate at the same extent(one extent in B), then insert two same
2345 * backrefs(both refer to the extent in B).
2346 */
2347 return 0;
2348}
2349
2350static void backref_insert(struct rb_root *root,
2351 struct sa_defrag_extent_backref *backref)
2352{
2353 struct rb_node **p = &root->rb_node;
2354 struct rb_node *parent = NULL;
2355 struct sa_defrag_extent_backref *entry;
2356 int ret;
2357
2358 while (*p) {
2359 parent = *p;
2360 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2361
2362 ret = backref_comp(backref, entry);
2363 if (ret < 0)
2364 p = &(*p)->rb_left;
2365 else
2366 p = &(*p)->rb_right;
2367 }
2368
2369 rb_link_node(&backref->node, parent, p);
2370 rb_insert_color(&backref->node, root);
2371}
2372
2373/*
2374 * Note the backref might has changed, and in this case we just return 0.
2375 */
2376static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2377 void *ctx)
2378{
2379 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002380 struct old_sa_defrag_extent *old = ctx;
2381 struct new_sa_defrag_extent *new = old->new;
2382 struct btrfs_path *path = new->path;
2383 struct btrfs_key key;
2384 struct btrfs_root *root;
2385 struct sa_defrag_extent_backref *backref;
2386 struct extent_buffer *leaf;
2387 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002388 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002389 int slot;
2390 int ret;
2391 u64 extent_offset;
2392 u64 num_bytes;
2393
2394 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002395 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002396 return 0;
2397
2398 key.objectid = root_id;
2399 key.type = BTRFS_ROOT_ITEM_KEY;
2400 key.offset = (u64)-1;
2401
Liu Bo38c227d2013-01-29 03:18:40 +00002402 root = btrfs_read_fs_root_no_name(fs_info, &key);
2403 if (IS_ERR(root)) {
2404 if (PTR_ERR(root) == -ENOENT)
2405 return 0;
2406 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002407 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002408 inum, offset, root_id);
2409 return PTR_ERR(root);
2410 }
2411
2412 key.objectid = inum;
2413 key.type = BTRFS_EXTENT_DATA_KEY;
2414 if (offset > (u64)-1 << 32)
2415 key.offset = 0;
2416 else
2417 key.offset = offset;
2418
2419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302420 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002421 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002422 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002423
2424 while (1) {
2425 cond_resched();
2426
2427 leaf = path->nodes[0];
2428 slot = path->slots[0];
2429
2430 if (slot >= btrfs_header_nritems(leaf)) {
2431 ret = btrfs_next_leaf(root, path);
2432 if (ret < 0) {
2433 goto out;
2434 } else if (ret > 0) {
2435 ret = 0;
2436 goto out;
2437 }
2438 continue;
2439 }
2440
2441 path->slots[0]++;
2442
2443 btrfs_item_key_to_cpu(leaf, &key, slot);
2444
2445 if (key.objectid > inum)
2446 goto out;
2447
2448 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2449 continue;
2450
2451 extent = btrfs_item_ptr(leaf, slot,
2452 struct btrfs_file_extent_item);
2453
2454 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2455 continue;
2456
Liu Boe68afa42013-07-01 22:13:26 +08002457 /*
2458 * 'offset' refers to the exact key.offset,
2459 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2460 * (key.offset - extent_offset).
2461 */
2462 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002463 continue;
2464
Liu Boe68afa42013-07-01 22:13:26 +08002465 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002466 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002467
Liu Bo38c227d2013-01-29 03:18:40 +00002468 if (extent_offset >= old->extent_offset + old->offset +
2469 old->len || extent_offset + num_bytes <=
2470 old->extent_offset + old->offset)
2471 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002472 break;
2473 }
2474
2475 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2476 if (!backref) {
2477 ret = -ENOENT;
2478 goto out;
2479 }
2480
2481 backref->root_id = root_id;
2482 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002483 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002484 backref->num_bytes = num_bytes;
2485 backref->extent_offset = extent_offset;
2486 backref->generation = btrfs_file_extent_generation(leaf, extent);
2487 backref->old = old;
2488 backref_insert(&new->root, backref);
2489 old->count++;
2490out:
2491 btrfs_release_path(path);
2492 WARN_ON(ret);
2493 return ret;
2494}
2495
2496static noinline bool record_extent_backrefs(struct btrfs_path *path,
2497 struct new_sa_defrag_extent *new)
2498{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002500 struct old_sa_defrag_extent *old, *tmp;
2501 int ret;
2502
2503 new->path = path;
2504
2505 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002506 ret = iterate_inodes_from_logical(old->bytenr +
2507 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002508 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002509 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002510 if (ret < 0 && ret != -ENOENT)
2511 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002512
2513 /* no backref to be processed for this extent */
2514 if (!old->count) {
2515 list_del(&old->list);
2516 kfree(old);
2517 }
2518 }
2519
2520 if (list_empty(&new->head))
2521 return false;
2522
2523 return true;
2524}
2525
2526static int relink_is_mergable(struct extent_buffer *leaf,
2527 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002528 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002529{
Liu Bo116e0022013-08-02 16:30:40 +08002530 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002531 return 0;
2532
2533 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2534 return 0;
2535
Liu Bo116e0022013-08-02 16:30:40 +08002536 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2537 return 0;
2538
2539 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002540 btrfs_file_extent_other_encoding(leaf, fi))
2541 return 0;
2542
2543 return 1;
2544}
2545
2546/*
2547 * Note the backref might has changed, and in this case we just return 0.
2548 */
2549static noinline int relink_extent_backref(struct btrfs_path *path,
2550 struct sa_defrag_extent_backref *prev,
2551 struct sa_defrag_extent_backref *backref)
2552{
2553 struct btrfs_file_extent_item *extent;
2554 struct btrfs_file_extent_item *item;
2555 struct btrfs_ordered_extent *ordered;
2556 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002557 struct btrfs_root *root;
2558 struct btrfs_key key;
2559 struct extent_buffer *leaf;
2560 struct old_sa_defrag_extent *old = backref->old;
2561 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002562 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002563 struct inode *inode;
2564 struct extent_state *cached = NULL;
2565 int ret = 0;
2566 u64 start;
2567 u64 len;
2568 u64 lock_start;
2569 u64 lock_end;
2570 bool merge = false;
2571 int index;
2572
2573 if (prev && prev->root_id == backref->root_id &&
2574 prev->inum == backref->inum &&
2575 prev->file_pos + prev->num_bytes == backref->file_pos)
2576 merge = true;
2577
2578 /* step 1: get root */
2579 key.objectid = backref->root_id;
2580 key.type = BTRFS_ROOT_ITEM_KEY;
2581 key.offset = (u64)-1;
2582
Liu Bo38c227d2013-01-29 03:18:40 +00002583 index = srcu_read_lock(&fs_info->subvol_srcu);
2584
2585 root = btrfs_read_fs_root_no_name(fs_info, &key);
2586 if (IS_ERR(root)) {
2587 srcu_read_unlock(&fs_info->subvol_srcu, index);
2588 if (PTR_ERR(root) == -ENOENT)
2589 return 0;
2590 return PTR_ERR(root);
2591 }
Liu Bo38c227d2013-01-29 03:18:40 +00002592
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002593 if (btrfs_root_readonly(root)) {
2594 srcu_read_unlock(&fs_info->subvol_srcu, index);
2595 return 0;
2596 }
2597
Liu Bo38c227d2013-01-29 03:18:40 +00002598 /* step 2: get inode */
2599 key.objectid = backref->inum;
2600 key.type = BTRFS_INODE_ITEM_KEY;
2601 key.offset = 0;
2602
2603 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2604 if (IS_ERR(inode)) {
2605 srcu_read_unlock(&fs_info->subvol_srcu, index);
2606 return 0;
2607 }
2608
2609 srcu_read_unlock(&fs_info->subvol_srcu, index);
2610
2611 /* step 3: relink backref */
2612 lock_start = backref->file_pos;
2613 lock_end = backref->file_pos + backref->num_bytes - 1;
2614 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002615 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002616
2617 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2618 if (ordered) {
2619 btrfs_put_ordered_extent(ordered);
2620 goto out_unlock;
2621 }
2622
2623 trans = btrfs_join_transaction(root);
2624 if (IS_ERR(trans)) {
2625 ret = PTR_ERR(trans);
2626 goto out_unlock;
2627 }
2628
2629 key.objectid = backref->inum;
2630 key.type = BTRFS_EXTENT_DATA_KEY;
2631 key.offset = backref->file_pos;
2632
2633 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2634 if (ret < 0) {
2635 goto out_free_path;
2636 } else if (ret > 0) {
2637 ret = 0;
2638 goto out_free_path;
2639 }
2640
2641 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2642 struct btrfs_file_extent_item);
2643
2644 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2645 backref->generation)
2646 goto out_free_path;
2647
2648 btrfs_release_path(path);
2649
2650 start = backref->file_pos;
2651 if (backref->extent_offset < old->extent_offset + old->offset)
2652 start += old->extent_offset + old->offset -
2653 backref->extent_offset;
2654
2655 len = min(backref->extent_offset + backref->num_bytes,
2656 old->extent_offset + old->offset + old->len);
2657 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2658
2659 ret = btrfs_drop_extents(trans, root, inode, start,
2660 start + len, 1);
2661 if (ret)
2662 goto out_free_path;
2663again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002664 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002665 key.type = BTRFS_EXTENT_DATA_KEY;
2666 key.offset = start;
2667
Liu Boa09a0a72013-03-11 09:20:58 +00002668 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002669 if (merge) {
2670 struct btrfs_file_extent_item *fi;
2671 u64 extent_len;
2672 struct btrfs_key found_key;
2673
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002674 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002675 if (ret < 0)
2676 goto out_free_path;
2677
2678 path->slots[0]--;
2679 leaf = path->nodes[0];
2680 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2681
2682 fi = btrfs_item_ptr(leaf, path->slots[0],
2683 struct btrfs_file_extent_item);
2684 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2685
Liu Bo116e0022013-08-02 16:30:40 +08002686 if (extent_len + found_key.offset == start &&
2687 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002688 btrfs_set_file_extent_num_bytes(leaf, fi,
2689 extent_len + len);
2690 btrfs_mark_buffer_dirty(leaf);
2691 inode_add_bytes(inode, len);
2692
2693 ret = 1;
2694 goto out_free_path;
2695 } else {
2696 merge = false;
2697 btrfs_release_path(path);
2698 goto again;
2699 }
2700 }
2701
2702 ret = btrfs_insert_empty_item(trans, root, path, &key,
2703 sizeof(*extent));
2704 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002705 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002706 goto out_free_path;
2707 }
2708
2709 leaf = path->nodes[0];
2710 item = btrfs_item_ptr(leaf, path->slots[0],
2711 struct btrfs_file_extent_item);
2712 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2713 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2714 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2715 btrfs_set_file_extent_num_bytes(leaf, item, len);
2716 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2717 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2718 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2719 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2720 btrfs_set_file_extent_encryption(leaf, item, 0);
2721 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2722
2723 btrfs_mark_buffer_dirty(leaf);
2724 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002725 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002726
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002727 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002728 new->disk_len, 0,
2729 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002730 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002731 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002732 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002733 goto out_free_path;
2734 }
2735
2736 ret = 1;
2737out_free_path:
2738 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002739 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002740 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002741out_unlock:
2742 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002743 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002744 iput(inode);
2745 return ret;
2746}
2747
Liu Bo6f519562013-10-29 10:45:05 +08002748static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2749{
2750 struct old_sa_defrag_extent *old, *tmp;
2751
2752 if (!new)
2753 return;
2754
2755 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002756 kfree(old);
2757 }
2758 kfree(new);
2759}
2760
Liu Bo38c227d2013-01-29 03:18:40 +00002761static void relink_file_extents(struct new_sa_defrag_extent *new)
2762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002763 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002764 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002765 struct sa_defrag_extent_backref *backref;
2766 struct sa_defrag_extent_backref *prev = NULL;
2767 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002768 struct rb_node *node;
2769 int ret;
2770
2771 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002772
2773 path = btrfs_alloc_path();
2774 if (!path)
2775 return;
2776
2777 if (!record_extent_backrefs(path, new)) {
2778 btrfs_free_path(path);
2779 goto out;
2780 }
2781 btrfs_release_path(path);
2782
2783 while (1) {
2784 node = rb_first(&new->root);
2785 if (!node)
2786 break;
2787 rb_erase(node, &new->root);
2788
2789 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2790
2791 ret = relink_extent_backref(path, prev, backref);
2792 WARN_ON(ret < 0);
2793
2794 kfree(prev);
2795
2796 if (ret == 1)
2797 prev = backref;
2798 else
2799 prev = NULL;
2800 cond_resched();
2801 }
2802 kfree(prev);
2803
2804 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002805out:
Liu Bo6f519562013-10-29 10:45:05 +08002806 free_sa_defrag_extent(new);
2807
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 atomic_dec(&fs_info->defrag_running);
2809 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002810}
2811
2812static struct new_sa_defrag_extent *
2813record_old_file_extents(struct inode *inode,
2814 struct btrfs_ordered_extent *ordered)
2815{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002817 struct btrfs_root *root = BTRFS_I(inode)->root;
2818 struct btrfs_path *path;
2819 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002820 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002821 struct new_sa_defrag_extent *new;
2822 int ret;
2823
2824 new = kmalloc(sizeof(*new), GFP_NOFS);
2825 if (!new)
2826 return NULL;
2827
2828 new->inode = inode;
2829 new->file_pos = ordered->file_offset;
2830 new->len = ordered->len;
2831 new->bytenr = ordered->start;
2832 new->disk_len = ordered->disk_len;
2833 new->compress_type = ordered->compress_type;
2834 new->root = RB_ROOT;
2835 INIT_LIST_HEAD(&new->head);
2836
2837 path = btrfs_alloc_path();
2838 if (!path)
2839 goto out_kfree;
2840
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002841 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002842 key.type = BTRFS_EXTENT_DATA_KEY;
2843 key.offset = new->file_pos;
2844
2845 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2846 if (ret < 0)
2847 goto out_free_path;
2848 if (ret > 0 && path->slots[0] > 0)
2849 path->slots[0]--;
2850
2851 /* find out all the old extents for the file range */
2852 while (1) {
2853 struct btrfs_file_extent_item *extent;
2854 struct extent_buffer *l;
2855 int slot;
2856 u64 num_bytes;
2857 u64 offset;
2858 u64 end;
2859 u64 disk_bytenr;
2860 u64 extent_offset;
2861
2862 l = path->nodes[0];
2863 slot = path->slots[0];
2864
2865 if (slot >= btrfs_header_nritems(l)) {
2866 ret = btrfs_next_leaf(root, path);
2867 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002868 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002869 else if (ret > 0)
2870 break;
2871 continue;
2872 }
2873
2874 btrfs_item_key_to_cpu(l, &key, slot);
2875
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002876 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002877 break;
2878 if (key.type != BTRFS_EXTENT_DATA_KEY)
2879 break;
2880 if (key.offset >= new->file_pos + new->len)
2881 break;
2882
2883 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2884
2885 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2886 if (key.offset + num_bytes < new->file_pos)
2887 goto next;
2888
2889 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2890 if (!disk_bytenr)
2891 goto next;
2892
2893 extent_offset = btrfs_file_extent_offset(l, extent);
2894
2895 old = kmalloc(sizeof(*old), GFP_NOFS);
2896 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002897 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002898
2899 offset = max(new->file_pos, key.offset);
2900 end = min(new->file_pos + new->len, key.offset + num_bytes);
2901
2902 old->bytenr = disk_bytenr;
2903 old->extent_offset = extent_offset;
2904 old->offset = offset - key.offset;
2905 old->len = end - offset;
2906 old->new = new;
2907 old->count = 0;
2908 list_add_tail(&old->list, &new->head);
2909next:
2910 path->slots[0]++;
2911 cond_resched();
2912 }
2913
2914 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002916
2917 return new;
2918
Liu Bo38c227d2013-01-29 03:18:40 +00002919out_free_path:
2920 btrfs_free_path(path);
2921out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002922 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002923 return NULL;
2924}
2925
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002926static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002927 u64 start, u64 len)
2928{
2929 struct btrfs_block_group_cache *cache;
2930
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002931 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002932 ASSERT(cache);
2933
2934 spin_lock(&cache->lock);
2935 cache->delalloc_bytes -= len;
2936 spin_unlock(&cache->lock);
2937
2938 btrfs_put_block_group(cache);
2939}
2940
Chris Masond352ac62008-09-29 15:18:18 -04002941/* as ordered data IO finishes, this gets called so we can finish
2942 * an ordered extent if the range of bytes in the file it covers are
2943 * fully written.
2944 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002945static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002946{
Josef Bacik5fd02042012-05-02 14:00:54 -04002947 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002948 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002949 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002950 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002951 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002952 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002953 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002954 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002955 int ret = 0;
2956 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002957 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002958 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002959 bool range_locked = false;
2960 bool clear_new_delalloc_bytes = false;
2961
2962 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2963 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2964 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2965 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002967 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002968
Josef Bacik5fd02042012-05-02 14:00:54 -04002969 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2970 ret = -EIO;
2971 goto out;
2972 }
2973
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002974 btrfs_free_io_failure_record(BTRFS_I(inode),
2975 ordered_extent->file_offset,
2976 ordered_extent->file_offset +
2977 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002978
Josef Bacik77cef2e2013-08-29 13:57:21 -04002979 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2980 truncated = true;
2981 logical_len = ordered_extent->truncated_len;
2982 /* Truncated the entire extent, don't bother adding */
2983 if (!logical_len)
2984 goto out;
2985 }
2986
Yan, Zhengc2167752009-11-12 09:34:21 +00002987 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002988 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002989
2990 /*
2991 * For mwrite(mmap + memset to write) case, we still reserve
2992 * space for NOCOW range.
2993 * As NOCOW won't cause a new delayed ref, just free the space
2994 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002995 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002996 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002997 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2998 if (nolock)
2999 trans = btrfs_join_transaction_nolock(root);
3000 else
3001 trans = btrfs_join_transaction(root);
3002 if (IS_ERR(trans)) {
3003 ret = PTR_ERR(trans);
3004 trans = NULL;
3005 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003006 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003007 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003008 ret = btrfs_update_inode_fallback(trans, root, inode);
3009 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003010 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003011 goto out;
3012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003013
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003014 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003015 lock_extent_bits(io_tree, ordered_extent->file_offset,
3016 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003017 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003018
Liu Bo38c227d2013-01-29 03:18:40 +00003019 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3020 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003021 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003022 if (ret) {
3023 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003024 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003025 /* the inode is shared */
3026 new = record_old_file_extents(inode, ordered_extent);
3027
3028 clear_extent_bit(io_tree, ordered_extent->file_offset,
3029 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003030 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003031 }
3032
Josef Bacik0cb59c92010-07-02 12:14:14 -04003033 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003034 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003035 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003036 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003037 if (IS_ERR(trans)) {
3038 ret = PTR_ERR(trans);
3039 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003040 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003041 }
Chris Masona79b7d42014-05-22 16:18:52 -07003042
Josef Bacik69fe2d72017-10-19 14:15:57 -04003043 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003044
Chris Masonc8b97812008-10-29 14:49:59 -04003045 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003046 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003047 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003048 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003049 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3050 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003051 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003052 ordered_extent->file_offset,
3053 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003054 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003055 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003056 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003057 ret = insert_reserved_file_extent(trans, inode,
3058 ordered_extent->file_offset,
3059 ordered_extent->start,
3060 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003061 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003062 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003063 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003064 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003065 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003066 ordered_extent->start,
3067 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003068 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003069 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3070 ordered_extent->file_offset, ordered_extent->len,
3071 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003072 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003073 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003074 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003075 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003076
Nikolay Borisovac01f262018-01-08 10:59:43 +02003077 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3078 if (ret) {
3079 btrfs_abort_transaction(trans, ret);
3080 goto out;
3081 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003082
Josef Bacik6c760c02012-11-09 10:53:21 -05003083 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3084 ret = btrfs_update_inode_fallback(trans, root, inode);
3085 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003086 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003087 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003088 }
3089 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003090out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003091 if (range_locked || clear_new_delalloc_bytes) {
3092 unsigned int clear_bits = 0;
3093
3094 if (range_locked)
3095 clear_bits |= EXTENT_LOCKED;
3096 if (clear_new_delalloc_bytes)
3097 clear_bits |= EXTENT_DELALLOC_NEW;
3098 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3099 ordered_extent->file_offset,
3100 ordered_extent->file_offset +
3101 ordered_extent->len - 1,
3102 clear_bits,
3103 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003104 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003105 }
3106
Miao Xiea698d0752012-09-20 01:51:59 -06003107 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003108 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003109
Josef Bacik77cef2e2013-08-29 13:57:21 -04003110 if (ret || truncated) {
3111 u64 start, end;
3112
3113 if (truncated)
3114 start = ordered_extent->file_offset + logical_len;
3115 else
3116 start = ordered_extent->file_offset;
3117 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003118 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003119
3120 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003121 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003122
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003123 /*
3124 * If the ordered extent had an IOERR or something else went
3125 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003126 * back to the allocator. We only free the extent in the
3127 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003128 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003129 if ((ret || !logical_len) &&
3130 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003131 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003132 btrfs_free_reserved_extent(fs_info,
3133 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003134 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003135 }
3136
3137
Josef Bacik5fd02042012-05-02 14:00:54 -04003138 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003139 * This needs to be done to make sure anybody waiting knows we are done
3140 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003141 */
3142 btrfs_remove_ordered_extent(inode, ordered_extent);
3143
Liu Bo38c227d2013-01-29 03:18:40 +00003144 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003145 if (new) {
3146 if (ret) {
3147 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003148 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003149 } else {
3150 relink_file_extents(new);
3151 }
3152 }
Liu Bo38c227d2013-01-29 03:18:40 +00003153
Chris Masone6dcd2d2008-07-17 12:53:50 -04003154 /* once for us */
3155 btrfs_put_ordered_extent(ordered_extent);
3156 /* once for the tree */
3157 btrfs_put_ordered_extent(ordered_extent);
3158
Josef Bacik5fd02042012-05-02 14:00:54 -04003159 return ret;
3160}
3161
3162static void finish_ordered_fn(struct btrfs_work *work)
3163{
3164 struct btrfs_ordered_extent *ordered_extent;
3165 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3166 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003167}
3168
David Sterbac3988d62017-02-17 15:18:32 +01003169static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003170 struct extent_state *state, int uptodate)
3171{
Josef Bacik5fd02042012-05-02 14:00:54 -04003172 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003174 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003175 struct btrfs_workqueue *wq;
3176 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003177
liubo1abe9b82011-03-24 11:18:59 +00003178 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3179
Chris Mason8b62b722009-09-02 16:53:46 -04003180 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003181 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3182 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003183 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003184
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003185 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003186 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003187 func = btrfs_freespace_write_helper;
3188 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003189 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003190 func = btrfs_endio_write_helper;
3191 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003192
Liu Bo9e0af232014-08-15 23:36:53 +08003193 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3194 NULL);
3195 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003196}
3197
Miao Xiedc380ae2014-09-12 18:43:55 +08003198static int __readpage_endio_check(struct inode *inode,
3199 struct btrfs_io_bio *io_bio,
3200 int icsum, struct page *page,
3201 int pgoff, u64 start, size_t len)
3202{
3203 char *kaddr;
3204 u32 csum_expected;
3205 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003206
3207 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3208
3209 kaddr = kmap_atomic(page);
3210 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003211 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003212 if (csum != csum_expected)
3213 goto zeroit;
3214
3215 kunmap_atomic(kaddr);
3216 return 0;
3217zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003218 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003219 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003220 memset(kaddr + pgoff, 1, len);
3221 flush_dcache_page(page);
3222 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003223 return -EIO;
3224}
3225
Chris Masond352ac62008-09-29 15:18:18 -04003226/*
Chris Masond352ac62008-09-29 15:18:18 -04003227 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003228 * if there's a match, we allow the bio to finish. If not, the code in
3229 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003230 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003231static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3232 u64 phy_offset, struct page *page,
3233 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003234{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003235 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003236 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003237 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003238 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003239
Chris Masond20f7042008-12-08 16:58:54 -05003240 if (PageChecked(page)) {
3241 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003242 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003243 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003244
3245 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003246 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003247
Yan Zheng17d217f2008-12-12 10:03:38 -05003248 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003249 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003250 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003251 return 0;
3252 }
3253
Miao Xiefacc8a222013-07-25 19:22:34 +08003254 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003255 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3256 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003257}
Chris Masonb888db22007-08-27 16:49:44 -04003258
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003259/*
3260 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3261 *
3262 * @inode: The inode we want to perform iput on
3263 *
3264 * This function uses the generic vfs_inode::i_count to track whether we should
3265 * just decrement it (in case it's > 1) or if this is the last iput then link
3266 * the inode to the delayed iput machinery. Delayed iputs are processed at
3267 * transaction commit time/superblock commit/cleaner kthread.
3268 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269void btrfs_add_delayed_iput(struct inode *inode)
3270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003272 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003273
3274 if (atomic_add_unless(&inode->i_count, -1, 1))
3275 return;
3276
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003277 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003278 ASSERT(list_empty(&binode->delayed_iput));
3279 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003280 spin_unlock(&fs_info->delayed_iput_lock);
3281}
3282
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003283void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003284{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003285
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003286 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003287 while (!list_empty(&fs_info->delayed_iputs)) {
3288 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003289
David Sterba8089fe62015-11-19 14:15:51 +01003290 inode = list_first_entry(&fs_info->delayed_iputs,
3291 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003292 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003293 spin_unlock(&fs_info->delayed_iput_lock);
3294 iput(&inode->vfs_inode);
3295 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003296 }
David Sterba8089fe62015-11-19 14:15:51 +01003297 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003298}
3299
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003301 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 * files in the subvolume, it removes orphan item and frees block_rsv
3303 * structure.
3304 */
3305void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3306 struct btrfs_root *root)
3307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003308 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003309 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 int ret;
3311
Josef Bacik8a35d952012-05-23 14:26:42 -04003312 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3314 return;
3315
Josef Bacik90290e12011-12-02 15:44:12 -05003316 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003317 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003318 spin_unlock(&root->orphan_lock);
3319 return;
3320 }
3321
3322 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3323 spin_unlock(&root->orphan_lock);
3324 return;
3325 }
3326
3327 block_rsv = root->orphan_block_rsv;
3328 root->orphan_block_rsv = NULL;
3329 spin_unlock(&root->orphan_lock);
3330
Miao Xie27cdeb72014-04-02 19:51:05 +08003331 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003332 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003333 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003335 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003336 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003337 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003338 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3339 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 }
3341
Josef Bacik90290e12011-12-02 15:44:12 -05003342 if (block_rsv) {
3343 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003344 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 }
3346}
3347
3348/*
Josef Bacik7b128762008-07-24 12:17:14 -04003349 * This creates an orphan entry for the given inode in case something goes
3350 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 *
3352 * NOTE: caller of this function should reserve 5 units of metadata for
3353 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003354 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003355int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3356 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003357{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3359 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003360 struct btrfs_block_rsv *block_rsv = NULL;
3361 int reserve = 0;
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003362 bool insert = false;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003364
Yan, Zhengd68fc572010-05-16 10:49:58 -04003365 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003366 block_rsv = btrfs_alloc_block_rsv(fs_info,
3367 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003368 if (!block_rsv)
3369 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003370 }
3371
Josef Bacik8a35d952012-05-23 14:26:42 -04003372 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003373 &inode->runtime_flags))
3374 insert = true;
Josef Bacik7b128762008-07-24 12:17:14 -04003375
Josef Bacik72ac3c02012-05-23 14:13:11 -04003376 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003377 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003378 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003379
3380 spin_lock(&root->orphan_lock);
3381 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3382 if (!root->orphan_block_rsv) {
3383 root->orphan_block_rsv = block_rsv;
3384 } else if (block_rsv) {
3385 btrfs_free_block_rsv(fs_info, block_rsv);
3386 block_rsv = NULL;
3387 }
3388
3389 if (insert)
3390 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003391 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003392
Yan, Zhengd68fc572010-05-16 10:49:58 -04003393 /* grab metadata reservation from transaction handle */
3394 if (reserve) {
3395 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003396 ASSERT(!ret);
3397 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003398 /*
3399 * dec doesn't need spin_lock as ->orphan_block_rsv
3400 * would be released only if ->orphan_inodes is
3401 * zero.
3402 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003403 atomic_dec(&root->orphan_inodes);
3404 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003405 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003406 if (insert)
3407 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003408 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003409 return ret;
3410 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003411 }
3412
3413 /* insert an orphan item to track this unlinked/truncated file */
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003414 if (insert) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003415 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003416 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003417 if (reserve) {
3418 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003419 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003420 btrfs_orphan_release_metadata(inode);
3421 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003422 /*
3423 * btrfs_orphan_commit_root may race with us and set
3424 * ->orphan_block_rsv to zero, in order to avoid that,
3425 * decrease ->orphan_inodes after everything is done.
3426 */
3427 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003428 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003429 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003430 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003431 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003432 return ret;
3433 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003434 }
3435 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003436 }
3437
Yan, Zhengd68fc572010-05-16 10:49:58 -04003438 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003439}
3440
3441/*
3442 * We have done the truncate/delete so we can go ahead and remove the orphan
3443 * item for this particular inode.
3444 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003445static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003446 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003447{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003448 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003449 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003450 int ret = 0;
3451
Josef Bacik8a35d952012-05-23 14:26:42 -04003452 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003453 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003455
Liu Bo1a932ef2018-01-25 11:02:54 -07003456 if (delete_item && trans)
3457 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3458
Josef Bacik72ac3c02012-05-23 14:13:11 -04003459 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003460 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003461 btrfs_orphan_release_metadata(inode);
3462
Liu Bo1a932ef2018-01-25 11:02:54 -07003463 /*
3464 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3465 * to zero, in order to avoid that, decrease ->orphan_inodes after
3466 * everything is done.
3467 */
3468 if (delete_item)
3469 atomic_dec(&root->orphan_inodes);
3470
Josef Bacik4ef31a42013-08-13 14:10:08 -04003471 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003472}
3473
3474/*
3475 * this cleans up any orphans that may be left on the list from the last use
3476 * of this root.
3477 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003478int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003479{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003480 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003481 struct btrfs_path *path;
3482 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003483 struct btrfs_key key, found_key;
3484 struct btrfs_trans_handle *trans;
3485 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003486 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003487 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3488
Yan, Zhengd68fc572010-05-16 10:49:58 -04003489 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003490 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003491
3492 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003493 if (!path) {
3494 ret = -ENOMEM;
3495 goto out;
3496 }
David Sterbae4058b52015-11-27 16:31:35 +01003497 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003498
3499 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003500 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003501 key.offset = (u64)-1;
3502
Josef Bacik7b128762008-07-24 12:17:14 -04003503 while (1) {
3504 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003505 if (ret < 0)
3506 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003507
3508 /*
3509 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003510 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003511 * find the key and see if we have stuff that matches
3512 */
3513 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003514 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003515 if (path->slots[0] == 0)
3516 break;
3517 path->slots[0]--;
3518 }
3519
3520 /* pull out the item */
3521 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003522 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3523
3524 /* make sure the item matches what we want */
3525 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3526 break;
David Sterba962a2982014-06-04 18:41:45 +02003527 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003528 break;
3529
3530 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003531 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003532
3533 /*
3534 * this is where we are basically btrfs_lookup, without the
3535 * crossing root thing. we store the inode number in the
3536 * offset of the orphan item.
3537 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003538
3539 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003540 btrfs_err(fs_info,
3541 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003542 ret = -EINVAL;
3543 goto out;
3544 }
3545
3546 last_objectid = found_key.offset;
3547
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003548 found_key.objectid = found_key.offset;
3549 found_key.type = BTRFS_INODE_ITEM_KEY;
3550 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003551 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303552 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003553 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003554 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003555
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003557 struct btrfs_root *dead_root;
3558 struct btrfs_fs_info *fs_info = root->fs_info;
3559 int is_dead_root = 0;
3560
3561 /*
3562 * this is an orphan in the tree root. Currently these
3563 * could come from 2 sources:
3564 * a) a snapshot deletion in progress
3565 * b) a free space cache inode
3566 * We need to distinguish those two, as the snapshot
3567 * orphan must not get deleted.
3568 * find_dead_roots already ran before us, so if this
3569 * is a snapshot deletion, we should find the root
3570 * in the dead_roots list
3571 */
3572 spin_lock(&fs_info->trans_lock);
3573 list_for_each_entry(dead_root, &fs_info->dead_roots,
3574 root_list) {
3575 if (dead_root->root_key.objectid ==
3576 found_key.objectid) {
3577 is_dead_root = 1;
3578 break;
3579 }
3580 }
3581 spin_unlock(&fs_info->trans_lock);
3582 if (is_dead_root) {
3583 /* prevent this orphan from being found again */
3584 key.offset = found_key.objectid - 1;
3585 continue;
3586 }
3587 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003588 /*
3589 * Inode is already gone but the orphan item is still there,
3590 * kill the orphan item.
3591 */
Filipe Manana67710892016-06-06 11:51:25 +01003592 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003593 trans = btrfs_start_transaction(root, 1);
3594 if (IS_ERR(trans)) {
3595 ret = PTR_ERR(trans);
3596 goto out;
3597 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003598 btrfs_debug(fs_info, "auto deleting %Lu",
3599 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003600 ret = btrfs_del_orphan_item(trans, root,
3601 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003602 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003603 if (ret)
3604 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003605 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003606 }
Josef Bacik7b128762008-07-24 12:17:14 -04003607
Josef Bacik7b128762008-07-24 12:17:14 -04003608 /*
3609 * add this inode to the orphan list so btrfs_orphan_del does
3610 * the proper thing when we hit it
3611 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003612 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3613 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003614 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003615
Josef Bacik7b128762008-07-24 12:17:14 -04003616 /* if we have links, this was a truncate, lets do that */
3617 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303618 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003619 iput(inode);
3620 continue;
3621 }
Josef Bacik7b128762008-07-24 12:17:14 -04003622 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003623
3624 /* 1 for the orphan item deletion. */
3625 trans = btrfs_start_transaction(root, 1);
3626 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003627 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003628 ret = PTR_ERR(trans);
3629 goto out;
3630 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003631 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003632 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003633 if (ret) {
3634 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003635 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003636 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003637
Filipe Manana213e8c52018-02-06 20:40:31 +00003638 ret = btrfs_truncate(inode, false);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003639 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003640 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003641 } else {
3642 nr_unlink++;
3643 }
3644
3645 /* this will do delete_inode and everything for us */
3646 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003647 if (ret)
3648 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003649 }
Miao Xie3254c872011-11-10 20:45:05 -05003650 /* release the path since we're done with it */
3651 btrfs_release_path(path);
3652
Yan, Zhengd68fc572010-05-16 10:49:58 -04003653 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3654
3655 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003656 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003657 (u64)-1);
3658
Miao Xie27cdeb72014-04-02 19:51:05 +08003659 if (root->orphan_block_rsv ||
3660 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003661 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003662 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003663 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003664 }
Josef Bacik7b128762008-07-24 12:17:14 -04003665
3666 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003667 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003668 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003669 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003670
3671out:
3672 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003673 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003674 btrfs_free_path(path);
3675 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003676}
3677
Chris Masond352ac62008-09-29 15:18:18 -04003678/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003679 * very simple check to peek ahead in the leaf looking for xattrs. If we
3680 * don't find any xattrs, we know there can't be any acls.
3681 *
3682 * slot is the slot the inode is in, objectid is the objectid of the inode
3683 */
3684static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003685 int slot, u64 objectid,
3686 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003687{
3688 u32 nritems = btrfs_header_nritems(leaf);
3689 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003690 static u64 xattr_access = 0;
3691 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003692 int scanned = 0;
3693
Josef Bacikf23b5a52013-06-19 10:16:26 -04003694 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003695 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3696 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3697 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3698 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003699 }
3700
Chris Mason46a53cc2009-04-27 11:47:50 -04003701 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003702 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003703 while (slot < nritems) {
3704 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3705
3706 /* we found a different objectid, there must not be acls */
3707 if (found_key.objectid != objectid)
3708 return 0;
3709
3710 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003711 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003712 if (*first_xattr_slot == -1)
3713 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003714 if (found_key.offset == xattr_access ||
3715 found_key.offset == xattr_default)
3716 return 1;
3717 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003718
3719 /*
3720 * we found a key greater than an xattr key, there can't
3721 * be any acls later on
3722 */
3723 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3724 return 0;
3725
3726 slot++;
3727 scanned++;
3728
3729 /*
3730 * it goes inode, inode backrefs, xattrs, extents,
3731 * so if there are a ton of hard links to an inode there can
3732 * be a lot of backrefs. Don't waste time searching too hard,
3733 * this is just an optimization
3734 */
3735 if (scanned >= 8)
3736 break;
3737 }
3738 /* we hit the end of the leaf before we found an xattr or
3739 * something larger than an xattr. We have to assume the inode
3740 * has acls
3741 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003742 if (*first_xattr_slot == -1)
3743 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003744 return 1;
3745}
3746
3747/*
Chris Masond352ac62008-09-29 15:18:18 -04003748 * read an inode from the btree into the in-memory inode
3749 */
Filipe Manana67710892016-06-06 11:51:25 +01003750static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003753 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003754 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 struct btrfs_inode_item *inode_item;
3756 struct btrfs_root *root = BTRFS_I(inode)->root;
3757 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003758 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003759 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003760 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003761 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003762 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003763 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003764
3765 ret = btrfs_fill_inode(inode, &rdev);
3766 if (!ret)
3767 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003768
3769 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003770 if (!path) {
3771 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003772 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003773 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003774
Chris Mason39279cc2007-06-12 06:35:45 -04003775 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003776
Chris Mason39279cc2007-06-12 06:35:45 -04003777 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003778 if (ret) {
3779 if (ret > 0)
3780 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003781 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003782 }
Chris Mason39279cc2007-06-12 06:35:45 -04003783
Chris Mason5f39d392007-10-15 16:14:19 -04003784 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003785
3786 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003787 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003788
Chris Mason5f39d392007-10-15 16:14:19 -04003789 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3790 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003791 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003792 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003793 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3794 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003795 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003796
David Sterbaa937b972014-12-12 17:39:12 +01003797 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3798 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
David Sterbaa937b972014-12-12 17:39:12 +01003800 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3801 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003802
David Sterbaa937b972014-12-12 17:39:12 +01003803 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3804 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
chandan r9cc97d62012-07-04 12:48:07 +05303806 BTRFS_I(inode)->i_otime.tv_sec =
3807 btrfs_timespec_sec(leaf, &inode_item->otime);
3808 BTRFS_I(inode)->i_otime.tv_nsec =
3809 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003810
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003811 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003812 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003813 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3814
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003815 inode_set_iversion_queried(inode,
3816 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003817 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003818 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003819 rdev = btrfs_inode_rdev(leaf, inode_item);
3820
Josef Bacikaec74772008-07-24 12:12:38 -04003821 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003822 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003823
3824cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003825 /*
3826 * If we were modified in the current generation and evicted from memory
3827 * and then re-read we need to do a full sync since we don't have any
3828 * idea about which extents were modified before we were evicted from
3829 * cache.
3830 *
3831 * This is required for both inode re-read from disk and delayed inode
3832 * in delayed_nodes_tree.
3833 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003834 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003835 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3836 &BTRFS_I(inode)->runtime_flags);
3837
Filipe Mananabde6c242015-07-24 00:00:19 +01003838 /*
3839 * We don't persist the id of the transaction where an unlink operation
3840 * against the inode was last made. So here we assume the inode might
3841 * have been evicted, and therefore the exact value of last_unlink_trans
3842 * lost, and set it to last_trans to avoid metadata inconsistencies
3843 * between the inode and its parent if the inode is fsync'ed and the log
3844 * replayed. For example, in the scenario:
3845 *
3846 * touch mydir/foo
3847 * ln mydir/foo mydir/bar
3848 * sync
3849 * unlink mydir/bar
3850 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3851 * xfs_io -c fsync mydir/foo
3852 * <power failure>
3853 * mount fs, triggers fsync log replay
3854 *
3855 * We must make sure that when we fsync our inode foo we also log its
3856 * parent inode, otherwise after log replay the parent still has the
3857 * dentry with the "bar" name but our inode foo has a link count of 1
3858 * and doesn't have an inode ref with the name "bar" anymore.
3859 *
3860 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003861 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003862 * transaction commits on fsync if our inode is a directory, or if our
3863 * inode is not a directory, logging its parent unnecessarily.
3864 */
3865 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3866
Miao Xie67de1172013-12-26 13:07:06 +08003867 path->slots[0]++;
3868 if (inode->i_nlink != 1 ||
3869 path->slots[0] >= btrfs_header_nritems(leaf))
3870 goto cache_acl;
3871
3872 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003873 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003874 goto cache_acl;
3875
3876 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3877 if (location.type == BTRFS_INODE_REF_KEY) {
3878 struct btrfs_inode_ref *ref;
3879
3880 ref = (struct btrfs_inode_ref *)ptr;
3881 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3882 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3883 struct btrfs_inode_extref *extref;
3884
3885 extref = (struct btrfs_inode_extref *)ptr;
3886 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3887 extref);
3888 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003889cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003890 /*
3891 * try to precache a NULL acl entry for files that don't have
3892 * any xattrs or acls
3893 */
Li Zefan33345d012011-04-20 10:31:50 +08003894 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003895 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003896 if (first_xattr_slot != -1) {
3897 path->slots[0] = first_xattr_slot;
3898 ret = btrfs_load_inode_props(inode, path);
3899 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003900 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003901 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003902 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003903 root->root_key.objectid, ret);
3904 }
3905 btrfs_free_path(path);
3906
Al Viro72c04902009-06-24 16:58:48 -04003907 if (!maybe_acls)
3908 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003909
Chris Mason39279cc2007-06-12 06:35:45 -04003910 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003911 case S_IFREG:
3912 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003913 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 inode->i_fop = &btrfs_file_operations;
3915 inode->i_op = &btrfs_file_inode_operations;
3916 break;
3917 case S_IFDIR:
3918 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003919 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003920 break;
3921 case S_IFLNK:
3922 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003923 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003924 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3925 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003926 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003927 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003928 init_special_inode(inode, inode->i_mode, rdev);
3929 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003930 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003931
David Sterba7b6a2212018-03-26 18:40:21 +02003932 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003933 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003934
3935make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003936 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003937 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003938 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003939}
3940
Chris Masond352ac62008-09-29 15:18:18 -04003941/*
3942 * given a leaf and an inode, copy the inode fields into the leaf
3943 */
Chris Masone02119d2008-09-05 16:13:11 -04003944static void fill_inode_item(struct btrfs_trans_handle *trans,
3945 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003946 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003947 struct inode *inode)
3948{
Liu Bo51fab692012-12-27 09:01:21 +00003949 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003950
Liu Bo51fab692012-12-27 09:01:21 +00003951 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003952
Liu Bo51fab692012-12-27 09:01:21 +00003953 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3954 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3955 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3956 &token);
3957 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3958 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003959
David Sterbaa937b972014-12-12 17:39:12 +01003960 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003961 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003962 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003963 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003964
David Sterbaa937b972014-12-12 17:39:12 +01003965 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003966 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003967 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003968 inode->i_mtime.tv_nsec, &token);
3969
David Sterbaa937b972014-12-12 17:39:12 +01003970 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003971 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003972 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003973 inode->i_ctime.tv_nsec, &token);
3974
chandan r9cc97d62012-07-04 12:48:07 +05303975 btrfs_set_token_timespec_sec(leaf, &item->otime,
3976 BTRFS_I(inode)->i_otime.tv_sec, &token);
3977 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3978 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3979
Liu Bo51fab692012-12-27 09:01:21 +00003980 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3981 &token);
3982 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3983 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003984 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3985 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003986 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3987 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3988 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3989 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003990}
3991
Chris Masond352ac62008-09-29 15:18:18 -04003992/*
3993 * copy everything in the in-memory inode into the btree.
3994 */
Chris Mason21151332011-11-10 20:39:08 -05003995static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003996 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003997{
3998 struct btrfs_inode_item *inode_item;
3999 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004000 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004001 int ret;
4002
4003 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004004 if (!path)
4005 return -ENOMEM;
4006
Chris Masonb9473432009-03-13 11:00:37 -04004007 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004008 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4009 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004010 if (ret) {
4011 if (ret > 0)
4012 ret = -ENOENT;
4013 goto failed;
4014 }
4015
Chris Mason5f39d392007-10-15 16:14:19 -04004016 leaf = path->nodes[0];
4017 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004018 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004019
Chris Masone02119d2008-09-05 16:13:11 -04004020 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004021 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004022 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004023 ret = 0;
4024failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004025 btrfs_free_path(path);
4026 return ret;
4027}
4028
Chris Masond352ac62008-09-29 15:18:18 -04004029/*
Chris Mason21151332011-11-10 20:39:08 -05004030 * copy everything in the in-memory inode into the btree.
4031 */
4032noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root, struct inode *inode)
4034{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004035 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004036 int ret;
4037
4038 /*
4039 * If the inode is a free space inode, we can deadlock during commit
4040 * if we put it into the delayed code.
4041 *
4042 * The data relocation inode should also be directly updated
4043 * without delay
4044 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004045 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004046 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004047 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004048 btrfs_update_root_times(trans, root);
4049
Chris Mason21151332011-11-10 20:39:08 -05004050 ret = btrfs_delayed_update_inode(trans, root, inode);
4051 if (!ret)
4052 btrfs_set_inode_last_trans(trans, inode);
4053 return ret;
4054 }
4055
4056 return btrfs_update_inode_item(trans, root, inode);
4057}
4058
Josef Bacikbe6aef62012-10-22 15:43:12 -04004059noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4060 struct btrfs_root *root,
4061 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004062{
4063 int ret;
4064
4065 ret = btrfs_update_inode(trans, root, inode);
4066 if (ret == -ENOSPC)
4067 return btrfs_update_inode_item(trans, root, inode);
4068 return ret;
4069}
4070
4071/*
Chris Masond352ac62008-09-29 15:18:18 -04004072 * unlink helper that gets used here in inode.c and in the tree logging
4073 * recovery code. It remove a link in a directory with a given name, and
4074 * also drops the back refs in the inode to the directory
4075 */
Al Viro92986792011-03-04 17:14:37 +00004076static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4077 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004078 struct btrfs_inode *dir,
4079 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004080 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004082 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004084 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004085 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004086 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004087 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004088 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004089 u64 ino = btrfs_ino(inode);
4090 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004091
4092 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004093 if (!path) {
4094 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004095 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004096 }
4097
Chris Masonb9473432009-03-13 11:00:37 -04004098 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004099 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004100 name, name_len, -1);
4101 if (IS_ERR(di)) {
4102 ret = PTR_ERR(di);
4103 goto err;
4104 }
4105 if (!di) {
4106 ret = -ENOENT;
4107 goto err;
4108 }
Chris Mason5f39d392007-10-15 16:14:19 -04004109 leaf = path->nodes[0];
4110 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004111 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004112 if (ret)
4113 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004114 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004115
Miao Xie67de1172013-12-26 13:07:06 +08004116 /*
4117 * If we don't have dir index, we have to get it by looking up
4118 * the inode ref, since we get the inode ref, remove it directly,
4119 * it is unnecessary to do delayed deletion.
4120 *
4121 * But if we have dir index, needn't search inode ref to get it.
4122 * Since the inode ref is close to the inode item, it is better
4123 * that we delay to delete it, and just do this deletion when
4124 * we update the inode item.
4125 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004126 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004127 ret = btrfs_delayed_delete_inode_ref(inode);
4128 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004129 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004130 goto skip_backref;
4131 }
4132 }
4133
Li Zefan33345d012011-04-20 10:31:50 +08004134 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4135 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004136 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004137 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004138 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004139 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004140 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004141 goto err;
4142 }
Miao Xie67de1172013-12-26 13:07:06 +08004143skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004144 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004145 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004146 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004147 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 }
Chris Mason39279cc2007-06-12 06:35:45 -04004149
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004150 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4151 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004153 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 goto err;
4155 }
Chris Masone02119d2008-09-05 16:13:11 -04004156
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4158 index);
Chris Mason6418c962010-10-30 07:34:24 -04004159 if (ret == -ENOENT)
4160 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004161 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004162 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004163err:
4164 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004165 if (ret)
4166 goto out;
4167
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004168 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004169 inode_inc_iversion(&inode->vfs_inode);
4170 inode_inc_iversion(&dir->vfs_inode);
4171 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4172 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4173 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004174out:
Chris Mason39279cc2007-06-12 06:35:45 -04004175 return ret;
4176}
4177
Al Viro92986792011-03-04 17:14:37 +00004178int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4179 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004180 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004181 const char *name, int name_len)
4182{
4183 int ret;
4184 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4185 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004186 drop_nlink(&inode->vfs_inode);
4187 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004188 }
4189 return ret;
4190}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191
4192/*
4193 * helper to start transaction for unlink and rmdir.
4194 *
Josef Bacikd52be812013-05-29 14:54:47 -04004195 * unlink and rmdir are special in btrfs, they do not always free space, so
4196 * if we cannot make our reservations the normal way try and see if there is
4197 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4198 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004199 */
Josef Bacikd52be812013-05-29 14:54:47 -04004200static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004201{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004202 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004203
Josef Bacike70bea52011-10-11 14:18:24 -04004204 /*
4205 * 1 for the possible orphan item
4206 * 1 for the dir item
4207 * 1 for the dir index
4208 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004209 * 1 for the inode
4210 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004211 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004212}
4213
Chris Mason39279cc2007-06-12 06:35:45 -04004214static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4215{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004216 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004217 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004218 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004219 int ret;
4220
Josef Bacikd52be812013-05-29 14:54:47 -04004221 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004222 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004223 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004224
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004225 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4226 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004227
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004228 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4229 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4230 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004231 if (ret)
4232 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004233
Yan, Zhenga22285a2010-05-16 10:48:46 -04004234 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004235 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004236 if (ret)
4237 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004238 }
Josef Bacik7b128762008-07-24 12:17:14 -04004239
Tsutomu Itohb5324022011-07-19 07:27:20 +00004240out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004241 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004242 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004243 return ret;
4244}
4245
Misono Tomohirof60a2362018-04-18 11:34:52 +09004246static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247 struct btrfs_root *root,
4248 struct inode *dir, u64 objectid,
4249 const char *name, int name_len)
4250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004251 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252 struct btrfs_path *path;
4253 struct extent_buffer *leaf;
4254 struct btrfs_dir_item *di;
4255 struct btrfs_key key;
4256 u64 index;
4257 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004258 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259
4260 path = btrfs_alloc_path();
4261 if (!path)
4262 return -ENOMEM;
4263
Li Zefan33345d012011-04-20 10:31:50 +08004264 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 if (IS_ERR_OR_NULL(di)) {
4267 if (!di)
4268 ret = -ENOENT;
4269 else
4270 ret = PTR_ERR(di);
4271 goto out;
4272 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004273
4274 leaf = path->nodes[0];
4275 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4276 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4277 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004278 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004279 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 goto out;
4281 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004282 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004283
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004284 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4285 root->root_key.objectid, dir_ino,
4286 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004289 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 goto out;
4291 }
Li Zefan33345d012011-04-20 10:31:50 +08004292 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004293 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 if (IS_ERR_OR_NULL(di)) {
4295 if (!di)
4296 ret = -ENOENT;
4297 else
4298 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004299 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 goto out;
4301 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004302
4303 leaf = path->nodes[0];
4304 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004305 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004306 index = key.offset;
4307 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004308 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004309
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004310 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004311 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004312 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004313 goto out;
4314 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004315
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004316 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004317 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004318 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004319 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004320 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004321 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004322out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004323 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004324 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004325}
4326
Misono Tomohiroec42f162018-04-18 11:34:13 +09004327/*
4328 * Helper to check if the subvolume references other subvolumes or if it's
4329 * default.
4330 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004331static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004332{
4333 struct btrfs_fs_info *fs_info = root->fs_info;
4334 struct btrfs_path *path;
4335 struct btrfs_dir_item *di;
4336 struct btrfs_key key;
4337 u64 dir_id;
4338 int ret;
4339
4340 path = btrfs_alloc_path();
4341 if (!path)
4342 return -ENOMEM;
4343
4344 /* Make sure this root isn't set as the default subvol */
4345 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4346 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4347 dir_id, "default", 7, 0);
4348 if (di && !IS_ERR(di)) {
4349 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4350 if (key.objectid == root->root_key.objectid) {
4351 ret = -EPERM;
4352 btrfs_err(fs_info,
4353 "deleting default subvolume %llu is not allowed",
4354 key.objectid);
4355 goto out;
4356 }
4357 btrfs_release_path(path);
4358 }
4359
4360 key.objectid = root->root_key.objectid;
4361 key.type = BTRFS_ROOT_REF_KEY;
4362 key.offset = (u64)-1;
4363
4364 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4365 if (ret < 0)
4366 goto out;
4367 BUG_ON(ret == 0);
4368
4369 ret = 0;
4370 if (path->slots[0] > 0) {
4371 path->slots[0]--;
4372 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4373 if (key.objectid == root->root_key.objectid &&
4374 key.type == BTRFS_ROOT_REF_KEY)
4375 ret = -ENOTEMPTY;
4376 }
4377out:
4378 btrfs_free_path(path);
4379 return ret;
4380}
4381
Nikolay Borisov20a68002018-04-27 14:36:24 +03004382/* Delete all dentries for inodes belonging to the root */
4383static void btrfs_prune_dentries(struct btrfs_root *root)
4384{
4385 struct btrfs_fs_info *fs_info = root->fs_info;
4386 struct rb_node *node;
4387 struct rb_node *prev;
4388 struct btrfs_inode *entry;
4389 struct inode *inode;
4390 u64 objectid = 0;
4391
4392 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4393 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4394
4395 spin_lock(&root->inode_lock);
4396again:
4397 node = root->inode_tree.rb_node;
4398 prev = NULL;
4399 while (node) {
4400 prev = node;
4401 entry = rb_entry(node, struct btrfs_inode, rb_node);
4402
4403 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
4404 node = node->rb_left;
4405 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
4406 node = node->rb_right;
4407 else
4408 break;
4409 }
4410 if (!node) {
4411 while (prev) {
4412 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4413 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
4414 node = prev;
4415 break;
4416 }
4417 prev = rb_next(prev);
4418 }
4419 }
4420 while (node) {
4421 entry = rb_entry(node, struct btrfs_inode, rb_node);
4422 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
4423 inode = igrab(&entry->vfs_inode);
4424 if (inode) {
4425 spin_unlock(&root->inode_lock);
4426 if (atomic_read(&inode->i_count) > 1)
4427 d_prune_aliases(inode);
4428 /*
4429 * btrfs_drop_inode will have it removed from the inode
4430 * cache when its usage count hits zero.
4431 */
4432 iput(inode);
4433 cond_resched();
4434 spin_lock(&root->inode_lock);
4435 goto again;
4436 }
4437
4438 if (cond_resched_lock(&root->inode_lock))
4439 goto again;
4440
4441 node = rb_next(node);
4442 }
4443 spin_unlock(&root->inode_lock);
4444}
4445
Misono Tomohirof60a2362018-04-18 11:34:52 +09004446int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4447{
4448 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4449 struct btrfs_root *root = BTRFS_I(dir)->root;
4450 struct inode *inode = d_inode(dentry);
4451 struct btrfs_root *dest = BTRFS_I(inode)->root;
4452 struct btrfs_trans_handle *trans;
4453 struct btrfs_block_rsv block_rsv;
4454 u64 root_flags;
4455 u64 qgroup_reserved;
4456 int ret;
4457 int err;
4458
4459 /*
4460 * Don't allow to delete a subvolume with send in progress. This is
4461 * inside the inode lock so the error handling that has to drop the bit
4462 * again is not run concurrently.
4463 */
4464 spin_lock(&dest->root_item_lock);
4465 root_flags = btrfs_root_flags(&dest->root_item);
4466 if (dest->send_in_progress == 0) {
4467 btrfs_set_root_flags(&dest->root_item,
4468 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4469 spin_unlock(&dest->root_item_lock);
4470 } else {
4471 spin_unlock(&dest->root_item_lock);
4472 btrfs_warn(fs_info,
4473 "attempt to delete subvolume %llu during send",
4474 dest->root_key.objectid);
4475 return -EPERM;
4476 }
4477
4478 down_write(&fs_info->subvol_sem);
4479
4480 err = may_destroy_subvol(dest);
4481 if (err)
4482 goto out_up_write;
4483
4484 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4485 /*
4486 * One for dir inode,
4487 * two for dir entries,
4488 * two for root ref/backref.
4489 */
4490 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
4491 5, &qgroup_reserved, true);
4492 if (err)
4493 goto out_up_write;
4494
4495 trans = btrfs_start_transaction(root, 0);
4496 if (IS_ERR(trans)) {
4497 err = PTR_ERR(trans);
4498 goto out_release;
4499 }
4500 trans->block_rsv = &block_rsv;
4501 trans->bytes_reserved = block_rsv.size;
4502
4503 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4504
4505 ret = btrfs_unlink_subvol(trans, root, dir,
4506 dest->root_key.objectid,
4507 dentry->d_name.name,
4508 dentry->d_name.len);
4509 if (ret) {
4510 err = ret;
4511 btrfs_abort_transaction(trans, ret);
4512 goto out_end_trans;
4513 }
4514
4515 btrfs_record_root_in_trans(trans, dest);
4516
4517 memset(&dest->root_item.drop_progress, 0,
4518 sizeof(dest->root_item.drop_progress));
4519 dest->root_item.drop_level = 0;
4520 btrfs_set_root_refs(&dest->root_item, 0);
4521
4522 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4523 ret = btrfs_insert_orphan_item(trans,
4524 fs_info->tree_root,
4525 dest->root_key.objectid);
4526 if (ret) {
4527 btrfs_abort_transaction(trans, ret);
4528 err = ret;
4529 goto out_end_trans;
4530 }
4531 }
4532
4533 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
4534 BTRFS_UUID_KEY_SUBVOL,
4535 dest->root_key.objectid);
4536 if (ret && ret != -ENOENT) {
4537 btrfs_abort_transaction(trans, ret);
4538 err = ret;
4539 goto out_end_trans;
4540 }
4541 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4542 ret = btrfs_uuid_tree_rem(trans, fs_info,
4543 dest->root_item.received_uuid,
4544 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4545 dest->root_key.objectid);
4546 if (ret && ret != -ENOENT) {
4547 btrfs_abort_transaction(trans, ret);
4548 err = ret;
4549 goto out_end_trans;
4550 }
4551 }
4552
4553out_end_trans:
4554 trans->block_rsv = NULL;
4555 trans->bytes_reserved = 0;
4556 ret = btrfs_end_transaction(trans);
4557 if (ret && !err)
4558 err = ret;
4559 inode->i_flags |= S_DEAD;
4560out_release:
4561 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4562out_up_write:
4563 up_write(&fs_info->subvol_sem);
4564 if (err) {
4565 spin_lock(&dest->root_item_lock);
4566 root_flags = btrfs_root_flags(&dest->root_item);
4567 btrfs_set_root_flags(&dest->root_item,
4568 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4569 spin_unlock(&dest->root_item_lock);
4570 } else {
4571 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004572 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004573 ASSERT(dest->send_in_progress == 0);
4574
4575 /* the last ref */
4576 if (dest->ino_cache_inode) {
4577 iput(dest->ino_cache_inode);
4578 dest->ino_cache_inode = NULL;
4579 }
4580 }
4581
4582 return err;
4583}
4584
Chris Mason39279cc2007-06-12 06:35:45 -04004585static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4586{
David Howells2b0143b2015-03-17 22:25:59 +00004587 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004588 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004589 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004591 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004592
David Sterbab3ae2442012-09-13 16:04:34 -06004593 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004594 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004595 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004596 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004597
Josef Bacikd52be812013-05-29 14:54:47 -04004598 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004599 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004600 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004601
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004602 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004603 err = btrfs_unlink_subvol(trans, root, dir,
4604 BTRFS_I(inode)->location.objectid,
4605 dentry->d_name.name,
4606 dentry->d_name.len);
4607 goto out;
4608 }
4609
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004610 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004611 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004612 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004613
Filipe Manana44f714d2016-06-06 16:11:13 +01004614 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4615
Chris Mason39279cc2007-06-12 06:35:45 -04004616 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004617 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4618 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4619 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004620 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004621 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004622 /*
4623 * Propagate the last_unlink_trans value of the deleted dir to
4624 * its parent directory. This is to prevent an unrecoverable
4625 * log tree in the case we do something like this:
4626 * 1) create dir foo
4627 * 2) create snapshot under dir foo
4628 * 3) delete the snapshot
4629 * 4) rmdir foo
4630 * 5) mkdir foo
4631 * 6) fsync foo or some file inside foo
4632 */
4633 if (last_unlink_trans >= trans->transid)
4634 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4635 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004636out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004637 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004638 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004639
Chris Mason39279cc2007-06-12 06:35:45 -04004640 return err;
4641}
4642
Chris Mason28f75a02015-02-04 06:59:29 -08004643static int truncate_space_check(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root,
4645 u64 bytes_deleted)
4646{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004647 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004648 int ret;
4649
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004650 /*
4651 * This is only used to apply pressure to the enospc system, we don't
4652 * intend to use this reservation at all.
4653 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004654 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004655 bytes_deleted *= fs_info->nodesize;
4656 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004657 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004658 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004660 trans->transid,
4661 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004662 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004663 }
Chris Mason28f75a02015-02-04 06:59:29 -08004664 return ret;
4665
4666}
4667
Josef Bacikddfae632017-10-19 14:16:02 -04004668/*
4669 * Return this if we need to call truncate_block for the last bit of the
4670 * truncate.
4671 */
4672#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004673
Chris Mason323ac952008-10-01 19:05:46 -04004674/*
Chris Mason39279cc2007-06-12 06:35:45 -04004675 * this can truncate away extent items, csum items and directory items.
4676 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004677 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004678 *
4679 * csum items that cross the new i_size are truncated to the new size
4680 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004681 *
4682 * min_type is the minimum key type to truncate down to. If set to 0, this
4683 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004684 */
Yan, Zheng80825102009-11-12 09:35:36 +00004685int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4686 struct btrfs_root *root,
4687 struct inode *inode,
4688 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004689{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004690 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004691 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004692 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004693 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004694 struct btrfs_key key;
4695 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004696 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004697 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004698 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004699 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004700 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004701 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004702 int found_extent;
4703 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004704 int pending_del_nr = 0;
4705 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004706 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004707 int ret;
4708 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004709 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004710 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004711 bool be_nice = false;
4712 bool should_throttle = false;
4713 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004714
4715 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004716
Chris Mason28ed1342014-12-17 09:41:04 -08004717 /*
4718 * for non-free space inodes and ref cows, we want to back off from
4719 * time to time
4720 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004721 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004722 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004723 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004724
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004725 path = btrfs_alloc_path();
4726 if (!path)
4727 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004728 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004729
Josef Bacik5dc562c2012-08-17 13:14:17 -04004730 /*
4731 * We want to drop from the next block forward in case this new size is
4732 * not block aligned since we will be keeping the last block of the
4733 * extent just the way it is.
4734 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004735 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004736 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004737 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004738 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004739 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004740
Miao Xie16cdcec2011-04-22 18:12:22 +08004741 /*
4742 * This function is also used to drop the items in the log tree before
4743 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4744 * it is used to drop the loged items. So we shouldn't kill the delayed
4745 * items.
4746 */
4747 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004748 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004749
Li Zefan33345d012011-04-20 10:31:50 +08004750 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004751 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004752 key.type = (u8)-1;
4753
Chris Mason85e21ba2008-01-29 15:11:36 -05004754search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004755 /*
4756 * with a 16K leaf size and 128MB extents, you can actually queue
4757 * up a huge file in a single leaf. Most of the time that
4758 * bytes_deleted is > 0, it will be huge by the time we get here
4759 */
Byongho Leeee221842015-12-15 01:42:10 +09004760 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004761 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004762 err = -EAGAIN;
4763 goto error;
4764 }
4765 }
4766
4767
Chris Masonb9473432009-03-13 11:00:37 -04004768 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004769 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004770 if (ret < 0) {
4771 err = ret;
4772 goto out;
4773 }
Chris Masond3977122009-01-05 21:25:51 -05004774
Chris Mason85e21ba2008-01-29 15:11:36 -05004775 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004776 /* there are no items in the tree for us to truncate, we're
4777 * done
4778 */
Yan, Zheng80825102009-11-12 09:35:36 +00004779 if (path->slots[0] == 0)
4780 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004781 path->slots[0]--;
4782 }
4783
Chris Masond3977122009-01-05 21:25:51 -05004784 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004785 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004786 leaf = path->nodes[0];
4787 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004788 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004789
Li Zefan33345d012011-04-20 10:31:50 +08004790 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004791 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004792
Chris Mason85e21ba2008-01-29 15:11:36 -05004793 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004794 break;
4795
Chris Mason5f39d392007-10-15 16:14:19 -04004796 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004797 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004798 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004799 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004800 extent_type = btrfs_file_extent_type(leaf, fi);
4801 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004802 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004803 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004804
4805 trace_btrfs_truncate_show_fi_regular(
4806 BTRFS_I(inode), leaf, fi,
4807 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004808 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004809 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004810 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004811
4812 trace_btrfs_truncate_show_fi_inline(
4813 BTRFS_I(inode), leaf, fi, path->slots[0],
4814 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004815 }
Yan008630c2007-11-07 13:31:09 -05004816 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004817 }
Yan, Zheng80825102009-11-12 09:35:36 +00004818 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004819 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004820 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004821 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004822 break;
4823 if (found_key.offset >= new_size)
4824 del_item = 1;
4825 else
4826 del_item = 0;
4827 }
Chris Mason39279cc2007-06-12 06:35:45 -04004828 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004829 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004830 if (found_type != BTRFS_EXTENT_DATA_KEY)
4831 goto delete;
4832
4833 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004834 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004835 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004836 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004837 u64 orig_num_bytes =
4838 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004839 extent_num_bytes = ALIGN(new_size -
4840 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004841 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004842 btrfs_set_file_extent_num_bytes(leaf, fi,
4843 extent_num_bytes);
4844 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004845 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004846 if (test_bit(BTRFS_ROOT_REF_COWS,
4847 &root->state) &&
4848 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004849 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004850 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004851 } else {
Chris Masondb945352007-10-15 16:15:53 -04004852 extent_num_bytes =
4853 btrfs_file_extent_disk_num_bytes(leaf,
4854 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004855 extent_offset = found_key.offset -
4856 btrfs_file_extent_offset(leaf, fi);
4857
Chris Mason39279cc2007-06-12 06:35:45 -04004858 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004859 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004860 if (extent_start != 0) {
4861 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004862 if (test_bit(BTRFS_ROOT_REF_COWS,
4863 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004864 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004865 }
4866 }
Chris Mason90692182008-02-08 13:49:28 -05004867 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004868 /*
4869 * we can't truncate inline items that have had
4870 * special encodings
4871 */
4872 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004873 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004874 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4875 btrfs_file_extent_compression(leaf, fi) == 0) {
4876 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004877
Josef Bacikddfae632017-10-19 14:16:02 -04004878 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4879 size = btrfs_file_extent_calc_inline_size(size);
4880 btrfs_truncate_item(root->fs_info, path, size, 1);
4881 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004882 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004883 * We have to bail so the last_size is set to
4884 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004885 */
Josef Bacikddfae632017-10-19 14:16:02 -04004886 err = NEED_TRUNCATE_BLOCK;
4887 break;
Chris Mason90692182008-02-08 13:49:28 -05004888 }
Josef Bacikddfae632017-10-19 14:16:02 -04004889
4890 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4891 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004892 }
Chris Mason179e29e2007-11-01 11:28:41 -04004893delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004894 if (del_item)
4895 last_size = found_key.offset;
4896 else
4897 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004898 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004899 if (!pending_del_nr) {
4900 /* no pending yet, add ourselves */
4901 pending_del_slot = path->slots[0];
4902 pending_del_nr = 1;
4903 } else if (pending_del_nr &&
4904 path->slots[0] + 1 == pending_del_slot) {
4905 /* hop on the pending chunk */
4906 pending_del_nr++;
4907 pending_del_slot = path->slots[0];
4908 } else {
Chris Masond3977122009-01-05 21:25:51 -05004909 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004910 }
Chris Mason39279cc2007-06-12 06:35:45 -04004911 } else {
4912 break;
4913 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004914 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004915
Miao Xie27cdeb72014-04-02 19:51:05 +08004916 if (found_extent &&
4917 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004918 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004919 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004920 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004921 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004922 extent_num_bytes, 0,
4923 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004924 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004925 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004926 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4927 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004928 trans->delayed_ref_updates * 2,
4929 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004930 if (be_nice) {
4931 if (truncate_space_check(trans, root,
4932 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004933 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004934 }
4935 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004936 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004937 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004938 }
Chris Mason39279cc2007-06-12 06:35:45 -04004939 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004940
Yan, Zheng80825102009-11-12 09:35:36 +00004941 if (found_type == BTRFS_INODE_ITEM_KEY)
4942 break;
4943
4944 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004945 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004946 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004947 if (pending_del_nr) {
4948 ret = btrfs_del_items(trans, root, path,
4949 pending_del_slot,
4950 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004951 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004952 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004953 goto error;
4954 }
Yan, Zheng80825102009-11-12 09:35:36 +00004955 pending_del_nr = 0;
4956 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004957 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004958 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004959 unsigned long updates = trans->delayed_ref_updates;
4960 if (updates) {
4961 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004962 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004963 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004964 if (ret && !err)
4965 err = ret;
4966 }
4967 }
Chris Mason28f75a02015-02-04 06:59:29 -08004968 /*
4969 * if we failed to refill our space rsv, bail out
4970 * and let the transaction restart
4971 */
4972 if (should_end) {
4973 err = -EAGAIN;
4974 goto error;
4975 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004976 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004977 } else {
4978 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004979 }
Chris Mason39279cc2007-06-12 06:35:45 -04004980 }
Yan, Zheng80825102009-11-12 09:35:36 +00004981out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004982 if (pending_del_nr) {
4983 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4984 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004985 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004986 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004987 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004988error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004989 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4990 ASSERT(last_size >= new_size);
4991 if (!err && last_size > new_size)
4992 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004993 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004994 }
Chris Mason28ed1342014-12-17 09:41:04 -08004995
Chris Mason39279cc2007-06-12 06:35:45 -04004996 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004997
Byongho Leeee221842015-12-15 01:42:10 +09004998 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004999 unsigned long updates = trans->delayed_ref_updates;
5000 if (updates) {
5001 trans->delayed_ref_updates = 0;
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02005002 ret = btrfs_run_delayed_refs(trans, updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08005003 if (ret && !err)
5004 err = ret;
5005 }
5006 }
Yan, Zheng80825102009-11-12 09:35:36 +00005007 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005008}
5009
Chris Masona52d9a82007-08-27 16:49:44 -04005010/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305011 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04005012 * @inode - inode that we're zeroing
5013 * @from - the offset to start zeroing
5014 * @len - the length to zero, 0 to zero the entire range respective to the
5015 * offset
5016 * @front - zero up to the offset instead of from the offset on
5017 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305018 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04005019 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04005020 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305021int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04005022 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04005023{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005024 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005025 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005026 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5027 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005028 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08005029 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005030 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005032 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305033 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04005034 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005035 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04005036 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305037 u64 block_start;
5038 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04005039
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02005040 if (IS_ALIGNED(offset, blocksize) &&
5041 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04005042 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305043
Josef Bacik8b62f872017-10-19 14:15:55 -04005044 block_start = round_down(from, blocksize);
5045 block_end = block_start + blocksize - 1;
5046
Qu Wenruo364ecf32017-02-27 15:10:38 +08005047 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04005048 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005049 if (ret)
5050 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005051
Chris Mason211c17f2008-05-15 09:13:45 -04005052again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005053 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005054 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08005055 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08005056 block_start, blocksize, true);
5057 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00005058 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005059 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005060 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005061
Chris Masona52d9a82007-08-27 16:49:44 -04005062 if (!PageUptodate(page)) {
5063 ret = btrfs_readpage(NULL, page);
5064 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005065 if (page->mapping != mapping) {
5066 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005067 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005068 goto again;
5069 }
Chris Masona52d9a82007-08-27 16:49:44 -04005070 if (!PageUptodate(page)) {
5071 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005072 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005073 }
5074 }
Chris Mason211c17f2008-05-15 09:13:45 -04005075 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005076
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305077 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005078 set_page_extent_mapped(page);
5079
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305080 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005081 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305082 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005083 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005084 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005085 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005086 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005087 btrfs_put_ordered_extent(ordered);
5088 goto again;
5089 }
5090
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305091 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06005092 EXTENT_DIRTY | EXTENT_DELALLOC |
5093 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01005094 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005095
Filipe Mananae3b8a482017-11-04 00:16:59 +00005096 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08005097 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005098 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305099 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005100 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005101 goto out_unlock;
5102 }
5103
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305104 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005105 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305106 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005107 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005108 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305109 memset(kaddr + (block_start - page_offset(page)),
5110 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005111 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305112 memset(kaddr + (block_start - page_offset(page)) + offset,
5113 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005114 flush_dcache_page(page);
5115 kunmap(page);
5116 }
Chris Mason247e7432008-07-17 12:53:51 -04005117 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005118 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005119 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005120
Chris Mason89642222008-07-24 09:41:53 -04005121out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005122 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005123 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005124 blocksize, true);
5125 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04005126 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005127 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005128out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005129 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005130 return ret;
5131}
5132
Josef Bacik16e75492013-10-22 12:18:51 -04005133static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5134 u64 offset, u64 len)
5135{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005136 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005137 struct btrfs_trans_handle *trans;
5138 int ret;
5139
5140 /*
5141 * Still need to make sure the inode looks like it's been updated so
5142 * that any holes get logged if we fsync.
5143 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005144 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5145 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005146 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5147 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5148 return 0;
5149 }
5150
5151 /*
5152 * 1 - for the one we're dropping
5153 * 1 - for the one we're adding
5154 * 1 - for updating the inode.
5155 */
5156 trans = btrfs_start_transaction(root, 3);
5157 if (IS_ERR(trans))
5158 return PTR_ERR(trans);
5159
5160 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5161 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005162 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005163 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005164 return ret;
5165 }
5166
David Sterbaf85b7372017-01-20 14:54:07 +01005167 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5168 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005169 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005170 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005171 else
5172 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005173 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005174 return ret;
5175}
5176
Josef Bacik695a0d02011-03-04 15:46:53 -05005177/*
5178 * This function puts in dummy file extents for the area we're creating a hole
5179 * for. So if we are truncating this file to a larger size we need to insert
5180 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5181 * the range between oldsize and size
5182 */
Josef Bacika41ad392011-01-31 15:30:16 -05005183int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005184{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005185 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005186 struct btrfs_root *root = BTRFS_I(inode)->root;
5187 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005188 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005189 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005190 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005191 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5192 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005193 u64 last_byte;
5194 u64 cur_offset;
5195 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005196 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005197
Josef Bacika71754f2013-06-17 17:14:39 -04005198 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305199 * If our size started in the middle of a block we need to zero out the
5200 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005201 * expose stale data.
5202 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305203 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005204 if (err)
5205 return err;
5206
Yan Zheng9036c102008-10-30 14:19:41 -04005207 if (size <= hole_start)
5208 return 0;
5209
Yan Zheng9036c102008-10-30 14:19:41 -04005210 while (1) {
5211 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005212
David Sterbaff13db42015-12-03 14:30:40 +01005213 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005214 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005215 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005216 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005217 if (!ordered)
5218 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005219 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005220 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005221 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005222 btrfs_put_ordered_extent(ordered);
5223 }
5224
Yan Zheng9036c102008-10-30 14:19:41 -04005225 cur_offset = hole_start;
5226 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02005227 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005228 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005229 if (IS_ERR(em)) {
5230 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005231 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005232 break;
5233 }
Yan Zheng9036c102008-10-30 14:19:41 -04005234 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005235 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005236 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005237 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005238 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005239
Josef Bacik16e75492013-10-22 12:18:51 -04005240 err = maybe_insert_hole(root, inode, cur_offset,
5241 hole_size);
5242 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005243 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005244 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005245 cur_offset + hole_size - 1, 0);
5246 hole_em = alloc_extent_map();
5247 if (!hole_em) {
5248 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5249 &BTRFS_I(inode)->runtime_flags);
5250 goto next;
5251 }
5252 hole_em->start = cur_offset;
5253 hole_em->len = hole_size;
5254 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005255
Josef Bacik5dc562c2012-08-17 13:14:17 -04005256 hole_em->block_start = EXTENT_MAP_HOLE;
5257 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005258 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005259 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005261 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005262 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005263
5264 while (1) {
5265 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005266 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005267 write_unlock(&em_tree->lock);
5268 if (err != -EEXIST)
5269 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005270 btrfs_drop_extent_cache(BTRFS_I(inode),
5271 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005272 cur_offset +
5273 hole_size - 1, 0);
5274 }
5275 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005276 }
Josef Bacik16e75492013-10-22 12:18:51 -04005277next:
Yan Zheng9036c102008-10-30 14:19:41 -04005278 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005279 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005280 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005281 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005282 break;
5283 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005284 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005285 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005286 return err;
5287}
5288
Eric Sandeen3972f262013-01-12 02:57:22 +00005289static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005290{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005291 struct btrfs_root *root = BTRFS_I(inode)->root;
5292 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005293 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005294 loff_t newsize = attr->ia_size;
5295 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005296 int ret;
5297
Eric Sandeen3972f262013-01-12 02:57:22 +00005298 /*
5299 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5300 * special case where we need to update the times despite not having
5301 * these flags set. For all other operations the VFS set these flags
5302 * explicitly if it wants a timestamp update.
5303 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005304 if (newsize != oldsize) {
5305 inode_inc_iversion(inode);
5306 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5307 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005308 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005309 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005310
Josef Bacika41ad392011-01-31 15:30:16 -05005311 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005312 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005313 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005314 * This is to ensure the snapshot captures a fully consistent
5315 * state of this file - if the snapshot captures this expanding
5316 * truncation, it must capture all writes that happened before
5317 * this truncation.
5318 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005319 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005320 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005321 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005322 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005323 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005324 }
Yan, Zheng80825102009-11-12 09:35:36 +00005325
Miao Xief4a2f4c2011-12-14 20:12:01 -05005326 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005327 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005328 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005329 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005330 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005331
5332 i_size_write(inode, newsize);
5333 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305334 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005335 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005336 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005337 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005338 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005339
Josef Bacika41ad392011-01-31 15:30:16 -05005340 /*
5341 * We're truncating a file that used to have good data down to
5342 * zero. Make sure it gets into the ordered flush list so that
5343 * any new writes get down to disk quickly.
5344 */
5345 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005346 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5347 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005348
Josef Bacikf3fe8202013-01-07 17:03:21 -05005349 /*
5350 * 1 for the orphan item we're going to add
5351 * 1 for the orphan item deletion.
5352 */
5353 trans = btrfs_start_transaction(root, 2);
5354 if (IS_ERR(trans))
5355 return PTR_ERR(trans);
5356
5357 /*
5358 * We need to do this in case we fail at _any_ point during the
5359 * actual truncate. Once we do the truncate_setsize we could
5360 * invalidate pages which forces any outstanding ordered io to
5361 * be instantly completed which will give us extents that need
5362 * to be truncated. If we fail to get an orphan inode down we
5363 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005364 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005365 * will be consistent.
5366 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005367 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005368 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005369 if (ret)
5370 return ret;
5371
Josef Bacika41ad392011-01-31 15:30:16 -05005372 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5373 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005374
5375 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005376 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005377 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005378 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005379
Filipe Manana213e8c52018-02-06 20:40:31 +00005380 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005381 if (ret && inode->i_nlink) {
5382 int err;
5383
Liu Bo19fd2df2016-12-01 13:01:02 -08005384 /* To get a stable disk_i_size */
5385 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5386 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005387 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005388 return err;
5389 }
5390
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005391 /*
5392 * failed to truncate, disk_i_size is only adjusted down
5393 * as we remove extents, so it should represent the true
5394 * size of the inode, so reset the in memory size and
5395 * delete our orphan entry.
5396 */
5397 trans = btrfs_join_transaction(root);
5398 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005399 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005400 return ret;
5401 }
5402 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005403 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005404 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005405 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005406 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005407 }
Yan, Zheng80825102009-11-12 09:35:36 +00005408 }
5409
Josef Bacika41ad392011-01-31 15:30:16 -05005410 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005411}
5412
Chris Mason39279cc2007-06-12 06:35:45 -04005413static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5414{
David Howells2b0143b2015-03-17 22:25:59 +00005415 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005416 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005417 int err;
5418
Li Zefanb83cc962010-12-20 16:04:08 +08005419 if (btrfs_root_readonly(root))
5420 return -EROFS;
5421
Jan Kara31051c82016-05-26 16:55:18 +02005422 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005423 if (err)
5424 return err;
5425
Chris Mason5a3f23d2009-03-31 13:27:11 -04005426 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005427 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005428 if (err)
5429 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005430 }
Yan Zheng9036c102008-10-30 14:19:41 -04005431
Christoph Hellwig10257742010-06-04 11:30:02 +02005432 if (attr->ia_valid) {
5433 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005434 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005435 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005436
Josef Bacik22c44fe2011-11-30 10:45:38 -05005437 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005438 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005439 }
5440
Chris Mason39279cc2007-06-12 06:35:45 -04005441 return err;
5442}
Chris Mason61295eb2008-01-14 16:24:38 -05005443
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005444/*
5445 * While truncating the inode pages during eviction, we get the VFS calling
5446 * btrfs_invalidatepage() against each page of the inode. This is slow because
5447 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5448 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5449 * extent_state structures over and over, wasting lots of time.
5450 *
5451 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5452 * those expensive operations on a per page basis and do only the ordered io
5453 * finishing, while we release here the extent_map and extent_state structures,
5454 * without the excessive merging and splitting.
5455 */
5456static void evict_inode_truncate_pages(struct inode *inode)
5457{
5458 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5459 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5460 struct rb_node *node;
5461
5462 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005463 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005464
5465 write_lock(&map_tree->lock);
5466 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5467 struct extent_map *em;
5468
5469 node = rb_first(&map_tree->map);
5470 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005471 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5472 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005473 remove_extent_mapping(map_tree, em);
5474 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005475 if (need_resched()) {
5476 write_unlock(&map_tree->lock);
5477 cond_resched();
5478 write_lock(&map_tree->lock);
5479 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005480 }
5481 write_unlock(&map_tree->lock);
5482
Filipe Manana6ca07092015-05-26 00:55:42 +01005483 /*
5484 * Keep looping until we have no more ranges in the io tree.
5485 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005486 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5487 * still in progress (unlocked the pages in the bio but did not yet
5488 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005489 * ranges can still be locked and eviction started because before
5490 * submitting those bios, which are executed by a separate task (work
5491 * queue kthread), inode references (inode->i_count) were not taken
5492 * (which would be dropped in the end io callback of each bio).
5493 * Therefore here we effectively end up waiting for those bios and
5494 * anyone else holding locked ranges without having bumped the inode's
5495 * reference count - if we don't do it, when they access the inode's
5496 * io_tree to unlock a range it may be too late, leading to an
5497 * use-after-free issue.
5498 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005499 spin_lock(&io_tree->lock);
5500 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5501 struct extent_state *state;
5502 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005503 u64 start;
5504 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005505
5506 node = rb_first(&io_tree->state);
5507 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005508 start = state->start;
5509 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005510 spin_unlock(&io_tree->lock);
5511
David Sterbaff13db42015-12-03 14:30:40 +01005512 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005513
5514 /*
5515 * If still has DELALLOC flag, the extent didn't reach disk,
5516 * and its reserved space won't be freed by delayed_ref.
5517 * So we need to free its reserved space here.
5518 * (Refer to comment in btrfs_invalidatepage, case 2)
5519 *
5520 * Note, end is the bytenr of last byte, so we need + 1 here.
5521 */
5522 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005523 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005524
Filipe Manana6ca07092015-05-26 00:55:42 +01005525 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005526 EXTENT_LOCKED | EXTENT_DIRTY |
5527 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005528 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005529
Filipe Manana7064dd52014-08-08 02:47:05 +01005530 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005531 spin_lock(&io_tree->lock);
5532 }
5533 spin_unlock(&io_tree->lock);
5534}
5535
Al Virobd555972010-06-07 11:35:40 -04005536void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005537{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005538 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005539 struct btrfs_trans_handle *trans;
5540 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005541 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005542 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005543 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005544 int ret;
5545
liubo1abe9b82011-03-24 11:18:59 +00005546 trace_btrfs_inode_evict(inode);
5547
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005548 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005549 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005550 return;
5551 }
5552
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005553 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005554
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005555 evict_inode_truncate_pages(inode);
5556
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005557 if (inode->i_nlink &&
5558 ((btrfs_root_refs(&root->root_item) != 0 &&
5559 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005560 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005561 goto no_delete;
5562
Chris Mason39279cc2007-06-12 06:35:45 -04005563 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005564 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005565 goto no_delete;
5566 }
Al Virobd555972010-06-07 11:35:40 -04005567 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005568 if (!special_file(inode->i_mode))
5569 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005570
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005571 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005572
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005573 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005574 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005575 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005576 goto no_delete;
5577 }
5578
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005580 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5581 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005582 goto no_delete;
5583 }
5584
Nikolay Borisovaa790212017-01-10 20:35:40 +02005585 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005586 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005587 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005588 goto no_delete;
5589 }
5590
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005591 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005592 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005593 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005594 goto no_delete;
5595 }
Josef Bacik4a338542011-08-29 11:01:31 -04005596 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005597 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005598 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005599
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005600 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005601
Josef Bacik4289a662011-08-05 13:22:24 -04005602 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005603 * This is a bit simpler than btrfs_truncate since we've already
5604 * reserved our space for our orphan item in the unlink, so we just
5605 * need to reserve some slack space in case we add bytes and update
5606 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005607 */
Yan, Zheng80825102009-11-12 09:35:36 +00005608 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005609 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5610 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005611
Josef Bacik726c35f2011-09-26 15:46:06 -04005612 /*
5613 * Try and steal from the global reserve since we will
5614 * likely not use this space anyway, we want to try as
5615 * hard as possible to get this to work.
5616 */
5617 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005618 steal_from_global++;
5619 else
5620 steal_from_global = 0;
5621 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005622
Josef Bacik3bce8762015-02-24 12:35:51 -08005623 /*
5624 * steal_from_global == 0: we reserved stuff, hooray!
5625 * steal_from_global == 1: we didn't reserve stuff, boo!
5626 * steal_from_global == 2: we've committed, still not a lot of
5627 * room but maybe we'll have room in the global reserve this
5628 * time.
5629 * steal_from_global == 3: abandon all hope!
5630 */
5631 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005632 btrfs_warn(fs_info,
5633 "Could not get space for a delete, will truncate on mount %d",
5634 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005635 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005636 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005637 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005638 }
5639
Miao Xie0e8c36a2012-12-19 06:59:51 +00005640 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005641 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005642 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005643 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005644 goto no_delete;
5645 }
5646
Josef Bacik3bce8762015-02-24 12:35:51 -08005647 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005648 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005649 * sure there is room to do it, if not we need to commit and try
5650 * again.
5651 */
5652 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005653 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005654 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005655 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005656 else
5657 ret = -ENOSPC;
5658 }
5659
5660 /*
5661 * Couldn't steal from the global reserve, we have too much
5662 * pending stuff built up, commit the transaction and try it
5663 * again.
5664 */
5665 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005666 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005667 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005668 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005669 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005670 goto no_delete;
5671 }
5672 continue;
5673 } else {
5674 steal_from_global = 0;
5675 }
5676
Josef Bacik4289a662011-08-05 13:22:24 -04005677 trans->block_rsv = rsv;
5678
Yan, Zhengd68fc572010-05-16 10:49:58 -04005679 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005680 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005681 break;
5682
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005683 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005684 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005685 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005686 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005687 }
5688
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005689 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005690
Josef Bacik4ef31a42013-08-13 14:10:08 -04005691 /*
5692 * Errors here aren't a big deal, it just means we leave orphan items
5693 * in the tree. They will be cleaned up on the next mount.
5694 */
Yan, Zheng80825102009-11-12 09:35:36 +00005695 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005696 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005697 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005698 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005699 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005700 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005701
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005702 trans->block_rsv = &fs_info->trans_block_rsv;
5703 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005704 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005705 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005706
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005707 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005708 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005709no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005710 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005711 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005712}
5713
5714/*
5715 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005716 * If no dir entries were found, returns -ENOENT.
5717 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005718 */
5719static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5720 struct btrfs_key *location)
5721{
5722 const char *name = dentry->d_name.name;
5723 int namelen = dentry->d_name.len;
5724 struct btrfs_dir_item *di;
5725 struct btrfs_path *path;
5726 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005727 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005728
5729 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005730 if (!path)
5731 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005732
David Sterbaf85b7372017-01-20 14:54:07 +01005733 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5734 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005735 if (!di) {
5736 ret = -ENOENT;
5737 goto out;
5738 }
5739 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005740 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005741 goto out;
5742 }
Chris Masond3977122009-01-05 21:25:51 -05005743
Chris Mason5f39d392007-10-15 16:14:19 -04005744 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005745 if (location->type != BTRFS_INODE_ITEM_KEY &&
5746 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005747 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005748 btrfs_warn(root->fs_info,
5749"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5750 __func__, name, btrfs_ino(BTRFS_I(dir)),
5751 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005752 }
Chris Mason39279cc2007-06-12 06:35:45 -04005753out:
Chris Mason39279cc2007-06-12 06:35:45 -04005754 btrfs_free_path(path);
5755 return ret;
5756}
5757
5758/*
5759 * when we hit a tree root in a directory, the btrfs part of the inode
5760 * needs to be changed to reflect the root directory of the tree root. This
5761 * is kind of like crossing a mount point.
5762 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005763static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 struct inode *dir,
5765 struct dentry *dentry,
5766 struct btrfs_key *location,
5767 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005768{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769 struct btrfs_path *path;
5770 struct btrfs_root *new_root;
5771 struct btrfs_root_ref *ref;
5772 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005773 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005774 int ret;
5775 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005776
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005777 path = btrfs_alloc_path();
5778 if (!path) {
5779 err = -ENOMEM;
5780 goto out;
5781 }
Chris Mason39279cc2007-06-12 06:35:45 -04005782
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005783 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005784 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5785 key.type = BTRFS_ROOT_REF_KEY;
5786 key.offset = location->objectid;
5787
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005788 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789 if (ret) {
5790 if (ret < 0)
5791 err = ret;
5792 goto out;
5793 }
Chris Mason39279cc2007-06-12 06:35:45 -04005794
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005795 leaf = path->nodes[0];
5796 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005797 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5799 goto out;
5800
5801 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5802 (unsigned long)(ref + 1),
5803 dentry->d_name.len);
5804 if (ret)
5805 goto out;
5806
David Sterbab3b4aa72011-04-21 01:20:15 +02005807 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005808
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005809 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005810 if (IS_ERR(new_root)) {
5811 err = PTR_ERR(new_root);
5812 goto out;
5813 }
5814
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005815 *sub_root = new_root;
5816 location->objectid = btrfs_root_dirid(&new_root->root_item);
5817 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005818 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005819 err = 0;
5820out:
5821 btrfs_free_path(path);
5822 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005823}
5824
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005825static void inode_tree_add(struct inode *inode)
5826{
5827 struct btrfs_root *root = BTRFS_I(inode)->root;
5828 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005829 struct rb_node **p;
5830 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005831 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005832 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005833
Al Viro1d3382cb2010-10-23 15:19:20 -04005834 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005835 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005836 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005837 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005838 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005839 while (*p) {
5840 parent = *p;
5841 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5842
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005843 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005844 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005845 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005846 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005847 else {
5848 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005849 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005850 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005851 RB_CLEAR_NODE(parent);
5852 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005853 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005854 }
5855 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005856 rb_link_node(new, parent, p);
5857 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005858 spin_unlock(&root->inode_lock);
5859}
5860
5861static void inode_tree_del(struct inode *inode)
5862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005863 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005864 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005865 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005866
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005867 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005868 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005869 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005870 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005871 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005872 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005873 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005874
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005875 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005876 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005877 spin_lock(&root->inode_lock);
5878 empty = RB_EMPTY_ROOT(&root->inode_tree);
5879 spin_unlock(&root->inode_lock);
5880 if (empty)
5881 btrfs_add_dead_root(root);
5882 }
5883}
5884
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005885
Chris Masone02119d2008-09-05 16:13:11 -04005886static int btrfs_init_locked_inode(struct inode *inode, void *p)
5887{
5888 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005889 inode->i_ino = args->location->objectid;
5890 memcpy(&BTRFS_I(inode)->location, args->location,
5891 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005892 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005893 return 0;
5894}
5895
5896static int btrfs_find_actor(struct inode *inode, void *opaque)
5897{
5898 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005899 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005900 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005901}
5902
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005903static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005904 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005905 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005906{
5907 struct inode *inode;
5908 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005909 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005910
Chris Mason90d3e592014-01-09 17:28:00 -08005911 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 args.root = root;
5913
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005914 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005915 btrfs_init_locked_inode,
5916 (void *)&args);
5917 return inode;
5918}
5919
Balaji Rao1a54ef82008-07-21 02:01:04 +05305920/* Get an inode object given its location and corresponding root.
5921 * Returns in *is_new if the inode was read from disk
5922 */
5923struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005924 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305925{
5926 struct inode *inode;
5927
Chris Mason90d3e592014-01-09 17:28:00 -08005928 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305929 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005930 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305931
5932 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005933 int ret;
5934
5935 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005936 if (!is_bad_inode(inode)) {
5937 inode_tree_add(inode);
5938 unlock_new_inode(inode);
5939 if (new)
5940 *new = 1;
5941 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005942 unlock_new_inode(inode);
5943 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005944 ASSERT(ret < 0);
5945 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005946 }
5947 }
5948
Balaji Rao1a54ef82008-07-21 02:01:04 +05305949 return inode;
5950}
5951
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005952static struct inode *new_simple_dir(struct super_block *s,
5953 struct btrfs_key *key,
5954 struct btrfs_root *root)
5955{
5956 struct inode *inode = new_inode(s);
5957
5958 if (!inode)
5959 return ERR_PTR(-ENOMEM);
5960
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005961 BTRFS_I(inode)->root = root;
5962 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005963 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005964
5965 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005966 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005967 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005968 inode->i_fop = &simple_dir_operations;
5969 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005970 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305971 inode->i_atime = inode->i_mtime;
5972 inode->i_ctime = inode->i_mtime;
5973 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005974
5975 return inode;
5976}
5977
Chris Mason3de45862008-11-17 21:02:50 -05005978struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005979{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005980 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005981 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005982 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005983 struct btrfs_root *sub_root = root;
5984 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005985 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005986 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005987
5988 if (dentry->d_name.len > BTRFS_NAME_LEN)
5989 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005990
Jeff Layton39e3c952012-11-28 11:30:53 -05005991 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005992 if (ret < 0)
5993 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005994
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005995 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005996 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005997 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005998 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005999
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006000 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006001 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006002 &location, &sub_root);
6003 if (ret < 0) {
6004 if (ret != -ENOENT)
6005 inode = ERR_PTR(ret);
6006 else
6007 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6008 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00006009 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006010 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006011 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04006012
Julia Lawall34d19ba2011-01-24 19:55:19 +00006013 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006014 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01006015 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006016 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006017 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04006018 if (ret) {
6019 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006020 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04006021 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00006022 }
6023
Chris Mason3de45862008-11-17 21:02:50 -05006024 return inode;
6025}
6026
Nick Pigginfe15ce42011-01-07 17:49:23 +11006027static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006028{
6029 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006030 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006031
Li Zefan848cce02012-02-21 17:04:28 +08006032 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006033 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006034
Li Zefan848cce02012-02-21 17:04:28 +08006035 if (inode) {
6036 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006037 if (btrfs_root_refs(&root->root_item) == 0)
6038 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006039
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006040 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006041 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006042 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006043 return 0;
6044}
6045
Chris Mason3de45862008-11-17 21:02:50 -05006046static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006047 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006048{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09006049 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04006050
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09006051 inode = btrfs_lookup_dentry(dir, dentry);
6052 if (IS_ERR(inode)) {
6053 if (PTR_ERR(inode) == -ENOENT)
6054 inode = NULL;
6055 else
6056 return ERR_CAST(inode);
6057 }
6058
Al Viro41d28bc2014-10-12 22:24:21 -04006059 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006060}
6061
Miao Xie16cdcec2011-04-22 18:12:22 +08006062unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04006063 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
6064};
6065
Josef Bacik23b5ec72017-07-24 15:14:25 -04006066/*
6067 * All this infrastructure exists because dir_emit can fault, and we are holding
6068 * the tree lock when doing readdir. For now just allocate a buffer and copy
6069 * our information into that, and then dir_emit from the buffer. This is
6070 * similar to what NFS does, only we don't keep the buffer around in pagecache
6071 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6072 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6073 * tree lock.
6074 */
6075static int btrfs_opendir(struct inode *inode, struct file *file)
6076{
6077 struct btrfs_file_private *private;
6078
6079 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6080 if (!private)
6081 return -ENOMEM;
6082 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6083 if (!private->filldir_buf) {
6084 kfree(private);
6085 return -ENOMEM;
6086 }
6087 file->private_data = private;
6088 return 0;
6089}
6090
6091struct dir_entry {
6092 u64 ino;
6093 u64 offset;
6094 unsigned type;
6095 int name_len;
6096};
6097
6098static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6099{
6100 while (entries--) {
6101 struct dir_entry *entry = addr;
6102 char *name = (char *)(entry + 1);
6103
David Sterba92d32172018-04-16 21:10:14 +02006104 ctx->pos = get_unaligned(&entry->offset);
6105 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6106 get_unaligned(&entry->ino),
6107 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006108 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006109 addr += sizeof(struct dir_entry) +
6110 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006111 ctx->pos++;
6112 }
6113 return 0;
6114}
6115
Al Viro9cdda8d2013-05-22 16:48:09 -04006116static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006117{
Al Viro9cdda8d2013-05-22 16:48:09 -04006118 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006119 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006120 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006121 struct btrfs_dir_item *di;
6122 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006123 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006124 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006125 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006126 struct list_head ins_list;
6127 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006128 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006129 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006130 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006131 char *name_ptr;
6132 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006133 int entries = 0;
6134 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006135 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006136 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006137
Al Viro9cdda8d2013-05-22 16:48:09 -04006138 if (!dir_emit_dots(file, ctx))
6139 return 0;
6140
David Woodhouse49593bf2008-08-17 17:08:36 +01006141 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006142 if (!path)
6143 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006144
Josef Bacik23b5ec72017-07-24 15:14:25 -04006145 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006146 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006147
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006148 INIT_LIST_HEAD(&ins_list);
6149 INIT_LIST_HEAD(&del_list);
6150 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006151
Josef Bacik23b5ec72017-07-24 15:14:25 -04006152again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006153 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006154 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006155 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006156
Chris Mason39279cc2007-06-12 06:35:45 -04006157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6158 if (ret < 0)
6159 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006160
6161 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006162 struct dir_entry *entry;
6163
Chris Mason5f39d392007-10-15 16:14:19 -04006164 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006165 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006166 if (slot >= btrfs_header_nritems(leaf)) {
6167 ret = btrfs_next_leaf(root, path);
6168 if (ret < 0)
6169 goto err;
6170 else if (ret > 0)
6171 break;
6172 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006173 }
Chris Mason3de45862008-11-17 21:02:50 -05006174
Chris Mason5f39d392007-10-15 16:14:19 -04006175 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6176
6177 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006178 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006179 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006180 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006181 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006182 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006183 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006184 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006185 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006186 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006187 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6188 PAGE_SIZE) {
6189 btrfs_release_path(path);
6190 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6191 if (ret)
6192 goto nopos;
6193 addr = private->filldir_buf;
6194 entries = 0;
6195 total_len = 0;
6196 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006197 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006198
6199 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006200 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006201 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006202 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6203 name_len);
David Sterba92d32172018-04-16 21:10:14 +02006204 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
6205 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006206 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006207 put_unaligned(location.objectid, &entry->ino);
6208 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006209 entries++;
6210 addr += sizeof(struct dir_entry) + name_len;
6211 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006212next:
6213 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006214 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006215 btrfs_release_path(path);
6216
6217 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6218 if (ret)
6219 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006220
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006221 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006222 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006223 goto nopos;
6224
Zach Browndb62efb2013-07-11 16:19:42 -07006225 /*
6226 * Stop new entries from being returned after we return the last
6227 * entry.
6228 *
6229 * New directory entries are assigned a strictly increasing
6230 * offset. This means that new entries created during readdir
6231 * are *guaranteed* to be seen in the future by that readdir.
6232 * This has broken buggy programs which operate on names as
6233 * they're returned by readdir. Until we re-use freed offsets
6234 * we have this hack to stop new entries from being returned
6235 * under the assumption that they'll never reach this huge
6236 * offset.
6237 *
6238 * This is being careful not to overflow 32bit loff_t unless the
6239 * last entry requires it because doing so has broken 32bit apps
6240 * in the past.
6241 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006242 if (ctx->pos >= INT_MAX)
6243 ctx->pos = LLONG_MAX;
6244 else
6245 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006246nopos:
6247 ret = 0;
6248err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006249 if (put)
6250 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006251 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006252 return ret;
6253}
6254
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006255int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006256{
6257 struct btrfs_root *root = BTRFS_I(inode)->root;
6258 struct btrfs_trans_handle *trans;
6259 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006260 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006261
Josef Bacik72ac3c02012-05-23 14:13:11 -04006262 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006263 return 0;
6264
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006265 if (btrfs_fs_closing(root->fs_info) &&
6266 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006267 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006268
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006269 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006270 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006271 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006272 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006273 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006274 if (IS_ERR(trans))
6275 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006276 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006277 }
6278 return ret;
6279}
6280
6281/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006282 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006283 * inode changes. But, it is most likely to find the inode in cache.
6284 * FIXME, needs more benchmarking...there are no reasons other than performance
6285 * to keep or drop this code.
6286 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006287static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006288{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006289 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006290 struct btrfs_root *root = BTRFS_I(inode)->root;
6291 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006292 int ret;
6293
Josef Bacik72ac3c02012-05-23 14:13:11 -04006294 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006295 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006296
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006297 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006298 if (IS_ERR(trans))
6299 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006300
6301 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006302 if (ret && ret == -ENOSPC) {
6303 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006304 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006305 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006306 if (IS_ERR(trans))
6307 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006308
Chris Mason94b60442010-05-26 11:02:00 -04006309 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006310 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006311 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006312 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006313 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006314
6315 return ret;
6316}
6317
6318/*
6319 * This is a copy of file_update_time. We need this so we can return error on
6320 * ENOSPC for updating the inode in the case of file write and mmap writes.
6321 */
Josef Bacike41f9412012-03-26 09:46:47 -04006322static int btrfs_update_time(struct inode *inode, struct timespec *now,
6323 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006324{
Alexander Block2bc5565282012-06-15 09:49:33 +02006325 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006326 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006327
6328 if (btrfs_root_readonly(root))
6329 return -EROFS;
6330
Josef Bacike41f9412012-03-26 09:46:47 -04006331 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006332 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006333 if (flags & S_CTIME)
6334 inode->i_ctime = *now;
6335 if (flags & S_MTIME)
6336 inode->i_mtime = *now;
6337 if (flags & S_ATIME)
6338 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006339 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006340}
6341
Chris Masond352ac62008-09-29 15:18:18 -04006342/*
6343 * find the highest existing sequence number in a directory
6344 * and then set the in-memory index_cnt variable to reflect
6345 * free sequence numbers
6346 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006347static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006348{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006349 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006350 struct btrfs_key key, found_key;
6351 struct btrfs_path *path;
6352 struct extent_buffer *leaf;
6353 int ret;
6354
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006355 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006356 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006357 key.offset = (u64)-1;
6358
6359 path = btrfs_alloc_path();
6360 if (!path)
6361 return -ENOMEM;
6362
6363 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6364 if (ret < 0)
6365 goto out;
6366 /* FIXME: we should be able to handle this */
6367 if (ret == 0)
6368 goto out;
6369 ret = 0;
6370
6371 /*
6372 * MAGIC NUMBER EXPLANATION:
6373 * since we search a directory based on f_pos we have to start at 2
6374 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6375 * else has to start at 2
6376 */
6377 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006378 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006379 goto out;
6380 }
6381
6382 path->slots[0]--;
6383
6384 leaf = path->nodes[0];
6385 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6386
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006387 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006388 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006389 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006390 goto out;
6391 }
6392
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006393 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006394out:
6395 btrfs_free_path(path);
6396 return ret;
6397}
6398
Chris Masond352ac62008-09-29 15:18:18 -04006399/*
6400 * helper to find a free sequence number in a given directory. This current
6401 * code is very simple, later versions will do smarter things in the btree
6402 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006403int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006404{
6405 int ret = 0;
6406
Nikolay Borisov877574e2017-02-20 13:50:33 +02006407 if (dir->index_cnt == (u64)-1) {
6408 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006409 if (ret) {
6410 ret = btrfs_set_inode_index_count(dir);
6411 if (ret)
6412 return ret;
6413 }
Josef Bacikaec74772008-07-24 12:12:38 -04006414 }
6415
Nikolay Borisov877574e2017-02-20 13:50:33 +02006416 *index = dir->index_cnt;
6417 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006418
6419 return ret;
6420}
6421
Chris Masonb0d5d102014-09-08 13:08:51 -07006422static int btrfs_insert_inode_locked(struct inode *inode)
6423{
6424 struct btrfs_iget_args args;
6425 args.location = &BTRFS_I(inode)->location;
6426 args.root = BTRFS_I(inode)->root;
6427
6428 return insert_inode_locked4(inode,
6429 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6430 btrfs_find_actor, &args);
6431}
6432
Anand Jain19aee8d2017-07-18 17:37:05 +08006433/*
6434 * Inherit flags from the parent inode.
6435 *
6436 * Currently only the compression flags and the cow flags are inherited.
6437 */
6438static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6439{
6440 unsigned int flags;
6441
6442 if (!dir)
6443 return;
6444
6445 flags = BTRFS_I(dir)->flags;
6446
6447 if (flags & BTRFS_INODE_NOCOMPRESS) {
6448 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6449 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6450 } else if (flags & BTRFS_INODE_COMPRESS) {
6451 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6452 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6453 }
6454
6455 if (flags & BTRFS_INODE_NODATACOW) {
6456 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6457 if (S_ISREG(inode->i_mode))
6458 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6459 }
6460
David Sterba7b6a2212018-03-26 18:40:21 +02006461 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006462}
6463
Chris Mason39279cc2007-06-12 06:35:45 -04006464static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6465 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006466 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006467 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006468 u64 ref_objectid, u64 objectid,
6469 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006470{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006471 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006472 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006473 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006474 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006475 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006476 struct btrfs_inode_ref *ref;
6477 struct btrfs_key key[2];
6478 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006479 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006480 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006482
Chris Mason5f39d392007-10-15 16:14:19 -04006483 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006484 if (!path)
6485 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006486
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006487 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006488 if (!inode) {
6489 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006490 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006491 }
Chris Mason39279cc2007-06-12 06:35:45 -04006492
Li Zefan581bb052011-04-20 10:06:11 +08006493 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006494 * O_TMPFILE, set link count to 0, so that after this point,
6495 * we fill in an inode item with the correct link count.
6496 */
6497 if (!name)
6498 set_nlink(inode, 0);
6499
6500 /*
Li Zefan581bb052011-04-20 10:06:11 +08006501 * we have to initialize this early, so we can reclaim the inode
6502 * number if we fail afterwards in this function.
6503 */
6504 inode->i_ino = objectid;
6505
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006506 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006507 trace_btrfs_inode_request(dir);
6508
Nikolay Borisov877574e2017-02-20 13:50:33 +02006509 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006510 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006511 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006512 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006513 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006514 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006515 } else if (dir) {
6516 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006517 }
6518 /*
6519 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006520 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006521 * number
6522 */
6523 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006524 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006525 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006526 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006527 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006528
Josef Bacik5dc562c2012-08-17 13:14:17 -04006529 /*
6530 * We could have gotten an inode number from somebody who was fsynced
6531 * and then removed in this same transaction, so let's just set full
6532 * sync since it will be a full sync anyway and this will blow away the
6533 * old info in the log.
6534 */
6535 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6536
Chris Mason9c583092008-01-29 15:15:18 -05006537 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006538 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006539 key[0].offset = 0;
6540
Chris Mason9c583092008-01-29 15:15:18 -05006541 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006542
6543 if (name) {
6544 /*
6545 * Start new inodes with an inode_ref. This is slightly more
6546 * efficient for small numbers of hard links since they will
6547 * be packed into one item. Extended refs will kick in if we
6548 * add more hard links than can fit in the ref item.
6549 */
6550 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006551 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006552 key[1].offset = ref_objectid;
6553
6554 sizes[1] = name_len + sizeof(*ref);
6555 }
Chris Mason9c583092008-01-29 15:15:18 -05006556
Chris Masonb0d5d102014-09-08 13:08:51 -07006557 location = &BTRFS_I(inode)->location;
6558 location->objectid = objectid;
6559 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006560 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006561
6562 ret = btrfs_insert_inode_locked(inode);
6563 if (ret < 0)
6564 goto fail;
6565
Chris Masonb9473432009-03-13 11:00:37 -04006566 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006567 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006568 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006569 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006570
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006571 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006572 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306573
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006574 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306575 inode->i_atime = inode->i_mtime;
6576 inode->i_ctime = inode->i_mtime;
6577 BTRFS_I(inode)->i_otime = inode->i_mtime;
6578
Chris Mason5f39d392007-10-15 16:14:19 -04006579 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6580 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006581 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006582 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006583 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006584
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006585 if (name) {
6586 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6587 struct btrfs_inode_ref);
6588 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6589 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6590 ptr = (unsigned long)(ref + 1);
6591 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6592 }
Chris Mason9c583092008-01-29 15:15:18 -05006593
Chris Mason5f39d392007-10-15 16:14:19 -04006594 btrfs_mark_buffer_dirty(path->nodes[0]);
6595 btrfs_free_path(path);
6596
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006597 btrfs_inherit_iflags(inode, dir);
6598
Al Viro569254b2011-07-24 17:08:40 -04006599 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006600 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006601 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006602 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006603 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6604 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006605 }
6606
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006607 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006608
6609 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006610 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006611
Alexander Block8ea05e32012-07-25 17:35:53 +02006612 btrfs_update_root_times(trans, root);
6613
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006614 ret = btrfs_inode_inherit_props(trans, inode, dir);
6615 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006616 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006617 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006618 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006619
Chris Mason39279cc2007-06-12 06:35:45 -04006620 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006621
6622fail_unlock:
6623 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006624fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006625 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006626 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006627 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006628 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006629 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006630}
6631
6632static inline u8 btrfs_inode_type(struct inode *inode)
6633{
6634 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6635}
6636
Chris Masond352ac62008-09-29 15:18:18 -04006637/*
6638 * utility function to add 'inode' into 'parent_inode' with
6639 * a give name and a given sequence number.
6640 * if 'add_backref' is true, also insert a backref from the
6641 * inode to the parent directory.
6642 */
Chris Masone02119d2008-09-05 16:13:11 -04006643int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006644 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006645 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006646{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006647 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006648 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006649 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006650 struct btrfs_root *root = parent_inode->root;
6651 u64 ino = btrfs_ino(inode);
6652 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006653
Li Zefan33345d012011-04-20 10:31:50 +08006654 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006655 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006656 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006657 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006658 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006659 key.offset = 0;
6660 }
Chris Mason39279cc2007-06-12 06:35:45 -04006661
Li Zefan33345d012011-04-20 10:31:50 +08006662 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006663 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6664 root->root_key.objectid, parent_ino,
6665 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006666 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006667 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6668 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006669 }
6670
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006671 /* Nothing to clean up yet */
6672 if (ret)
6673 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006674
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006675 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6676 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006677 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006678 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006679 goto fail_dir_item;
6680 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006681 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006682 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006683 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006684
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006685 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006686 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006687 inode_inc_iversion(&parent_inode->vfs_inode);
6688 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6689 current_time(&parent_inode->vfs_inode);
6690 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006691 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006692 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006693 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006694
6695fail_dir_item:
6696 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6697 u64 local_index;
6698 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006699 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6700 root->root_key.objectid, parent_ino,
6701 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006702
6703 } else if (add_backref) {
6704 u64 local_index;
6705 int err;
6706
6707 err = btrfs_del_inode_ref(trans, root, name, name_len,
6708 ino, parent_ino, &local_index);
6709 }
6710 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006711}
6712
6713static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006714 struct btrfs_inode *dir, struct dentry *dentry,
6715 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006716{
Josef Bacika1b075d2010-11-19 20:36:11 +00006717 int err = btrfs_add_link(trans, dir, inode,
6718 dentry->d_name.name, dentry->d_name.len,
6719 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006720 if (err > 0)
6721 err = -EEXIST;
6722 return err;
6723}
6724
Josef Bacik618e21d2007-07-11 10:18:17 -04006725static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006726 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006727{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006728 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006729 struct btrfs_trans_handle *trans;
6730 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006731 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006732 int err;
6733 int drop_inode = 0;
6734 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006735 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006736
Josef Bacik9ed74f22009-09-11 16:12:44 -04006737 /*
6738 * 2 for inode item and ref
6739 * 2 for dir items
6740 * 1 for xattr if selinux is on
6741 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006742 trans = btrfs_start_transaction(root, 5);
6743 if (IS_ERR(trans))
6744 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006745
Li Zefan581bb052011-04-20 10:06:11 +08006746 err = btrfs_find_free_ino(root, &objectid);
6747 if (err)
6748 goto out_unlock;
6749
Josef Bacikaec74772008-07-24 12:12:38 -04006750 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006751 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6752 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006753 if (IS_ERR(inode)) {
6754 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006755 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006756 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006757
Casey Schauflerad19db72011-12-15 10:09:07 -05006758 /*
6759 * If the active LSM wants to access the inode during
6760 * d_instantiate it needs these. Smack checks to see
6761 * if the filesystem supports xattrs by looking at the
6762 * ops vector.
6763 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006764 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006765 init_special_inode(inode, inode->i_mode, rdev);
6766
6767 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006768 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006769 goto out_unlock_inode;
6770
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006771 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6772 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006773 if (err) {
6774 goto out_unlock_inode;
6775 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006776 btrfs_update_inode(trans, root, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04006777 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006778 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006779
Josef Bacik618e21d2007-07-11 10:18:17 -04006780out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006781 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006782 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006783 if (drop_inode) {
6784 inode_dec_link_count(inode);
6785 iput(inode);
6786 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006787 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006788
6789out_unlock_inode:
6790 drop_inode = 1;
6791 unlock_new_inode(inode);
6792 goto out_unlock;
6793
Josef Bacik618e21d2007-07-11 10:18:17 -04006794}
6795
Chris Mason39279cc2007-06-12 06:35:45 -04006796static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006797 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006798{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006799 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006800 struct btrfs_trans_handle *trans;
6801 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006802 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006803 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006804 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006805 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006806 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006807
Josef Bacik9ed74f22009-09-11 16:12:44 -04006808 /*
6809 * 2 for inode item and ref
6810 * 2 for dir items
6811 * 1 for xattr if selinux is on
6812 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006813 trans = btrfs_start_transaction(root, 5);
6814 if (IS_ERR(trans))
6815 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006816
Li Zefan581bb052011-04-20 10:06:11 +08006817 err = btrfs_find_free_ino(root, &objectid);
6818 if (err)
6819 goto out_unlock;
6820
Josef Bacikaec74772008-07-24 12:12:38 -04006821 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006822 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6823 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006824 if (IS_ERR(inode)) {
6825 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006827 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006828 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006829 /*
6830 * If the active LSM wants to access the inode during
6831 * d_instantiate it needs these. Smack checks to see
6832 * if the filesystem supports xattrs by looking at the
6833 * ops vector.
6834 */
6835 inode->i_fop = &btrfs_file_operations;
6836 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006837 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006838
6839 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6840 if (err)
6841 goto out_unlock_inode;
6842
6843 err = btrfs_update_inode(trans, root, inode);
6844 if (err)
6845 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006846
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006847 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6848 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006849 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006850 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006851
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006852 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006853 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006854
Chris Mason39279cc2007-06-12 06:35:45 -04006855out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006856 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006857 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006858 inode_dec_link_count(inode);
6859 iput(inode);
6860 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006861 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006862 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006863
6864out_unlock_inode:
6865 unlock_new_inode(inode);
6866 goto out_unlock;
6867
Chris Mason39279cc2007-06-12 06:35:45 -04006868}
6869
6870static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6871 struct dentry *dentry)
6872{
Filipe Manana271dba42016-01-05 16:24:05 +00006873 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006874 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006875 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006877 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006878 int err;
6879 int drop_inode = 0;
6880
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006881 /* do not allow sys_link's with other subvols of the same device */
6882 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006883 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006884
Mark Fashehf1863732012-08-08 11:32:27 -07006885 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006886 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006887
Nikolay Borisov877574e2017-02-20 13:50:33 +02006888 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006889 if (err)
6890 goto fail;
6891
Yan, Zhenga22285a2010-05-16 10:48:46 -04006892 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006893 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006894 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006895 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006896 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006897 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006898 if (IS_ERR(trans)) {
6899 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006900 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006901 goto fail;
6902 }
Chris Mason5f39d392007-10-15 16:14:19 -04006903
Miao Xie67de1172013-12-26 13:07:06 +08006904 /* There are several dir indexes for this inode, clear the cache. */
6905 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006906 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006907 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006908 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006909 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006910 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006911
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006912 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6913 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006914
Yan, Zhenga5719522009-09-24 09:17:31 -04006915 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006916 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006917 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006918 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006919 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006920 if (err)
6921 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006922 if (inode->i_nlink == 1) {
6923 /*
6924 * If new hard link count is 1, it's a file created
6925 * with open(2) O_TMPFILE flag.
6926 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006927 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006928 if (err)
6929 goto fail;
6930 }
Al Viro08c422c2011-12-23 07:58:13 -05006931 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006932 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006933 }
Chris Mason39279cc2007-06-12 06:35:45 -04006934
Chris Mason1832a6d2007-12-21 16:27:21 -05006935fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006936 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006937 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006938 if (drop_inode) {
6939 inode_dec_link_count(inode);
6940 iput(inode);
6941 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006942 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006943 return err;
6944}
6945
Al Viro18bb1db2011-07-26 01:41:39 -04006946static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006947{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006948 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006949 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006950 struct btrfs_trans_handle *trans;
6951 struct btrfs_root *root = BTRFS_I(dir)->root;
6952 int err = 0;
6953 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006954 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006955 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006956
Josef Bacik9ed74f22009-09-11 16:12:44 -04006957 /*
6958 * 2 items for inode and ref
6959 * 2 items for dir items
6960 * 1 for xattr if selinux is on
6961 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006962 trans = btrfs_start_transaction(root, 5);
6963 if (IS_ERR(trans))
6964 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006965
Li Zefan581bb052011-04-20 10:06:11 +08006966 err = btrfs_find_free_ino(root, &objectid);
6967 if (err)
6968 goto out_fail;
6969
Josef Bacikaec74772008-07-24 12:12:38 -04006970 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006971 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6972 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006973 if (IS_ERR(inode)) {
6974 err = PTR_ERR(inode);
6975 goto out_fail;
6976 }
Chris Mason5f39d392007-10-15 16:14:19 -04006977
Chris Mason39279cc2007-06-12 06:35:45 -04006978 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006979 /* these must be set before we unlock the inode */
6980 inode->i_op = &btrfs_dir_inode_operations;
6981 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006982
Eric Paris2a7dba32011-02-01 11:05:39 -05006983 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006984 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006985 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006986
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006987 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006988 err = btrfs_update_inode(trans, root, inode);
6989 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006990 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006991
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006992 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6993 dentry->d_name.name,
6994 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006995 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006996 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006997
Al Viro1e2e5472018-05-04 08:23:01 -04006998 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006999 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007000
7001out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007002 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08007003 if (drop_on_err) {
7004 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007005 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08007006 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007007 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04007008 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07007009
7010out_fail_inode:
7011 unlock_new_inode(inode);
7012 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04007013}
7014
Chris Masonc8b97812008-10-29 14:49:59 -04007015static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09007016 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04007017 size_t pg_offset, u64 extent_offset,
7018 struct btrfs_file_extent_item *item)
7019{
7020 int ret;
7021 struct extent_buffer *leaf = path->nodes[0];
7022 char *tmp;
7023 size_t max_size;
7024 unsigned long inline_size;
7025 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08007026 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04007027
7028 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08007029 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04007030 max_size = btrfs_file_extent_ram_bytes(leaf, item);
7031 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01007032 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04007033 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04007034 if (!tmp)
7035 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04007036 ptr = btrfs_file_extent_inline_start(item);
7037
7038 read_extent_buffer(leaf, tmp, ptr, inline_size);
7039
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007040 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08007041 ret = btrfs_decompress(compress_type, tmp, page,
7042 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05007043
7044 /*
7045 * decompression code contains a memset to fill in any space between the end
7046 * of the uncompressed data and the end of max_size in case the decompressed
7047 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7048 * the end of an inline extent and the beginning of the next block, so we
7049 * cover that region here.
7050 */
7051
7052 if (max_size + pg_offset < PAGE_SIZE) {
7053 char *map = kmap(page);
7054 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
7055 kunmap(page);
7056 }
Chris Masonc8b97812008-10-29 14:49:59 -04007057 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04007058 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04007059}
7060
Chris Masond352ac62008-09-29 15:18:18 -04007061/*
7062 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05007063 * the ugly parts come from merging extents from the disk with the in-ram
7064 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04007065 * where the in-ram extents might be locked pending data=ordered completion.
7066 *
7067 * This also copies inline extents directly into the page.
7068 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007069struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
7070 struct page *page,
7071 size_t pg_offset, u64 start, u64 len,
7072 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04007073{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007074 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04007075 int ret;
7076 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007077 u64 extent_start = 0;
7078 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007079 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04007080 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04007081 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007082 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007083 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007084 struct extent_buffer *leaf;
7085 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007086 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007087 struct extent_map_tree *em_tree = &inode->extent_tree;
7088 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01007089 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04007090
Chris Mason890871b2009-09-02 16:24:52 -04007091 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007092 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04007093 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007094 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04007095 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007096
Chris Masona52d9a82007-08-27 16:49:44 -04007097 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007098 if (em->start > start || em->start + em->len <= start)
7099 free_extent_map(em);
7100 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007101 free_extent_map(em);
7102 else
7103 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007104 }
David Sterba172ddd62011-04-21 00:48:27 +02007105 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007106 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05007107 err = -ENOMEM;
7108 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007109 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007110 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05007111 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007112 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007113 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007114 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007115
7116 if (!path) {
7117 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04007118 if (!path) {
7119 err = -ENOMEM;
7120 goto out;
7121 }
7122 /*
7123 * Chances are we'll be called again, so go ahead and do
7124 * readahead
7125 */
David Sterbae4058b52015-11-27 16:31:35 +01007126 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04007127 }
7128
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007129 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007130 if (ret < 0) {
7131 err = ret;
7132 goto out;
7133 }
7134
7135 if (ret != 0) {
7136 if (path->slots[0] == 0)
7137 goto not_found;
7138 path->slots[0]--;
7139 }
7140
Chris Mason5f39d392007-10-15 16:14:19 -04007141 leaf = path->nodes[0];
7142 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007143 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007144 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007145 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007146 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007147 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007148 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007149 /*
7150 * If we backup past the first extent we want to move forward
7151 * and see if there is an extent in front of us, otherwise we'll
7152 * say there is a hole for our whole search range which can
7153 * cause problems.
7154 */
7155 extent_end = start;
7156 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007157 }
7158
Chris Mason5f39d392007-10-15 16:14:19 -04007159 found_type = btrfs_file_extent_type(leaf, item);
7160 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007161 if (found_type == BTRFS_FILE_EXTENT_REG ||
7162 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007163 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007164 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007165
7166 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7167 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007168 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7169 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007170 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007171 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007172 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007173
7174 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7175 path->slots[0],
7176 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007177 }
Josef Bacik25a50342013-10-14 12:08:38 -04007178next:
Yan Zheng9036c102008-10-30 14:19:41 -04007179 if (start >= extent_end) {
7180 path->slots[0]++;
7181 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7182 ret = btrfs_next_leaf(root, path);
7183 if (ret < 0) {
7184 err = ret;
7185 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007186 }
Yan Zheng9036c102008-10-30 14:19:41 -04007187 if (ret > 0)
7188 goto not_found;
7189 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007190 }
Yan Zheng9036c102008-10-30 14:19:41 -04007191 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7192 if (found_key.objectid != objectid ||
7193 found_key.type != BTRFS_EXTENT_DATA_KEY)
7194 goto not_found;
7195 if (start + len <= found_key.offset)
7196 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007197 if (start > found_key.offset)
7198 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007199 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007200 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007201 em->len = found_key.offset - start;
7202 goto not_found_em;
7203 }
7204
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007205 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007206 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007207
Yan Zhengd899e052008-10-30 14:25:28 -04007208 if (found_type == BTRFS_FILE_EXTENT_REG ||
7209 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007210 goto insert;
7211 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007212 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007213 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007214 size_t size;
7215 size_t extent_offset;
7216 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007217
Filipe Manana7ffbb592014-06-09 03:48:05 +01007218 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007219 goto out;
Yan689f9342007-10-29 11:41:07 -04007220
Chris Mason514ac8a2014-01-03 21:07:00 -08007221 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007222 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007223 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7224 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007225 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007226 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007227 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007228 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007229 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007230 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007231 if (btrfs_file_extent_compression(leaf, item) !=
7232 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007233 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007234 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007235 if (ret) {
7236 err = ret;
7237 goto out;
7238 }
Chris Masonc8b97812008-10-29 14:49:59 -04007239 } else {
7240 map = kmap(page);
7241 read_extent_buffer(leaf, map + pg_offset, ptr,
7242 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007243 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007244 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007245 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007246 copy_size);
7247 }
Chris Masonc8b97812008-10-29 14:49:59 -04007248 kunmap(page);
7249 }
Chris Mason179e29e2007-11-01 11:28:41 -04007250 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007251 }
Chris Masond1310b22008-01-24 16:13:08 -05007252 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007253 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007254 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007255 }
7256not_found:
7257 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007258 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007259 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007260not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007261 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007262insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007263 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007264 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007265 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007266 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7267 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007268 err = -EIO;
7269 goto out;
7270 }
Chris Masond1310b22008-01-24 16:13:08 -05007271
7272 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007273 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007274 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007275 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007276out:
liubo1abe9b82011-03-24 11:18:59 +00007277
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007278 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007279
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007280 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007281 if (err) {
7282 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007283 return ERR_PTR(err);
7284 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007285 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007286 return em;
7287}
7288
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007289struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7290 struct page *page,
7291 size_t pg_offset, u64 start, u64 len,
7292 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007293{
7294 struct extent_map *em;
7295 struct extent_map *hole_em = NULL;
7296 u64 range_start = start;
7297 u64 end;
7298 u64 found;
7299 u64 found_end;
7300 int err = 0;
7301
7302 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7303 if (IS_ERR(em))
7304 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007305 /*
7306 * If our em maps to:
7307 * - a hole or
7308 * - a pre-alloc extent,
7309 * there might actually be delalloc bytes behind it.
7310 */
7311 if (em->block_start != EXTENT_MAP_HOLE &&
7312 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7313 return em;
7314 else
7315 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007316
7317 /* check to see if we've wrapped (len == -1 or similar) */
7318 end = start + len;
7319 if (end < start)
7320 end = (u64)-1;
7321 else
7322 end -= 1;
7323
7324 em = NULL;
7325
7326 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007327 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007328 end, len, EXTENT_DELALLOC, 1);
7329 found_end = range_start + found;
7330 if (found_end < range_start)
7331 found_end = (u64)-1;
7332
7333 /*
7334 * we didn't find anything useful, return
7335 * the original results from get_extent()
7336 */
7337 if (range_start > end || found_end <= start) {
7338 em = hole_em;
7339 hole_em = NULL;
7340 goto out;
7341 }
7342
7343 /* adjust the range_start to make sure it doesn't
7344 * go backwards from the start they passed in
7345 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307346 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007347 found = found_end - range_start;
7348
7349 if (found > 0) {
7350 u64 hole_start = start;
7351 u64 hole_len = len;
7352
David Sterba172ddd62011-04-21 00:48:27 +02007353 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007354 if (!em) {
7355 err = -ENOMEM;
7356 goto out;
7357 }
7358 /*
7359 * when btrfs_get_extent can't find anything it
7360 * returns one huge hole
7361 *
7362 * make sure what it found really fits our range, and
7363 * adjust to make sure it is based on the start from
7364 * the caller
7365 */
7366 if (hole_em) {
7367 u64 calc_end = extent_map_end(hole_em);
7368
7369 if (calc_end <= start || (hole_em->start > end)) {
7370 free_extent_map(hole_em);
7371 hole_em = NULL;
7372 } else {
7373 hole_start = max(hole_em->start, start);
7374 hole_len = calc_end - hole_start;
7375 }
7376 }
7377 em->bdev = NULL;
7378 if (hole_em && range_start > hole_start) {
7379 /* our hole starts before our delalloc, so we
7380 * have to return just the parts of the hole
7381 * that go until the delalloc starts
7382 */
7383 em->len = min(hole_len,
7384 range_start - hole_start);
7385 em->start = hole_start;
7386 em->orig_start = hole_start;
7387 /*
7388 * don't adjust block start at all,
7389 * it is fixed at EXTENT_MAP_HOLE
7390 */
7391 em->block_start = hole_em->block_start;
7392 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007393 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7394 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007395 } else {
7396 em->start = range_start;
7397 em->len = found;
7398 em->orig_start = range_start;
7399 em->block_start = EXTENT_MAP_DELALLOC;
7400 em->block_len = found;
7401 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007402 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007403 return hole_em;
7404 }
7405out:
7406
7407 free_extent_map(hole_em);
7408 if (err) {
7409 free_extent_map(em);
7410 return ERR_PTR(err);
7411 }
7412 return em;
7413}
7414
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007415static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7416 const u64 start,
7417 const u64 len,
7418 const u64 orig_start,
7419 const u64 block_start,
7420 const u64 block_len,
7421 const u64 orig_block_len,
7422 const u64 ram_bytes,
7423 const int type)
7424{
7425 struct extent_map *em = NULL;
7426 int ret;
7427
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007428 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007429 em = create_io_em(inode, start, len, orig_start,
7430 block_start, block_len, orig_block_len,
7431 ram_bytes,
7432 BTRFS_COMPRESS_NONE, /* compress_type */
7433 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007434 if (IS_ERR(em))
7435 goto out;
7436 }
7437 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7438 len, block_len, type);
7439 if (ret) {
7440 if (em) {
7441 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007442 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007443 start + len - 1, 0);
7444 }
7445 em = ERR_PTR(ret);
7446 }
7447 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007448
7449 return em;
7450}
7451
Josef Bacik4b46fce2010-05-23 11:00:55 -04007452static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7453 u64 start, u64 len)
7454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007455 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007456 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007457 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007458 struct btrfs_key ins;
7459 u64 alloc_hint;
7460 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007461
Josef Bacik4b46fce2010-05-23 11:00:55 -04007462 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007463 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007464 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007465 if (ret)
7466 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007467
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007468 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7469 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007470 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007471 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007472 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007473 btrfs_free_reserved_extent(fs_info, ins.objectid,
7474 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007475
Josef Bacik4b46fce2010-05-23 11:00:55 -04007476 return em;
7477}
7478
Chris Mason46bfbb52010-05-26 11:04:10 -04007479/*
7480 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7481 * block must be cow'd
7482 */
Josef Bacik00361582013-08-14 14:02:47 -04007483noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007484 u64 *orig_start, u64 *orig_block_len,
7485 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007486{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007487 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007488 struct btrfs_path *path;
7489 int ret;
7490 struct extent_buffer *leaf;
7491 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007492 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007493 struct btrfs_file_extent_item *fi;
7494 struct btrfs_key key;
7495 u64 disk_bytenr;
7496 u64 backref_offset;
7497 u64 extent_end;
7498 u64 num_bytes;
7499 int slot;
7500 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007501 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007502
Chris Mason46bfbb52010-05-26 11:04:10 -04007503 path = btrfs_alloc_path();
7504 if (!path)
7505 return -ENOMEM;
7506
David Sterbaf85b7372017-01-20 14:54:07 +01007507 ret = btrfs_lookup_file_extent(NULL, root, path,
7508 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007509 if (ret < 0)
7510 goto out;
7511
7512 slot = path->slots[0];
7513 if (ret == 1) {
7514 if (slot == 0) {
7515 /* can't find the item, must cow */
7516 ret = 0;
7517 goto out;
7518 }
7519 slot--;
7520 }
7521 ret = 0;
7522 leaf = path->nodes[0];
7523 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007524 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007525 key.type != BTRFS_EXTENT_DATA_KEY) {
7526 /* not our file or wrong item type, must cow */
7527 goto out;
7528 }
7529
7530 if (key.offset > offset) {
7531 /* Wrong offset, must cow */
7532 goto out;
7533 }
7534
7535 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7536 found_type = btrfs_file_extent_type(leaf, fi);
7537 if (found_type != BTRFS_FILE_EXTENT_REG &&
7538 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7539 /* not a regular extent, must cow */
7540 goto out;
7541 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007542
7543 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7544 goto out;
7545
Miao Xiee77751a2013-12-27 21:11:50 +08007546 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7547 if (extent_end <= offset)
7548 goto out;
7549
Chris Mason46bfbb52010-05-26 11:04:10 -04007550 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007551 if (disk_bytenr == 0)
7552 goto out;
7553
7554 if (btrfs_file_extent_compression(leaf, fi) ||
7555 btrfs_file_extent_encryption(leaf, fi) ||
7556 btrfs_file_extent_other_encoding(leaf, fi))
7557 goto out;
7558
Chris Mason46bfbb52010-05-26 11:04:10 -04007559 backref_offset = btrfs_file_extent_offset(leaf, fi);
7560
Josef Bacik7ee9e442013-06-21 16:37:03 -04007561 if (orig_start) {
7562 *orig_start = key.offset - backref_offset;
7563 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7564 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7565 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007566
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007567 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007568 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007569
7570 num_bytes = min(offset + *len, extent_end) - offset;
7571 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7572 u64 range_end;
7573
Jeff Mahoneyda170662016-06-15 09:22:56 -04007574 range_end = round_up(offset + num_bytes,
7575 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007576 ret = test_range_bit(io_tree, offset, range_end,
7577 EXTENT_DELALLOC, 0, NULL);
7578 if (ret) {
7579 ret = -EAGAIN;
7580 goto out;
7581 }
7582 }
7583
Josef Bacik1bda19e2013-10-18 12:10:36 -04007584 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007585
7586 /*
7587 * look for other files referencing this extent, if we
7588 * find any we must cow
7589 */
Josef Bacik00361582013-08-14 14:02:47 -04007590
Liu Boe4c3b2d2017-01-30 12:25:28 -08007591 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007592 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007593 if (ret) {
7594 ret = 0;
7595 goto out;
7596 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007597
7598 /*
7599 * adjust disk_bytenr and num_bytes to cover just the bytes
7600 * in this extent we are about to write. If there
7601 * are any csums in that range we have to cow in order
7602 * to keep the csums correct
7603 */
7604 disk_bytenr += backref_offset;
7605 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007606 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7607 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007608 /*
7609 * all of the above have passed, it is safe to overwrite this extent
7610 * without cow
7611 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007612 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007613 ret = 1;
7614out:
7615 btrfs_free_path(path);
7616 return ret;
7617}
7618
Josef Bacikeb838e72012-07-31 16:28:48 -04007619static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7620 struct extent_state **cached_state, int writing)
7621{
7622 struct btrfs_ordered_extent *ordered;
7623 int ret = 0;
7624
7625 while (1) {
7626 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007627 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007628 /*
7629 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007630 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007631 * extents in this range.
7632 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007633 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007634 lockend - lockstart + 1);
7635
7636 /*
7637 * We need to make sure there are no buffered pages in this
7638 * range either, we could have raced between the invalidate in
7639 * generic_file_direct_write and locking the extent. The
7640 * invalidate needs to happen so that reads after a write do not
7641 * get stale data.
7642 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007643 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007644 (!writing || !filemap_range_has_page(inode->i_mapping,
7645 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007646 break;
7647
7648 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007649 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007650
7651 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007652 /*
7653 * If we are doing a DIO read and the ordered extent we
7654 * found is for a buffered write, we can not wait for it
7655 * to complete and retry, because if we do so we can
7656 * deadlock with concurrent buffered writes on page
7657 * locks. This happens only if our DIO read covers more
7658 * than one extent map, if at this point has already
7659 * created an ordered extent for a previous extent map
7660 * and locked its range in the inode's io tree, and a
7661 * concurrent write against that previous extent map's
7662 * range and this range started (we unlock the ranges
7663 * in the io tree only when the bios complete and
7664 * buffered writes always lock pages before attempting
7665 * to lock range in the io tree).
7666 */
7667 if (writing ||
7668 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7669 btrfs_start_ordered_extent(inode, ordered, 1);
7670 else
7671 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 btrfs_put_ordered_extent(ordered);
7673 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007674 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007675 * We could trigger writeback for this range (and wait
7676 * for it to complete) and then invalidate the pages for
7677 * this range (through invalidate_inode_pages2_range()),
7678 * but that can lead us to a deadlock with a concurrent
7679 * call to readpages() (a buffered read or a defrag call
7680 * triggered a readahead) on a page lock due to an
7681 * ordered dio extent we created before but did not have
7682 * yet a corresponding bio submitted (whence it can not
7683 * complete), which makes readpages() wait for that
7684 * ordered extent to complete while holding a lock on
7685 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007686 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007687 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007688 }
7689
Filipe Mananaade77022016-02-18 14:28:55 +00007690 if (ret)
7691 break;
7692
Josef Bacikeb838e72012-07-31 16:28:48 -04007693 cond_resched();
7694 }
7695
7696 return ret;
7697}
7698
Liu Bo6f9994d2017-01-31 07:50:22 -08007699/* The callers of this must take lock_extent() */
7700static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7701 u64 orig_start, u64 block_start,
7702 u64 block_len, u64 orig_block_len,
7703 u64 ram_bytes, int compress_type,
7704 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007705{
7706 struct extent_map_tree *em_tree;
7707 struct extent_map *em;
7708 struct btrfs_root *root = BTRFS_I(inode)->root;
7709 int ret;
7710
Liu Bo6f9994d2017-01-31 07:50:22 -08007711 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7712 type == BTRFS_ORDERED_COMPRESSED ||
7713 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007714 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007715
Josef Bacik69ffb542012-09-11 15:40:07 -04007716 em_tree = &BTRFS_I(inode)->extent_tree;
7717 em = alloc_extent_map();
7718 if (!em)
7719 return ERR_PTR(-ENOMEM);
7720
7721 em->start = start;
7722 em->orig_start = orig_start;
7723 em->len = len;
7724 em->block_len = block_len;
7725 em->block_start = block_start;
7726 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007727 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007728 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007729 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007730 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007731 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007732 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007733 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007734 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7735 em->compress_type = compress_type;
7736 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007737
7738 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007739 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007740 em->start + em->len - 1, 0);
7741 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007742 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007743 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007744 /*
7745 * The caller has taken lock_extent(), who could race with us
7746 * to add em?
7747 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007748 } while (ret == -EEXIST);
7749
7750 if (ret) {
7751 free_extent_map(em);
7752 return ERR_PTR(ret);
7753 }
7754
Liu Bo6f9994d2017-01-31 07:50:22 -08007755 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007756 return em;
7757}
7758
Josef Bacik4b46fce2010-05-23 11:00:55 -04007759static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7760 struct buffer_head *bh_result, int create)
7761{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007762 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007763 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007764 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307765 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007766 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007767 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007768 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007769 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007770 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007771
Miao Xie172a5042013-02-21 02:48:22 -07007772 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007773 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007774 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007775 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007776
Josef Bacikc3298612012-08-03 16:49:19 -04007777 lockstart = start;
7778 lockend = start + len - 1;
7779
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007780 if (current->journal_info) {
7781 /*
7782 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007783 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007784 * confused.
7785 */
chandan50745b02015-08-28 21:10:13 +05307786 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007787 current->journal_info = NULL;
7788 }
7789
Josef Bacikeb838e72012-07-31 16:28:48 -04007790 /*
7791 * If this errors out it's because we couldn't invalidate pagecache for
7792 * this range and we need to fallback to buffered.
7793 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007794 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7795 create)) {
7796 ret = -ENOTBLK;
7797 goto err;
7798 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007799
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007800 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007801 if (IS_ERR(em)) {
7802 ret = PTR_ERR(em);
7803 goto unlock_err;
7804 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007805
7806 /*
7807 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7808 * io. INLINE is special, and we could probably kludge it in here, but
7809 * it's still buffered so for safety lets just fall back to the generic
7810 * buffered path.
7811 *
7812 * For COMPRESSED we _have_ to read the entire extent in so we can
7813 * decompress it, so there will be buffering required no matter what we
7814 * do, so go ahead and fallback to buffered.
7815 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007816 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817 * to buffered IO. Don't blame me, this is the price we pay for using
7818 * the generic code.
7819 */
7820 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7821 em->block_start == EXTENT_MAP_INLINE) {
7822 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 ret = -ENOTBLK;
7824 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825 }
7826
7827 /* Just a good old fashioned hole, return */
7828 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7829 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7830 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007831 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832 }
7833
7834 /*
7835 * We don't allocate a new extent in the following cases
7836 *
7837 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7838 * existing extent.
7839 * 2) The extent is marked as PREALLOC. We're good to go here and can
7840 * just use the extent.
7841 *
7842 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007843 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007844 len = min(len, em->len - (start - em->start));
7845 lockstart = start + len;
7846 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007847 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007848
7849 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7850 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7851 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007852 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007853 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007854
7855 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7856 type = BTRFS_ORDERED_PREALLOC;
7857 else
7858 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007859 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007861
Josef Bacik00361582013-08-14 14:02:47 -04007862 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007863 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007864 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007865 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007866
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007867 em2 = btrfs_create_dio_extent(inode, start, len,
7868 orig_start, block_start,
7869 len, orig_block_len,
7870 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007871 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007872 if (type == BTRFS_ORDERED_PREALLOC) {
7873 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007874 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007875 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007876 if (em2 && IS_ERR(em2)) {
7877 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007878 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007879 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007880 /*
7881 * For inode marked NODATACOW or extent marked PREALLOC,
7882 * use the existing or preallocated extent, so does not
7883 * need to adjust btrfs_space_info's bytes_may_use.
7884 */
7885 btrfs_free_reserved_data_space_noquota(inode,
7886 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007887 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007889 }
Josef Bacik00361582013-08-14 14:02:47 -04007890
Chris Mason46bfbb52010-05-26 11:04:10 -04007891 /*
7892 * this will cow the extent, reset the len in case we changed
7893 * it above
7894 */
7895 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007896 free_extent_map(em);
7897 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007898 if (IS_ERR(em)) {
7899 ret = PTR_ERR(em);
7900 goto unlock_err;
7901 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007902 len = min(len, em->len - (start - em->start));
7903unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7905 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007906 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007907 bh_result->b_bdev = em->bdev;
7908 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007909 if (create) {
7910 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7911 set_buffer_new(bh_result);
7912
7913 /*
7914 * Need to update the i_size under the extent lock so buffered
7915 * readers will get the updated i_size when we unlock.
7916 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007917 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007918 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007919
chandan50745b02015-08-28 21:10:13 +05307920 WARN_ON(dio_data->reserve < len);
7921 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007922 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307923 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007924 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007925
Josef Bacikeb838e72012-07-31 16:28:48 -04007926 /*
7927 * In the case of write we need to clear and unlock the entire range,
7928 * in the case of read we need to unlock only the end area that we
7929 * aren't using if there is any left over space.
7930 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007931 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007932 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7933 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007934 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007935 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007936 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007937 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007938
Josef Bacik4b46fce2010-05-23 11:00:55 -04007939 free_extent_map(em);
7940
7941 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007942
7943unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007944 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007945 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007946err:
chandan50745b02015-08-28 21:10:13 +05307947 if (dio_data)
7948 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007949 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007950}
7951
Omar Sandoval58efbc92017-08-22 23:45:59 -07007952static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7953 struct bio *bio,
7954 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007955{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007956 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007957 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007958
Mike Christie37226b22016-06-05 14:31:52 -05007959 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007960
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007961 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007962 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007963 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007964
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007965 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007966
Miao Xie8b110e32014-09-12 18:44:03 +08007967 return ret;
7968}
7969
7970static int btrfs_check_dio_repairable(struct inode *inode,
7971 struct bio *failed_bio,
7972 struct io_failure_record *failrec,
7973 int failed_mirror)
7974{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007976 int num_copies;
7977
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007978 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007979 if (num_copies == 1) {
7980 /*
7981 * we only have a single copy of the data, so don't bother with
7982 * all the retry and error correction code that follows. no
7983 * matter what the error is, it is very likely to persist.
7984 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007985 btrfs_debug(fs_info,
7986 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7987 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007988 return 0;
7989 }
7990
7991 failrec->failed_mirror = failed_mirror;
7992 failrec->this_mirror++;
7993 if (failrec->this_mirror == failed_mirror)
7994 failrec->this_mirror++;
7995
7996 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007997 btrfs_debug(fs_info,
7998 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7999 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008000 return 0;
8001 }
8002
8003 return 1;
8004}
8005
Omar Sandoval58efbc92017-08-22 23:45:59 -07008006static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8007 struct page *page, unsigned int pgoff,
8008 u64 start, u64 end, int failed_mirror,
8009 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008010{
8011 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008012 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8013 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008014 struct bio *bio;
8015 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008016 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008017 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008018 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008019 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08008020 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08008021
Mike Christie37226b22016-06-05 14:31:52 -05008022 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008023
8024 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8025 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008026 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008027
8028 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8029 failed_mirror);
8030 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008031 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008032 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008033 }
8034
Liu Bo17347ce2017-05-15 15:33:27 -07008035 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08008036 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07008037 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08008038 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008039 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008040
8041 isector = start - btrfs_io_bio(failed_bio)->logical;
8042 isector >>= inode->i_sb->s_blocksize_bits;
8043 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008045 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008046
8047 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008048 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008049 read_mode, failrec->this_mirror, failrec->in_validation);
8050
Omar Sandoval58efbc92017-08-22 23:45:59 -07008051 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8052 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008053 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008054 bio_put(bio);
8055 }
8056
Omar Sandoval58efbc92017-08-22 23:45:59 -07008057 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008058}
8059
8060struct btrfs_retry_complete {
8061 struct completion done;
8062 struct inode *inode;
8063 u64 start;
8064 int uptodate;
8065};
8066
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008067static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008068{
8069 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008070 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008071 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008072 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008073 int i;
8074
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008075 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008076 goto end;
8077
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008079 io_tree = &BTRFS_I(inode)->io_tree;
8080 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08008081 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082
Miao Xie8b110e32014-09-12 18:44:03 +08008083 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008084 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008085 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008086 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8087 io_tree, done->start, bvec->bv_page,
8088 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008089end:
8090 complete(&done->done);
8091 bio_put(bio);
8092}
8093
Omar Sandoval58efbc92017-08-22 23:45:59 -07008094static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8095 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008096{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008098 struct bio_vec bvec;
8099 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008100 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 unsigned int pgoff;
8103 u32 sectorsize;
8104 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008105 blk_status_t ret;
8106 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008107
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308108 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008109 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110
Miao Xiec1dc0892014-09-12 18:43:56 +08008111 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008112 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008113 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008114
Liu Bo17347ce2017-05-15 15:33:27 -07008115 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8116 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8117 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118
8119next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008120 done.uptodate = 0;
8121 done.start = start;
8122 init_completion(&done.done);
8123
Liu Bo17347ce2017-05-15 15:33:27 -07008124 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 pgoff, start, start + sectorsize - 1,
8126 io_bio->mirror_num,
8127 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008128 if (ret) {
8129 err = ret;
8130 goto next;
8131 }
Miao Xie8b110e32014-09-12 18:44:03 +08008132
David Sterba9c17f6c2017-07-19 19:26:45 +02008133 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008134
8135 if (!done.uptodate) {
8136 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008138 }
8139
Liu Bo629ebf42017-05-15 17:20:07 -07008140next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308141 start += sectorsize;
8142
Liu Bo97bf5a52017-04-07 13:11:10 -07008143 nr_sectors--;
8144 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008146 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 goto next_block_or_try_again;
8148 }
Miao Xie8b110e32014-09-12 18:44:03 +08008149 }
8150
Liu Bo629ebf42017-05-15 17:20:07 -07008151 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008152}
8153
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008154static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008155{
8156 struct btrfs_retry_complete *done = bio->bi_private;
8157 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008158 struct extent_io_tree *io_tree, *failure_tree;
8159 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008160 struct bio_vec *bvec;
8161 int uptodate;
8162 int ret;
8163 int i;
8164
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008165 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008166 goto end;
8167
8168 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308169
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308170 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008171 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172
Josef Bacik7870d082017-05-05 11:57:15 -04008173 io_tree = &BTRFS_I(inode)->io_tree;
8174 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8175
David Sterbac09abff2017-07-13 18:10:07 +02008176 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008177 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008178 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8179 bvec->bv_offset, done->start,
8180 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008181 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008182 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8183 failure_tree, io_tree, done->start,
8184 bvec->bv_page,
8185 btrfs_ino(BTRFS_I(inode)),
8186 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008187 else
8188 uptodate = 0;
8189 }
8190
8191 done->uptodate = uptodate;
8192end:
8193 complete(&done->done);
8194 bio_put(bio);
8195}
8196
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008197static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8198 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008199{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308200 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008201 struct bio_vec bvec;
8202 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008203 struct btrfs_retry_complete done;
8204 u64 start;
8205 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308206 u32 sectorsize;
8207 int nr_sectors;
8208 unsigned int pgoff;
8209 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008210 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008211 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008212 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008213
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308214 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008215 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308216
Omar Sandoval58efbc92017-08-22 23:45:59 -07008217 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008218 start = io_bio->logical;
8219 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008220 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008221
Liu Bo17347ce2017-05-15 15:33:27 -07008222 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8223 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308224
Liu Bo17347ce2017-05-15 15:33:27 -07008225 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308226next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008227 if (uptodate) {
8228 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8229 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8230 bvec.bv_page, pgoff, start, sectorsize);
8231 if (likely(!ret))
8232 goto next;
8233 }
Miao Xie8b110e32014-09-12 18:44:03 +08008234try_again:
8235 done.uptodate = 0;
8236 done.start = start;
8237 init_completion(&done.done);
8238
Omar Sandoval58efbc92017-08-22 23:45:59 -07008239 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8240 pgoff, start, start + sectorsize - 1,
8241 io_bio->mirror_num, btrfs_retry_endio,
8242 &done);
8243 if (status) {
8244 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008245 goto next;
8246 }
8247
David Sterba9c17f6c2017-07-19 19:26:45 +02008248 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008249
8250 if (!done.uptodate) {
8251 /* We might have another mirror, so try again */
8252 goto try_again;
8253 }
8254next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308255 offset += sectorsize;
8256 start += sectorsize;
8257
8258 ASSERT(nr_sectors);
8259
Liu Bo97bf5a52017-04-07 13:11:10 -07008260 nr_sectors--;
8261 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308262 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008263 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308264 goto next_block;
8265 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008266 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008267
8268 return err;
8269}
8270
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008271static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8272 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008273{
8274 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8275
8276 if (skip_csum) {
8277 if (unlikely(err))
8278 return __btrfs_correct_data_nocsum(inode, io_bio);
8279 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008280 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008281 } else {
8282 return __btrfs_subio_endio_read(inode, io_bio, err);
8283 }
8284}
8285
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008286static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008287{
8288 struct btrfs_dio_private *dip = bio->bi_private;
8289 struct inode *inode = dip->inode;
8290 struct bio *dio_bio;
8291 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008292 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008293
Liu Bo99c4e3b2017-09-15 15:06:51 -06008294 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008295 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008296
Josef Bacik4b46fce2010-05-23 11:00:55 -04008297 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008298 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008299 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008300
Josef Bacik4b46fce2010-05-23 11:00:55 -04008301 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008302
Liu Bo99c4e3b2017-09-15 15:06:51 -06008303 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008304 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008305
8306 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008308 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008309}
8310
Qu Wenruo524272602017-03-08 10:25:52 +08008311static void __endio_write_update_ordered(struct inode *inode,
8312 const u64 offset, const u64 bytes,
8313 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008316 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008317 struct btrfs_workqueue *wq;
8318 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008319 u64 ordered_offset = offset;
8320 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008321 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008322
Qu Wenruo524272602017-03-08 10:25:52 +08008323 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8324 wq = fs_info->endio_freespace_worker;
8325 func = btrfs_freespace_write_helper;
8326 } else {
8327 wq = fs_info->endio_write_workers;
8328 func = btrfs_endio_write_helper;
8329 }
8330
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008331 while (ordered_offset < offset + bytes) {
8332 last_offset = ordered_offset;
8333 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8334 &ordered_offset,
8335 ordered_bytes,
8336 uptodate)) {
8337 btrfs_init_work(&ordered->work, func,
8338 finish_ordered_fn,
8339 NULL, NULL);
8340 btrfs_queue_work(wq, &ordered->work);
8341 }
8342 /*
8343 * If btrfs_dec_test_ordered_pending does not find any ordered
8344 * extent in the range, we can exit.
8345 */
8346 if (ordered_offset == last_offset)
8347 return;
8348 /*
8349 * Our bio might span multiple ordered extents. In this case
8350 * we keep goin until we have accounted the whole dio.
8351 */
8352 if (ordered_offset < offset + bytes) {
8353 ordered_bytes = offset + bytes - ordered_offset;
8354 ordered = NULL;
8355 }
Chris Mason163cf092010-11-28 19:56:33 -05008356 }
Filipe Manana14543772015-11-24 16:23:54 +00008357}
8358
8359static void btrfs_endio_direct_write(struct bio *bio)
8360{
8361 struct btrfs_dio_private *dip = bio->bi_private;
8362 struct bio *dio_bio = dip->dio_bio;
8363
Qu Wenruo524272602017-03-08 10:25:52 +08008364 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008365 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008366
Josef Bacik4b46fce2010-05-23 11:00:55 -04008367 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008368
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008369 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008370 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008371 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008372}
8373
David Sterbad0ee3932018-03-08 14:35:48 +01008374static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008375 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008376{
Josef Bacikc6100a42017-05-05 11:57:13 -04008377 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008378 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008379 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008380 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008381 return 0;
8382}
8383
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008384static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008385{
8386 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008387 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008388
Miao Xie8b110e32014-09-12 18:44:03 +08008389 if (err)
8390 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008391 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008392 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8393 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008394 (unsigned long long)bio->bi_iter.bi_sector,
8395 bio->bi_iter.bi_size, err);
8396
8397 if (dip->subio_endio)
8398 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008399
8400 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008401 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008402 * We want to perceive the errors flag being set before
8403 * decrementing the reference count. We don't need a barrier
8404 * since atomic operations with a return value are fully
8405 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008406 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008407 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008408 }
8409
8410 /* if there are more bios still pending for this dio, just exit */
8411 if (!atomic_dec_and_test(&dip->pending_bios))
8412 goto out;
8413
Chris Mason9be33952013-05-17 18:30:14 -04008414 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008415 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008416 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008417 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008418 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008419 }
8420out:
8421 bio_put(bio);
8422}
8423
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008424static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008425 struct btrfs_dio_private *dip,
8426 struct bio *bio,
8427 u64 file_offset)
8428{
8429 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8430 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008431 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008432
8433 /*
8434 * We load all the csum data we need when we submit
8435 * the first bio to reduce the csum tree search and
8436 * contention.
8437 */
8438 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008439 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008440 file_offset);
8441 if (ret)
8442 return ret;
8443 }
8444
8445 if (bio == dip->orig_bio)
8446 return 0;
8447
8448 file_offset -= dip->logical_offset;
8449 file_offset >>= inode->i_sb->s_blocksize_bits;
8450 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8451
8452 return 0;
8453}
8454
David Sterbad0ee3932018-03-08 14:35:48 +01008455static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8456 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008457{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008458 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008459 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008460 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008461 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008462
Liu Bo4c274bc2017-11-01 17:19:27 -06008463 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008464 if (async_submit)
8465 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8466
Josef Bacik5fd02042012-05-02 14:00:54 -04008467 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008468 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008469 if (ret)
8470 goto err;
8471 }
Miao Xiee65e1532010-11-22 03:04:43 +00008472
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008473 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008474 goto map;
8475
8476 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008477 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8478 file_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01008479 btrfs_submit_bio_start_direct_io,
8480 btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008481 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008482 } else if (write) {
8483 /*
8484 * If we aren't doing async submit, calculate the csum of the
8485 * bio now.
8486 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008487 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008488 if (ret)
8489 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008490 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008491 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008492 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008493 if (ret)
8494 goto err;
8495 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008496map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008497 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008498err:
Miao Xiee65e1532010-11-22 03:04:43 +00008499 return ret;
8500}
8501
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008502static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008503{
8504 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008505 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008506 struct bio *bio;
8507 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008508 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008509 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008510 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008511 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008512 u64 submit_len;
8513 int clone_offset = 0;
8514 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308515 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008516 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008517
Kent Overstreet4f024f32013-10-11 15:44:27 -07008518 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008519 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008520 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8521 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008522 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008523 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008524
Liu Bo725130b2017-05-16 09:51:39 -07008525 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008526 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008527 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008528 goto submit;
8529 }
8530
David Woodhouse53b381b2013-01-29 18:40:14 -05008531 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008532 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008533 async_submit = 0;
8534 else
8535 async_submit = 1;
8536
Liu Bo725130b2017-05-16 09:51:39 -07008537 /* bio split */
8538 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008539 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008540 do {
Liu Bo725130b2017-05-16 09:51:39 -07008541 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008542
Liu Bo725130b2017-05-16 09:51:39 -07008543 /*
8544 * This will never fail as it's passing GPF_NOFS and
8545 * the allocation is backed by btrfs_bioset.
8546 */
Liu Boe4770942017-05-16 10:57:14 -07008547 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008548 clone_len);
8549 bio->bi_private = dip;
8550 bio->bi_end_io = btrfs_end_dio_bio;
8551 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008552
Liu Bo725130b2017-05-16 09:51:39 -07008553 ASSERT(submit_len >= clone_len);
8554 submit_len -= clone_len;
8555 if (submit_len == 0)
8556 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008557
Liu Bo725130b2017-05-16 09:51:39 -07008558 /*
8559 * Increase the count before we submit the bio so we know
8560 * the end IO handler won't happen before we increase the
8561 * count. Otherwise, the dip might get freed before we're
8562 * done setting it up.
8563 */
8564 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008565
David Sterbad0ee3932018-03-08 14:35:48 +01008566 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008567 async_submit);
8568 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008569 bio_put(bio);
8570 atomic_dec(&dip->pending_bios);
8571 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008572 }
Liu Bo725130b2017-05-16 09:51:39 -07008573
8574 clone_offset += clone_len;
8575 start_sector += clone_len >> 9;
8576 file_offset += clone_len;
8577
8578 map_length = submit_len;
8579 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8580 start_sector << 9, &map_length, NULL, 0);
8581 if (ret)
8582 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008583 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008584
Josef Bacik02f57c72011-04-06 14:25:44 -04008585submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008586 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008587 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008588 return 0;
8589
8590 bio_put(bio);
8591out_err:
8592 dip->errors = 1;
8593 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008594 * Before atomic variable goto zero, we must make sure dip->errors is
8595 * perceived to be set. This ordering is ensured by the fact that an
8596 * atomic operations with a return value are fully ordered as per
8597 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008598 */
Miao Xiee65e1532010-11-22 03:04:43 +00008599 if (atomic_dec_and_test(&dip->pending_bios))
8600 bio_io_error(dip->orig_bio);
8601
8602 /* bio_end_io() will handle error, so we needn't return it */
8603 return 0;
8604}
8605
Mike Christie8a4c1e42016-06-05 14:31:50 -05008606static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8607 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008608{
Filipe Manana61de7182015-07-01 12:13:10 +01008609 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008610 struct bio *bio = NULL;
8611 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008612 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008613 int ret = 0;
8614
David Sterba8b6c1d52017-06-02 17:48:13 +02008615 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008616
Miao Xiec1dc0892014-09-12 18:43:56 +08008617 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008618 if (!dip) {
8619 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008620 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008621 }
8622
8623 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008624 dip->inode = inode;
8625 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008626 dip->bytes = dio_bio->bi_iter.bi_size;
8627 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008628 bio->bi_private = dip;
8629 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008630 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008631 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008632 io_bio = btrfs_io_bio(bio);
8633 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008634
Miao Xiec1dc0892014-09-12 18:43:56 +08008635 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008636 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008637 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008638 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008639 dip->subio_endio = btrfs_subio_endio_read;
8640 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008641
Filipe Mananaf28a4922015-12-08 19:23:20 +00008642 /*
8643 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8644 * even if we fail to submit a bio, because in such case we do the
8645 * corresponding error handling below and it must not be done a second
8646 * time by btrfs_direct_IO().
8647 */
8648 if (write) {
8649 struct btrfs_dio_data *dio_data = current->journal_info;
8650
8651 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8652 dip->bytes;
8653 dio_data->unsubmitted_oe_range_start =
8654 dio_data->unsubmitted_oe_range_end;
8655 }
8656
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008657 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008658 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008659 return;
Chris Mason9be33952013-05-17 18:30:14 -04008660
Liu Bo3892ac92017-04-17 15:00:28 -07008661 if (io_bio->end_io)
8662 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008663
Josef Bacik4b46fce2010-05-23 11:00:55 -04008664free_ordered:
8665 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008666 * If we arrived here it means either we failed to submit the dip
8667 * or we either failed to clone the dio_bio or failed to allocate the
8668 * dip. If we cloned the dio_bio and allocated the dip, we can just
8669 * call bio_endio against our io_bio so that we get proper resource
8670 * cleanup if we fail to submit the dip, otherwise, we must do the
8671 * same as btrfs_endio_direct_[write|read] because we can't call these
8672 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008673 */
Liu Bo3892ac92017-04-17 15:00:28 -07008674 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008675 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008676 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008677 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008678 * and all the cleanup and final put for dio_bio (through
8679 * dio_end_io()).
8680 */
8681 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008682 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008683 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008684 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008685 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008686 file_offset,
8687 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008688 false);
Filipe Manana14543772015-11-24 16:23:54 +00008689 else
Filipe Manana61de7182015-07-01 12:13:10 +01008690 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8691 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008692
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008693 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008694 /*
8695 * Releases and cleans up our dio_bio, no need to bio_put()
8696 * nor bio_endio()/bio_io_error() against dio_bio.
8697 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008698 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008699 }
Liu Bo3892ac92017-04-17 15:00:28 -07008700 if (bio)
8701 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008702 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008703}
8704
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008705static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008706 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008707{
8708 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008709 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008710 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008711 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008712
8713 if (offset & blocksize_mask)
8714 goto out;
8715
Al Viro28060d52014-03-22 05:15:17 -04008716 if (iov_iter_alignment(iter) & blocksize_mask)
8717 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008718
Al Viro28060d52014-03-22 05:15:17 -04008719 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008720 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008721 return 0;
8722 /*
8723 * Check to make sure we don't have duplicate iov_base's in this
8724 * iovec, if so return EINVAL, otherwise we'll get csum errors
8725 * when reading back.
8726 */
8727 for (seg = 0; seg < iter->nr_segs; seg++) {
8728 for (i = seg + 1; i < iter->nr_segs; i++) {
8729 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008730 goto out;
8731 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008732 }
8733 retval = 0;
8734out:
8735 return retval;
8736}
Josef Bacikeb838e72012-07-31 16:28:48 -04008737
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008738static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008739{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008740 struct file *file = iocb->ki_filp;
8741 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308743 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008744 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008745 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008746 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008747 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008748 bool wakeup = true;
8749 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008750 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008751
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008752 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008753 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008754
Jens Axboefe0f07d2015-04-15 17:05:48 -06008755 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008756
Josef Bacik0e267c42013-07-02 10:38:02 -04008757 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008758 * The generic stuff only does filemap_write_and_wait_range, which
8759 * isn't enough if we've written compressed pages to this area, so
8760 * we need to flush the dirty pages again to make absolutely sure
8761 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008762 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008763 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008764 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8765 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008766 filemap_fdatawrite_range(inode->i_mapping, offset,
8767 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008768
Omar Sandoval6f673762015-03-16 04:33:52 -07008769 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008770 /*
8771 * If the write DIO is beyond the EOF, we need update
8772 * the isize, but it is protected by i_mutex. So we can
8773 * not unlock the i_mutex at this case.
8774 */
8775 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008776 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008777 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008778 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008779 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8780 ret = -EAGAIN;
8781 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008782 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008783 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8784 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008785 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008786 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008787
8788 /*
8789 * We need to know how many extents we reserved so that we can
8790 * do the accounting properly if we go over the number we
8791 * originally calculated. Abuse current->journal_info for this.
8792 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008793 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008794 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008795 dio_data.unsubmitted_oe_range_start = (u64)offset;
8796 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308797 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308798 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008799 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8800 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008801 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008802 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8803 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008804 }
8805
Omar Sandoval17f8c842015-03-16 04:33:50 -07008806 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008807 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008808 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008809 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008810 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308811 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008812 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008813 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308814 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008815 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008816 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008817 /*
8818 * On error we might have left some ordered extents
8819 * without submitting corresponding bios for them, so
8820 * cleanup them up to avoid other tasks getting them
8821 * and waiting for them to complete forever.
8822 */
8823 if (dio_data.unsubmitted_oe_range_start <
8824 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008825 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008826 dio_data.unsubmitted_oe_range_start,
8827 dio_data.unsubmitted_oe_range_end -
8828 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008829 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008830 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008831 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008832 offset, count - (size_t)ret, true);
8833 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008834 }
Miao Xie38851cc2013-02-08 07:04:11 +00008835out:
Miao Xie2e60a512013-02-08 07:01:08 +00008836 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008837 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008838 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008839 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008840
Qu Wenruo364ecf32017-02-27 15:10:38 +08008841 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008842 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008843}
8844
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008845#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8846
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008847static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8848 __u64 start, __u64 len)
8849{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008850 int ret;
8851
8852 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8853 if (ret)
8854 return ret;
8855
David Sterba2135fb92017-06-23 04:09:57 +02008856 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008857}
8858
Chris Mason9ebefb182007-06-15 13:50:00 -04008859int btrfs_readpage(struct file *file, struct page *page)
8860{
Chris Masond1310b22008-01-24 16:13:08 -05008861 struct extent_io_tree *tree;
8862 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008863 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008864}
Chris Mason1832a6d2007-12-21 16:27:21 -05008865
Chris Mason39279cc2007-06-12 06:35:45 -04008866static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8867{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008868 struct inode *inode = page->mapping->host;
8869 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008870
8871 if (current->flags & PF_MEMALLOC) {
8872 redirty_page_for_writepage(wbc, page);
8873 unlock_page(page);
8874 return 0;
8875 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008876
8877 /*
8878 * If we are under memory pressure we will call this directly from the
8879 * VM, we need to make sure we have the inode referenced for the ordered
8880 * extent. If not just return like we didn't do anything.
8881 */
8882 if (!igrab(inode)) {
8883 redirty_page_for_writepage(wbc, page);
8884 return AOP_WRITEPAGE_ACTIVATE;
8885 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008886 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008887 btrfs_add_delayed_iput(inode);
8888 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008889}
Chris Mason39279cc2007-06-12 06:35:45 -04008890
Eric Sandeen48a3b632013-04-25 20:41:01 +00008891static int btrfs_writepages(struct address_space *mapping,
8892 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008893{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008894 return extent_writepages(mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008895}
8896
Chris Mason3ab2fb52007-11-08 10:59:22 -05008897static int
8898btrfs_readpages(struct file *file, struct address_space *mapping,
8899 struct list_head *pages, unsigned nr_pages)
8900{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008901 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008902}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008903
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008905{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008906 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008907 if (ret == 1) {
8908 ClearPagePrivate(page);
8909 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008910 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008911 }
8912 return ret;
8913}
Chris Mason39279cc2007-06-12 06:35:45 -04008914
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8916{
Chris Mason98509cf2008-09-11 15:51:43 -04008917 if (PageWriteback(page) || PageDirty(page))
8918 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008919 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008920}
8921
Lukas Czernerd47992f2013-05-21 23:17:23 -04008922static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8923 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008924{
Josef Bacik5fd02042012-05-02 14:00:54 -04008925 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008926 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008927 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008928 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008929 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008930 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308931 u64 start;
8932 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008933 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008934
Chris Mason8b62b722009-09-02 16:53:46 -04008935 /*
8936 * we have the page locked, so new writeback can't start,
8937 * and the dirty bit won't be cleared while we are here.
8938 *
8939 * Wait for IO on this page so that we can safely clear
8940 * the PagePrivate2 bit and do ordered accounting
8941 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008942 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008943
Josef Bacik5fd02042012-05-02 14:00:54 -04008944 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945 if (offset) {
8946 btrfs_releasepage(page, GFP_NOFS);
8947 return;
8948 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008949
8950 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008951 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308952again:
8953 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008954 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308955 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308957 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008958 /*
8959 * IO on this page will never be started, so we need
8960 * to account for any ordered extents now
8961 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008962 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308963 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008964 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008965 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008966 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008967 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008968 /*
8969 * whoever cleared the private bit is responsible
8970 * for the finish_ordered_io
8971 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008972 if (TestClearPagePrivate2(page)) {
8973 struct btrfs_ordered_inode_tree *tree;
8974 u64 new_len;
8975
8976 tree = &BTRFS_I(inode)->ordered_tree;
8977
8978 spin_lock_irq(&tree->lock);
8979 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308980 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008981 if (new_len < ordered->truncated_len)
8982 ordered->truncated_len = new_len;
8983 spin_unlock_irq(&tree->lock);
8984
8985 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308986 start,
8987 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008988 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008989 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008990 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008991 if (!inode_evicting) {
8992 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308993 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008994 &cached_state);
8995 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308996
8997 start = end + 1;
8998 if (start < page_end)
8999 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009000 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009001
Qu Wenruob9d0b382015-09-29 10:35:16 +08009002 /*
9003 * Qgroup reserved space handler
9004 * Page here will be either
9005 * 1) Already written to disk
9006 * In this case, its reserved space is released from data rsv map
9007 * and will be freed by delayed_ref handler finally.
9008 * So even we call qgroup_free_data(), it won't decrease reserved
9009 * space.
9010 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009011 * This means the reserved space should be freed here. However,
9012 * if a truncate invalidates the page (by clearing PageDirty)
9013 * and the page is accounted for while allocating extent
9014 * in btrfs_check_data_free_space() we let delayed_ref to
9015 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009016 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009017 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009018 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009019 if (!inode_evicting) {
9020 clear_extent_bit(tree, page_start, page_end,
9021 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009022 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9023 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009024 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009025
9026 __btrfs_releasepage(page, GFP_NOFS);
9027 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028
Chris Mason4a096752008-07-21 10:29:44 -04009029 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009030 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009031 ClearPagePrivate(page);
9032 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009033 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009034 }
Chris Mason39279cc2007-06-12 06:35:45 -04009035}
9036
Chris Mason9ebefb182007-06-15 13:50:00 -04009037/*
9038 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9039 * called from a page fault handler when a page is first dirtied. Hence we must
9040 * be careful to check for EOF conditions here. We set the page up correctly
9041 * for a written page which means we get ENOSPC checking when writing into
9042 * holes and correct delalloc and unwritten extent mapping on filesystems that
9043 * support these features.
9044 *
9045 * We are not allowed to take the i_mutex here so we have to play games to
9046 * protect against truncate races as the page could now be beyond EOF. Because
9047 * vmtruncate() writes the inode size before removing pages, once we have the
9048 * page lock we can determine safely if the page is beyond EOF. If it is not
9049 * beyond EOF, then the page is guaranteed safe against truncation until we
9050 * unlock the page.
9051 */
Dave Jiang11bac802017-02-24 14:56:41 -08009052int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009053{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009054 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009055 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009056 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009057 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9058 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009059 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009060 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009061 char *kaddr;
9062 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009063 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009064 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009065 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309066 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009067 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309069 u64 end;
9070
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009071 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009072
Jan Karab2b5ef52012-06-12 16:20:45 +02009073 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009074 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009075 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309076 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009077
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309078 /*
9079 * Reserving delalloc space after obtaining the page lock can lead to
9080 * deadlock. For example, if a dirty page is locked by this function
9081 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9082 * dirty page write out, then the btrfs_writepage() function could
9083 * end up waiting indefinitely to get a lock on the page currently
9084 * being processed by btrfs_page_mkwrite() function.
9085 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009086 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309087 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009088 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009089 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009090 reserved = 1;
9091 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009092 if (ret) {
9093 if (ret == -ENOMEM)
9094 ret = VM_FAULT_OOM;
9095 else /* -ENOSPC, -EIO, etc */
9096 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009097 if (reserved)
9098 goto out;
9099 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009100 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009101
Nick Piggin56a76f82009-03-31 15:23:23 -07009102 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009103again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009104 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009105 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009106
Chris Mason9ebefb182007-06-15 13:50:00 -04009107 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009108 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009109 /* page got truncated out from underneath us */
9110 goto out_unlock;
9111 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009112 wait_on_page_writeback(page);
9113
David Sterbaff13db42015-12-03 14:30:40 +01009114 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009115 set_page_extent_mapped(page);
9116
Chris Masoneb84ae02008-07-17 13:53:27 -04009117 /*
9118 * we can't set the delalloc bits if there are pending ordered
9119 * extents. Drop our locks and wait for them to finish
9120 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009121 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9122 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009123 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009124 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009125 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009126 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009127 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009128 btrfs_put_ordered_extent(ordered);
9129 goto again;
9130 }
9131
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009132 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009133 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009134 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009135 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309136 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009137 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009138 page_start, PAGE_SIZE - reserved_space,
9139 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309140 }
9141 }
9142
Josef Bacikfbf19082009-10-01 17:10:23 -04009143 /*
Liu Bo54160342016-12-13 12:15:19 -08009144 * page_mkwrite gets called when the page is firstly dirtied after it's
9145 * faulted in, but write(2) could also dirty a page and set delalloc
9146 * bits, thus in this case for space account reason, we still need to
9147 * clear any delalloc bits within this page range since we have to
9148 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009149 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309150 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009151 EXTENT_DIRTY | EXTENT_DELALLOC |
9152 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009153 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009154
Filipe Mananae3b8a482017-11-04 00:16:59 +00009155 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009156 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009157 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009158 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009159 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009160 ret = VM_FAULT_SIGBUS;
9161 goto out_unlock;
9162 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009163 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009164
9165 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009166 if (page_start + PAGE_SIZE > size)
9167 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009168 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009169 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009170
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009171 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009172 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009173 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009174 flush_dcache_page(page);
9175 kunmap(page);
9176 }
Chris Mason247e7432008-07-17 12:53:51 -04009177 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009178 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009179 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009180
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009181 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009182 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009183 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009184
David Sterbae43bbe52017-12-12 21:43:52 +01009185 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009186
9187out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009188 if (!ret) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009189 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009190 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009191 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009192 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009193 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009194 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009195out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009196 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009197 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009198 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009199out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009200 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009201 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009202 return ret;
9203}
9204
Filipe Manana213e8c52018-02-06 20:40:31 +00009205static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009206{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009207 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009208 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009209 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009210 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009211 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009212 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009213 u64 mask = fs_info->sectorsize - 1;
9214 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009215
Filipe Manana213e8c52018-02-06 20:40:31 +00009216 if (!skip_writeback) {
9217 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9218 (u64)-1);
9219 if (ret)
9220 return ret;
9221 }
Chris Mason39279cc2007-06-12 06:35:45 -04009222
Josef Bacikfcb80c22011-05-03 10:40:22 -04009223 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009224 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009225 * 3 things going on here
9226 *
9227 * 1) We need to reserve space for our orphan item and the space to
9228 * delete our orphan item. Lord knows we don't want to have a dangling
9229 * orphan item because we didn't reserve space to remove it.
9230 *
9231 * 2) We need to reserve space to update our inode.
9232 *
9233 * 3) We need to have something to cache all the space that is going to
9234 * be free'd up by the truncate operation, but also have some slack
9235 * space reserved in case it uses space during the truncate (thank you
9236 * very much snapshotting).
9237 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009238 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009240 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009241 * doesn't end up using space reserved for updating the inode or
9242 * removing the orphan item. We also need to be able to stop the
9243 * transaction and start a new one, which means we need to be able to
9244 * update the inode several times, and we have no idea of knowing how
9245 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009246 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009247 * Then there is the orphan item, which does indeed need to be held on
9248 * to for the whole operation, and we need nobody to touch this reserved
9249 * space except the orphan code.
9250 *
9251 * So that leaves us with
9252 *
9253 * 1) root->orphan_block_rsv - for the orphan deletion.
9254 * 2) rsv - for the truncate reservation, which we will steal from the
9255 * transaction reservation.
9256 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9257 * updating the inode.
9258 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009259 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009260 if (!rsv)
9261 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009262 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009263 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009264
Josef Bacik907cbce2011-08-08 13:46:15 -04009265 /*
Josef Bacik07127182011-08-19 10:29:59 -04009266 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009267 * 1 for updating the inode.
9268 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009269 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009270 if (IS_ERR(trans)) {
9271 err = PTR_ERR(trans);
9272 goto out;
9273 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009274
Josef Bacik907cbce2011-08-08 13:46:15 -04009275 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009276 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009277 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009278 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009279
Chris Mason5a3f23d2009-03-31 13:27:11 -04009280 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009281 * So if we truncate and then write and fsync we normally would just
9282 * write the extents that changed, which is a problem if we need to
9283 * first truncate that entire inode. So set this flag so we write out
9284 * all of the extents in the inode to the sync log so we're completely
9285 * safe.
9286 */
9287 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009288 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009289
Yan, Zheng80825102009-11-12 09:35:36 +00009290 while (1) {
9291 ret = btrfs_truncate_inode_items(trans, root, inode,
9292 inode->i_size,
9293 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009294 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009295 if (ret != -ENOSPC && ret != -EAGAIN) {
Omar Sandovald5014732018-05-22 09:47:58 -07009296 if (ret < 0)
9297 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009298 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009299 }
Chris Mason39279cc2007-06-12 06:35:45 -04009300
Yan, Zheng80825102009-11-12 09:35:36 +00009301 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009302 if (ret) {
9303 err = ret;
9304 break;
9305 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009306
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009307 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009308 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009309
9310 trans = btrfs_start_transaction(root, 2);
9311 if (IS_ERR(trans)) {
9312 ret = err = PTR_ERR(trans);
9313 trans = NULL;
9314 break;
9315 }
9316
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009317 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009318 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009319 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009320 BUG_ON(ret); /* shouldn't happen */
9321 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009322 }
9323
Josef Bacikddfae632017-10-19 14:16:02 -04009324 /*
9325 * We can't call btrfs_truncate_block inside a trans handle as we could
9326 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9327 * we've truncated everything except the last little bit, and can do
9328 * btrfs_truncate_block and then update the disk_i_size.
9329 */
9330 if (ret == NEED_TRUNCATE_BLOCK) {
9331 btrfs_end_transaction(trans);
9332 btrfs_btree_balance_dirty(fs_info);
9333
9334 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9335 if (ret)
9336 goto out;
9337 trans = btrfs_start_transaction(root, 1);
9338 if (IS_ERR(trans)) {
9339 ret = PTR_ERR(trans);
9340 goto out;
9341 }
9342 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9343 }
9344
Yan, Zheng80825102009-11-12 09:35:36 +00009345 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009346 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009347 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009348 if (ret)
9349 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009350 }
9351
Chris Mason917c16b2011-11-08 14:49:59 -05009352 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009353 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009354 ret = btrfs_update_inode(trans, root, inode);
9355 if (ret && !err)
9356 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009357
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009358 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009359 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009360 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009361out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009362 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009363
Josef Bacik3893e332011-01-31 16:03:11 -05009364 if (ret && !err)
9365 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009366
Josef Bacik3893e332011-01-31 16:03:11 -05009367 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009368}
9369
Sven Wegener3b963622008-06-09 21:57:42 -04009370/*
Chris Masond352ac62008-09-29 15:18:18 -04009371 * create a new subvolume directory/inode (helper for the ioctl).
9372 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009373int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009374 struct btrfs_root *new_root,
9375 struct btrfs_root *parent_root,
9376 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009377{
Chris Mason39279cc2007-06-12 06:35:45 -04009378 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009379 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009380 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009381
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009382 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9383 new_dirid, new_dirid,
9384 S_IFDIR | (~current_umask() & S_IRWXUGO),
9385 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009386 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009387 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009388 inode->i_op = &btrfs_dir_inode_operations;
9389 inode->i_fop = &btrfs_dir_file_operations;
9390
Miklos Szeredibfe86842011-10-28 14:13:29 +02009391 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009392 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009393 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009394
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009395 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9396 if (err)
9397 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009398 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009399 new_root->root_key.objectid, err);
9400
Yan, Zheng76dda932009-09-21 16:00:26 -04009401 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009402
Yan, Zheng76dda932009-09-21 16:00:26 -04009403 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009404 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009405}
9406
Chris Mason39279cc2007-06-12 06:35:45 -04009407struct inode *btrfs_alloc_inode(struct super_block *sb)
9408{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009409 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009410 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009411 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009412
David Sterba712e36c2017-10-31 17:08:27 +01009413 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009414 if (!ei)
9415 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009416
9417 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009419 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009420 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009421 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009422 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009423 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009424 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009425 ei->disk_i_size = 0;
9426 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009427 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009428 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009429 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009430 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009431 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009432
Josef Bacik9e0baf62011-07-15 15:16:44 +00009433 spin_lock_init(&ei->lock);
9434 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009435 if (sb->s_magic != BTRFS_TEST_MAGIC)
9436 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9437 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009438 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009439 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009440 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009441
Miao Xie16cdcec2011-04-22 18:12:22 +08009442 ei->delayed_node = NULL;
9443
chandan r9cc97d62012-07-04 12:48:07 +05309444 ei->i_otime.tv_sec = 0;
9445 ei->i_otime.tv_nsec = 0;
9446
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009447 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009448 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009449 extent_io_tree_init(&ei->io_tree, inode);
9450 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009451 ei->io_tree.track_uptodate = 1;
9452 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009453 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009454 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009455 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009456 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009457 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009458 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009459 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009460 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009461
9462 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009463}
9464
Josef Bacikaaedb552013-10-11 14:44:09 -04009465#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9466void btrfs_test_destroy_inode(struct inode *inode)
9467{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009468 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009469 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9470}
9471#endif
9472
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009473static void btrfs_i_callback(struct rcu_head *head)
9474{
9475 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009476 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9477}
9478
Chris Mason39279cc2007-06-12 06:35:45 -04009479void btrfs_destroy_inode(struct inode *inode)
9480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009482 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009483 struct btrfs_root *root = BTRFS_I(inode)->root;
9484
Al Virob3d9b7a2012-06-09 13:51:19 -04009485 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009486 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009487 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9488 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009489 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009490 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009491 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009492 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009493 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009494
Chris Mason5a3f23d2009-03-31 13:27:11 -04009495 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009496 * This can happen where we create an inode, but somebody else also
9497 * created the same inode and we need to destroy the one we already
9498 * created.
9499 */
9500 if (!root)
9501 goto free;
9502
Josef Bacik8a35d952012-05-23 14:26:42 -04009503 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9504 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009505 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009506 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009507 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009508 }
Josef Bacik7b128762008-07-24 12:17:14 -04009509
Chris Masond3977122009-01-05 21:25:51 -05009510 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009511 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9512 if (!ordered)
9513 break;
9514 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009515 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009516 "found ordered extent %llu %llu on inode cleanup",
9517 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009518 btrfs_remove_ordered_extent(inode, ordered);
9519 btrfs_put_ordered_extent(ordered);
9520 btrfs_put_ordered_extent(ordered);
9521 }
9522 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009523 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009524 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009525 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009526free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009527 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009528}
9529
Al Viro45321ac2010-06-07 13:43:19 -04009530int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009531{
9532 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009533
Naohiro Aota6379ef92013-06-06 09:56:34 +00009534 if (root == NULL)
9535 return 1;
9536
Liu Bofa6ac872013-02-20 14:10:23 +00009537 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009538 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009539 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009540 else
Al Viro45321ac2010-06-07 13:43:19 -04009541 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009542}
9543
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009544static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009545{
9546 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9547
9548 inode_init_once(&ei->vfs_inode);
9549}
9550
David Sterbae67c7182018-02-19 17:24:18 +01009551void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009552{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009553 /*
9554 * Make sure all delayed rcu free inodes are flushed before we
9555 * destroy cache.
9556 */
9557 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009558 kmem_cache_destroy(btrfs_inode_cachep);
9559 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009560 kmem_cache_destroy(btrfs_path_cachep);
9561 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009562}
9563
Liu Bof5c29bd2017-11-02 17:21:50 -06009564int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009565{
David Sterba837e1972012-09-07 03:00:48 -06009566 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009567 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009568 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9569 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009570 if (!btrfs_inode_cachep)
9571 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009572
David Sterba837e1972012-09-07 03:00:48 -06009573 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009574 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009575 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009576 if (!btrfs_trans_handle_cachep)
9577 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009578
David Sterba837e1972012-09-07 03:00:48 -06009579 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009580 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009581 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009582 if (!btrfs_path_cachep)
9583 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009584
David Sterba837e1972012-09-07 03:00:48 -06009585 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009586 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009587 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009588 if (!btrfs_free_space_cachep)
9589 goto fail;
9590
Chris Mason39279cc2007-06-12 06:35:45 -04009591 return 0;
9592fail:
9593 btrfs_destroy_cachep();
9594 return -ENOMEM;
9595}
9596
David Howellsa528d352017-01-31 16:46:22 +00009597static int btrfs_getattr(const struct path *path, struct kstat *stat,
9598 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009599{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009600 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009601 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009602 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009603 u32 bi_flags = BTRFS_I(inode)->flags;
9604
9605 stat->result_mask |= STATX_BTIME;
9606 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9607 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9608 if (bi_flags & BTRFS_INODE_APPEND)
9609 stat->attributes |= STATX_ATTR_APPEND;
9610 if (bi_flags & BTRFS_INODE_COMPRESS)
9611 stat->attributes |= STATX_ATTR_COMPRESSED;
9612 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9613 stat->attributes |= STATX_ATTR_IMMUTABLE;
9614 if (bi_flags & BTRFS_INODE_NODUMP)
9615 stat->attributes |= STATX_ATTR_NODUMP;
9616
9617 stat->attributes_mask |= (STATX_ATTR_APPEND |
9618 STATX_ATTR_COMPRESSED |
9619 STATX_ATTR_IMMUTABLE |
9620 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009621
Chris Mason39279cc2007-06-12 06:35:45 -04009622 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009623 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009624
9625 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009626 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009627 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009628 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009629 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009630 return 0;
9631}
9632
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009633static int btrfs_rename_exchange(struct inode *old_dir,
9634 struct dentry *old_dentry,
9635 struct inode *new_dir,
9636 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009637{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009638 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009639 struct btrfs_trans_handle *trans;
9640 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009641 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009642 struct inode *new_inode = new_dentry->d_inode;
9643 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009644 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009645 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009646 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9647 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648 u64 old_idx = 0;
9649 u64 new_idx = 0;
9650 u64 root_objectid;
9651 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009652 bool root_log_pinned = false;
9653 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654
9655 /* we only allow rename subvolume link between subvolumes */
9656 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9657 return -EXDEV;
9658
9659 /* close the race window with snapshot create/destroy ioctl */
9660 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009661 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009663 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664
9665 /*
9666 * We want to reserve the absolute worst case amount of items. So if
9667 * both inodes are subvols and we need to unlink them then that would
9668 * require 4 item modifications, but if they are both normal inodes it
9669 * would require 5 item modifications, so we'll assume their normal
9670 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9671 * should cover the worst case number of items we'll modify.
9672 */
9673 trans = btrfs_start_transaction(root, 12);
9674 if (IS_ERR(trans)) {
9675 ret = PTR_ERR(trans);
9676 goto out_notrans;
9677 }
9678
9679 /*
9680 * We need to find a free sequence number both in the source and
9681 * in the destination directory for the exchange.
9682 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009683 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 if (ret)
9685 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009686 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009687 if (ret)
9688 goto out_fail;
9689
9690 BTRFS_I(old_inode)->dir_index = 0ULL;
9691 BTRFS_I(new_inode)->dir_index = 0ULL;
9692
9693 /* Reference for the source. */
9694 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9695 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009696 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009698 btrfs_pin_log_trans(root);
9699 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 ret = btrfs_insert_inode_ref(trans, dest,
9701 new_dentry->d_name.name,
9702 new_dentry->d_name.len,
9703 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009704 btrfs_ino(BTRFS_I(new_dir)),
9705 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 if (ret)
9707 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 }
9709
9710 /* And now for the dest. */
9711 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9712 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009715 btrfs_pin_log_trans(dest);
9716 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 ret = btrfs_insert_inode_ref(trans, root,
9718 old_dentry->d_name.name,
9719 old_dentry->d_name.len,
9720 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009721 btrfs_ino(BTRFS_I(old_dir)),
9722 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009723 if (ret)
9724 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 }
9726
9727 /* Update inode version and ctime/mtime. */
9728 inode_inc_iversion(old_dir);
9729 inode_inc_iversion(new_dir);
9730 inode_inc_iversion(old_inode);
9731 inode_inc_iversion(new_inode);
9732 old_dir->i_ctime = old_dir->i_mtime = ctime;
9733 new_dir->i_ctime = new_dir->i_mtime = ctime;
9734 old_inode->i_ctime = ctime;
9735 new_inode->i_ctime = ctime;
9736
9737 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009738 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9739 BTRFS_I(old_inode), 1);
9740 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9741 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009742 }
9743
9744 /* src is a subvolume */
9745 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9746 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9747 ret = btrfs_unlink_subvol(trans, root, old_dir,
9748 root_objectid,
9749 old_dentry->d_name.name,
9750 old_dentry->d_name.len);
9751 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009752 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9753 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754 old_dentry->d_name.name,
9755 old_dentry->d_name.len);
9756 if (!ret)
9757 ret = btrfs_update_inode(trans, root, old_inode);
9758 }
9759 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009760 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009761 goto out_fail;
9762 }
9763
9764 /* dest is a subvolume */
9765 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9766 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9767 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9768 root_objectid,
9769 new_dentry->d_name.name,
9770 new_dentry->d_name.len);
9771 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009772 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9773 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 new_dentry->d_name.name,
9775 new_dentry->d_name.len);
9776 if (!ret)
9777 ret = btrfs_update_inode(trans, dest, new_inode);
9778 }
9779 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009780 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 goto out_fail;
9782 }
9783
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009784 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009785 new_dentry->d_name.name,
9786 new_dentry->d_name.len, 0, old_idx);
9787 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009788 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 goto out_fail;
9790 }
9791
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009792 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009793 old_dentry->d_name.name,
9794 old_dentry->d_name.len, 0, new_idx);
9795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797 goto out_fail;
9798 }
9799
9800 if (old_inode->i_nlink == 1)
9801 BTRFS_I(old_inode)->dir_index = old_idx;
9802 if (new_inode->i_nlink == 1)
9803 BTRFS_I(new_inode)->dir_index = new_idx;
9804
Filipe Manana86e8aa02016-05-05 02:02:27 +01009805 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009807 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9808 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009810 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009812 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009813 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009814 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9815 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009816 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009817 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 }
9819out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009820 /*
9821 * If we have pinned a log and an error happened, we unpin tasks
9822 * trying to sync the log and force them to fallback to a transaction
9823 * commit if the log currently contains any of the inodes involved in
9824 * this rename operation (to ensure we do not persist a log with an
9825 * inconsistent state for any of these inodes or leading to any
9826 * inconsistencies when replayed). If the transaction was aborted, the
9827 * abortion reason is propagated to userspace when attempting to commit
9828 * the transaction. If the log does not contain any of these inodes, we
9829 * allow the tasks to sync it.
9830 */
9831 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009832 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9833 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9834 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009835 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009836 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009837 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009838
9839 if (root_log_pinned) {
9840 btrfs_end_log_trans(root);
9841 root_log_pinned = false;
9842 }
9843 if (dest_log_pinned) {
9844 btrfs_end_log_trans(dest);
9845 dest_log_pinned = false;
9846 }
9847 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009848 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009849out_notrans:
9850 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009851 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009852 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009853 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009854
9855 return ret;
9856}
9857
9858static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9859 struct btrfs_root *root,
9860 struct inode *dir,
9861 struct dentry *dentry)
9862{
9863 int ret;
9864 struct inode *inode;
9865 u64 objectid;
9866 u64 index;
9867
9868 ret = btrfs_find_free_ino(root, &objectid);
9869 if (ret)
9870 return ret;
9871
9872 inode = btrfs_new_inode(trans, root, dir,
9873 dentry->d_name.name,
9874 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009875 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009876 objectid,
9877 S_IFCHR | WHITEOUT_MODE,
9878 &index);
9879
9880 if (IS_ERR(inode)) {
9881 ret = PTR_ERR(inode);
9882 return ret;
9883 }
9884
9885 inode->i_op = &btrfs_special_inode_operations;
9886 init_special_inode(inode, inode->i_mode,
9887 WHITEOUT_DEV);
9888
9889 ret = btrfs_init_inode_security(trans, inode, dir,
9890 &dentry->d_name);
9891 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009892 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009894 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9895 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009897 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009898
9899 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009900out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009901 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009902 if (ret)
9903 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009904 iput(inode);
9905
Filipe Mananac9901612016-05-05 01:41:57 +01009906 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009907}
9908
Chris Mason39279cc2007-06-12 06:35:45 -04009909static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009910 struct inode *new_dir, struct dentry *new_dentry,
9911 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009912{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009913 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009914 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009915 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009916 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9917 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009918 struct inode *new_inode = d_inode(new_dentry);
9919 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009920 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009921 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009922 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009923 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009924 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009925
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009926 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009927 return -EPERM;
9928
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009930 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009931 return -EXDEV;
9932
Li Zefan33345d012011-04-20 10:31:50 +08009933 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009934 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009935 return -ENOTEMPTY;
9936
Chris Mason39279cc2007-06-12 06:35:45 -04009937 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009938 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009939 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009940
9941
9942 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009943 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009944 new_dentry->d_name.name,
9945 new_dentry->d_name.len);
9946
9947 if (ret) {
9948 if (ret == -EEXIST) {
9949 /* we shouldn't get
9950 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309951 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009952 return ret;
9953 }
9954 } else {
9955 /* maybe -EOVERFLOW */
9956 return ret;
9957 }
9958 }
9959 ret = 0;
9960
Chris Mason5a3f23d2009-03-31 13:27:11 -04009961 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009962 * we're using rename to replace one file with another. Start IO on it
9963 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009964 */
Chris Mason8d875f92014-08-12 10:47:42 -07009965 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009966 filemap_flush(old_inode->i_mapping);
9967
Yan, Zheng76dda932009-09-21 16:00:26 -04009968 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009969 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009970 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009971 /*
9972 * We want to reserve the absolute worst case amount of items. So if
9973 * both inodes are subvols and we need to unlink them then that would
9974 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009975 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009976 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9977 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009978 * If our rename has the whiteout flag, we need more 5 units for the
9979 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9980 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009981 */
Filipe Manana5062af32016-05-05 10:26:26 +01009982 trans_num_items = 11;
9983 if (flags & RENAME_WHITEOUT)
9984 trans_num_items += 5;
9985 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009986 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009987 ret = PTR_ERR(trans);
9988 goto out_notrans;
9989 }
Chris Mason5f39d392007-10-15 16:14:19 -04009990
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009991 if (dest != root)
9992 btrfs_record_root_in_trans(trans, dest);
9993
Nikolay Borisov877574e2017-02-20 13:50:33 +02009994 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009995 if (ret)
9996 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009997
Miao Xie67de1172013-12-26 13:07:06 +08009998 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009999 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010000 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010001 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010002 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010003 btrfs_pin_log_trans(root);
10004 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010005 ret = btrfs_insert_inode_ref(trans, dest,
10006 new_dentry->d_name.name,
10007 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010008 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010009 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010010 if (ret)
10011 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010012 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010013
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010014 inode_inc_iversion(old_dir);
10015 inode_inc_iversion(new_dir);
10016 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010017 old_dir->i_ctime = old_dir->i_mtime =
10018 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010019 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010020
Chris Mason12fcfd22009-03-24 10:24:20 -040010021 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010022 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10023 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010024
Li Zefan33345d012011-04-20 10:31:50 +080010025 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010026 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10027 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10028 old_dentry->d_name.name,
10029 old_dentry->d_name.len);
10030 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010031 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10032 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010033 old_dentry->d_name.name,
10034 old_dentry->d_name.len);
10035 if (!ret)
10036 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010037 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010038 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010039 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010040 goto out_fail;
10041 }
Chris Mason39279cc2007-06-12 06:35:45 -040010042
10043 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010044 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010045 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010046 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010047 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10048 root_objectid = BTRFS_I(new_inode)->location.objectid;
10049 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10050 root_objectid,
10051 new_dentry->d_name.name,
10052 new_dentry->d_name.len);
10053 BUG_ON(new_inode->i_nlink == 0);
10054 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010055 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10056 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010057 new_dentry->d_name.name,
10058 new_dentry->d_name.len);
10059 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010060 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010061 ret = btrfs_orphan_add(trans,
10062 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010063 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010064 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010065 goto out_fail;
10066 }
Chris Mason39279cc2007-06-12 06:35:45 -040010067 }
Josef Bacikaec74772008-07-24 12:12:38 -040010068
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010069 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010070 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010071 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010072 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010073 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010074 goto out_fail;
10075 }
Chris Mason39279cc2007-06-12 06:35:45 -040010076
Miao Xie67de1172013-12-26 13:07:06 +080010077 if (old_inode->i_nlink == 1)
10078 BTRFS_I(old_inode)->dir_index = index;
10079
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010080 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010081 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010082
David Sterbaf85b7372017-01-20 14:54:07 +010010083 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10084 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010085 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010086 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010087 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010088
10089 if (flags & RENAME_WHITEOUT) {
10090 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10091 old_dentry);
10092
10093 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010094 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010095 goto out_fail;
10096 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010097 }
Chris Mason39279cc2007-06-12 06:35:45 -040010098out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010099 /*
10100 * If we have pinned the log and an error happened, we unpin tasks
10101 * trying to sync the log and force them to fallback to a transaction
10102 * commit if the log currently contains any of the inodes involved in
10103 * this rename operation (to ensure we do not persist a log with an
10104 * inconsistent state for any of these inodes or leading to any
10105 * inconsistencies when replayed). If the transaction was aborted, the
10106 * abortion reason is propagated to userspace when attempting to commit
10107 * the transaction. If the log does not contain any of these inodes, we
10108 * allow the tasks to sync it.
10109 */
10110 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010111 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10112 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10113 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010114 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010115 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010116 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010117
10118 btrfs_end_log_trans(root);
10119 log_pinned = false;
10120 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010121 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010122out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010123 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010124 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010125
Chris Mason39279cc2007-06-12 06:35:45 -040010126 return ret;
10127}
10128
Miklos Szeredi80ace852014-07-23 15:15:32 +020010129static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10130 struct inode *new_dir, struct dentry *new_dentry,
10131 unsigned int flags)
10132{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010133 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010134 return -EINVAL;
10135
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010136 if (flags & RENAME_EXCHANGE)
10137 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10138 new_dentry);
10139
10140 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010141}
10142
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010143struct btrfs_delalloc_work {
10144 struct inode *inode;
10145 struct completion completion;
10146 struct list_head list;
10147 struct btrfs_work work;
10148};
10149
Miao Xie8ccf6f192012-10-25 09:28:04 +000010150static void btrfs_run_delalloc_work(struct btrfs_work *work)
10151{
10152 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010153 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010154
10155 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10156 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010157 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010158 filemap_flush(inode->i_mapping);
10159 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10160 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010161 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010162
Nikolay Borisov076da912018-04-23 10:54:16 +030010163 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164 complete(&delalloc_work->completion);
10165}
10166
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010167static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010168{
10169 struct btrfs_delalloc_work *work;
10170
David Sterba100d5702015-12-08 14:39:32 +010010171 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010172 if (!work)
10173 return NULL;
10174
10175 init_completion(&work->completion);
10176 INIT_LIST_HEAD(&work->list);
10177 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010178 WARN_ON_ONCE(!inode);
10179 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10180 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010181
10182 return work;
10183}
10184
Chris Masond352ac62008-09-29 15:18:18 -040010185/*
10186 * some fairly slow code that needs optimization. This walks the list
10187 * of all the inodes with pending delalloc and forces them to disk.
10188 */
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010189static int start_delalloc_inodes(struct btrfs_root *root, int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010190{
Chris Masonea8c2812008-08-04 23:17:27 -040010191 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010192 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010193 struct btrfs_delalloc_work *work, *next;
10194 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010195 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010196 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010197
Miao Xie8ccf6f192012-10-25 09:28:04 +000010198 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010199 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010200
Miao Xie573bfb72014-03-06 13:55:03 +080010201 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010202 spin_lock(&root->delalloc_lock);
10203 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010204 while (!list_empty(&splice)) {
10205 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010206 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010207
Miao Xieeb73c1b2013-05-15 07:48:22 +000010208 list_move_tail(&binode->delalloc_inodes,
10209 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010210 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010211 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010212 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010213 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010214 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010215 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010216
Nikolay Borisov076da912018-04-23 10:54:16 +030010217 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010218 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010219 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010220 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010221 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010222 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010223 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010224 btrfs_queue_work(root->fs_info->flush_workers,
10225 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010226 ret++;
10227 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010228 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010229 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010230 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010231 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010232 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010233
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010234out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010235 list_for_each_entry_safe(work, next, &works, list) {
10236 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010237 wait_for_completion(&work->completion);
10238 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010239 }
10240
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010241 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010242 spin_lock(&root->delalloc_lock);
10243 list_splice_tail(&splice, &root->delalloc_inodes);
10244 spin_unlock(&root->delalloc_lock);
10245 }
Miao Xie573bfb72014-03-06 13:55:03 +080010246 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 return ret;
10248}
10249
Nikolay Borisov76f32e22018-04-23 10:54:14 +030010250int btrfs_start_delalloc_inodes(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010252 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010253 int ret;
10254
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010255 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010256 return -EROFS;
10257
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010258 ret = start_delalloc_inodes(root, -1);
Miao Xie6c255e62014-03-06 13:55:01 +080010259 if (ret > 0)
10260 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010261 return ret;
10262}
10263
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010264int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010265{
10266 struct btrfs_root *root;
10267 struct list_head splice;
10268 int ret;
10269
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010270 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010271 return -EROFS;
10272
10273 INIT_LIST_HEAD(&splice);
10274
Miao Xie573bfb72014-03-06 13:55:03 +080010275 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 spin_lock(&fs_info->delalloc_root_lock);
10277 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010278 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010279 root = list_first_entry(&splice, struct btrfs_root,
10280 delalloc_root);
10281 root = btrfs_grab_fs_root(root);
10282 BUG_ON(!root);
10283 list_move_tail(&root->delalloc_root,
10284 &fs_info->delalloc_roots);
10285 spin_unlock(&fs_info->delalloc_root_lock);
10286
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010287 ret = start_delalloc_inodes(root, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010288 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010289 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010290 goto out;
10291
Miao Xie6c255e62014-03-06 13:55:01 +080010292 if (nr != -1) {
10293 nr -= ret;
10294 WARN_ON(nr < 0);
10295 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010296 spin_lock(&fs_info->delalloc_root_lock);
10297 }
10298 spin_unlock(&fs_info->delalloc_root_lock);
10299
Miao Xie6c255e62014-03-06 13:55:01 +080010300 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010301out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010302 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010303 spin_lock(&fs_info->delalloc_root_lock);
10304 list_splice_tail(&splice, &fs_info->delalloc_roots);
10305 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010306 }
Miao Xie573bfb72014-03-06 13:55:03 +080010307 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010308 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010309}
10310
Chris Mason39279cc2007-06-12 06:35:45 -040010311static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10312 const char *symname)
10313{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010315 struct btrfs_trans_handle *trans;
10316 struct btrfs_root *root = BTRFS_I(dir)->root;
10317 struct btrfs_path *path;
10318 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010319 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010320 int err;
10321 int drop_inode = 0;
10322 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010323 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010324 int name_len;
10325 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010326 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010327 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010328 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010329
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010330 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010331 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010332 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010333
Josef Bacik9ed74f22009-09-11 16:12:44 -040010334 /*
10335 * 2 items for inode item and ref
10336 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010337 * 1 item for updating parent inode item
10338 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010339 * 1 item for xattr if selinux is on
10340 */
Filipe Manana9269d122015-12-31 18:16:29 +000010341 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010342 if (IS_ERR(trans))
10343 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010344
Li Zefan581bb052011-04-20 10:06:11 +080010345 err = btrfs_find_free_ino(root, &objectid);
10346 if (err)
10347 goto out_unlock;
10348
Josef Bacikaec74772008-07-24 12:12:38 -040010349 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010350 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10351 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010352 if (IS_ERR(inode)) {
10353 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010354 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010355 }
Chris Mason39279cc2007-06-12 06:35:45 -040010356
Casey Schauflerad19db72011-12-15 10:09:07 -050010357 /*
10358 * If the active LSM wants to access the inode during
10359 * d_instantiate it needs these. Smack checks to see
10360 * if the filesystem supports xattrs by looking at the
10361 * ops vector.
10362 */
10363 inode->i_fop = &btrfs_file_operations;
10364 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010365 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010366 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10367
10368 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10369 if (err)
10370 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010371
Chris Mason39279cc2007-06-12 06:35:45 -040010372 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010373 if (!path) {
10374 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010375 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010376 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010377 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010378 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010379 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010380 datasize = btrfs_file_extent_calc_inline_size(name_len);
10381 err = btrfs_insert_empty_item(trans, root, path, &key,
10382 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010383 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010384 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010385 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010386 }
Chris Mason5f39d392007-10-15 16:14:19 -040010387 leaf = path->nodes[0];
10388 ei = btrfs_item_ptr(leaf, path->slots[0],
10389 struct btrfs_file_extent_item);
10390 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10391 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010392 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010393 btrfs_set_file_extent_encryption(leaf, ei, 0);
10394 btrfs_set_file_extent_compression(leaf, ei, 0);
10395 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10396 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10397
Chris Mason39279cc2007-06-12 06:35:45 -040010398 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010399 write_extent_buffer(leaf, symname, ptr, name_len);
10400 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010401 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010402
Chris Mason39279cc2007-06-12 06:35:45 -040010403 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010404 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010405 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010406 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010407 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010408 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010409 /*
10410 * Last step, add directory indexes for our symlink inode. This is the
10411 * last step to avoid extra cleanup of these indexes if an error happens
10412 * elsewhere above.
10413 */
10414 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010415 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10416 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010417 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010418 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010419 goto out_unlock_inode;
10420 }
10421
Al Viro1e2e5472018-05-04 08:23:01 -040010422 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010423
10424out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010425 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010426 if (drop_inode) {
10427 inode_dec_link_count(inode);
10428 iput(inode);
10429 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010430 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010431 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010432
10433out_unlock_inode:
10434 drop_inode = 1;
10435 unlock_new_inode(inode);
10436 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010437}
Chris Mason16432982008-04-10 10:23:21 -040010438
Josef Bacik0af3d002010-06-21 14:48:16 -040010439static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10440 u64 start, u64 num_bytes, u64 min_size,
10441 loff_t actual_len, u64 *alloc_hint,
10442 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010444 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010445 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10446 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010447 struct btrfs_root *root = BTRFS_I(inode)->root;
10448 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010449 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010450 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010451 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010452 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010453 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010454 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010455 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010456
Josef Bacik0af3d002010-06-21 14:48:16 -040010457 if (trans)
10458 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010459 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010460 if (own_trans) {
10461 trans = btrfs_start_transaction(root, 3);
10462 if (IS_ERR(trans)) {
10463 ret = PTR_ERR(trans);
10464 break;
10465 }
Yan Zhengd899e052008-10-30 14:25:28 -040010466 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010467
Byongho Leeee221842015-12-15 01:42:10 +090010468 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010469 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010470 /*
10471 * If we are severely fragmented we could end up with really
10472 * small allocations, so if the allocator is returning small
10473 * chunks lets make its job easier by only searching for those
10474 * sized chunks.
10475 */
10476 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010477 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10478 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010479 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010480 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010481 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010482 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010483 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010484 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010485
Josef Bacik0b670dc2015-09-23 17:11:16 -040010486 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010487 ret = insert_reserved_file_extent(trans, inode,
10488 cur_offset, ins.objectid,
10489 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010490 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010491 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010492 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010493 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010494 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010495 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010496 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010497 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010498 break;
10499 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010500
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010501 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010502 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010503
Josef Bacik5dc562c2012-08-17 13:14:17 -040010504 em = alloc_extent_map();
10505 if (!em) {
10506 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10507 &BTRFS_I(inode)->runtime_flags);
10508 goto next;
10509 }
10510
10511 em->start = cur_offset;
10512 em->orig_start = cur_offset;
10513 em->len = ins.offset;
10514 em->block_start = ins.objectid;
10515 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010516 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010517 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010518 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010519 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10520 em->generation = trans->transid;
10521
10522 while (1) {
10523 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010524 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010525 write_unlock(&em_tree->lock);
10526 if (ret != -EEXIST)
10527 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010528 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010529 cur_offset + ins.offset - 1,
10530 0);
10531 }
10532 free_extent_map(em);
10533next:
Yan Zhengd899e052008-10-30 14:25:28 -040010534 num_bytes -= ins.offset;
10535 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010536 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010537
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010538 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010539 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010540 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010541 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010542 (actual_len > inode->i_size) &&
10543 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010544 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010545 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010546 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010547 i_size = cur_offset;
10548 i_size_write(inode, i_size);
10549 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010550 }
10551
Yan Zhengd899e052008-10-30 14:25:28 -040010552 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010553
10554 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010555 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010556 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010557 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010558 break;
10559 }
Yan Zhengd899e052008-10-30 14:25:28 -040010560
Josef Bacik0af3d002010-06-21 14:48:16 -040010561 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010562 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010563 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010564 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010565 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010566 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010567 return ret;
10568}
10569
Josef Bacik0af3d002010-06-21 14:48:16 -040010570int btrfs_prealloc_file_range(struct inode *inode, int mode,
10571 u64 start, u64 num_bytes, u64 min_size,
10572 loff_t actual_len, u64 *alloc_hint)
10573{
10574 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10575 min_size, actual_len, alloc_hint,
10576 NULL);
10577}
10578
10579int btrfs_prealloc_file_range_trans(struct inode *inode,
10580 struct btrfs_trans_handle *trans, int mode,
10581 u64 start, u64 num_bytes, u64 min_size,
10582 loff_t actual_len, u64 *alloc_hint)
10583{
10584 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10585 min_size, actual_len, alloc_hint, trans);
10586}
10587
Chris Masone6dcd2d2008-07-17 12:53:50 -040010588static int btrfs_set_page_dirty(struct page *page)
10589{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010590 return __set_page_dirty_nobuffers(page);
10591}
10592
Al Viro10556cb2011-06-20 19:28:19 -040010593static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010594{
Li Zefanb83cc962010-12-20 16:04:08 +080010595 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010596 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010597
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010598 if (mask & MAY_WRITE &&
10599 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10600 if (btrfs_root_readonly(root))
10601 return -EROFS;
10602 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10603 return -EACCES;
10604 }
Al Viro2830ba72011-06-20 19:16:29 -040010605 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010606}
Chris Mason39279cc2007-06-12 06:35:45 -040010607
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010608static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10609{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010610 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010611 struct btrfs_trans_handle *trans;
10612 struct btrfs_root *root = BTRFS_I(dir)->root;
10613 struct inode *inode = NULL;
10614 u64 objectid;
10615 u64 index;
10616 int ret = 0;
10617
10618 /*
10619 * 5 units required for adding orphan entry
10620 */
10621 trans = btrfs_start_transaction(root, 5);
10622 if (IS_ERR(trans))
10623 return PTR_ERR(trans);
10624
10625 ret = btrfs_find_free_ino(root, &objectid);
10626 if (ret)
10627 goto out;
10628
10629 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010630 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010631 if (IS_ERR(inode)) {
10632 ret = PTR_ERR(inode);
10633 inode = NULL;
10634 goto out;
10635 }
10636
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010637 inode->i_fop = &btrfs_file_operations;
10638 inode->i_op = &btrfs_file_inode_operations;
10639
10640 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010641 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10642
Chris Masonb0d5d102014-09-08 13:08:51 -070010643 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10644 if (ret)
10645 goto out_inode;
10646
10647 ret = btrfs_update_inode(trans, root, inode);
10648 if (ret)
10649 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010650 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010651 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010652 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010653
Filipe Manana5762b5c2014-08-01 00:10:32 +010010654 /*
10655 * We set number of links to 0 in btrfs_new_inode(), and here we set
10656 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10657 * through:
10658 *
10659 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10660 */
10661 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010662 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010663 d_tmpfile(dentry, inode);
10664 mark_inode_dirty(inode);
10665
10666out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010667 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010668 if (ret)
10669 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010670 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010671 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010672
10673out_inode:
10674 unlock_new_inode(inode);
10675 goto out;
10676
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010677}
10678
David Sterba20a7db82017-02-17 16:24:29 +010010679__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010680static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010681{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010682 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010683}
10684
Josef Bacikc6100a42017-05-05 11:57:13 -040010685static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10686{
10687 struct inode *inode = private_data;
10688 return btrfs_sb(inode->i_sb);
10689}
10690
10691static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10692 u64 start, u64 end)
10693{
10694 struct inode *inode = private_data;
10695 u64 isize;
10696
10697 isize = i_size_read(inode);
10698 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10699 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10700 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10701 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10702 }
10703}
10704
10705void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10706{
10707 struct inode *inode = private_data;
10708 unsigned long index = start >> PAGE_SHIFT;
10709 unsigned long end_index = end >> PAGE_SHIFT;
10710 struct page *page;
10711
10712 while (index <= end_index) {
10713 page = find_get_page(inode->i_mapping, index);
10714 ASSERT(page); /* Pages should be in the extent_io_tree */
10715 set_page_writeback(page);
10716 put_page(page);
10717 index++;
10718 }
10719}
10720
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010721static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010722 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010723 .lookup = btrfs_lookup,
10724 .create = btrfs_create,
10725 .unlink = btrfs_unlink,
10726 .link = btrfs_link,
10727 .mkdir = btrfs_mkdir,
10728 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010729 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010730 .symlink = btrfs_symlink,
10731 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010732 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010733 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010734 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010735 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010736 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010737 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010738 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010739};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010740static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010741 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010742 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010743 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010744};
Yan, Zheng76dda932009-09-21 16:00:26 -040010745
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010746static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010747 .llseek = generic_file_llseek,
10748 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010749 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010750 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010751 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010752#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010753 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010754#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010755 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010756 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010757};
10758
David Sterba20e55062015-11-19 11:42:28 +010010759static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010760 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010761 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010762 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010763 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010764 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010765 .tree_fs_info = iotree_fs_info,
10766 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010767
10768 /* optional callbacks */
10769 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010770 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010771 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010772 .set_bit_hook = btrfs_set_bit_hook,
10773 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010774 .merge_extent_hook = btrfs_merge_extent_hook,
10775 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010776 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010777};
10778
Chris Mason35054392009-01-21 13:11:13 -050010779/*
10780 * btrfs doesn't support the bmap operation because swapfiles
10781 * use bmap to make a mapping of extents in the file. They assume
10782 * these extents won't change over the life of the file and they
10783 * use the bmap result to do IO directly to the drive.
10784 *
10785 * the btrfs bmap call would return logical addresses that aren't
10786 * suitable for IO and they also will change frequently as COW
10787 * operations happen. So, swapfile + btrfs == corruption.
10788 *
10789 * For now we're avoiding this by dropping bmap.
10790 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010791static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010792 .readpage = btrfs_readpage,
10793 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010794 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010795 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010796 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010797 .invalidatepage = btrfs_invalidatepage,
10798 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010799 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010800 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010801};
10802
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010803static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010804 .readpage = btrfs_readpage,
10805 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010806 .invalidatepage = btrfs_invalidatepage,
10807 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010808};
10809
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010810static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010811 .getattr = btrfs_getattr,
10812 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010813 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010814 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010815 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010816 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010817 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010818 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010819};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010820static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010821 .getattr = btrfs_getattr,
10822 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010823 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010824 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010825 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010826 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010827 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010828};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010829static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010830 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010831 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010832 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010833 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010834 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010835 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010836};
Yan, Zheng76dda932009-09-21 16:00:26 -040010837
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010838const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010839 .d_delete = btrfs_dentry_delete,
10840};