blob: 6757fe136177c2dad487295ef485556495888acc [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 Sterbaee863952015-02-16 19:41:40 +01001159 /*
1160 * atomic_sub_return implies a barrier for waitqueue_active
1161 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001163 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001164 waitqueue_active(&fs_info->async_submit_wait))
1165 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001166
Chris Masond3977122009-01-05 21:25:51 -05001167 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001168 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001169}
Chris Masonc8b97812008-10-29 14:49:59 -04001170
Chris Mason771ed682008-11-06 22:02:51 -05001171static noinline void async_cow_free(struct btrfs_work *work)
1172{
1173 struct async_cow *async_cow;
1174 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001175 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001176 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001177 kfree(async_cow);
1178}
1179
1180static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1181 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001182 unsigned long *nr_written,
1183 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001184{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001185 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001186 struct async_cow *async_cow;
1187 struct btrfs_root *root = BTRFS_I(inode)->root;
1188 unsigned long nr_pages;
1189 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001190
Chris Masona3429ab2009-10-08 12:30:20 -04001191 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001192 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001193 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001194 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001195 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001196 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001197 async_cow->root = root;
1198 async_cow->locked_page = locked_page;
1199 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001200 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001201
Wang Shilongf79707b2014-07-17 11:44:09 +08001202 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001203 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001204 cur_end = end;
1205 else
Byongho Leeee221842015-12-15 01:42:10 +09001206 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001207
1208 async_cow->end = cur_end;
1209 INIT_LIST_HEAD(&async_cow->extents);
1210
Liu Bo9e0af232014-08-15 23:36:53 +08001211 btrfs_init_work(&async_cow->work,
1212 btrfs_delalloc_helper,
1213 async_cow_start, async_cow_submit,
1214 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001215
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001216 nr_pages = (cur_end - start + PAGE_SIZE) >>
1217 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001218 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001219
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001220 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001221
Chris Mason771ed682008-11-06 22:02:51 -05001222 *nr_written += nr_pages;
1223 start = cur_end + 1;
1224 }
1225 *page_started = 1;
1226 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227}
1228
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001229static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001230 u64 bytenr, u64 num_bytes)
1231{
1232 int ret;
1233 struct btrfs_ordered_sum *sums;
1234 LIST_HEAD(list);
1235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001237 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001238 if (ret == 0 && list_empty(&list))
1239 return 0;
1240
1241 while (!list_empty(&list)) {
1242 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1243 list_del(&sums->list);
1244 kfree(sums);
1245 }
Liu Bo58113752018-01-31 17:09:13 -07001246 if (ret < 0)
1247 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 return 1;
1249}
1250
Chris Masond352ac62008-09-29 15:18:18 -04001251/*
1252 * when nowcow writeback call back. This checks for snapshots or COW copies
1253 * of the extents that exist in the file, and COWs the file as required.
1254 *
1255 * If no cow copies or snapshots exist, we write directly to the existing
1256 * blocks on disk
1257 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001258static noinline int run_delalloc_nocow(struct inode *inode,
1259 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001260 u64 start, u64 end, int *page_started, int force,
1261 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_root *root = BTRFS_I(inode)->root;
1265 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001269 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 u64 cow_start;
1271 u64 cur_offset;
1272 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 u64 disk_bytenr;
1275 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001276 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001277 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001280 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 int nocow;
1282 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001283 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001284 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
1286 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001287 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001288 extent_clear_unlock_delalloc(inode, start, end, end,
1289 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001291 EXTENT_DO_ACCOUNTING |
1292 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 PAGE_CLEAR_DIRTY |
1294 PAGE_SET_WRITEBACK |
1295 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001296 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001297 }
Li Zefan82d59022011-04-20 10:33:24 +08001298
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001299 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 cow_start = (u64)-1;
1302 cur_offset = start;
1303 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001304 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001306 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001307 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key,
1311 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001312 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 found_key.type == BTRFS_EXTENT_DATA_KEY)
1314 path->slots[0]--;
1315 }
1316 check_prev = 0;
1317next_slot:
1318 leaf = path->nodes[0];
1319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1320 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001321 if (ret < 0) {
1322 if (cow_start != (u64)-1)
1323 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001324 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001325 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 if (ret > 0)
1327 break;
1328 leaf = path->nodes[0];
1329 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001330
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 nocow = 0;
1332 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001335
Filipe Manana1d512cb2015-11-09 00:33:58 +00001336 if (found_key.objectid > ino)
1337 break;
1338 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1339 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1340 path->slots[0]++;
1341 goto next_slot;
1342 }
1343 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 found_key.offset > end)
1345 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001346
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 if (found_key.offset > cur_offset) {
1348 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001349 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 goto out_check;
1351 }
Chris Masonc31f8832008-01-08 15:46:31 -05001352
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 fi = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_file_extent_item);
1355 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001356
Josef Bacikcc95bef2013-04-04 14:31:27 -04001357 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001358 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1359 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 extent_end = found_key.offset +
1363 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001364 disk_num_bytes =
1365 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 if (extent_end <= start) {
1367 path->slots[0]++;
1368 goto next_slot;
1369 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 if (disk_bytenr == 0)
1371 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 if (btrfs_file_extent_compression(leaf, fi) ||
1373 btrfs_file_extent_encryption(leaf, fi) ||
1374 btrfs_file_extent_other_encoding(leaf, fi))
1375 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001376 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1377 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001378 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001380 ret = btrfs_cross_ref_exist(root, ino,
1381 found_key.offset -
1382 extent_offset, disk_bytenr);
1383 if (ret) {
1384 /*
1385 * ret could be -EIO if the above fails to read
1386 * metadata.
1387 */
1388 if (ret < 0) {
1389 if (cow_start != (u64)-1)
1390 cur_offset = cow_start;
1391 goto error;
1392 }
1393
1394 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001395 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001396 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 disk_bytenr += cur_offset - found_key.offset;
1399 num_bytes = min(end + 1, extent_end) - cur_offset;
1400 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 * if there are pending snapshots for this root,
1402 * we fall into common COW way.
1403 */
1404 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001405 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001406 if (!err)
1407 goto out_check;
1408 }
1409 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 * force cow if csum exists in the range.
1411 * this ensure that csum for a given extent are
1412 * either valid or do not exist.
1413 */
Liu Bo58113752018-01-31 17:09:13 -07001414 ret = csum_exist_in_range(fs_info, disk_bytenr,
1415 num_bytes);
1416 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001417 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001418 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001419
1420 /*
1421 * ret could be -EIO if the above fails to read
1422 * metadata.
1423 */
1424 if (ret < 0) {
1425 if (cow_start != (u64)-1)
1426 cur_offset = cow_start;
1427 goto error;
1428 }
1429 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001430 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001431 }
1432 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1433 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001434 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001435 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001436 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 nocow = 1;
1438 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1439 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001440 btrfs_file_extent_inline_len(leaf,
1441 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001442 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001443 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 } else {
1445 BUG_ON(1);
1446 }
1447out_check:
1448 if (extent_end <= start) {
1449 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001450 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001451 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001452 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001453 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 goto next_slot;
1455 }
1456 if (!nocow) {
1457 if (cow_start == (u64)-1)
1458 cow_start = cur_offset;
1459 cur_offset = extent_end;
1460 if (cur_offset > end)
1461 break;
1462 path->slots[0]++;
1463 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001464 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001465
David Sterbab3b4aa72011-04-21 01:20:15 +02001466 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001467 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001468 ret = cow_file_range(inode, locked_page,
1469 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001470 end, page_started, nr_written, 1,
1471 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001472 if (ret) {
1473 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001474 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001475 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001476 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001477 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001478 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001479 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001481 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001482
Yan Zhengd899e052008-10-30 14:25:28 -04001483 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001484 u64 orig_start = found_key.offset - extent_offset;
1485
1486 em = create_io_em(inode, cur_offset, num_bytes,
1487 orig_start,
1488 disk_bytenr, /* block_start */
1489 num_bytes, /* block_len */
1490 disk_num_bytes, /* orig_block_len */
1491 ram_bytes, BTRFS_COMPRESS_NONE,
1492 BTRFS_ORDERED_PREALLOC);
1493 if (IS_ERR(em)) {
1494 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001495 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001496 if (nocow)
1497 btrfs_dec_nocow_writers(fs_info,
1498 disk_bytenr);
1499 ret = PTR_ERR(em);
1500 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001501 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001502 free_extent_map(em);
1503 }
1504
1505 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001506 type = BTRFS_ORDERED_PREALLOC;
1507 } else {
1508 type = BTRFS_ORDERED_NOCOW;
1509 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001510
1511 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001512 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001513 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001514 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001515 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001516
Yan, Zhengefa56462010-05-16 10:49:59 -04001517 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001518 BTRFS_DATA_RELOC_TREE_OBJECTID)
1519 /*
1520 * Error handled later, as we must prevent
1521 * extent_clear_unlock_delalloc() in error handler
1522 * from freeing metadata of created ordered extent.
1523 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001524 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1525 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001526
Josef Bacikc2790a22013-07-29 11:20:47 -04001527 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001528 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001529 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001530 EXTENT_DELALLOC |
1531 EXTENT_CLEAR_DATA_RESV,
1532 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1533
Wang Shilonge9894fd2014-03-27 11:12:25 +08001534 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001535 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001536 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001537
1538 /*
1539 * btrfs_reloc_clone_csums() error, now we're OK to call error
1540 * handler, as metadata for created ordered extent will only
1541 * be freed by btrfs_finish_ordered_io().
1542 */
1543 if (ret)
1544 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 if (cur_offset > end)
1546 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001547 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001548 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001549
Josef Bacik17ca04a2012-05-31 15:58:55 -04001550 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001551 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001552 cur_offset = end;
1553 }
1554
Yan Zheng80ff3852008-10-30 14:20:02 -04001555 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001556 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1557 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001558 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 }
1561
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001563 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001564 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001565 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001566 EXTENT_DELALLOC | EXTENT_DEFRAG |
1567 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1568 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001569 PAGE_SET_WRITEBACK |
1570 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001571 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001572 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001573}
1574
Wang Shilong47059d92014-07-03 18:22:07 +08001575static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1576{
1577
1578 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1579 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1580 return 0;
1581
1582 /*
1583 * @defrag_bytes is a hint value, no spinlock held here,
1584 * if is not zero, it means the file is defragging.
1585 * Force cow if given extent needs to be defragged.
1586 */
1587 if (BTRFS_I(inode)->defrag_bytes &&
1588 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1589 EXTENT_DEFRAG, 0, NULL))
1590 return 1;
1591
1592 return 0;
1593}
1594
Chris Masond352ac62008-09-29 15:18:18 -04001595/*
1596 * extent_io.c call back to do delayed allocation processing
1597 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001598static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001599 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001600 unsigned long *nr_written,
1601 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001602{
Josef Bacikc6100a42017-05-05 11:57:13 -04001603 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001604 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001605 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001606 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001607
Wang Shilong47059d92014-07-03 18:22:07 +08001608 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001609 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001610 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001611 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001612 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001613 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001614 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001615 ret = cow_file_range(inode, locked_page, start, end, end,
1616 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001617 } else {
1618 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1619 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001620 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001621 page_started, nr_written,
1622 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001623 }
Qu Wenruo524272602017-03-08 10:25:52 +08001624 if (ret)
1625 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001626 return ret;
1627}
1628
Josef Bacikc6100a42017-05-05 11:57:13 -04001629static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001630 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001631{
Josef Bacikc6100a42017-05-05 11:57:13 -04001632 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001633 u64 size;
1634
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001635 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001636 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001637 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001638
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639 size = orig->end - orig->start + 1;
1640 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001641 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001642 u64 new_size;
1643
1644 /*
Josef Bacikba117212015-03-13 15:01:24 -04001645 * See the explanation in btrfs_merge_extent_hook, the same
1646 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001647 */
1648 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001649 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001650 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001651 num_extents += count_max_extents(new_size);
1652 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653 return;
1654 }
1655
Josef Bacik9e0baf62011-07-15 15:16:44 +00001656 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001657 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001658 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659}
1660
1661/*
1662 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1663 * extents so we can keep track of new extents that are just merged onto old
1664 * extents, such as when we are doing sequential writes, so we can properly
1665 * account for the metadata space we'll need.
1666 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001667static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001668 struct extent_state *new,
1669 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001670{
Josef Bacikc6100a42017-05-05 11:57:13 -04001671 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001672 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001673 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001674
Josef Bacik9ed74f22009-09-11 16:12:44 -04001675 /* not delalloc, ignore it */
1676 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001677 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001678
Josef Bacik8461a3d2015-03-13 15:12:08 -04001679 if (new->start > other->start)
1680 new_size = new->end - other->start + 1;
1681 else
1682 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001683
1684 /* we're not bigger than the max, unreserve the space and go */
1685 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1686 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001687 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001688 spin_unlock(&BTRFS_I(inode)->lock);
1689 return;
1690 }
1691
1692 /*
Josef Bacikba117212015-03-13 15:01:24 -04001693 * We have to add up either side to figure out how many extents were
1694 * accounted for before we merged into one big extent. If the number of
1695 * extents we accounted for is <= the amount we need for the new range
1696 * then we can return, otherwise drop. Think of it like this
1697 *
1698 * [ 4k][MAX_SIZE]
1699 *
1700 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1701 * need 2 outstanding extents, on one side we have 1 and the other side
1702 * we have 1 so they are == and we can return. But in this case
1703 *
1704 * [MAX_SIZE+4k][MAX_SIZE+4k]
1705 *
1706 * Each range on their own accounts for 2 extents, but merged together
1707 * they are only 3 extents worth of accounting, so we need to drop in
1708 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001709 */
Josef Bacikba117212015-03-13 15:01:24 -04001710 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001711 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001712 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001713 num_extents += count_max_extents(old_size);
1714 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001715 return;
1716
Josef Bacik9e0baf62011-07-15 15:16:44 +00001717 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001718 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001719 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001720}
1721
Miao Xieeb73c1b2013-05-15 07:48:22 +00001722static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1723 struct inode *inode)
1724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1726
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 spin_lock(&root->delalloc_lock);
1728 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1729 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1730 &root->delalloc_inodes);
1731 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1732 &BTRFS_I(inode)->runtime_flags);
1733 root->nr_delalloc_inodes++;
1734 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001735 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001736 BUG_ON(!list_empty(&root->delalloc_root));
1737 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001738 &fs_info->delalloc_roots);
1739 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740 }
1741 }
1742 spin_unlock(&root->delalloc_lock);
1743}
1744
Nikolay Borisov2b877332018-04-27 12:21:51 +03001745
1746void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1747 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001748{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001749 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001750
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001751 if (!list_empty(&inode->delalloc_inodes)) {
1752 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001753 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001754 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001755 root->nr_delalloc_inodes--;
1756 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001757 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001758 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 BUG_ON(list_empty(&root->delalloc_root));
1760 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001761 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001762 }
1763 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001764}
1765
1766static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1767 struct btrfs_inode *inode)
1768{
1769 spin_lock(&root->delalloc_lock);
1770 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001771 spin_unlock(&root->delalloc_lock);
1772}
1773
Chris Masond352ac62008-09-29 15:18:18 -04001774/*
1775 * extent_io.c set_bit_hook, used to track delayed allocation
1776 * bytes in this file, and to maintain the list of inodes that
1777 * have pending delalloc work to be done.
1778 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001779static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001780 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001781{
Josef Bacikc6100a42017-05-05 11:57:13 -04001782 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001783
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1785
Wang Shilong47059d92014-07-03 18:22:07 +08001786 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1787 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001788 /*
1789 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001790 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001791 * bit, which is only set or cleared with irqs on
1792 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001793 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001794 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001795 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001796 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001797 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001798
Josef Bacik8b62f872017-10-19 14:15:55 -04001799 spin_lock(&BTRFS_I(inode)->lock);
1800 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1801 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001802
Josef Bacik6a3891c2015-03-16 17:38:52 -04001803 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001804 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001805 return;
1806
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001807 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1808 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001809 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001810 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001811 if (*bits & EXTENT_DEFRAG)
1812 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001813 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001814 &BTRFS_I(inode)->runtime_flags))
1815 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001816 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001817 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001818
1819 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1820 (*bits & EXTENT_DELALLOC_NEW)) {
1821 spin_lock(&BTRFS_I(inode)->lock);
1822 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1823 state->start;
1824 spin_unlock(&BTRFS_I(inode)->lock);
1825 }
Chris Mason291d6732008-01-29 15:55:23 -05001826}
1827
Chris Masond352ac62008-09-29 15:18:18 -04001828/*
1829 * extent_io.c clear_bit_hook, see set_bit_hook for why
1830 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001831static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001832 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001833 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001834{
Josef Bacikc6100a42017-05-05 11:57:13 -04001835 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001836 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001837 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001838 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001839
Filipe Manana4a4b9642017-07-27 19:52:55 +01001840 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1841 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001842 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001843 spin_unlock(&inode->lock);
1844 }
Wang Shilong47059d92014-07-03 18:22:07 +08001845
Chris Mason75eff682008-12-15 15:54:40 -05001846 /*
1847 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001848 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001849 * bit, which is only set or cleared with irqs on
1850 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001851 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001852 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001853 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001854
Josef Bacik8b62f872017-10-19 14:15:55 -04001855 spin_lock(&inode->lock);
1856 btrfs_mod_outstanding_extents(inode, -num_extents);
1857 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001858
Josef Bacikb6d08f02013-09-27 14:57:43 -04001859 /*
1860 * We don't reserve metadata space for space cache inodes so we
1861 * don't need to call dellalloc_release_metadata if there is an
1862 * error.
1863 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001864 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001866 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001867
Josef Bacik6a3891c2015-03-16 17:38:52 -04001868 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001870 return;
1871
Filipe Mananaa315e682017-03-06 23:04:20 +00001872 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1873 do_list && !(state->state & EXTENT_NORESERVE) &&
1874 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001875 btrfs_free_reserved_data_space_noquota(
1876 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001877 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001878
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001879 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1880 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001881 spin_lock(&inode->lock);
1882 inode->delalloc_bytes -= len;
1883 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001884 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001885 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001886 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001887 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001888 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001889
1890 if ((state->state & EXTENT_DELALLOC_NEW) &&
1891 (*bits & EXTENT_DELALLOC_NEW)) {
1892 spin_lock(&inode->lock);
1893 ASSERT(inode->new_delalloc_bytes >= len);
1894 inode->new_delalloc_bytes -= len;
1895 spin_unlock(&inode->lock);
1896 }
Chris Mason291d6732008-01-29 15:55:23 -05001897}
1898
Chris Masond352ac62008-09-29 15:18:18 -04001899/*
1900 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1901 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001902 *
1903 * return 1 if page cannot be merged to bio
1904 * return 0 if page can be merged to bio
1905 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001906 */
Mike Christie81a75f672016-06-05 14:31:54 -05001907int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001908 size_t size, struct bio *bio,
1909 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001911 struct inode *inode = page->mapping->host;
1912 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001913 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001914 u64 length = 0;
1915 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001916 int ret;
1917
Chris Mason771ed682008-11-06 22:02:51 -05001918 if (bio_flags & EXTENT_BIO_COMPRESSED)
1919 return 0;
1920
Kent Overstreet4f024f32013-10-11 15:44:27 -07001921 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001922 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001923 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1924 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001925 if (ret < 0)
1926 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001927 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001928 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001929 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001930}
1931
Chris Masond352ac62008-09-29 15:18:18 -04001932/*
1933 * in order to insert checksums into the metadata in large chunks,
1934 * we wait until bio submission time. All the pages in the bio are
1935 * checksummed and sums are attached onto the ordered extent record.
1936 *
1937 * At IO completion time the cums attached on the ordered extent record
1938 * are inserted into the btree
1939 */
David Sterbad0ee3932018-03-08 14:35:48 +01001940static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001941 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001942{
Josef Bacikc6100a42017-05-05 11:57:13 -04001943 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001944 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001945
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001946 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001947 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001948 return 0;
1949}
Chris Masone0156402008-04-16 11:15:20 -04001950
Chris Mason4a69a412008-11-06 22:03:00 -05001951/*
1952 * in order to insert checksums into the metadata in large chunks,
1953 * we wait until bio submission time. All the pages in the bio are
1954 * checksummed and sums are attached onto the ordered extent record.
1955 *
1956 * At IO completion time the cums attached on the ordered extent record
1957 * are inserted into the btree
1958 */
David Sterbad0ee3932018-03-08 14:35:48 +01001959static blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001960 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001961{
Josef Bacikc6100a42017-05-05 11:57:13 -04001962 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001963 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001964 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001966 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001967 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001968 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001969 bio_endio(bio);
1970 }
Stefan Behrens61891922012-11-05 18:51:52 +01001971 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001972}
1973
Chris Masond352ac62008-09-29 15:18:18 -04001974/*
Chris Masoncad321a2008-12-17 14:51:42 -05001975 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001976 * on write, or reading the csums from the tree before a read.
1977 *
1978 * Rules about async/sync submit,
1979 * a) read: sync submit
1980 *
1981 * b) write without checksum: sync submit
1982 *
1983 * c) write with checksum:
1984 * c-1) if bio is issued by fsync: sync submit
1985 * (sync_writers != 0)
1986 *
1987 * c-2) if root is reloc root: sync submit
1988 * (only in case of buffered IO)
1989 *
1990 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001991 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001992static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001993 int mirror_num, unsigned long bio_flags,
1994 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001995{
Josef Bacikc6100a42017-05-05 11:57:13 -04001996 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001997 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001998 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301999 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002000 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002001 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002002 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002003
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002004 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002005
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002006 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302007 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002008
Mike Christie37226b22016-06-05 14:31:52 -05002009 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002010 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002011 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002012 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002013
Chris Masond20f7042008-12-08 16:58:54 -05002014 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002015 ret = btrfs_submit_compressed_read(inode, bio,
2016 mirror_num,
2017 bio_flags);
2018 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002019 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002020 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002021 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002022 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002023 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002024 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002025 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002026 /* csum items have already been cloned */
2027 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2028 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002029 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002030 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2031 bio_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01002032 btrfs_submit_bio_start,
2033 btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002034 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002035 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002036 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002037 if (ret)
2038 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002039 }
2040
Chris Mason0b86a832008-03-24 15:01:56 -04002041mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002042 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002043
2044out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002045 if (ret) {
2046 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002047 bio_endio(bio);
2048 }
Stefan Behrens61891922012-11-05 18:51:52 +01002049 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002050}
Chris Mason6885f302008-02-20 16:11:05 -05002051
Chris Masond352ac62008-09-29 15:18:18 -04002052/*
2053 * given a list of ordered sums record them in the inode. This happens
2054 * at IO completion time based on sums calculated at bio submission time.
2055 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002056static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002057 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002058{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002059 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002060 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002061
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002062 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002063 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002064 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002065 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002066 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002067 if (ret)
2068 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002069 }
2070 return 0;
2071}
2072
Josef Bacik2ac55d42010-02-03 19:33:23 +00002073int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002074 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002075 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002076{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002077 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002078 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002079 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002080}
2081
Chris Masond352ac62008-09-29 15:18:18 -04002082/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002083struct btrfs_writepage_fixup {
2084 struct page *page;
2085 struct btrfs_work work;
2086};
2087
Christoph Hellwigb2950862008-12-02 09:54:17 -05002088static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002089{
2090 struct btrfs_writepage_fixup *fixup;
2091 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002092 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002093 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002094 struct page *page;
2095 struct inode *inode;
2096 u64 page_start;
2097 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002098 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002099
2100 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2101 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002102again:
Chris Mason247e7432008-07-17 12:53:51 -04002103 lock_page(page);
2104 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2105 ClearPageChecked(page);
2106 goto out_page;
2107 }
2108
2109 inode = page->mapping->host;
2110 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002112
David Sterbaff13db42015-12-03 14:30:40 +01002113 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002114 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002115
2116 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002117 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002118 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002119
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002120 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002121 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002122 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002123 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002124 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002125 unlock_page(page);
2126 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002127 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002128 goto again;
2129 }
Chris Mason247e7432008-07-17 12:53:51 -04002130
Qu Wenruo364ecf32017-02-27 15:10:38 +08002131 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002132 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002133 if (ret) {
2134 mapping_set_error(page->mapping, ret);
2135 end_extent_writepage(page, ret, page_start, page_end);
2136 ClearPageChecked(page);
2137 goto out;
2138 }
2139
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002140 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2141 &cached_state, 0);
2142 if (ret) {
2143 mapping_set_error(page->mapping, ret);
2144 end_extent_writepage(page, ret, page_start, page_end);
2145 ClearPageChecked(page);
2146 goto out;
2147 }
2148
Chris Mason247e7432008-07-17 12:53:51 -04002149 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002150 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002151 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002152out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002153 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002154 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002155out_page:
2156 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002157 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002158 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002159 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002160}
2161
2162/*
2163 * There are a few paths in the higher layers of the kernel that directly
2164 * set the page dirty bit without asking the filesystem if it is a
2165 * good idea. This causes problems because we want to make sure COW
2166 * properly happens and the data=ordered rules are followed.
2167 *
Chris Masonc8b97812008-10-29 14:49:59 -04002168 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002169 * hasn't been properly setup for IO. We kick off an async process
2170 * to fix it up. The async helper will wait for ordered extents, set
2171 * the delalloc bit and make it safe to write the page.
2172 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002173static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002174{
2175 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002177 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002178
Chris Mason8b62b722009-09-02 16:53:46 -04002179 /* this page is properly in the ordered list */
2180 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002181 return 0;
2182
2183 if (PageChecked(page))
2184 return -EAGAIN;
2185
2186 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2187 if (!fixup)
2188 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002189
Chris Mason247e7432008-07-17 12:53:51 -04002190 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002191 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002192 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2193 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002194 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002195 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002196 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002197}
2198
Yan Zhengd899e052008-10-30 14:25:28 -04002199static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2200 struct inode *inode, u64 file_pos,
2201 u64 disk_bytenr, u64 disk_num_bytes,
2202 u64 num_bytes, u64 ram_bytes,
2203 u8 compression, u8 encryption,
2204 u16 other_encoding, int extent_type)
2205{
2206 struct btrfs_root *root = BTRFS_I(inode)->root;
2207 struct btrfs_file_extent_item *fi;
2208 struct btrfs_path *path;
2209 struct extent_buffer *leaf;
2210 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002211 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002212 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002213 int ret;
2214
2215 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002216 if (!path)
2217 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002218
Chris Masona1ed8352009-09-11 12:27:37 -04002219 /*
2220 * we may be replacing one extent in the tree with another.
2221 * The new extent is pinned in the extent map, and we don't want
2222 * to drop it from the cache until it is completely in the btree.
2223 *
2224 * So, tell btrfs_drop_extents to leave this extent in the cache.
2225 * the caller is expected to unpin it and allow it to be merged
2226 * with the others.
2227 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002228 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2229 file_pos + num_bytes, NULL, 0,
2230 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231 if (ret)
2232 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002233
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002234 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002235 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002236 ins.offset = file_pos;
2237 ins.type = BTRFS_EXTENT_DATA_KEY;
2238
2239 path->leave_spinning = 1;
2240 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2241 sizeof(*fi));
2242 if (ret)
2243 goto out;
2244 }
Yan Zhengd899e052008-10-30 14:25:28 -04002245 leaf = path->nodes[0];
2246 fi = btrfs_item_ptr(leaf, path->slots[0],
2247 struct btrfs_file_extent_item);
2248 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2249 btrfs_set_file_extent_type(leaf, fi, extent_type);
2250 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2251 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2252 btrfs_set_file_extent_offset(leaf, fi, 0);
2253 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2254 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2255 btrfs_set_file_extent_compression(leaf, fi, compression);
2256 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2257 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002258
Yan Zhengd899e052008-10-30 14:25:28 -04002259 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002260 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002261
2262 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002263
2264 ins.objectid = disk_bytenr;
2265 ins.offset = disk_num_bytes;
2266 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002267
Qu Wenruo297d7502015-09-08 17:08:37 +08002268 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002269 * Release the reserved range from inode dirty range map, as it is
2270 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002271 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002272 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2273 if (ret < 0)
2274 goto out;
2275 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002276 ret = btrfs_alloc_reserved_file_extent(trans, root,
2277 btrfs_ino(BTRFS_I(inode)),
2278 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279out:
Yan Zhengd899e052008-10-30 14:25:28 -04002280 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002281
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002283}
2284
Liu Bo38c227d2013-01-29 03:18:40 +00002285/* snapshot-aware defrag */
2286struct sa_defrag_extent_backref {
2287 struct rb_node node;
2288 struct old_sa_defrag_extent *old;
2289 u64 root_id;
2290 u64 inum;
2291 u64 file_pos;
2292 u64 extent_offset;
2293 u64 num_bytes;
2294 u64 generation;
2295};
2296
2297struct old_sa_defrag_extent {
2298 struct list_head list;
2299 struct new_sa_defrag_extent *new;
2300
2301 u64 extent_offset;
2302 u64 bytenr;
2303 u64 offset;
2304 u64 len;
2305 int count;
2306};
2307
2308struct new_sa_defrag_extent {
2309 struct rb_root root;
2310 struct list_head head;
2311 struct btrfs_path *path;
2312 struct inode *inode;
2313 u64 file_pos;
2314 u64 len;
2315 u64 bytenr;
2316 u64 disk_len;
2317 u8 compress_type;
2318};
2319
2320static int backref_comp(struct sa_defrag_extent_backref *b1,
2321 struct sa_defrag_extent_backref *b2)
2322{
2323 if (b1->root_id < b2->root_id)
2324 return -1;
2325 else if (b1->root_id > b2->root_id)
2326 return 1;
2327
2328 if (b1->inum < b2->inum)
2329 return -1;
2330 else if (b1->inum > b2->inum)
2331 return 1;
2332
2333 if (b1->file_pos < b2->file_pos)
2334 return -1;
2335 else if (b1->file_pos > b2->file_pos)
2336 return 1;
2337
2338 /*
2339 * [------------------------------] ===> (a range of space)
2340 * |<--->| |<---->| =============> (fs/file tree A)
2341 * |<---------------------------->| ===> (fs/file tree B)
2342 *
2343 * A range of space can refer to two file extents in one tree while
2344 * refer to only one file extent in another tree.
2345 *
2346 * So we may process a disk offset more than one time(two extents in A)
2347 * and locate at the same extent(one extent in B), then insert two same
2348 * backrefs(both refer to the extent in B).
2349 */
2350 return 0;
2351}
2352
2353static void backref_insert(struct rb_root *root,
2354 struct sa_defrag_extent_backref *backref)
2355{
2356 struct rb_node **p = &root->rb_node;
2357 struct rb_node *parent = NULL;
2358 struct sa_defrag_extent_backref *entry;
2359 int ret;
2360
2361 while (*p) {
2362 parent = *p;
2363 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2364
2365 ret = backref_comp(backref, entry);
2366 if (ret < 0)
2367 p = &(*p)->rb_left;
2368 else
2369 p = &(*p)->rb_right;
2370 }
2371
2372 rb_link_node(&backref->node, parent, p);
2373 rb_insert_color(&backref->node, root);
2374}
2375
2376/*
2377 * Note the backref might has changed, and in this case we just return 0.
2378 */
2379static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2380 void *ctx)
2381{
2382 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002383 struct old_sa_defrag_extent *old = ctx;
2384 struct new_sa_defrag_extent *new = old->new;
2385 struct btrfs_path *path = new->path;
2386 struct btrfs_key key;
2387 struct btrfs_root *root;
2388 struct sa_defrag_extent_backref *backref;
2389 struct extent_buffer *leaf;
2390 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002391 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002392 int slot;
2393 int ret;
2394 u64 extent_offset;
2395 u64 num_bytes;
2396
2397 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002398 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002399 return 0;
2400
2401 key.objectid = root_id;
2402 key.type = BTRFS_ROOT_ITEM_KEY;
2403 key.offset = (u64)-1;
2404
Liu Bo38c227d2013-01-29 03:18:40 +00002405 root = btrfs_read_fs_root_no_name(fs_info, &key);
2406 if (IS_ERR(root)) {
2407 if (PTR_ERR(root) == -ENOENT)
2408 return 0;
2409 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002410 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002411 inum, offset, root_id);
2412 return PTR_ERR(root);
2413 }
2414
2415 key.objectid = inum;
2416 key.type = BTRFS_EXTENT_DATA_KEY;
2417 if (offset > (u64)-1 << 32)
2418 key.offset = 0;
2419 else
2420 key.offset = offset;
2421
2422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302423 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002424 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002425 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002426
2427 while (1) {
2428 cond_resched();
2429
2430 leaf = path->nodes[0];
2431 slot = path->slots[0];
2432
2433 if (slot >= btrfs_header_nritems(leaf)) {
2434 ret = btrfs_next_leaf(root, path);
2435 if (ret < 0) {
2436 goto out;
2437 } else if (ret > 0) {
2438 ret = 0;
2439 goto out;
2440 }
2441 continue;
2442 }
2443
2444 path->slots[0]++;
2445
2446 btrfs_item_key_to_cpu(leaf, &key, slot);
2447
2448 if (key.objectid > inum)
2449 goto out;
2450
2451 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2452 continue;
2453
2454 extent = btrfs_item_ptr(leaf, slot,
2455 struct btrfs_file_extent_item);
2456
2457 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2458 continue;
2459
Liu Boe68afa42013-07-01 22:13:26 +08002460 /*
2461 * 'offset' refers to the exact key.offset,
2462 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2463 * (key.offset - extent_offset).
2464 */
2465 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002466 continue;
2467
Liu Boe68afa42013-07-01 22:13:26 +08002468 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002469 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002470
Liu Bo38c227d2013-01-29 03:18:40 +00002471 if (extent_offset >= old->extent_offset + old->offset +
2472 old->len || extent_offset + num_bytes <=
2473 old->extent_offset + old->offset)
2474 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002475 break;
2476 }
2477
2478 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2479 if (!backref) {
2480 ret = -ENOENT;
2481 goto out;
2482 }
2483
2484 backref->root_id = root_id;
2485 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002486 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002487 backref->num_bytes = num_bytes;
2488 backref->extent_offset = extent_offset;
2489 backref->generation = btrfs_file_extent_generation(leaf, extent);
2490 backref->old = old;
2491 backref_insert(&new->root, backref);
2492 old->count++;
2493out:
2494 btrfs_release_path(path);
2495 WARN_ON(ret);
2496 return ret;
2497}
2498
2499static noinline bool record_extent_backrefs(struct btrfs_path *path,
2500 struct new_sa_defrag_extent *new)
2501{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002503 struct old_sa_defrag_extent *old, *tmp;
2504 int ret;
2505
2506 new->path = path;
2507
2508 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002509 ret = iterate_inodes_from_logical(old->bytenr +
2510 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002511 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002512 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002513 if (ret < 0 && ret != -ENOENT)
2514 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002515
2516 /* no backref to be processed for this extent */
2517 if (!old->count) {
2518 list_del(&old->list);
2519 kfree(old);
2520 }
2521 }
2522
2523 if (list_empty(&new->head))
2524 return false;
2525
2526 return true;
2527}
2528
2529static int relink_is_mergable(struct extent_buffer *leaf,
2530 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002531 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002532{
Liu Bo116e0022013-08-02 16:30:40 +08002533 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002534 return 0;
2535
2536 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2537 return 0;
2538
Liu Bo116e0022013-08-02 16:30:40 +08002539 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2540 return 0;
2541
2542 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002543 btrfs_file_extent_other_encoding(leaf, fi))
2544 return 0;
2545
2546 return 1;
2547}
2548
2549/*
2550 * Note the backref might has changed, and in this case we just return 0.
2551 */
2552static noinline int relink_extent_backref(struct btrfs_path *path,
2553 struct sa_defrag_extent_backref *prev,
2554 struct sa_defrag_extent_backref *backref)
2555{
2556 struct btrfs_file_extent_item *extent;
2557 struct btrfs_file_extent_item *item;
2558 struct btrfs_ordered_extent *ordered;
2559 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002560 struct btrfs_root *root;
2561 struct btrfs_key key;
2562 struct extent_buffer *leaf;
2563 struct old_sa_defrag_extent *old = backref->old;
2564 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002565 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002566 struct inode *inode;
2567 struct extent_state *cached = NULL;
2568 int ret = 0;
2569 u64 start;
2570 u64 len;
2571 u64 lock_start;
2572 u64 lock_end;
2573 bool merge = false;
2574 int index;
2575
2576 if (prev && prev->root_id == backref->root_id &&
2577 prev->inum == backref->inum &&
2578 prev->file_pos + prev->num_bytes == backref->file_pos)
2579 merge = true;
2580
2581 /* step 1: get root */
2582 key.objectid = backref->root_id;
2583 key.type = BTRFS_ROOT_ITEM_KEY;
2584 key.offset = (u64)-1;
2585
Liu Bo38c227d2013-01-29 03:18:40 +00002586 index = srcu_read_lock(&fs_info->subvol_srcu);
2587
2588 root = btrfs_read_fs_root_no_name(fs_info, &key);
2589 if (IS_ERR(root)) {
2590 srcu_read_unlock(&fs_info->subvol_srcu, index);
2591 if (PTR_ERR(root) == -ENOENT)
2592 return 0;
2593 return PTR_ERR(root);
2594 }
Liu Bo38c227d2013-01-29 03:18:40 +00002595
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002596 if (btrfs_root_readonly(root)) {
2597 srcu_read_unlock(&fs_info->subvol_srcu, index);
2598 return 0;
2599 }
2600
Liu Bo38c227d2013-01-29 03:18:40 +00002601 /* step 2: get inode */
2602 key.objectid = backref->inum;
2603 key.type = BTRFS_INODE_ITEM_KEY;
2604 key.offset = 0;
2605
2606 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2607 if (IS_ERR(inode)) {
2608 srcu_read_unlock(&fs_info->subvol_srcu, index);
2609 return 0;
2610 }
2611
2612 srcu_read_unlock(&fs_info->subvol_srcu, index);
2613
2614 /* step 3: relink backref */
2615 lock_start = backref->file_pos;
2616 lock_end = backref->file_pos + backref->num_bytes - 1;
2617 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002618 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002619
2620 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2621 if (ordered) {
2622 btrfs_put_ordered_extent(ordered);
2623 goto out_unlock;
2624 }
2625
2626 trans = btrfs_join_transaction(root);
2627 if (IS_ERR(trans)) {
2628 ret = PTR_ERR(trans);
2629 goto out_unlock;
2630 }
2631
2632 key.objectid = backref->inum;
2633 key.type = BTRFS_EXTENT_DATA_KEY;
2634 key.offset = backref->file_pos;
2635
2636 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2637 if (ret < 0) {
2638 goto out_free_path;
2639 } else if (ret > 0) {
2640 ret = 0;
2641 goto out_free_path;
2642 }
2643
2644 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2645 struct btrfs_file_extent_item);
2646
2647 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2648 backref->generation)
2649 goto out_free_path;
2650
2651 btrfs_release_path(path);
2652
2653 start = backref->file_pos;
2654 if (backref->extent_offset < old->extent_offset + old->offset)
2655 start += old->extent_offset + old->offset -
2656 backref->extent_offset;
2657
2658 len = min(backref->extent_offset + backref->num_bytes,
2659 old->extent_offset + old->offset + old->len);
2660 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2661
2662 ret = btrfs_drop_extents(trans, root, inode, start,
2663 start + len, 1);
2664 if (ret)
2665 goto out_free_path;
2666again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002667 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002668 key.type = BTRFS_EXTENT_DATA_KEY;
2669 key.offset = start;
2670
Liu Boa09a0a72013-03-11 09:20:58 +00002671 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002672 if (merge) {
2673 struct btrfs_file_extent_item *fi;
2674 u64 extent_len;
2675 struct btrfs_key found_key;
2676
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002677 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002678 if (ret < 0)
2679 goto out_free_path;
2680
2681 path->slots[0]--;
2682 leaf = path->nodes[0];
2683 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2684
2685 fi = btrfs_item_ptr(leaf, path->slots[0],
2686 struct btrfs_file_extent_item);
2687 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2688
Liu Bo116e0022013-08-02 16:30:40 +08002689 if (extent_len + found_key.offset == start &&
2690 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002691 btrfs_set_file_extent_num_bytes(leaf, fi,
2692 extent_len + len);
2693 btrfs_mark_buffer_dirty(leaf);
2694 inode_add_bytes(inode, len);
2695
2696 ret = 1;
2697 goto out_free_path;
2698 } else {
2699 merge = false;
2700 btrfs_release_path(path);
2701 goto again;
2702 }
2703 }
2704
2705 ret = btrfs_insert_empty_item(trans, root, path, &key,
2706 sizeof(*extent));
2707 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002708 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002709 goto out_free_path;
2710 }
2711
2712 leaf = path->nodes[0];
2713 item = btrfs_item_ptr(leaf, path->slots[0],
2714 struct btrfs_file_extent_item);
2715 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2716 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2717 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2718 btrfs_set_file_extent_num_bytes(leaf, item, len);
2719 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2720 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2721 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2722 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2723 btrfs_set_file_extent_encryption(leaf, item, 0);
2724 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2725
2726 btrfs_mark_buffer_dirty(leaf);
2727 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002728 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002729
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002730 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002731 new->disk_len, 0,
2732 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002733 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002734 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002735 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002736 goto out_free_path;
2737 }
2738
2739 ret = 1;
2740out_free_path:
2741 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002742 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002743 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002744out_unlock:
2745 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002746 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002747 iput(inode);
2748 return ret;
2749}
2750
Liu Bo6f519562013-10-29 10:45:05 +08002751static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2752{
2753 struct old_sa_defrag_extent *old, *tmp;
2754
2755 if (!new)
2756 return;
2757
2758 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002759 kfree(old);
2760 }
2761 kfree(new);
2762}
2763
Liu Bo38c227d2013-01-29 03:18:40 +00002764static void relink_file_extents(struct new_sa_defrag_extent *new)
2765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002766 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002767 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002768 struct sa_defrag_extent_backref *backref;
2769 struct sa_defrag_extent_backref *prev = NULL;
2770 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002771 struct rb_node *node;
2772 int ret;
2773
2774 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002775
2776 path = btrfs_alloc_path();
2777 if (!path)
2778 return;
2779
2780 if (!record_extent_backrefs(path, new)) {
2781 btrfs_free_path(path);
2782 goto out;
2783 }
2784 btrfs_release_path(path);
2785
2786 while (1) {
2787 node = rb_first(&new->root);
2788 if (!node)
2789 break;
2790 rb_erase(node, &new->root);
2791
2792 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2793
2794 ret = relink_extent_backref(path, prev, backref);
2795 WARN_ON(ret < 0);
2796
2797 kfree(prev);
2798
2799 if (ret == 1)
2800 prev = backref;
2801 else
2802 prev = NULL;
2803 cond_resched();
2804 }
2805 kfree(prev);
2806
2807 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002808out:
Liu Bo6f519562013-10-29 10:45:05 +08002809 free_sa_defrag_extent(new);
2810
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 atomic_dec(&fs_info->defrag_running);
2812 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002813}
2814
2815static struct new_sa_defrag_extent *
2816record_old_file_extents(struct inode *inode,
2817 struct btrfs_ordered_extent *ordered)
2818{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002819 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002820 struct btrfs_root *root = BTRFS_I(inode)->root;
2821 struct btrfs_path *path;
2822 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002823 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002824 struct new_sa_defrag_extent *new;
2825 int ret;
2826
2827 new = kmalloc(sizeof(*new), GFP_NOFS);
2828 if (!new)
2829 return NULL;
2830
2831 new->inode = inode;
2832 new->file_pos = ordered->file_offset;
2833 new->len = ordered->len;
2834 new->bytenr = ordered->start;
2835 new->disk_len = ordered->disk_len;
2836 new->compress_type = ordered->compress_type;
2837 new->root = RB_ROOT;
2838 INIT_LIST_HEAD(&new->head);
2839
2840 path = btrfs_alloc_path();
2841 if (!path)
2842 goto out_kfree;
2843
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002844 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002845 key.type = BTRFS_EXTENT_DATA_KEY;
2846 key.offset = new->file_pos;
2847
2848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2849 if (ret < 0)
2850 goto out_free_path;
2851 if (ret > 0 && path->slots[0] > 0)
2852 path->slots[0]--;
2853
2854 /* find out all the old extents for the file range */
2855 while (1) {
2856 struct btrfs_file_extent_item *extent;
2857 struct extent_buffer *l;
2858 int slot;
2859 u64 num_bytes;
2860 u64 offset;
2861 u64 end;
2862 u64 disk_bytenr;
2863 u64 extent_offset;
2864
2865 l = path->nodes[0];
2866 slot = path->slots[0];
2867
2868 if (slot >= btrfs_header_nritems(l)) {
2869 ret = btrfs_next_leaf(root, path);
2870 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002871 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002872 else if (ret > 0)
2873 break;
2874 continue;
2875 }
2876
2877 btrfs_item_key_to_cpu(l, &key, slot);
2878
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002879 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002880 break;
2881 if (key.type != BTRFS_EXTENT_DATA_KEY)
2882 break;
2883 if (key.offset >= new->file_pos + new->len)
2884 break;
2885
2886 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2887
2888 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2889 if (key.offset + num_bytes < new->file_pos)
2890 goto next;
2891
2892 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2893 if (!disk_bytenr)
2894 goto next;
2895
2896 extent_offset = btrfs_file_extent_offset(l, extent);
2897
2898 old = kmalloc(sizeof(*old), GFP_NOFS);
2899 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002900 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002901
2902 offset = max(new->file_pos, key.offset);
2903 end = min(new->file_pos + new->len, key.offset + num_bytes);
2904
2905 old->bytenr = disk_bytenr;
2906 old->extent_offset = extent_offset;
2907 old->offset = offset - key.offset;
2908 old->len = end - offset;
2909 old->new = new;
2910 old->count = 0;
2911 list_add_tail(&old->list, &new->head);
2912next:
2913 path->slots[0]++;
2914 cond_resched();
2915 }
2916
2917 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002918 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002919
2920 return new;
2921
Liu Bo38c227d2013-01-29 03:18:40 +00002922out_free_path:
2923 btrfs_free_path(path);
2924out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002925 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002926 return NULL;
2927}
2928
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002929static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002930 u64 start, u64 len)
2931{
2932 struct btrfs_block_group_cache *cache;
2933
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002935 ASSERT(cache);
2936
2937 spin_lock(&cache->lock);
2938 cache->delalloc_bytes -= len;
2939 spin_unlock(&cache->lock);
2940
2941 btrfs_put_block_group(cache);
2942}
2943
Chris Masond352ac62008-09-29 15:18:18 -04002944/* as ordered data IO finishes, this gets called so we can finish
2945 * an ordered extent if the range of bytes in the file it covers are
2946 * fully written.
2947 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002948static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002949{
Josef Bacik5fd02042012-05-02 14:00:54 -04002950 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002951 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002952 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002953 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002954 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002955 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002956 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002957 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002958 int ret = 0;
2959 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002960 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002961 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002962 bool range_locked = false;
2963 bool clear_new_delalloc_bytes = false;
2964
2965 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2966 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2967 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2968 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002969
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002970 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002971
Josef Bacik5fd02042012-05-02 14:00:54 -04002972 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2973 ret = -EIO;
2974 goto out;
2975 }
2976
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002977 btrfs_free_io_failure_record(BTRFS_I(inode),
2978 ordered_extent->file_offset,
2979 ordered_extent->file_offset +
2980 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002981
Josef Bacik77cef2e2013-08-29 13:57:21 -04002982 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2983 truncated = true;
2984 logical_len = ordered_extent->truncated_len;
2985 /* Truncated the entire extent, don't bother adding */
2986 if (!logical_len)
2987 goto out;
2988 }
2989
Yan, Zhengc2167752009-11-12 09:34:21 +00002990 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002992
2993 /*
2994 * For mwrite(mmap + memset to write) case, we still reserve
2995 * space for NOCOW range.
2996 * As NOCOW won't cause a new delayed ref, just free the space
2997 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002998 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002999 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003000 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3001 if (nolock)
3002 trans = btrfs_join_transaction_nolock(root);
3003 else
3004 trans = btrfs_join_transaction(root);
3005 if (IS_ERR(trans)) {
3006 ret = PTR_ERR(trans);
3007 trans = NULL;
3008 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003009 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003010 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003011 ret = btrfs_update_inode_fallback(trans, root, inode);
3012 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003013 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003014 goto out;
3015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003016
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003017 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003018 lock_extent_bits(io_tree, ordered_extent->file_offset,
3019 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003020 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021
Liu Bo38c227d2013-01-29 03:18:40 +00003022 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3023 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003024 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003025 if (ret) {
3026 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003027 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003028 /* the inode is shared */
3029 new = record_old_file_extents(inode, ordered_extent);
3030
3031 clear_extent_bit(io_tree, ordered_extent->file_offset,
3032 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003033 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003034 }
3035
Josef Bacik0cb59c92010-07-02 12:14:14 -04003036 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003037 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003038 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003039 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003040 if (IS_ERR(trans)) {
3041 ret = PTR_ERR(trans);
3042 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003043 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003044 }
Chris Masona79b7d42014-05-22 16:18:52 -07003045
Josef Bacik69fe2d72017-10-19 14:15:57 -04003046 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003047
Chris Masonc8b97812008-10-29 14:49:59 -04003048 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003049 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003050 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003051 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003052 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3053 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003054 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003055 ordered_extent->file_offset,
3056 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003057 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003058 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003059 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003060 ret = insert_reserved_file_extent(trans, inode,
3061 ordered_extent->file_offset,
3062 ordered_extent->start,
3063 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003064 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003065 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003066 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003067 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003068 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003069 ordered_extent->start,
3070 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003071 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003072 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3073 ordered_extent->file_offset, ordered_extent->len,
3074 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003075 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003076 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003077 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003078 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003079
Nikolay Borisovac01f262018-01-08 10:59:43 +02003080 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3081 if (ret) {
3082 btrfs_abort_transaction(trans, ret);
3083 goto out;
3084 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003085
Josef Bacik6c760c02012-11-09 10:53:21 -05003086 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3087 ret = btrfs_update_inode_fallback(trans, root, inode);
3088 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003089 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003090 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003091 }
3092 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003093out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003094 if (range_locked || clear_new_delalloc_bytes) {
3095 unsigned int clear_bits = 0;
3096
3097 if (range_locked)
3098 clear_bits |= EXTENT_LOCKED;
3099 if (clear_new_delalloc_bytes)
3100 clear_bits |= EXTENT_DELALLOC_NEW;
3101 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3102 ordered_extent->file_offset,
3103 ordered_extent->file_offset +
3104 ordered_extent->len - 1,
3105 clear_bits,
3106 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003107 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003108 }
3109
Miao Xiea698d0752012-09-20 01:51:59 -06003110 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003111 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003112
Josef Bacik77cef2e2013-08-29 13:57:21 -04003113 if (ret || truncated) {
3114 u64 start, end;
3115
3116 if (truncated)
3117 start = ordered_extent->file_offset + logical_len;
3118 else
3119 start = ordered_extent->file_offset;
3120 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003121 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003122
3123 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003124 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003125
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003126 /*
3127 * If the ordered extent had an IOERR or something else went
3128 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003129 * back to the allocator. We only free the extent in the
3130 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003131 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003132 if ((ret || !logical_len) &&
3133 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003134 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003135 btrfs_free_reserved_extent(fs_info,
3136 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003137 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003138 }
3139
3140
Josef Bacik5fd02042012-05-02 14:00:54 -04003141 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003142 * This needs to be done to make sure anybody waiting knows we are done
3143 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003144 */
3145 btrfs_remove_ordered_extent(inode, ordered_extent);
3146
Liu Bo38c227d2013-01-29 03:18:40 +00003147 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003148 if (new) {
3149 if (ret) {
3150 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003151 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003152 } else {
3153 relink_file_extents(new);
3154 }
3155 }
Liu Bo38c227d2013-01-29 03:18:40 +00003156
Chris Masone6dcd2d2008-07-17 12:53:50 -04003157 /* once for us */
3158 btrfs_put_ordered_extent(ordered_extent);
3159 /* once for the tree */
3160 btrfs_put_ordered_extent(ordered_extent);
3161
Josef Bacik5fd02042012-05-02 14:00:54 -04003162 return ret;
3163}
3164
3165static void finish_ordered_fn(struct btrfs_work *work)
3166{
3167 struct btrfs_ordered_extent *ordered_extent;
3168 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3169 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003170}
3171
David Sterbac3988d62017-02-17 15:18:32 +01003172static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003173 struct extent_state *state, int uptodate)
3174{
Josef Bacik5fd02042012-05-02 14:00:54 -04003175 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003177 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003178 struct btrfs_workqueue *wq;
3179 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003180
liubo1abe9b82011-03-24 11:18:59 +00003181 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3182
Chris Mason8b62b722009-09-02 16:53:46 -04003183 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003184 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3185 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003186 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003187
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003188 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003189 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003190 func = btrfs_freespace_write_helper;
3191 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003192 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003193 func = btrfs_endio_write_helper;
3194 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003195
Liu Bo9e0af232014-08-15 23:36:53 +08003196 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3197 NULL);
3198 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003199}
3200
Miao Xiedc380ae2014-09-12 18:43:55 +08003201static int __readpage_endio_check(struct inode *inode,
3202 struct btrfs_io_bio *io_bio,
3203 int icsum, struct page *page,
3204 int pgoff, u64 start, size_t len)
3205{
3206 char *kaddr;
3207 u32 csum_expected;
3208 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003209
3210 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3211
3212 kaddr = kmap_atomic(page);
3213 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003214 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003215 if (csum != csum_expected)
3216 goto zeroit;
3217
3218 kunmap_atomic(kaddr);
3219 return 0;
3220zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003221 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003222 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003223 memset(kaddr + pgoff, 1, len);
3224 flush_dcache_page(page);
3225 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003226 return -EIO;
3227}
3228
Chris Masond352ac62008-09-29 15:18:18 -04003229/*
Chris Masond352ac62008-09-29 15:18:18 -04003230 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003231 * if there's a match, we allow the bio to finish. If not, the code in
3232 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003233 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003234static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3235 u64 phy_offset, struct page *page,
3236 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003237{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003238 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003239 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003240 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003241 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003242
Chris Masond20f7042008-12-08 16:58:54 -05003243 if (PageChecked(page)) {
3244 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003245 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003246 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003247
3248 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003249 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003250
Yan Zheng17d217f2008-12-12 10:03:38 -05003251 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003252 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003253 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003254 return 0;
3255 }
3256
Miao Xiefacc8a222013-07-25 19:22:34 +08003257 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003258 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3259 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003260}
Chris Masonb888db22007-08-27 16:49:44 -04003261
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003262/*
3263 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3264 *
3265 * @inode: The inode we want to perform iput on
3266 *
3267 * This function uses the generic vfs_inode::i_count to track whether we should
3268 * just decrement it (in case it's > 1) or if this is the last iput then link
3269 * the inode to the delayed iput machinery. Delayed iputs are processed at
3270 * transaction commit time/superblock commit/cleaner kthread.
3271 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003272void btrfs_add_delayed_iput(struct inode *inode)
3273{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003275 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003276
3277 if (atomic_add_unless(&inode->i_count, -1, 1))
3278 return;
3279
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003280 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003281 ASSERT(list_empty(&binode->delayed_iput));
3282 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283 spin_unlock(&fs_info->delayed_iput_lock);
3284}
3285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003286void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003287{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003288
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003289 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003290 while (!list_empty(&fs_info->delayed_iputs)) {
3291 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003292
David Sterba8089fe62015-11-19 14:15:51 +01003293 inode = list_first_entry(&fs_info->delayed_iputs,
3294 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003295 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003296 spin_unlock(&fs_info->delayed_iput_lock);
3297 iput(&inode->vfs_inode);
3298 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003299 }
David Sterba8089fe62015-11-19 14:15:51 +01003300 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003301}
3302
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003304 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 * files in the subvolume, it removes orphan item and frees block_rsv
3306 * structure.
3307 */
3308void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3309 struct btrfs_root *root)
3310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003311 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003312 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 int ret;
3314
Josef Bacik8a35d952012-05-23 14:26:42 -04003315 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3317 return;
3318
Josef Bacik90290e12011-12-02 15:44:12 -05003319 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003320 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003321 spin_unlock(&root->orphan_lock);
3322 return;
3323 }
3324
3325 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3326 spin_unlock(&root->orphan_lock);
3327 return;
3328 }
3329
3330 block_rsv = root->orphan_block_rsv;
3331 root->orphan_block_rsv = NULL;
3332 spin_unlock(&root->orphan_lock);
3333
Miao Xie27cdeb72014-04-02 19:51:05 +08003334 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003335 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003336 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003338 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003339 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003340 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003341 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3342 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003343 }
3344
Josef Bacik90290e12011-12-02 15:44:12 -05003345 if (block_rsv) {
3346 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003347 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003348 }
3349}
3350
3351/*
Josef Bacik7b128762008-07-24 12:17:14 -04003352 * This creates an orphan entry for the given inode in case something goes
3353 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 *
3355 * NOTE: caller of this function should reserve 5 units of metadata for
3356 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003357 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003358int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3359 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003360{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003361 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3362 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 struct btrfs_block_rsv *block_rsv = NULL;
3364 int reserve = 0;
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003365 bool insert = false;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003366 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003367
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003369 block_rsv = btrfs_alloc_block_rsv(fs_info,
3370 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003371 if (!block_rsv)
3372 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003373 }
3374
Josef Bacik8a35d952012-05-23 14:26:42 -04003375 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003376 &inode->runtime_flags))
3377 insert = true;
Josef Bacik7b128762008-07-24 12:17:14 -04003378
Josef Bacik72ac3c02012-05-23 14:13:11 -04003379 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003380 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003381 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003382
3383 spin_lock(&root->orphan_lock);
3384 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3385 if (!root->orphan_block_rsv) {
3386 root->orphan_block_rsv = block_rsv;
3387 } else if (block_rsv) {
3388 btrfs_free_block_rsv(fs_info, block_rsv);
3389 block_rsv = NULL;
3390 }
3391
3392 if (insert)
3393 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003394 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003395
Yan, Zhengd68fc572010-05-16 10:49:58 -04003396 /* grab metadata reservation from transaction handle */
3397 if (reserve) {
3398 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003399 ASSERT(!ret);
3400 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003401 /*
3402 * dec doesn't need spin_lock as ->orphan_block_rsv
3403 * would be released only if ->orphan_inodes is
3404 * zero.
3405 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003406 atomic_dec(&root->orphan_inodes);
3407 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003408 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003409 if (insert)
3410 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003411 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003412 return ret;
3413 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003414 }
3415
3416 /* insert an orphan item to track this unlinked/truncated file */
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003417 if (insert) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003418 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003419 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003420 if (reserve) {
3421 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003422 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003423 btrfs_orphan_release_metadata(inode);
3424 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003425 /*
3426 * btrfs_orphan_commit_root may race with us and set
3427 * ->orphan_block_rsv to zero, in order to avoid that,
3428 * decrease ->orphan_inodes after everything is done.
3429 */
3430 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003431 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003432 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003433 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003434 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003435 return ret;
3436 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003437 }
3438 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003439 }
3440
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003442}
3443
3444/*
3445 * We have done the truncate/delete so we can go ahead and remove the orphan
3446 * item for this particular inode.
3447 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003448static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003449 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003450{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003451 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003452 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003453 int ret = 0;
3454
Josef Bacik8a35d952012-05-23 14:26:42 -04003455 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003456 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003457 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003458
Liu Bo1a932ef2018-01-25 11:02:54 -07003459 if (delete_item && trans)
3460 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3461
Josef Bacik72ac3c02012-05-23 14:13:11 -04003462 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003463 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003464 btrfs_orphan_release_metadata(inode);
3465
Liu Bo1a932ef2018-01-25 11:02:54 -07003466 /*
3467 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3468 * to zero, in order to avoid that, decrease ->orphan_inodes after
3469 * everything is done.
3470 */
3471 if (delete_item)
3472 atomic_dec(&root->orphan_inodes);
3473
Josef Bacik4ef31a42013-08-13 14:10:08 -04003474 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003475}
3476
3477/*
3478 * this cleans up any orphans that may be left on the list from the last use
3479 * of this root.
3480 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003481int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003483 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003484 struct btrfs_path *path;
3485 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003486 struct btrfs_key key, found_key;
3487 struct btrfs_trans_handle *trans;
3488 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003489 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003490 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3491
Yan, Zhengd68fc572010-05-16 10:49:58 -04003492 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003493 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003494
3495 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003496 if (!path) {
3497 ret = -ENOMEM;
3498 goto out;
3499 }
David Sterbae4058b52015-11-27 16:31:35 +01003500 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003501
3502 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003503 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003504 key.offset = (u64)-1;
3505
Josef Bacik7b128762008-07-24 12:17:14 -04003506 while (1) {
3507 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003508 if (ret < 0)
3509 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003510
3511 /*
3512 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003513 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003514 * find the key and see if we have stuff that matches
3515 */
3516 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003517 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003518 if (path->slots[0] == 0)
3519 break;
3520 path->slots[0]--;
3521 }
3522
3523 /* pull out the item */
3524 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003525 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3526
3527 /* make sure the item matches what we want */
3528 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3529 break;
David Sterba962a2982014-06-04 18:41:45 +02003530 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003531 break;
3532
3533 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003534 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003535
3536 /*
3537 * this is where we are basically btrfs_lookup, without the
3538 * crossing root thing. we store the inode number in the
3539 * offset of the orphan item.
3540 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003541
3542 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 btrfs_err(fs_info,
3544 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003545 ret = -EINVAL;
3546 goto out;
3547 }
3548
3549 last_objectid = found_key.offset;
3550
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003551 found_key.objectid = found_key.offset;
3552 found_key.type = BTRFS_INODE_ITEM_KEY;
3553 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003554 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303555 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003556 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003557 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003558
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003560 struct btrfs_root *dead_root;
3561 struct btrfs_fs_info *fs_info = root->fs_info;
3562 int is_dead_root = 0;
3563
3564 /*
3565 * this is an orphan in the tree root. Currently these
3566 * could come from 2 sources:
3567 * a) a snapshot deletion in progress
3568 * b) a free space cache inode
3569 * We need to distinguish those two, as the snapshot
3570 * orphan must not get deleted.
3571 * find_dead_roots already ran before us, so if this
3572 * is a snapshot deletion, we should find the root
3573 * in the dead_roots list
3574 */
3575 spin_lock(&fs_info->trans_lock);
3576 list_for_each_entry(dead_root, &fs_info->dead_roots,
3577 root_list) {
3578 if (dead_root->root_key.objectid ==
3579 found_key.objectid) {
3580 is_dead_root = 1;
3581 break;
3582 }
3583 }
3584 spin_unlock(&fs_info->trans_lock);
3585 if (is_dead_root) {
3586 /* prevent this orphan from being found again */
3587 key.offset = found_key.objectid - 1;
3588 continue;
3589 }
3590 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003591 /*
3592 * Inode is already gone but the orphan item is still there,
3593 * kill the orphan item.
3594 */
Filipe Manana67710892016-06-06 11:51:25 +01003595 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003596 trans = btrfs_start_transaction(root, 1);
3597 if (IS_ERR(trans)) {
3598 ret = PTR_ERR(trans);
3599 goto out;
3600 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003601 btrfs_debug(fs_info, "auto deleting %Lu",
3602 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003603 ret = btrfs_del_orphan_item(trans, root,
3604 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003605 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003606 if (ret)
3607 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003608 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003609 }
Josef Bacik7b128762008-07-24 12:17:14 -04003610
Josef Bacik7b128762008-07-24 12:17:14 -04003611 /*
3612 * add this inode to the orphan list so btrfs_orphan_del does
3613 * the proper thing when we hit it
3614 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003615 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3616 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003617 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003618
Josef Bacik7b128762008-07-24 12:17:14 -04003619 /* if we have links, this was a truncate, lets do that */
3620 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303621 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003622 iput(inode);
3623 continue;
3624 }
Josef Bacik7b128762008-07-24 12:17:14 -04003625 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003626
3627 /* 1 for the orphan item deletion. */
3628 trans = btrfs_start_transaction(root, 1);
3629 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003630 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003631 ret = PTR_ERR(trans);
3632 goto out;
3633 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003634 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003635 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003636 if (ret) {
3637 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003638 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003639 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003640
Filipe Manana213e8c52018-02-06 20:40:31 +00003641 ret = btrfs_truncate(inode, false);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003642 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003643 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003644 } else {
3645 nr_unlink++;
3646 }
3647
3648 /* this will do delete_inode and everything for us */
3649 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003650 if (ret)
3651 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003652 }
Miao Xie3254c872011-11-10 20:45:05 -05003653 /* release the path since we're done with it */
3654 btrfs_release_path(path);
3655
Yan, Zhengd68fc572010-05-16 10:49:58 -04003656 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3657
3658 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003659 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003660 (u64)-1);
3661
Miao Xie27cdeb72014-04-02 19:51:05 +08003662 if (root->orphan_block_rsv ||
3663 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003664 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003665 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003666 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003667 }
Josef Bacik7b128762008-07-24 12:17:14 -04003668
3669 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003670 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003671 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003672 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003673
3674out:
3675 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003676 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003677 btrfs_free_path(path);
3678 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003679}
3680
Chris Masond352ac62008-09-29 15:18:18 -04003681/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003682 * very simple check to peek ahead in the leaf looking for xattrs. If we
3683 * don't find any xattrs, we know there can't be any acls.
3684 *
3685 * slot is the slot the inode is in, objectid is the objectid of the inode
3686 */
3687static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003688 int slot, u64 objectid,
3689 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003690{
3691 u32 nritems = btrfs_header_nritems(leaf);
3692 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003693 static u64 xattr_access = 0;
3694 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003695 int scanned = 0;
3696
Josef Bacikf23b5a52013-06-19 10:16:26 -04003697 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003698 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3699 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3700 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3701 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003702 }
3703
Chris Mason46a53cc2009-04-27 11:47:50 -04003704 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003705 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003706 while (slot < nritems) {
3707 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3708
3709 /* we found a different objectid, there must not be acls */
3710 if (found_key.objectid != objectid)
3711 return 0;
3712
3713 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003714 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003715 if (*first_xattr_slot == -1)
3716 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003717 if (found_key.offset == xattr_access ||
3718 found_key.offset == xattr_default)
3719 return 1;
3720 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003721
3722 /*
3723 * we found a key greater than an xattr key, there can't
3724 * be any acls later on
3725 */
3726 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3727 return 0;
3728
3729 slot++;
3730 scanned++;
3731
3732 /*
3733 * it goes inode, inode backrefs, xattrs, extents,
3734 * so if there are a ton of hard links to an inode there can
3735 * be a lot of backrefs. Don't waste time searching too hard,
3736 * this is just an optimization
3737 */
3738 if (scanned >= 8)
3739 break;
3740 }
3741 /* we hit the end of the leaf before we found an xattr or
3742 * something larger than an xattr. We have to assume the inode
3743 * has acls
3744 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003745 if (*first_xattr_slot == -1)
3746 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003747 return 1;
3748}
3749
3750/*
Chris Masond352ac62008-09-29 15:18:18 -04003751 * read an inode from the btree into the in-memory inode
3752 */
Filipe Manana67710892016-06-06 11:51:25 +01003753static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003754{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003755 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003756 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003757 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003758 struct btrfs_inode_item *inode_item;
3759 struct btrfs_root *root = BTRFS_I(inode)->root;
3760 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003761 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003762 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003763 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003764 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003765 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003766 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003767
3768 ret = btrfs_fill_inode(inode, &rdev);
3769 if (!ret)
3770 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003771
3772 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003773 if (!path) {
3774 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003775 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003776 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003777
Chris Mason39279cc2007-06-12 06:35:45 -04003778 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003779
Chris Mason39279cc2007-06-12 06:35:45 -04003780 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003781 if (ret) {
3782 if (ret > 0)
3783 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003784 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003785 }
Chris Mason39279cc2007-06-12 06:35:45 -04003786
Chris Mason5f39d392007-10-15 16:14:19 -04003787 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003788
3789 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003790 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003791
Chris Mason5f39d392007-10-15 16:14:19 -04003792 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3793 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003794 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003795 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003796 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3797 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003798 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003799
David Sterbaa937b972014-12-12 17:39:12 +01003800 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3801 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003802
David Sterbaa937b972014-12-12 17:39:12 +01003803 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3804 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
David Sterbaa937b972014-12-12 17:39:12 +01003806 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3807 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003808
chandan r9cc97d62012-07-04 12:48:07 +05303809 BTRFS_I(inode)->i_otime.tv_sec =
3810 btrfs_timespec_sec(leaf, &inode_item->otime);
3811 BTRFS_I(inode)->i_otime.tv_nsec =
3812 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003814 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003815 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003816 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3817
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003818 inode_set_iversion_queried(inode,
3819 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003820 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003821 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003822 rdev = btrfs_inode_rdev(leaf, inode_item);
3823
Josef Bacikaec74772008-07-24 12:12:38 -04003824 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003825 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003826
3827cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003828 /*
3829 * If we were modified in the current generation and evicted from memory
3830 * and then re-read we need to do a full sync since we don't have any
3831 * idea about which extents were modified before we were evicted from
3832 * cache.
3833 *
3834 * This is required for both inode re-read from disk and delayed inode
3835 * in delayed_nodes_tree.
3836 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003837 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003838 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3839 &BTRFS_I(inode)->runtime_flags);
3840
Filipe Mananabde6c242015-07-24 00:00:19 +01003841 /*
3842 * We don't persist the id of the transaction where an unlink operation
3843 * against the inode was last made. So here we assume the inode might
3844 * have been evicted, and therefore the exact value of last_unlink_trans
3845 * lost, and set it to last_trans to avoid metadata inconsistencies
3846 * between the inode and its parent if the inode is fsync'ed and the log
3847 * replayed. For example, in the scenario:
3848 *
3849 * touch mydir/foo
3850 * ln mydir/foo mydir/bar
3851 * sync
3852 * unlink mydir/bar
3853 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3854 * xfs_io -c fsync mydir/foo
3855 * <power failure>
3856 * mount fs, triggers fsync log replay
3857 *
3858 * We must make sure that when we fsync our inode foo we also log its
3859 * parent inode, otherwise after log replay the parent still has the
3860 * dentry with the "bar" name but our inode foo has a link count of 1
3861 * and doesn't have an inode ref with the name "bar" anymore.
3862 *
3863 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003864 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003865 * transaction commits on fsync if our inode is a directory, or if our
3866 * inode is not a directory, logging its parent unnecessarily.
3867 */
3868 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3869
Miao Xie67de1172013-12-26 13:07:06 +08003870 path->slots[0]++;
3871 if (inode->i_nlink != 1 ||
3872 path->slots[0] >= btrfs_header_nritems(leaf))
3873 goto cache_acl;
3874
3875 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003876 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003877 goto cache_acl;
3878
3879 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3880 if (location.type == BTRFS_INODE_REF_KEY) {
3881 struct btrfs_inode_ref *ref;
3882
3883 ref = (struct btrfs_inode_ref *)ptr;
3884 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3885 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3886 struct btrfs_inode_extref *extref;
3887
3888 extref = (struct btrfs_inode_extref *)ptr;
3889 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3890 extref);
3891 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003892cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003893 /*
3894 * try to precache a NULL acl entry for files that don't have
3895 * any xattrs or acls
3896 */
Li Zefan33345d012011-04-20 10:31:50 +08003897 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003898 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003899 if (first_xattr_slot != -1) {
3900 path->slots[0] = first_xattr_slot;
3901 ret = btrfs_load_inode_props(inode, path);
3902 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003903 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003904 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003905 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003906 root->root_key.objectid, ret);
3907 }
3908 btrfs_free_path(path);
3909
Al Viro72c04902009-06-24 16:58:48 -04003910 if (!maybe_acls)
3911 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003912
Chris Mason39279cc2007-06-12 06:35:45 -04003913 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003914 case S_IFREG:
3915 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003916 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003917 inode->i_fop = &btrfs_file_operations;
3918 inode->i_op = &btrfs_file_inode_operations;
3919 break;
3920 case S_IFDIR:
3921 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003922 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003923 break;
3924 case S_IFLNK:
3925 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003926 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003927 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3928 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003929 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003930 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003931 init_special_inode(inode, inode->i_mode, rdev);
3932 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003933 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003934
3935 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003936 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003937
3938make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003939 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003940 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003941 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003942}
3943
Chris Masond352ac62008-09-29 15:18:18 -04003944/*
3945 * given a leaf and an inode, copy the inode fields into the leaf
3946 */
Chris Masone02119d2008-09-05 16:13:11 -04003947static void fill_inode_item(struct btrfs_trans_handle *trans,
3948 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003949 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003950 struct inode *inode)
3951{
Liu Bo51fab692012-12-27 09:01:21 +00003952 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003953
Liu Bo51fab692012-12-27 09:01:21 +00003954 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003955
Liu Bo51fab692012-12-27 09:01:21 +00003956 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3957 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3958 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3959 &token);
3960 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3961 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003962
David Sterbaa937b972014-12-12 17:39:12 +01003963 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003964 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003965 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003966 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003967
David Sterbaa937b972014-12-12 17:39:12 +01003968 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003969 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003970 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003971 inode->i_mtime.tv_nsec, &token);
3972
David Sterbaa937b972014-12-12 17:39:12 +01003973 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003974 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003975 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003976 inode->i_ctime.tv_nsec, &token);
3977
chandan r9cc97d62012-07-04 12:48:07 +05303978 btrfs_set_token_timespec_sec(leaf, &item->otime,
3979 BTRFS_I(inode)->i_otime.tv_sec, &token);
3980 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3981 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3982
Liu Bo51fab692012-12-27 09:01:21 +00003983 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3984 &token);
3985 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3986 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003987 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3988 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003989 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3990 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3991 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3992 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003993}
3994
Chris Masond352ac62008-09-29 15:18:18 -04003995/*
3996 * copy everything in the in-memory inode into the btree.
3997 */
Chris Mason21151332011-11-10 20:39:08 -05003998static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003999 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004000{
4001 struct btrfs_inode_item *inode_item;
4002 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004003 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004004 int ret;
4005
4006 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004007 if (!path)
4008 return -ENOMEM;
4009
Chris Masonb9473432009-03-13 11:00:37 -04004010 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004011 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4012 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004013 if (ret) {
4014 if (ret > 0)
4015 ret = -ENOENT;
4016 goto failed;
4017 }
4018
Chris Mason5f39d392007-10-15 16:14:19 -04004019 leaf = path->nodes[0];
4020 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004021 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004022
Chris Masone02119d2008-09-05 16:13:11 -04004023 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004024 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004025 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004026 ret = 0;
4027failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004028 btrfs_free_path(path);
4029 return ret;
4030}
4031
Chris Masond352ac62008-09-29 15:18:18 -04004032/*
Chris Mason21151332011-11-10 20:39:08 -05004033 * copy everything in the in-memory inode into the btree.
4034 */
4035noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4036 struct btrfs_root *root, struct inode *inode)
4037{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004038 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004039 int ret;
4040
4041 /*
4042 * If the inode is a free space inode, we can deadlock during commit
4043 * if we put it into the delayed code.
4044 *
4045 * The data relocation inode should also be directly updated
4046 * without delay
4047 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004048 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004049 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004050 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004051 btrfs_update_root_times(trans, root);
4052
Chris Mason21151332011-11-10 20:39:08 -05004053 ret = btrfs_delayed_update_inode(trans, root, inode);
4054 if (!ret)
4055 btrfs_set_inode_last_trans(trans, inode);
4056 return ret;
4057 }
4058
4059 return btrfs_update_inode_item(trans, root, inode);
4060}
4061
Josef Bacikbe6aef62012-10-22 15:43:12 -04004062noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4063 struct btrfs_root *root,
4064 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004065{
4066 int ret;
4067
4068 ret = btrfs_update_inode(trans, root, inode);
4069 if (ret == -ENOSPC)
4070 return btrfs_update_inode_item(trans, root, inode);
4071 return ret;
4072}
4073
4074/*
Chris Masond352ac62008-09-29 15:18:18 -04004075 * unlink helper that gets used here in inode.c and in the tree logging
4076 * recovery code. It remove a link in a directory with a given name, and
4077 * also drops the back refs in the inode to the directory
4078 */
Al Viro92986792011-03-04 17:14:37 +00004079static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4080 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004081 struct btrfs_inode *dir,
4082 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004083 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004084{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004085 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004086 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004088 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004089 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004090 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004091 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004092 u64 ino = btrfs_ino(inode);
4093 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004094
4095 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004096 if (!path) {
4097 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004098 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004099 }
4100
Chris Masonb9473432009-03-13 11:00:37 -04004101 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004102 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004103 name, name_len, -1);
4104 if (IS_ERR(di)) {
4105 ret = PTR_ERR(di);
4106 goto err;
4107 }
4108 if (!di) {
4109 ret = -ENOENT;
4110 goto err;
4111 }
Chris Mason5f39d392007-10-15 16:14:19 -04004112 leaf = path->nodes[0];
4113 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004114 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004115 if (ret)
4116 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004117 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004118
Miao Xie67de1172013-12-26 13:07:06 +08004119 /*
4120 * If we don't have dir index, we have to get it by looking up
4121 * the inode ref, since we get the inode ref, remove it directly,
4122 * it is unnecessary to do delayed deletion.
4123 *
4124 * But if we have dir index, needn't search inode ref to get it.
4125 * Since the inode ref is close to the inode item, it is better
4126 * that we delay to delete it, and just do this deletion when
4127 * we update the inode item.
4128 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004129 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004130 ret = btrfs_delayed_delete_inode_ref(inode);
4131 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004132 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004133 goto skip_backref;
4134 }
4135 }
4136
Li Zefan33345d012011-04-20 10:31:50 +08004137 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4138 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004139 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004140 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004141 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004142 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004143 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004144 goto err;
4145 }
Miao Xie67de1172013-12-26 13:07:06 +08004146skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004147 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004149 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004150 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004151 }
Chris Mason39279cc2007-06-12 06:35:45 -04004152
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004153 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4154 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004156 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004157 goto err;
4158 }
Chris Masone02119d2008-09-05 16:13:11 -04004159
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004160 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4161 index);
Chris Mason6418c962010-10-30 07:34:24 -04004162 if (ret == -ENOENT)
4163 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004164 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004165 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004166err:
4167 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004168 if (ret)
4169 goto out;
4170
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004171 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004172 inode_inc_iversion(&inode->vfs_inode);
4173 inode_inc_iversion(&dir->vfs_inode);
4174 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4175 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4176 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004177out:
Chris Mason39279cc2007-06-12 06:35:45 -04004178 return ret;
4179}
4180
Al Viro92986792011-03-04 17:14:37 +00004181int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4182 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004183 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004184 const char *name, int name_len)
4185{
4186 int ret;
4187 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4188 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004189 drop_nlink(&inode->vfs_inode);
4190 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004191 }
4192 return ret;
4193}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004194
4195/*
4196 * helper to start transaction for unlink and rmdir.
4197 *
Josef Bacikd52be812013-05-29 14:54:47 -04004198 * unlink and rmdir are special in btrfs, they do not always free space, so
4199 * if we cannot make our reservations the normal way try and see if there is
4200 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4201 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004202 */
Josef Bacikd52be812013-05-29 14:54:47 -04004203static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004205 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206
Josef Bacike70bea52011-10-11 14:18:24 -04004207 /*
4208 * 1 for the possible orphan item
4209 * 1 for the dir item
4210 * 1 for the dir index
4211 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004212 * 1 for the inode
4213 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004214 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004215}
4216
Chris Mason39279cc2007-06-12 06:35:45 -04004217static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4218{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004219 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004220 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004221 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004222 int ret;
4223
Josef Bacikd52be812013-05-29 14:54:47 -04004224 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004225 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004226 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004227
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004228 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4229 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004230
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004231 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4232 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4233 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004234 if (ret)
4235 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004236
Yan, Zhenga22285a2010-05-16 10:48:46 -04004237 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004238 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004239 if (ret)
4240 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004241 }
Josef Bacik7b128762008-07-24 12:17:14 -04004242
Tsutomu Itohb5324022011-07-19 07:27:20 +00004243out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004244 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004245 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004246 return ret;
4247}
4248
Misono Tomohirof60a2362018-04-18 11:34:52 +09004249static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250 struct btrfs_root *root,
4251 struct inode *dir, u64 objectid,
4252 const char *name, int name_len)
4253{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004254 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255 struct btrfs_path *path;
4256 struct extent_buffer *leaf;
4257 struct btrfs_dir_item *di;
4258 struct btrfs_key key;
4259 u64 index;
4260 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004261 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004262
4263 path = btrfs_alloc_path();
4264 if (!path)
4265 return -ENOMEM;
4266
Li Zefan33345d012011-04-20 10:31:50 +08004267 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004269 if (IS_ERR_OR_NULL(di)) {
4270 if (!di)
4271 ret = -ENOENT;
4272 else
4273 ret = PTR_ERR(di);
4274 goto out;
4275 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004276
4277 leaf = path->nodes[0];
4278 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4279 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4280 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004281 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004282 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004283 goto out;
4284 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004285 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004286
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004287 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4288 root->root_key.objectid, dir_ino,
4289 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004291 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004292 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004293 goto out;
4294 }
Li Zefan33345d012011-04-20 10:31:50 +08004295 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004296 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 if (IS_ERR_OR_NULL(di)) {
4298 if (!di)
4299 ret = -ENOENT;
4300 else
4301 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004302 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303 goto out;
4304 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004305
4306 leaf = path->nodes[0];
4307 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004308 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004309 index = key.offset;
4310 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004311 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004312
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004313 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004314 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004315 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004316 goto out;
4317 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004318
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004319 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004320 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004321 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004322 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004323 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004324 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004325out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004326 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004327 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004328}
4329
Misono Tomohiroec42f162018-04-18 11:34:13 +09004330/*
4331 * Helper to check if the subvolume references other subvolumes or if it's
4332 * default.
4333 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004334static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004335{
4336 struct btrfs_fs_info *fs_info = root->fs_info;
4337 struct btrfs_path *path;
4338 struct btrfs_dir_item *di;
4339 struct btrfs_key key;
4340 u64 dir_id;
4341 int ret;
4342
4343 path = btrfs_alloc_path();
4344 if (!path)
4345 return -ENOMEM;
4346
4347 /* Make sure this root isn't set as the default subvol */
4348 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4349 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4350 dir_id, "default", 7, 0);
4351 if (di && !IS_ERR(di)) {
4352 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4353 if (key.objectid == root->root_key.objectid) {
4354 ret = -EPERM;
4355 btrfs_err(fs_info,
4356 "deleting default subvolume %llu is not allowed",
4357 key.objectid);
4358 goto out;
4359 }
4360 btrfs_release_path(path);
4361 }
4362
4363 key.objectid = root->root_key.objectid;
4364 key.type = BTRFS_ROOT_REF_KEY;
4365 key.offset = (u64)-1;
4366
4367 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4368 if (ret < 0)
4369 goto out;
4370 BUG_ON(ret == 0);
4371
4372 ret = 0;
4373 if (path->slots[0] > 0) {
4374 path->slots[0]--;
4375 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4376 if (key.objectid == root->root_key.objectid &&
4377 key.type == BTRFS_ROOT_REF_KEY)
4378 ret = -ENOTEMPTY;
4379 }
4380out:
4381 btrfs_free_path(path);
4382 return ret;
4383}
4384
Misono Tomohirof60a2362018-04-18 11:34:52 +09004385int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4386{
4387 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4388 struct btrfs_root *root = BTRFS_I(dir)->root;
4389 struct inode *inode = d_inode(dentry);
4390 struct btrfs_root *dest = BTRFS_I(inode)->root;
4391 struct btrfs_trans_handle *trans;
4392 struct btrfs_block_rsv block_rsv;
4393 u64 root_flags;
4394 u64 qgroup_reserved;
4395 int ret;
4396 int err;
4397
4398 /*
4399 * Don't allow to delete a subvolume with send in progress. This is
4400 * inside the inode lock so the error handling that has to drop the bit
4401 * again is not run concurrently.
4402 */
4403 spin_lock(&dest->root_item_lock);
4404 root_flags = btrfs_root_flags(&dest->root_item);
4405 if (dest->send_in_progress == 0) {
4406 btrfs_set_root_flags(&dest->root_item,
4407 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4408 spin_unlock(&dest->root_item_lock);
4409 } else {
4410 spin_unlock(&dest->root_item_lock);
4411 btrfs_warn(fs_info,
4412 "attempt to delete subvolume %llu during send",
4413 dest->root_key.objectid);
4414 return -EPERM;
4415 }
4416
4417 down_write(&fs_info->subvol_sem);
4418
4419 err = may_destroy_subvol(dest);
4420 if (err)
4421 goto out_up_write;
4422
4423 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4424 /*
4425 * One for dir inode,
4426 * two for dir entries,
4427 * two for root ref/backref.
4428 */
4429 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
4430 5, &qgroup_reserved, true);
4431 if (err)
4432 goto out_up_write;
4433
4434 trans = btrfs_start_transaction(root, 0);
4435 if (IS_ERR(trans)) {
4436 err = PTR_ERR(trans);
4437 goto out_release;
4438 }
4439 trans->block_rsv = &block_rsv;
4440 trans->bytes_reserved = block_rsv.size;
4441
4442 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4443
4444 ret = btrfs_unlink_subvol(trans, root, dir,
4445 dest->root_key.objectid,
4446 dentry->d_name.name,
4447 dentry->d_name.len);
4448 if (ret) {
4449 err = ret;
4450 btrfs_abort_transaction(trans, ret);
4451 goto out_end_trans;
4452 }
4453
4454 btrfs_record_root_in_trans(trans, dest);
4455
4456 memset(&dest->root_item.drop_progress, 0,
4457 sizeof(dest->root_item.drop_progress));
4458 dest->root_item.drop_level = 0;
4459 btrfs_set_root_refs(&dest->root_item, 0);
4460
4461 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4462 ret = btrfs_insert_orphan_item(trans,
4463 fs_info->tree_root,
4464 dest->root_key.objectid);
4465 if (ret) {
4466 btrfs_abort_transaction(trans, ret);
4467 err = ret;
4468 goto out_end_trans;
4469 }
4470 }
4471
4472 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
4473 BTRFS_UUID_KEY_SUBVOL,
4474 dest->root_key.objectid);
4475 if (ret && ret != -ENOENT) {
4476 btrfs_abort_transaction(trans, ret);
4477 err = ret;
4478 goto out_end_trans;
4479 }
4480 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4481 ret = btrfs_uuid_tree_rem(trans, fs_info,
4482 dest->root_item.received_uuid,
4483 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4484 dest->root_key.objectid);
4485 if (ret && ret != -ENOENT) {
4486 btrfs_abort_transaction(trans, ret);
4487 err = ret;
4488 goto out_end_trans;
4489 }
4490 }
4491
4492out_end_trans:
4493 trans->block_rsv = NULL;
4494 trans->bytes_reserved = 0;
4495 ret = btrfs_end_transaction(trans);
4496 if (ret && !err)
4497 err = ret;
4498 inode->i_flags |= S_DEAD;
4499out_release:
4500 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4501out_up_write:
4502 up_write(&fs_info->subvol_sem);
4503 if (err) {
4504 spin_lock(&dest->root_item_lock);
4505 root_flags = btrfs_root_flags(&dest->root_item);
4506 btrfs_set_root_flags(&dest->root_item,
4507 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4508 spin_unlock(&dest->root_item_lock);
4509 } else {
4510 d_invalidate(dentry);
4511 btrfs_invalidate_inodes(dest);
4512 ASSERT(dest->send_in_progress == 0);
4513
4514 /* the last ref */
4515 if (dest->ino_cache_inode) {
4516 iput(dest->ino_cache_inode);
4517 dest->ino_cache_inode = NULL;
4518 }
4519 }
4520
4521 return err;
4522}
4523
Chris Mason39279cc2007-06-12 06:35:45 -04004524static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4525{
David Howells2b0143b2015-03-17 22:25:59 +00004526 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004527 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004528 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004529 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004530 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004531
David Sterbab3ae2442012-09-13 16:04:34 -06004532 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004533 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004534 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004535 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004536
Josef Bacikd52be812013-05-29 14:54:47 -04004537 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004538 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004539 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004540
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004541 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004542 err = btrfs_unlink_subvol(trans, root, dir,
4543 BTRFS_I(inode)->location.objectid,
4544 dentry->d_name.name,
4545 dentry->d_name.len);
4546 goto out;
4547 }
4548
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004549 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004550 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004551 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004552
Filipe Manana44f714d2016-06-06 16:11:13 +01004553 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4554
Chris Mason39279cc2007-06-12 06:35:45 -04004555 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004556 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4557 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4558 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004559 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004560 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004561 /*
4562 * Propagate the last_unlink_trans value of the deleted dir to
4563 * its parent directory. This is to prevent an unrecoverable
4564 * log tree in the case we do something like this:
4565 * 1) create dir foo
4566 * 2) create snapshot under dir foo
4567 * 3) delete the snapshot
4568 * 4) rmdir foo
4569 * 5) mkdir foo
4570 * 6) fsync foo or some file inside foo
4571 */
4572 if (last_unlink_trans >= trans->transid)
4573 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4574 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004575out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004576 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004577 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004578
Chris Mason39279cc2007-06-12 06:35:45 -04004579 return err;
4580}
4581
Chris Mason28f75a02015-02-04 06:59:29 -08004582static int truncate_space_check(struct btrfs_trans_handle *trans,
4583 struct btrfs_root *root,
4584 u64 bytes_deleted)
4585{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004586 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004587 int ret;
4588
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004589 /*
4590 * This is only used to apply pressure to the enospc system, we don't
4591 * intend to use this reservation at all.
4592 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004593 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004594 bytes_deleted *= fs_info->nodesize;
4595 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004596 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004597 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004598 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004599 trans->transid,
4600 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004601 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004602 }
Chris Mason28f75a02015-02-04 06:59:29 -08004603 return ret;
4604
4605}
4606
Josef Bacikddfae632017-10-19 14:16:02 -04004607/*
4608 * Return this if we need to call truncate_block for the last bit of the
4609 * truncate.
4610 */
4611#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004612
Chris Mason323ac952008-10-01 19:05:46 -04004613/*
Chris Mason39279cc2007-06-12 06:35:45 -04004614 * this can truncate away extent items, csum items and directory items.
4615 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004616 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004617 *
4618 * csum items that cross the new i_size are truncated to the new size
4619 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004620 *
4621 * min_type is the minimum key type to truncate down to. If set to 0, this
4622 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004623 */
Yan, Zheng80825102009-11-12 09:35:36 +00004624int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4625 struct btrfs_root *root,
4626 struct inode *inode,
4627 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004628{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004629 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004630 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004631 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004632 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004633 struct btrfs_key key;
4634 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004635 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004636 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004637 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004638 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004639 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004640 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004641 int found_extent;
4642 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004643 int pending_del_nr = 0;
4644 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004645 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004646 int ret;
4647 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004648 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004649 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004650 bool be_nice = false;
4651 bool should_throttle = false;
4652 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004653
4654 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004655
Chris Mason28ed1342014-12-17 09:41:04 -08004656 /*
4657 * for non-free space inodes and ref cows, we want to back off from
4658 * time to time
4659 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004660 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004661 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004662 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004663
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004664 path = btrfs_alloc_path();
4665 if (!path)
4666 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004667 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004668
Josef Bacik5dc562c2012-08-17 13:14:17 -04004669 /*
4670 * We want to drop from the next block forward in case this new size is
4671 * not block aligned since we will be keeping the last block of the
4672 * extent just the way it is.
4673 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004674 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004675 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004676 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004677 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004678 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004679
Miao Xie16cdcec2011-04-22 18:12:22 +08004680 /*
4681 * This function is also used to drop the items in the log tree before
4682 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4683 * it is used to drop the loged items. So we shouldn't kill the delayed
4684 * items.
4685 */
4686 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004687 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004688
Li Zefan33345d012011-04-20 10:31:50 +08004689 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004690 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004691 key.type = (u8)-1;
4692
Chris Mason85e21ba2008-01-29 15:11:36 -05004693search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004694 /*
4695 * with a 16K leaf size and 128MB extents, you can actually queue
4696 * up a huge file in a single leaf. Most of the time that
4697 * bytes_deleted is > 0, it will be huge by the time we get here
4698 */
Byongho Leeee221842015-12-15 01:42:10 +09004699 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004700 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004701 err = -EAGAIN;
4702 goto error;
4703 }
4704 }
4705
4706
Chris Masonb9473432009-03-13 11:00:37 -04004707 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004708 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004709 if (ret < 0) {
4710 err = ret;
4711 goto out;
4712 }
Chris Masond3977122009-01-05 21:25:51 -05004713
Chris Mason85e21ba2008-01-29 15:11:36 -05004714 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004715 /* there are no items in the tree for us to truncate, we're
4716 * done
4717 */
Yan, Zheng80825102009-11-12 09:35:36 +00004718 if (path->slots[0] == 0)
4719 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004720 path->slots[0]--;
4721 }
4722
Chris Masond3977122009-01-05 21:25:51 -05004723 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004724 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004725 leaf = path->nodes[0];
4726 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004727 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004728
Li Zefan33345d012011-04-20 10:31:50 +08004729 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004730 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004731
Chris Mason85e21ba2008-01-29 15:11:36 -05004732 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004733 break;
4734
Chris Mason5f39d392007-10-15 16:14:19 -04004735 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004736 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004737 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004738 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004739 extent_type = btrfs_file_extent_type(leaf, fi);
4740 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004741 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004742 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004743
4744 trace_btrfs_truncate_show_fi_regular(
4745 BTRFS_I(inode), leaf, fi,
4746 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004747 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004748 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004749 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004750
4751 trace_btrfs_truncate_show_fi_inline(
4752 BTRFS_I(inode), leaf, fi, path->slots[0],
4753 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004754 }
Yan008630c2007-11-07 13:31:09 -05004755 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004756 }
Yan, Zheng80825102009-11-12 09:35:36 +00004757 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004758 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004759 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004760 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004761 break;
4762 if (found_key.offset >= new_size)
4763 del_item = 1;
4764 else
4765 del_item = 0;
4766 }
Chris Mason39279cc2007-06-12 06:35:45 -04004767 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004768 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004769 if (found_type != BTRFS_EXTENT_DATA_KEY)
4770 goto delete;
4771
4772 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004773 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004774 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004775 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004776 u64 orig_num_bytes =
4777 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004778 extent_num_bytes = ALIGN(new_size -
4779 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004781 btrfs_set_file_extent_num_bytes(leaf, fi,
4782 extent_num_bytes);
4783 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004784 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004785 if (test_bit(BTRFS_ROOT_REF_COWS,
4786 &root->state) &&
4787 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004788 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004789 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004790 } else {
Chris Masondb945352007-10-15 16:15:53 -04004791 extent_num_bytes =
4792 btrfs_file_extent_disk_num_bytes(leaf,
4793 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004794 extent_offset = found_key.offset -
4795 btrfs_file_extent_offset(leaf, fi);
4796
Chris Mason39279cc2007-06-12 06:35:45 -04004797 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004798 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004799 if (extent_start != 0) {
4800 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004801 if (test_bit(BTRFS_ROOT_REF_COWS,
4802 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004803 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004804 }
4805 }
Chris Mason90692182008-02-08 13:49:28 -05004806 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004807 /*
4808 * we can't truncate inline items that have had
4809 * special encodings
4810 */
4811 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004812 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004813 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4814 btrfs_file_extent_compression(leaf, fi) == 0) {
4815 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004816
Josef Bacikddfae632017-10-19 14:16:02 -04004817 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4818 size = btrfs_file_extent_calc_inline_size(size);
4819 btrfs_truncate_item(root->fs_info, path, size, 1);
4820 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004821 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004822 * We have to bail so the last_size is set to
4823 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004824 */
Josef Bacikddfae632017-10-19 14:16:02 -04004825 err = NEED_TRUNCATE_BLOCK;
4826 break;
Chris Mason90692182008-02-08 13:49:28 -05004827 }
Josef Bacikddfae632017-10-19 14:16:02 -04004828
4829 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4830 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004831 }
Chris Mason179e29e2007-11-01 11:28:41 -04004832delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004833 if (del_item)
4834 last_size = found_key.offset;
4835 else
4836 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004837 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004838 if (!pending_del_nr) {
4839 /* no pending yet, add ourselves */
4840 pending_del_slot = path->slots[0];
4841 pending_del_nr = 1;
4842 } else if (pending_del_nr &&
4843 path->slots[0] + 1 == pending_del_slot) {
4844 /* hop on the pending chunk */
4845 pending_del_nr++;
4846 pending_del_slot = path->slots[0];
4847 } else {
Chris Masond3977122009-01-05 21:25:51 -05004848 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004849 }
Chris Mason39279cc2007-06-12 06:35:45 -04004850 } else {
4851 break;
4852 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004853 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004854
Miao Xie27cdeb72014-04-02 19:51:05 +08004855 if (found_extent &&
4856 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004858 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004859 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004860 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004861 extent_num_bytes, 0,
4862 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004863 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004864 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004865 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4866 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004867 trans->delayed_ref_updates * 2,
4868 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004869 if (be_nice) {
4870 if (truncate_space_check(trans, root,
4871 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004872 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004873 }
4874 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004875 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004876 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004877 }
Chris Mason39279cc2007-06-12 06:35:45 -04004878 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004879
Yan, Zheng80825102009-11-12 09:35:36 +00004880 if (found_type == BTRFS_INODE_ITEM_KEY)
4881 break;
4882
4883 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004884 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004885 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004886 if (pending_del_nr) {
4887 ret = btrfs_del_items(trans, root, path,
4888 pending_del_slot,
4889 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004890 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004891 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004892 goto error;
4893 }
Yan, Zheng80825102009-11-12 09:35:36 +00004894 pending_del_nr = 0;
4895 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004896 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004897 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004898 unsigned long updates = trans->delayed_ref_updates;
4899 if (updates) {
4900 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004901 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004902 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004903 if (ret && !err)
4904 err = ret;
4905 }
4906 }
Chris Mason28f75a02015-02-04 06:59:29 -08004907 /*
4908 * if we failed to refill our space rsv, bail out
4909 * and let the transaction restart
4910 */
4911 if (should_end) {
4912 err = -EAGAIN;
4913 goto error;
4914 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004915 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004916 } else {
4917 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004918 }
Chris Mason39279cc2007-06-12 06:35:45 -04004919 }
Yan, Zheng80825102009-11-12 09:35:36 +00004920out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004921 if (pending_del_nr) {
4922 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4923 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004924 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004925 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004926 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004927error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004928 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4929 ASSERT(last_size >= new_size);
4930 if (!err && last_size > new_size)
4931 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004932 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004933 }
Chris Mason28ed1342014-12-17 09:41:04 -08004934
Chris Mason39279cc2007-06-12 06:35:45 -04004935 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004936
Byongho Leeee221842015-12-15 01:42:10 +09004937 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004938 unsigned long updates = trans->delayed_ref_updates;
4939 if (updates) {
4940 trans->delayed_ref_updates = 0;
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02004941 ret = btrfs_run_delayed_refs(trans, updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004942 if (ret && !err)
4943 err = ret;
4944 }
4945 }
Yan, Zheng80825102009-11-12 09:35:36 +00004946 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004947}
4948
Chris Masona52d9a82007-08-27 16:49:44 -04004949/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304950 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004951 * @inode - inode that we're zeroing
4952 * @from - the offset to start zeroing
4953 * @len - the length to zero, 0 to zero the entire range respective to the
4954 * offset
4955 * @front - zero up to the offset instead of from the offset on
4956 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304957 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004958 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004959 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304960int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004961 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004962{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004963 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004964 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4966 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004967 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004968 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004969 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004970 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004971 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304972 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004973 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004974 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004975 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304976 u64 block_start;
4977 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004978
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004979 if (IS_ALIGNED(offset, blocksize) &&
4980 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004981 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304982
Josef Bacik8b62f872017-10-19 14:15:55 -04004983 block_start = round_down(from, blocksize);
4984 block_end = block_start + blocksize - 1;
4985
Qu Wenruo364ecf32017-02-27 15:10:38 +08004986 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004987 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004988 if (ret)
4989 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004990
Chris Mason211c17f2008-05-15 09:13:45 -04004991again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004992 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004993 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004994 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004995 block_start, blocksize, true);
4996 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004997 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004998 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004999 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005000
Chris Masona52d9a82007-08-27 16:49:44 -04005001 if (!PageUptodate(page)) {
5002 ret = btrfs_readpage(NULL, page);
5003 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005004 if (page->mapping != mapping) {
5005 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005006 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005007 goto again;
5008 }
Chris Masona52d9a82007-08-27 16:49:44 -04005009 if (!PageUptodate(page)) {
5010 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005011 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005012 }
5013 }
Chris Mason211c17f2008-05-15 09:13:45 -04005014 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005015
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305016 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005017 set_page_extent_mapped(page);
5018
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305019 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005020 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305021 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005022 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005023 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005024 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005025 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005026 btrfs_put_ordered_extent(ordered);
5027 goto again;
5028 }
5029
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305030 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06005031 EXTENT_DIRTY | EXTENT_DELALLOC |
5032 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01005033 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005034
Filipe Mananae3b8a482017-11-04 00:16:59 +00005035 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08005036 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005037 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305038 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005039 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005040 goto out_unlock;
5041 }
5042
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305043 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005044 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305045 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005046 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005047 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305048 memset(kaddr + (block_start - page_offset(page)),
5049 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005050 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305051 memset(kaddr + (block_start - page_offset(page)) + offset,
5052 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005053 flush_dcache_page(page);
5054 kunmap(page);
5055 }
Chris Mason247e7432008-07-17 12:53:51 -04005056 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005057 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005058 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005059
Chris Mason89642222008-07-24 09:41:53 -04005060out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005061 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005062 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005063 blocksize, true);
5064 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04005065 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005066 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005067out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005068 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005069 return ret;
5070}
5071
Josef Bacik16e75492013-10-22 12:18:51 -04005072static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5073 u64 offset, u64 len)
5074{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005075 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005076 struct btrfs_trans_handle *trans;
5077 int ret;
5078
5079 /*
5080 * Still need to make sure the inode looks like it's been updated so
5081 * that any holes get logged if we fsync.
5082 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005083 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5084 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005085 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5086 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5087 return 0;
5088 }
5089
5090 /*
5091 * 1 - for the one we're dropping
5092 * 1 - for the one we're adding
5093 * 1 - for updating the inode.
5094 */
5095 trans = btrfs_start_transaction(root, 3);
5096 if (IS_ERR(trans))
5097 return PTR_ERR(trans);
5098
5099 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5100 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005101 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005102 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005103 return ret;
5104 }
5105
David Sterbaf85b7372017-01-20 14:54:07 +01005106 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5107 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005108 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005109 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005110 else
5111 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005112 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005113 return ret;
5114}
5115
Josef Bacik695a0d02011-03-04 15:46:53 -05005116/*
5117 * This function puts in dummy file extents for the area we're creating a hole
5118 * for. So if we are truncating this file to a larger size we need to insert
5119 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5120 * the range between oldsize and size
5121 */
Josef Bacika41ad392011-01-31 15:30:16 -05005122int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005124 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005125 struct btrfs_root *root = BTRFS_I(inode)->root;
5126 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005127 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005128 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005129 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005130 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5131 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005132 u64 last_byte;
5133 u64 cur_offset;
5134 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005135 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005136
Josef Bacika71754f2013-06-17 17:14:39 -04005137 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305138 * If our size started in the middle of a block we need to zero out the
5139 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005140 * expose stale data.
5141 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305142 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005143 if (err)
5144 return err;
5145
Yan Zheng9036c102008-10-30 14:19:41 -04005146 if (size <= hole_start)
5147 return 0;
5148
Yan Zheng9036c102008-10-30 14:19:41 -04005149 while (1) {
5150 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005151
David Sterbaff13db42015-12-03 14:30:40 +01005152 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005153 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005154 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005155 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005156 if (!ordered)
5157 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005158 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005159 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005160 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005161 btrfs_put_ordered_extent(ordered);
5162 }
5163
Yan Zheng9036c102008-10-30 14:19:41 -04005164 cur_offset = hole_start;
5165 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02005166 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005167 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005168 if (IS_ERR(em)) {
5169 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005170 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005171 break;
5172 }
Yan Zheng9036c102008-10-30 14:19:41 -04005173 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005174 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005175 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005176 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005177 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005178
Josef Bacik16e75492013-10-22 12:18:51 -04005179 err = maybe_insert_hole(root, inode, cur_offset,
5180 hole_size);
5181 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005182 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005183 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005184 cur_offset + hole_size - 1, 0);
5185 hole_em = alloc_extent_map();
5186 if (!hole_em) {
5187 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5188 &BTRFS_I(inode)->runtime_flags);
5189 goto next;
5190 }
5191 hole_em->start = cur_offset;
5192 hole_em->len = hole_size;
5193 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005194
Josef Bacik5dc562c2012-08-17 13:14:17 -04005195 hole_em->block_start = EXTENT_MAP_HOLE;
5196 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005197 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005198 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005199 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005200 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005201 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005202
5203 while (1) {
5204 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005205 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005206 write_unlock(&em_tree->lock);
5207 if (err != -EEXIST)
5208 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005209 btrfs_drop_extent_cache(BTRFS_I(inode),
5210 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005211 cur_offset +
5212 hole_size - 1, 0);
5213 }
5214 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005215 }
Josef Bacik16e75492013-10-22 12:18:51 -04005216next:
Yan Zheng9036c102008-10-30 14:19:41 -04005217 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005218 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005219 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005220 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005221 break;
5222 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005223 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005224 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005225 return err;
5226}
5227
Eric Sandeen3972f262013-01-12 02:57:22 +00005228static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005229{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005230 struct btrfs_root *root = BTRFS_I(inode)->root;
5231 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005232 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005233 loff_t newsize = attr->ia_size;
5234 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005235 int ret;
5236
Eric Sandeen3972f262013-01-12 02:57:22 +00005237 /*
5238 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5239 * special case where we need to update the times despite not having
5240 * these flags set. For all other operations the VFS set these flags
5241 * explicitly if it wants a timestamp update.
5242 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005243 if (newsize != oldsize) {
5244 inode_inc_iversion(inode);
5245 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5246 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005247 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005248 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005249
Josef Bacika41ad392011-01-31 15:30:16 -05005250 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005251 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005252 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005253 * This is to ensure the snapshot captures a fully consistent
5254 * state of this file - if the snapshot captures this expanding
5255 * truncation, it must capture all writes that happened before
5256 * this truncation.
5257 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005258 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005259 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005260 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005261 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005262 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005263 }
Yan, Zheng80825102009-11-12 09:35:36 +00005264
Miao Xief4a2f4c2011-12-14 20:12:01 -05005265 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005266 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005267 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005268 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005269 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005270
5271 i_size_write(inode, newsize);
5272 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305273 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005274 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005275 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005276 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005277 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005278
Josef Bacika41ad392011-01-31 15:30:16 -05005279 /*
5280 * We're truncating a file that used to have good data down to
5281 * zero. Make sure it gets into the ordered flush list so that
5282 * any new writes get down to disk quickly.
5283 */
5284 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005285 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5286 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005287
Josef Bacikf3fe8202013-01-07 17:03:21 -05005288 /*
5289 * 1 for the orphan item we're going to add
5290 * 1 for the orphan item deletion.
5291 */
5292 trans = btrfs_start_transaction(root, 2);
5293 if (IS_ERR(trans))
5294 return PTR_ERR(trans);
5295
5296 /*
5297 * We need to do this in case we fail at _any_ point during the
5298 * actual truncate. Once we do the truncate_setsize we could
5299 * invalidate pages which forces any outstanding ordered io to
5300 * be instantly completed which will give us extents that need
5301 * to be truncated. If we fail to get an orphan inode down we
5302 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005303 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005304 * will be consistent.
5305 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005306 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005307 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005308 if (ret)
5309 return ret;
5310
Josef Bacika41ad392011-01-31 15:30:16 -05005311 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5312 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005313
5314 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005315 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005316 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005317 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005318
Filipe Manana213e8c52018-02-06 20:40:31 +00005319 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005320 if (ret && inode->i_nlink) {
5321 int err;
5322
Liu Bo19fd2df2016-12-01 13:01:02 -08005323 /* To get a stable disk_i_size */
5324 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5325 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005326 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005327 return err;
5328 }
5329
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005330 /*
5331 * failed to truncate, disk_i_size is only adjusted down
5332 * as we remove extents, so it should represent the true
5333 * size of the inode, so reset the in memory size and
5334 * delete our orphan entry.
5335 */
5336 trans = btrfs_join_transaction(root);
5337 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005338 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005339 return ret;
5340 }
5341 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005342 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005343 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005344 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005345 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005346 }
Yan, Zheng80825102009-11-12 09:35:36 +00005347 }
5348
Josef Bacika41ad392011-01-31 15:30:16 -05005349 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005350}
5351
Chris Mason39279cc2007-06-12 06:35:45 -04005352static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5353{
David Howells2b0143b2015-03-17 22:25:59 +00005354 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005356 int err;
5357
Li Zefanb83cc962010-12-20 16:04:08 +08005358 if (btrfs_root_readonly(root))
5359 return -EROFS;
5360
Jan Kara31051c82016-05-26 16:55:18 +02005361 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005362 if (err)
5363 return err;
5364
Chris Mason5a3f23d2009-03-31 13:27:11 -04005365 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005366 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005367 if (err)
5368 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005369 }
Yan Zheng9036c102008-10-30 14:19:41 -04005370
Christoph Hellwig10257742010-06-04 11:30:02 +02005371 if (attr->ia_valid) {
5372 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005373 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005374 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005375
Josef Bacik22c44fe2011-11-30 10:45:38 -05005376 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005377 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005378 }
5379
Chris Mason39279cc2007-06-12 06:35:45 -04005380 return err;
5381}
Chris Mason61295eb2008-01-14 16:24:38 -05005382
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005383/*
5384 * While truncating the inode pages during eviction, we get the VFS calling
5385 * btrfs_invalidatepage() against each page of the inode. This is slow because
5386 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5387 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5388 * extent_state structures over and over, wasting lots of time.
5389 *
5390 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5391 * those expensive operations on a per page basis and do only the ordered io
5392 * finishing, while we release here the extent_map and extent_state structures,
5393 * without the excessive merging and splitting.
5394 */
5395static void evict_inode_truncate_pages(struct inode *inode)
5396{
5397 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5398 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5399 struct rb_node *node;
5400
5401 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005402 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005403
5404 write_lock(&map_tree->lock);
5405 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5406 struct extent_map *em;
5407
5408 node = rb_first(&map_tree->map);
5409 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005410 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5411 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005412 remove_extent_mapping(map_tree, em);
5413 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005414 if (need_resched()) {
5415 write_unlock(&map_tree->lock);
5416 cond_resched();
5417 write_lock(&map_tree->lock);
5418 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005419 }
5420 write_unlock(&map_tree->lock);
5421
Filipe Manana6ca07092015-05-26 00:55:42 +01005422 /*
5423 * Keep looping until we have no more ranges in the io tree.
5424 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005425 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5426 * still in progress (unlocked the pages in the bio but did not yet
5427 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005428 * ranges can still be locked and eviction started because before
5429 * submitting those bios, which are executed by a separate task (work
5430 * queue kthread), inode references (inode->i_count) were not taken
5431 * (which would be dropped in the end io callback of each bio).
5432 * Therefore here we effectively end up waiting for those bios and
5433 * anyone else holding locked ranges without having bumped the inode's
5434 * reference count - if we don't do it, when they access the inode's
5435 * io_tree to unlock a range it may be too late, leading to an
5436 * use-after-free issue.
5437 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005438 spin_lock(&io_tree->lock);
5439 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5440 struct extent_state *state;
5441 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005442 u64 start;
5443 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005444
5445 node = rb_first(&io_tree->state);
5446 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005447 start = state->start;
5448 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005449 spin_unlock(&io_tree->lock);
5450
David Sterbaff13db42015-12-03 14:30:40 +01005451 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005452
5453 /*
5454 * If still has DELALLOC flag, the extent didn't reach disk,
5455 * and its reserved space won't be freed by delayed_ref.
5456 * So we need to free its reserved space here.
5457 * (Refer to comment in btrfs_invalidatepage, case 2)
5458 *
5459 * Note, end is the bytenr of last byte, so we need + 1 here.
5460 */
5461 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005462 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005463
Filipe Manana6ca07092015-05-26 00:55:42 +01005464 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005465 EXTENT_LOCKED | EXTENT_DIRTY |
5466 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005467 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005468
Filipe Manana7064dd52014-08-08 02:47:05 +01005469 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005470 spin_lock(&io_tree->lock);
5471 }
5472 spin_unlock(&io_tree->lock);
5473}
5474
Al Virobd555972010-06-07 11:35:40 -04005475void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005478 struct btrfs_trans_handle *trans;
5479 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005480 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005481 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005482 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005483 int ret;
5484
liubo1abe9b82011-03-24 11:18:59 +00005485 trace_btrfs_inode_evict(inode);
5486
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005487 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005488 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005489 return;
5490 }
5491
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005492 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005493
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005494 evict_inode_truncate_pages(inode);
5495
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005496 if (inode->i_nlink &&
5497 ((btrfs_root_refs(&root->root_item) != 0 &&
5498 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005499 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005500 goto no_delete;
5501
Chris Mason39279cc2007-06-12 06:35:45 -04005502 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005503 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005504 goto no_delete;
5505 }
Al Virobd555972010-06-07 11:35:40 -04005506 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005507 if (!special_file(inode->i_mode))
5508 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005509
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005510 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005511
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005512 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005513 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005514 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005515 goto no_delete;
5516 }
5517
Yan, Zheng76dda932009-09-21 16:00:26 -04005518 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005519 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5520 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005521 goto no_delete;
5522 }
5523
Nikolay Borisovaa790212017-01-10 20:35:40 +02005524 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005525 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005526 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005527 goto no_delete;
5528 }
5529
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005530 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005531 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005532 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005533 goto no_delete;
5534 }
Josef Bacik4a338542011-08-29 11:01:31 -04005535 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005536 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005538
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005539 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005540
Josef Bacik4289a662011-08-05 13:22:24 -04005541 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005542 * This is a bit simpler than btrfs_truncate since we've already
5543 * reserved our space for our orphan item in the unlink, so we just
5544 * need to reserve some slack space in case we add bytes and update
5545 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005546 */
Yan, Zheng80825102009-11-12 09:35:36 +00005547 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005548 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5549 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005550
Josef Bacik726c35f2011-09-26 15:46:06 -04005551 /*
5552 * Try and steal from the global reserve since we will
5553 * likely not use this space anyway, we want to try as
5554 * hard as possible to get this to work.
5555 */
5556 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005557 steal_from_global++;
5558 else
5559 steal_from_global = 0;
5560 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005561
Josef Bacik3bce8762015-02-24 12:35:51 -08005562 /*
5563 * steal_from_global == 0: we reserved stuff, hooray!
5564 * steal_from_global == 1: we didn't reserve stuff, boo!
5565 * steal_from_global == 2: we've committed, still not a lot of
5566 * room but maybe we'll have room in the global reserve this
5567 * time.
5568 * steal_from_global == 3: abandon all hope!
5569 */
5570 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005571 btrfs_warn(fs_info,
5572 "Could not get space for a delete, will truncate on mount %d",
5573 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005574 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005575 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005576 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005577 }
5578
Miao Xie0e8c36a2012-12-19 06:59:51 +00005579 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005580 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005581 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005582 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005583 goto no_delete;
5584 }
5585
Josef Bacik3bce8762015-02-24 12:35:51 -08005586 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005587 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005588 * sure there is room to do it, if not we need to commit and try
5589 * again.
5590 */
5591 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005592 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005593 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005594 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005595 else
5596 ret = -ENOSPC;
5597 }
5598
5599 /*
5600 * Couldn't steal from the global reserve, we have too much
5601 * pending stuff built up, commit the transaction and try it
5602 * again.
5603 */
5604 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005605 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005606 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005607 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005608 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005609 goto no_delete;
5610 }
5611 continue;
5612 } else {
5613 steal_from_global = 0;
5614 }
5615
Josef Bacik4289a662011-08-05 13:22:24 -04005616 trans->block_rsv = rsv;
5617
Yan, Zhengd68fc572010-05-16 10:49:58 -04005618 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005619 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005620 break;
5621
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005622 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005623 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005624 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005625 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005626 }
5627
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005628 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005629
Josef Bacik4ef31a42013-08-13 14:10:08 -04005630 /*
5631 * Errors here aren't a big deal, it just means we leave orphan items
5632 * in the tree. They will be cleaned up on the next mount.
5633 */
Yan, Zheng80825102009-11-12 09:35:36 +00005634 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005635 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005636 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005637 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005638 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005639 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005640
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005641 trans->block_rsv = &fs_info->trans_block_rsv;
5642 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005643 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005644 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005645
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005646 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005647 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005648no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005649 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005650 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005651}
5652
5653/*
5654 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005655 * If no dir entries were found, returns -ENOENT.
5656 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005657 */
5658static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5659 struct btrfs_key *location)
5660{
5661 const char *name = dentry->d_name.name;
5662 int namelen = dentry->d_name.len;
5663 struct btrfs_dir_item *di;
5664 struct btrfs_path *path;
5665 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005666 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005667
5668 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005669 if (!path)
5670 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005671
David Sterbaf85b7372017-01-20 14:54:07 +01005672 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5673 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005674 if (!di) {
5675 ret = -ENOENT;
5676 goto out;
5677 }
5678 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005679 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005680 goto out;
5681 }
Chris Masond3977122009-01-05 21:25:51 -05005682
Chris Mason5f39d392007-10-15 16:14:19 -04005683 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005684 if (location->type != BTRFS_INODE_ITEM_KEY &&
5685 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005686 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005687 btrfs_warn(root->fs_info,
5688"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5689 __func__, name, btrfs_ino(BTRFS_I(dir)),
5690 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005691 }
Chris Mason39279cc2007-06-12 06:35:45 -04005692out:
Chris Mason39279cc2007-06-12 06:35:45 -04005693 btrfs_free_path(path);
5694 return ret;
5695}
5696
5697/*
5698 * when we hit a tree root in a directory, the btrfs part of the inode
5699 * needs to be changed to reflect the root directory of the tree root. This
5700 * is kind of like crossing a mount point.
5701 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005702static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005703 struct inode *dir,
5704 struct dentry *dentry,
5705 struct btrfs_key *location,
5706 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005707{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005708 struct btrfs_path *path;
5709 struct btrfs_root *new_root;
5710 struct btrfs_root_ref *ref;
5711 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005712 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005713 int ret;
5714 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005715
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005716 path = btrfs_alloc_path();
5717 if (!path) {
5718 err = -ENOMEM;
5719 goto out;
5720 }
Chris Mason39279cc2007-06-12 06:35:45 -04005721
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005723 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5724 key.type = BTRFS_ROOT_REF_KEY;
5725 key.offset = location->objectid;
5726
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005727 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005728 if (ret) {
5729 if (ret < 0)
5730 err = ret;
5731 goto out;
5732 }
Chris Mason39279cc2007-06-12 06:35:45 -04005733
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005734 leaf = path->nodes[0];
5735 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005736 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5738 goto out;
5739
5740 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5741 (unsigned long)(ref + 1),
5742 dentry->d_name.len);
5743 if (ret)
5744 goto out;
5745
David Sterbab3b4aa72011-04-21 01:20:15 +02005746 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005748 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749 if (IS_ERR(new_root)) {
5750 err = PTR_ERR(new_root);
5751 goto out;
5752 }
5753
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005754 *sub_root = new_root;
5755 location->objectid = btrfs_root_dirid(&new_root->root_item);
5756 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005757 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 err = 0;
5759out:
5760 btrfs_free_path(path);
5761 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005762}
5763
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005764static void inode_tree_add(struct inode *inode)
5765{
5766 struct btrfs_root *root = BTRFS_I(inode)->root;
5767 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005768 struct rb_node **p;
5769 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005770 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005771 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005772
Al Viro1d3382cb2010-10-23 15:19:20 -04005773 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005774 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005775 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005776 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005777 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005778 while (*p) {
5779 parent = *p;
5780 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5781
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005782 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005783 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005784 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005785 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005786 else {
5787 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005788 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005789 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005790 RB_CLEAR_NODE(parent);
5791 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005792 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005793 }
5794 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005795 rb_link_node(new, parent, p);
5796 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005797 spin_unlock(&root->inode_lock);
5798}
5799
5800static void inode_tree_del(struct inode *inode)
5801{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005802 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005803 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005804 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005805
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005806 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005807 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005808 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005809 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005810 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005811 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005812 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005813
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005814 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005815 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005816 spin_lock(&root->inode_lock);
5817 empty = RB_EMPTY_ROOT(&root->inode_tree);
5818 spin_unlock(&root->inode_lock);
5819 if (empty)
5820 btrfs_add_dead_root(root);
5821 }
5822}
5823
Jeff Mahoney143bede2012-03-01 14:56:26 +01005824void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005825{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005826 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005827 struct rb_node *node;
5828 struct rb_node *prev;
5829 struct btrfs_inode *entry;
5830 struct inode *inode;
5831 u64 objectid = 0;
5832
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005833 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005834 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005835
5836 spin_lock(&root->inode_lock);
5837again:
5838 node = root->inode_tree.rb_node;
5839 prev = NULL;
5840 while (node) {
5841 prev = node;
5842 entry = rb_entry(node, struct btrfs_inode, rb_node);
5843
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005844 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005845 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005846 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005847 node = node->rb_right;
5848 else
5849 break;
5850 }
5851 if (!node) {
5852 while (prev) {
5853 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005854 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005855 node = prev;
5856 break;
5857 }
5858 prev = rb_next(prev);
5859 }
5860 }
5861 while (node) {
5862 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005863 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005864 inode = igrab(&entry->vfs_inode);
5865 if (inode) {
5866 spin_unlock(&root->inode_lock);
5867 if (atomic_read(&inode->i_count) > 1)
5868 d_prune_aliases(inode);
5869 /*
Al Viro45321ac2010-06-07 13:43:19 -04005870 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005871 * the inode cache when its usage count
5872 * hits zero.
5873 */
5874 iput(inode);
5875 cond_resched();
5876 spin_lock(&root->inode_lock);
5877 goto again;
5878 }
5879
5880 if (cond_resched_lock(&root->inode_lock))
5881 goto again;
5882
5883 node = rb_next(node);
5884 }
5885 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005886}
5887
Chris Masone02119d2008-09-05 16:13:11 -04005888static int btrfs_init_locked_inode(struct inode *inode, void *p)
5889{
5890 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005891 inode->i_ino = args->location->objectid;
5892 memcpy(&BTRFS_I(inode)->location, args->location,
5893 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005894 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005895 return 0;
5896}
5897
5898static int btrfs_find_actor(struct inode *inode, void *opaque)
5899{
5900 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005901 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005902 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005903}
5904
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005905static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005906 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005907 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005908{
5909 struct inode *inode;
5910 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005911 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005912
Chris Mason90d3e592014-01-09 17:28:00 -08005913 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005914 args.root = root;
5915
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005916 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005917 btrfs_init_locked_inode,
5918 (void *)&args);
5919 return inode;
5920}
5921
Balaji Rao1a54ef82008-07-21 02:01:04 +05305922/* Get an inode object given its location and corresponding root.
5923 * Returns in *is_new if the inode was read from disk
5924 */
5925struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005926 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305927{
5928 struct inode *inode;
5929
Chris Mason90d3e592014-01-09 17:28:00 -08005930 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305931 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005932 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305933
5934 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005935 int ret;
5936
5937 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005938 if (!is_bad_inode(inode)) {
5939 inode_tree_add(inode);
5940 unlock_new_inode(inode);
5941 if (new)
5942 *new = 1;
5943 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005944 unlock_new_inode(inode);
5945 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005946 ASSERT(ret < 0);
5947 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005948 }
5949 }
5950
Balaji Rao1a54ef82008-07-21 02:01:04 +05305951 return inode;
5952}
5953
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005954static struct inode *new_simple_dir(struct super_block *s,
5955 struct btrfs_key *key,
5956 struct btrfs_root *root)
5957{
5958 struct inode *inode = new_inode(s);
5959
5960 if (!inode)
5961 return ERR_PTR(-ENOMEM);
5962
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005963 BTRFS_I(inode)->root = root;
5964 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005965 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005966
5967 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005968 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005969 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005970 inode->i_fop = &simple_dir_operations;
5971 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005972 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305973 inode->i_atime = inode->i_mtime;
5974 inode->i_ctime = inode->i_mtime;
5975 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005976
5977 return inode;
5978}
5979
Chris Mason3de45862008-11-17 21:02:50 -05005980struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005982 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005983 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005984 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005985 struct btrfs_root *sub_root = root;
5986 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005987 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005988 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005989
5990 if (dentry->d_name.len > BTRFS_NAME_LEN)
5991 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005992
Jeff Layton39e3c952012-11-28 11:30:53 -05005993 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005994 if (ret < 0)
5995 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005996
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005997 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005998 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005999 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006000 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006001
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006002 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006003 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006004 &location, &sub_root);
6005 if (ret < 0) {
6006 if (ret != -ENOENT)
6007 inode = ERR_PTR(ret);
6008 else
6009 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6010 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00006011 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006012 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006013 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04006014
Julia Lawall34d19ba2011-01-24 19:55:19 +00006015 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006016 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01006017 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006018 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006019 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04006020 if (ret) {
6021 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006022 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04006023 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00006024 }
6025
Chris Mason3de45862008-11-17 21:02:50 -05006026 return inode;
6027}
6028
Nick Pigginfe15ce42011-01-07 17:49:23 +11006029static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006030{
6031 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006032 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006033
Li Zefan848cce02012-02-21 17:04:28 +08006034 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006035 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006036
Li Zefan848cce02012-02-21 17:04:28 +08006037 if (inode) {
6038 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006039 if (btrfs_root_refs(&root->root_item) == 0)
6040 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006041
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006042 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006043 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006044 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006045 return 0;
6046}
6047
Chris Mason3de45862008-11-17 21:02:50 -05006048static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006049 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006050{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09006051 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04006052
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09006053 inode = btrfs_lookup_dentry(dir, dentry);
6054 if (IS_ERR(inode)) {
6055 if (PTR_ERR(inode) == -ENOENT)
6056 inode = NULL;
6057 else
6058 return ERR_CAST(inode);
6059 }
6060
Al Viro41d28bc2014-10-12 22:24:21 -04006061 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006062}
6063
Miao Xie16cdcec2011-04-22 18:12:22 +08006064unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04006065 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
6066};
6067
Josef Bacik23b5ec72017-07-24 15:14:25 -04006068/*
6069 * All this infrastructure exists because dir_emit can fault, and we are holding
6070 * the tree lock when doing readdir. For now just allocate a buffer and copy
6071 * our information into that, and then dir_emit from the buffer. This is
6072 * similar to what NFS does, only we don't keep the buffer around in pagecache
6073 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6074 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6075 * tree lock.
6076 */
6077static int btrfs_opendir(struct inode *inode, struct file *file)
6078{
6079 struct btrfs_file_private *private;
6080
6081 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6082 if (!private)
6083 return -ENOMEM;
6084 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6085 if (!private->filldir_buf) {
6086 kfree(private);
6087 return -ENOMEM;
6088 }
6089 file->private_data = private;
6090 return 0;
6091}
6092
6093struct dir_entry {
6094 u64 ino;
6095 u64 offset;
6096 unsigned type;
6097 int name_len;
6098};
6099
6100static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6101{
6102 while (entries--) {
6103 struct dir_entry *entry = addr;
6104 char *name = (char *)(entry + 1);
6105
David Sterba92d32172018-04-16 21:10:14 +02006106 ctx->pos = get_unaligned(&entry->offset);
6107 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6108 get_unaligned(&entry->ino),
6109 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006110 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006111 addr += sizeof(struct dir_entry) +
6112 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006113 ctx->pos++;
6114 }
6115 return 0;
6116}
6117
Al Viro9cdda8d2013-05-22 16:48:09 -04006118static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006119{
Al Viro9cdda8d2013-05-22 16:48:09 -04006120 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006121 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006122 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006123 struct btrfs_dir_item *di;
6124 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006125 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006126 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006127 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006128 struct list_head ins_list;
6129 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006130 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006131 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006132 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006133 char *name_ptr;
6134 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006135 int entries = 0;
6136 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006137 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006138 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006139
Al Viro9cdda8d2013-05-22 16:48:09 -04006140 if (!dir_emit_dots(file, ctx))
6141 return 0;
6142
David Woodhouse49593bf2008-08-17 17:08:36 +01006143 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006144 if (!path)
6145 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006146
Josef Bacik23b5ec72017-07-24 15:14:25 -04006147 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006148 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006149
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006150 INIT_LIST_HEAD(&ins_list);
6151 INIT_LIST_HEAD(&del_list);
6152 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006153
Josef Bacik23b5ec72017-07-24 15:14:25 -04006154again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006155 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006156 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006157 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006158
Chris Mason39279cc2007-06-12 06:35:45 -04006159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6160 if (ret < 0)
6161 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006162
6163 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006164 struct dir_entry *entry;
6165
Chris Mason5f39d392007-10-15 16:14:19 -04006166 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006167 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006168 if (slot >= btrfs_header_nritems(leaf)) {
6169 ret = btrfs_next_leaf(root, path);
6170 if (ret < 0)
6171 goto err;
6172 else if (ret > 0)
6173 break;
6174 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006175 }
Chris Mason3de45862008-11-17 21:02:50 -05006176
Chris Mason5f39d392007-10-15 16:14:19 -04006177 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6178
6179 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006180 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006181 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006182 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006183 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006184 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006185 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006186 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006187 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006188 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006189 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6190 PAGE_SIZE) {
6191 btrfs_release_path(path);
6192 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6193 if (ret)
6194 goto nopos;
6195 addr = private->filldir_buf;
6196 entries = 0;
6197 total_len = 0;
6198 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006199 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006200
6201 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006202 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006203 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006204 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6205 name_len);
David Sterba92d32172018-04-16 21:10:14 +02006206 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
6207 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006208 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006209 put_unaligned(location.objectid, &entry->ino);
6210 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006211 entries++;
6212 addr += sizeof(struct dir_entry) + name_len;
6213 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006214next:
6215 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006216 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006217 btrfs_release_path(path);
6218
6219 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6220 if (ret)
6221 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006222
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006223 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006224 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006225 goto nopos;
6226
Zach Browndb62efb2013-07-11 16:19:42 -07006227 /*
6228 * Stop new entries from being returned after we return the last
6229 * entry.
6230 *
6231 * New directory entries are assigned a strictly increasing
6232 * offset. This means that new entries created during readdir
6233 * are *guaranteed* to be seen in the future by that readdir.
6234 * This has broken buggy programs which operate on names as
6235 * they're returned by readdir. Until we re-use freed offsets
6236 * we have this hack to stop new entries from being returned
6237 * under the assumption that they'll never reach this huge
6238 * offset.
6239 *
6240 * This is being careful not to overflow 32bit loff_t unless the
6241 * last entry requires it because doing so has broken 32bit apps
6242 * in the past.
6243 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006244 if (ctx->pos >= INT_MAX)
6245 ctx->pos = LLONG_MAX;
6246 else
6247 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006248nopos:
6249 ret = 0;
6250err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006251 if (put)
6252 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006253 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006254 return ret;
6255}
6256
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006257int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006258{
6259 struct btrfs_root *root = BTRFS_I(inode)->root;
6260 struct btrfs_trans_handle *trans;
6261 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006262 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006263
Josef Bacik72ac3c02012-05-23 14:13:11 -04006264 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006265 return 0;
6266
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006267 if (btrfs_fs_closing(root->fs_info) &&
6268 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006269 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006270
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006271 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006272 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006273 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006274 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006275 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006276 if (IS_ERR(trans))
6277 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006278 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006279 }
6280 return ret;
6281}
6282
6283/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006284 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006285 * inode changes. But, it is most likely to find the inode in cache.
6286 * FIXME, needs more benchmarking...there are no reasons other than performance
6287 * to keep or drop this code.
6288 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006289static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006290{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006292 struct btrfs_root *root = BTRFS_I(inode)->root;
6293 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006294 int ret;
6295
Josef Bacik72ac3c02012-05-23 14:13:11 -04006296 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006297 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006298
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006299 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006300 if (IS_ERR(trans))
6301 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006302
6303 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006304 if (ret && ret == -ENOSPC) {
6305 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006306 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006307 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006308 if (IS_ERR(trans))
6309 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006310
Chris Mason94b60442010-05-26 11:02:00 -04006311 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006312 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006313 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006314 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006315 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006316
6317 return ret;
6318}
6319
6320/*
6321 * This is a copy of file_update_time. We need this so we can return error on
6322 * ENOSPC for updating the inode in the case of file write and mmap writes.
6323 */
Josef Bacike41f9412012-03-26 09:46:47 -04006324static int btrfs_update_time(struct inode *inode, struct timespec *now,
6325 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006326{
Alexander Block2bc5565282012-06-15 09:49:33 +02006327 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006328 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006329
6330 if (btrfs_root_readonly(root))
6331 return -EROFS;
6332
Josef Bacike41f9412012-03-26 09:46:47 -04006333 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006334 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006335 if (flags & S_CTIME)
6336 inode->i_ctime = *now;
6337 if (flags & S_MTIME)
6338 inode->i_mtime = *now;
6339 if (flags & S_ATIME)
6340 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006341 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006342}
6343
Chris Masond352ac62008-09-29 15:18:18 -04006344/*
6345 * find the highest existing sequence number in a directory
6346 * and then set the in-memory index_cnt variable to reflect
6347 * free sequence numbers
6348 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006349static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006350{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006351 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006352 struct btrfs_key key, found_key;
6353 struct btrfs_path *path;
6354 struct extent_buffer *leaf;
6355 int ret;
6356
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006357 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006358 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006359 key.offset = (u64)-1;
6360
6361 path = btrfs_alloc_path();
6362 if (!path)
6363 return -ENOMEM;
6364
6365 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6366 if (ret < 0)
6367 goto out;
6368 /* FIXME: we should be able to handle this */
6369 if (ret == 0)
6370 goto out;
6371 ret = 0;
6372
6373 /*
6374 * MAGIC NUMBER EXPLANATION:
6375 * since we search a directory based on f_pos we have to start at 2
6376 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6377 * else has to start at 2
6378 */
6379 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006380 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006381 goto out;
6382 }
6383
6384 path->slots[0]--;
6385
6386 leaf = path->nodes[0];
6387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6388
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006389 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006390 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006391 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006392 goto out;
6393 }
6394
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006395 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006396out:
6397 btrfs_free_path(path);
6398 return ret;
6399}
6400
Chris Masond352ac62008-09-29 15:18:18 -04006401/*
6402 * helper to find a free sequence number in a given directory. This current
6403 * code is very simple, later versions will do smarter things in the btree
6404 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006405int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006406{
6407 int ret = 0;
6408
Nikolay Borisov877574e2017-02-20 13:50:33 +02006409 if (dir->index_cnt == (u64)-1) {
6410 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006411 if (ret) {
6412 ret = btrfs_set_inode_index_count(dir);
6413 if (ret)
6414 return ret;
6415 }
Josef Bacikaec74772008-07-24 12:12:38 -04006416 }
6417
Nikolay Borisov877574e2017-02-20 13:50:33 +02006418 *index = dir->index_cnt;
6419 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006420
6421 return ret;
6422}
6423
Chris Masonb0d5d102014-09-08 13:08:51 -07006424static int btrfs_insert_inode_locked(struct inode *inode)
6425{
6426 struct btrfs_iget_args args;
6427 args.location = &BTRFS_I(inode)->location;
6428 args.root = BTRFS_I(inode)->root;
6429
6430 return insert_inode_locked4(inode,
6431 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6432 btrfs_find_actor, &args);
6433}
6434
Anand Jain19aee8d2017-07-18 17:37:05 +08006435/*
6436 * Inherit flags from the parent inode.
6437 *
6438 * Currently only the compression flags and the cow flags are inherited.
6439 */
6440static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6441{
6442 unsigned int flags;
6443
6444 if (!dir)
6445 return;
6446
6447 flags = BTRFS_I(dir)->flags;
6448
6449 if (flags & BTRFS_INODE_NOCOMPRESS) {
6450 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6451 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6452 } else if (flags & BTRFS_INODE_COMPRESS) {
6453 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6454 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6455 }
6456
6457 if (flags & BTRFS_INODE_NODATACOW) {
6458 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6459 if (S_ISREG(inode->i_mode))
6460 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6461 }
6462
6463 btrfs_update_iflags(inode);
6464}
6465
Chris Mason39279cc2007-06-12 06:35:45 -04006466static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6467 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006468 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006469 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006470 u64 ref_objectid, u64 objectid,
6471 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006472{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006473 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006474 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006475 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006476 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006477 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006478 struct btrfs_inode_ref *ref;
6479 struct btrfs_key key[2];
6480 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006481 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006482 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006483 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006484
Chris Mason5f39d392007-10-15 16:14:19 -04006485 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006486 if (!path)
6487 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006488
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006489 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006490 if (!inode) {
6491 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006492 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006493 }
Chris Mason39279cc2007-06-12 06:35:45 -04006494
Li Zefan581bb052011-04-20 10:06:11 +08006495 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006496 * O_TMPFILE, set link count to 0, so that after this point,
6497 * we fill in an inode item with the correct link count.
6498 */
6499 if (!name)
6500 set_nlink(inode, 0);
6501
6502 /*
Li Zefan581bb052011-04-20 10:06:11 +08006503 * we have to initialize this early, so we can reclaim the inode
6504 * number if we fail afterwards in this function.
6505 */
6506 inode->i_ino = objectid;
6507
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006508 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006509 trace_btrfs_inode_request(dir);
6510
Nikolay Borisov877574e2017-02-20 13:50:33 +02006511 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006512 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006513 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006514 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006515 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006516 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006517 } else if (dir) {
6518 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006519 }
6520 /*
6521 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006522 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006523 * number
6524 */
6525 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006526 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006527 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006528 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006529 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006530
Josef Bacik5dc562c2012-08-17 13:14:17 -04006531 /*
6532 * We could have gotten an inode number from somebody who was fsynced
6533 * and then removed in this same transaction, so let's just set full
6534 * sync since it will be a full sync anyway and this will blow away the
6535 * old info in the log.
6536 */
6537 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6538
Chris Mason9c583092008-01-29 15:15:18 -05006539 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006540 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006541 key[0].offset = 0;
6542
Chris Mason9c583092008-01-29 15:15:18 -05006543 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006544
6545 if (name) {
6546 /*
6547 * Start new inodes with an inode_ref. This is slightly more
6548 * efficient for small numbers of hard links since they will
6549 * be packed into one item. Extended refs will kick in if we
6550 * add more hard links than can fit in the ref item.
6551 */
6552 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006553 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006554 key[1].offset = ref_objectid;
6555
6556 sizes[1] = name_len + sizeof(*ref);
6557 }
Chris Mason9c583092008-01-29 15:15:18 -05006558
Chris Masonb0d5d102014-09-08 13:08:51 -07006559 location = &BTRFS_I(inode)->location;
6560 location->objectid = objectid;
6561 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006562 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006563
6564 ret = btrfs_insert_inode_locked(inode);
6565 if (ret < 0)
6566 goto fail;
6567
Chris Masonb9473432009-03-13 11:00:37 -04006568 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006569 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006570 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006571 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006572
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006573 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006574 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306575
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006576 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306577 inode->i_atime = inode->i_mtime;
6578 inode->i_ctime = inode->i_mtime;
6579 BTRFS_I(inode)->i_otime = inode->i_mtime;
6580
Chris Mason5f39d392007-10-15 16:14:19 -04006581 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6582 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006583 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006584 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006585 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006586
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006587 if (name) {
6588 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6589 struct btrfs_inode_ref);
6590 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6591 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6592 ptr = (unsigned long)(ref + 1);
6593 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6594 }
Chris Mason9c583092008-01-29 15:15:18 -05006595
Chris Mason5f39d392007-10-15 16:14:19 -04006596 btrfs_mark_buffer_dirty(path->nodes[0]);
6597 btrfs_free_path(path);
6598
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006599 btrfs_inherit_iflags(inode, dir);
6600
Al Viro569254b2011-07-24 17:08:40 -04006601 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006602 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006603 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006604 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006605 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6606 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006607 }
6608
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006609 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006610
6611 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006612 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006613
Alexander Block8ea05e32012-07-25 17:35:53 +02006614 btrfs_update_root_times(trans, root);
6615
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006616 ret = btrfs_inode_inherit_props(trans, inode, dir);
6617 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006618 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006619 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006620 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006621
Chris Mason39279cc2007-06-12 06:35:45 -04006622 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006623
6624fail_unlock:
6625 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006626fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006627 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006628 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006629 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006630 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006631 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006632}
6633
6634static inline u8 btrfs_inode_type(struct inode *inode)
6635{
6636 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6637}
6638
Chris Masond352ac62008-09-29 15:18:18 -04006639/*
6640 * utility function to add 'inode' into 'parent_inode' with
6641 * a give name and a given sequence number.
6642 * if 'add_backref' is true, also insert a backref from the
6643 * inode to the parent directory.
6644 */
Chris Masone02119d2008-09-05 16:13:11 -04006645int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006646 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006647 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006648{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006650 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006651 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006652 struct btrfs_root *root = parent_inode->root;
6653 u64 ino = btrfs_ino(inode);
6654 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006655
Li Zefan33345d012011-04-20 10:31:50 +08006656 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006657 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006658 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006659 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006660 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006661 key.offset = 0;
6662 }
Chris Mason39279cc2007-06-12 06:35:45 -04006663
Li Zefan33345d012011-04-20 10:31:50 +08006664 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006665 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6666 root->root_key.objectid, parent_ino,
6667 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006668 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006669 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6670 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006671 }
6672
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006673 /* Nothing to clean up yet */
6674 if (ret)
6675 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006676
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006677 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6678 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006679 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006680 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006681 goto fail_dir_item;
6682 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006683 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006684 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006685 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006686
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006687 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006688 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006689 inode_inc_iversion(&parent_inode->vfs_inode);
6690 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6691 current_time(&parent_inode->vfs_inode);
6692 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006693 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006694 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006695 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006696
6697fail_dir_item:
6698 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6699 u64 local_index;
6700 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006701 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6702 root->root_key.objectid, parent_ino,
6703 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006704
6705 } else if (add_backref) {
6706 u64 local_index;
6707 int err;
6708
6709 err = btrfs_del_inode_ref(trans, root, name, name_len,
6710 ino, parent_ino, &local_index);
6711 }
6712 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006713}
6714
6715static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006716 struct btrfs_inode *dir, struct dentry *dentry,
6717 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006718{
Josef Bacika1b075d2010-11-19 20:36:11 +00006719 int err = btrfs_add_link(trans, dir, inode,
6720 dentry->d_name.name, dentry->d_name.len,
6721 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006722 if (err > 0)
6723 err = -EEXIST;
6724 return err;
6725}
6726
Josef Bacik618e21d2007-07-11 10:18:17 -04006727static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006728 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006729{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006730 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006731 struct btrfs_trans_handle *trans;
6732 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006733 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006734 int err;
6735 int drop_inode = 0;
6736 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006737 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006738
Josef Bacik9ed74f22009-09-11 16:12:44 -04006739 /*
6740 * 2 for inode item and ref
6741 * 2 for dir items
6742 * 1 for xattr if selinux is on
6743 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006744 trans = btrfs_start_transaction(root, 5);
6745 if (IS_ERR(trans))
6746 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006747
Li Zefan581bb052011-04-20 10:06:11 +08006748 err = btrfs_find_free_ino(root, &objectid);
6749 if (err)
6750 goto out_unlock;
6751
Josef Bacikaec74772008-07-24 12:12:38 -04006752 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006753 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6754 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006755 if (IS_ERR(inode)) {
6756 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006757 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006758 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006759
Casey Schauflerad19db72011-12-15 10:09:07 -05006760 /*
6761 * If the active LSM wants to access the inode during
6762 * d_instantiate it needs these. Smack checks to see
6763 * if the filesystem supports xattrs by looking at the
6764 * ops vector.
6765 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006766 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006767 init_special_inode(inode, inode->i_mode, rdev);
6768
6769 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006770 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006771 goto out_unlock_inode;
6772
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006773 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6774 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006775 if (err) {
6776 goto out_unlock_inode;
6777 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006778 btrfs_update_inode(trans, root, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04006779 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006780 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006781
Josef Bacik618e21d2007-07-11 10:18:17 -04006782out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006783 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006784 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006785 if (drop_inode) {
6786 inode_dec_link_count(inode);
6787 iput(inode);
6788 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006789 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006790
6791out_unlock_inode:
6792 drop_inode = 1;
6793 unlock_new_inode(inode);
6794 goto out_unlock;
6795
Josef Bacik618e21d2007-07-11 10:18:17 -04006796}
6797
Chris Mason39279cc2007-06-12 06:35:45 -04006798static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006799 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006800{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006801 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006802 struct btrfs_trans_handle *trans;
6803 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006804 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006805 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006806 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006807 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006808 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006809
Josef Bacik9ed74f22009-09-11 16:12:44 -04006810 /*
6811 * 2 for inode item and ref
6812 * 2 for dir items
6813 * 1 for xattr if selinux is on
6814 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006815 trans = btrfs_start_transaction(root, 5);
6816 if (IS_ERR(trans))
6817 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006818
Li Zefan581bb052011-04-20 10:06:11 +08006819 err = btrfs_find_free_ino(root, &objectid);
6820 if (err)
6821 goto out_unlock;
6822
Josef Bacikaec74772008-07-24 12:12:38 -04006823 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006824 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6825 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006826 if (IS_ERR(inode)) {
6827 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006828 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006829 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006830 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006831 /*
6832 * If the active LSM wants to access the inode during
6833 * d_instantiate it needs these. Smack checks to see
6834 * if the filesystem supports xattrs by looking at the
6835 * ops vector.
6836 */
6837 inode->i_fop = &btrfs_file_operations;
6838 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006839 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006840
6841 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6842 if (err)
6843 goto out_unlock_inode;
6844
6845 err = btrfs_update_inode(trans, root, inode);
6846 if (err)
6847 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006848
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006849 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6850 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006851 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006852 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006853
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006854 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006855 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006856
Chris Mason39279cc2007-06-12 06:35:45 -04006857out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006858 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006859 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006860 inode_dec_link_count(inode);
6861 iput(inode);
6862 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006863 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006864 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006865
6866out_unlock_inode:
6867 unlock_new_inode(inode);
6868 goto out_unlock;
6869
Chris Mason39279cc2007-06-12 06:35:45 -04006870}
6871
6872static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6873 struct dentry *dentry)
6874{
Filipe Manana271dba42016-01-05 16:24:05 +00006875 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006876 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006877 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006879 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006880 int err;
6881 int drop_inode = 0;
6882
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006883 /* do not allow sys_link's with other subvols of the same device */
6884 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006885 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006886
Mark Fashehf1863732012-08-08 11:32:27 -07006887 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006888 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006889
Nikolay Borisov877574e2017-02-20 13:50:33 +02006890 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006891 if (err)
6892 goto fail;
6893
Yan, Zhenga22285a2010-05-16 10:48:46 -04006894 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006895 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006896 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006897 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006898 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006899 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006900 if (IS_ERR(trans)) {
6901 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006902 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006903 goto fail;
6904 }
Chris Mason5f39d392007-10-15 16:14:19 -04006905
Miao Xie67de1172013-12-26 13:07:06 +08006906 /* There are several dir indexes for this inode, clear the cache. */
6907 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006908 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006909 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006910 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006911 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006912 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006913
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006914 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6915 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006916
Yan, Zhenga5719522009-09-24 09:17:31 -04006917 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006918 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006919 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006920 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006921 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006922 if (err)
6923 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006924 if (inode->i_nlink == 1) {
6925 /*
6926 * If new hard link count is 1, it's a file created
6927 * with open(2) O_TMPFILE flag.
6928 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006929 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006930 if (err)
6931 goto fail;
6932 }
Al Viro08c422c2011-12-23 07:58:13 -05006933 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006934 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006935 }
Chris Mason39279cc2007-06-12 06:35:45 -04006936
Chris Mason1832a6d2007-12-21 16:27:21 -05006937fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006938 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006939 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006940 if (drop_inode) {
6941 inode_dec_link_count(inode);
6942 iput(inode);
6943 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006944 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006945 return err;
6946}
6947
Al Viro18bb1db2011-07-26 01:41:39 -04006948static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006949{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006950 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006951 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006952 struct btrfs_trans_handle *trans;
6953 struct btrfs_root *root = BTRFS_I(dir)->root;
6954 int err = 0;
6955 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006956 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006957 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006958
Josef Bacik9ed74f22009-09-11 16:12:44 -04006959 /*
6960 * 2 items for inode and ref
6961 * 2 items for dir items
6962 * 1 for xattr if selinux is on
6963 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006964 trans = btrfs_start_transaction(root, 5);
6965 if (IS_ERR(trans))
6966 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006967
Li Zefan581bb052011-04-20 10:06:11 +08006968 err = btrfs_find_free_ino(root, &objectid);
6969 if (err)
6970 goto out_fail;
6971
Josef Bacikaec74772008-07-24 12:12:38 -04006972 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006973 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6974 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006975 if (IS_ERR(inode)) {
6976 err = PTR_ERR(inode);
6977 goto out_fail;
6978 }
Chris Mason5f39d392007-10-15 16:14:19 -04006979
Chris Mason39279cc2007-06-12 06:35:45 -04006980 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006981 /* these must be set before we unlock the inode */
6982 inode->i_op = &btrfs_dir_inode_operations;
6983 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006984
Eric Paris2a7dba32011-02-01 11:05:39 -05006985 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006986 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006987 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006988
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006989 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006990 err = btrfs_update_inode(trans, root, inode);
6991 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006992 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006993
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006994 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6995 dentry->d_name.name,
6996 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006997 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006998 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006999
Al Viro1e2e5472018-05-04 08:23:01 -04007000 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007001 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007002
7003out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007004 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08007005 if (drop_on_err) {
7006 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007007 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08007008 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007009 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04007010 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07007011
7012out_fail_inode:
7013 unlock_new_inode(inode);
7014 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04007015}
7016
Chris Masonc8b97812008-10-29 14:49:59 -04007017static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09007018 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04007019 size_t pg_offset, u64 extent_offset,
7020 struct btrfs_file_extent_item *item)
7021{
7022 int ret;
7023 struct extent_buffer *leaf = path->nodes[0];
7024 char *tmp;
7025 size_t max_size;
7026 unsigned long inline_size;
7027 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08007028 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04007029
7030 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08007031 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04007032 max_size = btrfs_file_extent_ram_bytes(leaf, item);
7033 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01007034 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04007035 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04007036 if (!tmp)
7037 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04007038 ptr = btrfs_file_extent_inline_start(item);
7039
7040 read_extent_buffer(leaf, tmp, ptr, inline_size);
7041
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007042 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08007043 ret = btrfs_decompress(compress_type, tmp, page,
7044 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05007045
7046 /*
7047 * decompression code contains a memset to fill in any space between the end
7048 * of the uncompressed data and the end of max_size in case the decompressed
7049 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7050 * the end of an inline extent and the beginning of the next block, so we
7051 * cover that region here.
7052 */
7053
7054 if (max_size + pg_offset < PAGE_SIZE) {
7055 char *map = kmap(page);
7056 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
7057 kunmap(page);
7058 }
Chris Masonc8b97812008-10-29 14:49:59 -04007059 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04007060 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04007061}
7062
Chris Masond352ac62008-09-29 15:18:18 -04007063/*
7064 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05007065 * the ugly parts come from merging extents from the disk with the in-ram
7066 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04007067 * where the in-ram extents might be locked pending data=ordered completion.
7068 *
7069 * This also copies inline extents directly into the page.
7070 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007071struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
7072 struct page *page,
7073 size_t pg_offset, u64 start, u64 len,
7074 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04007075{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04007077 int ret;
7078 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007079 u64 extent_start = 0;
7080 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007081 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04007082 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04007083 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007084 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007085 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007086 struct extent_buffer *leaf;
7087 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007088 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007089 struct extent_map_tree *em_tree = &inode->extent_tree;
7090 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01007091 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04007092
Chris Mason890871b2009-09-02 16:24:52 -04007093 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007094 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04007095 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007096 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04007097 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007098
Chris Masona52d9a82007-08-27 16:49:44 -04007099 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007100 if (em->start > start || em->start + em->len <= start)
7101 free_extent_map(em);
7102 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007103 free_extent_map(em);
7104 else
7105 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007106 }
David Sterba172ddd62011-04-21 00:48:27 +02007107 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007108 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05007109 err = -ENOMEM;
7110 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007111 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007112 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05007113 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007114 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007115 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007116 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007117
7118 if (!path) {
7119 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04007120 if (!path) {
7121 err = -ENOMEM;
7122 goto out;
7123 }
7124 /*
7125 * Chances are we'll be called again, so go ahead and do
7126 * readahead
7127 */
David Sterbae4058b52015-11-27 16:31:35 +01007128 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04007129 }
7130
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007131 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007132 if (ret < 0) {
7133 err = ret;
7134 goto out;
7135 }
7136
7137 if (ret != 0) {
7138 if (path->slots[0] == 0)
7139 goto not_found;
7140 path->slots[0]--;
7141 }
7142
Chris Mason5f39d392007-10-15 16:14:19 -04007143 leaf = path->nodes[0];
7144 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007145 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007146 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007148 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007149 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007150 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007151 /*
7152 * If we backup past the first extent we want to move forward
7153 * and see if there is an extent in front of us, otherwise we'll
7154 * say there is a hole for our whole search range which can
7155 * cause problems.
7156 */
7157 extent_end = start;
7158 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007159 }
7160
Chris Mason5f39d392007-10-15 16:14:19 -04007161 found_type = btrfs_file_extent_type(leaf, item);
7162 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007163 if (found_type == BTRFS_FILE_EXTENT_REG ||
7164 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007165 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007166 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007167
7168 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7169 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007170 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7171 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007172 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007173 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007174 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007175
7176 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7177 path->slots[0],
7178 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007179 }
Josef Bacik25a50342013-10-14 12:08:38 -04007180next:
Yan Zheng9036c102008-10-30 14:19:41 -04007181 if (start >= extent_end) {
7182 path->slots[0]++;
7183 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7184 ret = btrfs_next_leaf(root, path);
7185 if (ret < 0) {
7186 err = ret;
7187 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007188 }
Yan Zheng9036c102008-10-30 14:19:41 -04007189 if (ret > 0)
7190 goto not_found;
7191 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007192 }
Yan Zheng9036c102008-10-30 14:19:41 -04007193 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7194 if (found_key.objectid != objectid ||
7195 found_key.type != BTRFS_EXTENT_DATA_KEY)
7196 goto not_found;
7197 if (start + len <= found_key.offset)
7198 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007199 if (start > found_key.offset)
7200 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007201 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007202 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007203 em->len = found_key.offset - start;
7204 goto not_found_em;
7205 }
7206
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007207 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007208 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007209
Yan Zhengd899e052008-10-30 14:25:28 -04007210 if (found_type == BTRFS_FILE_EXTENT_REG ||
7211 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007212 goto insert;
7213 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007214 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007215 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007216 size_t size;
7217 size_t extent_offset;
7218 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007219
Filipe Manana7ffbb592014-06-09 03:48:05 +01007220 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007221 goto out;
Yan689f9342007-10-29 11:41:07 -04007222
Chris Mason514ac8a2014-01-03 21:07:00 -08007223 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007224 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007225 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7226 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007227 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007228 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007229 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007230 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007231 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007232 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007233 if (btrfs_file_extent_compression(leaf, item) !=
7234 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007235 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007236 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007237 if (ret) {
7238 err = ret;
7239 goto out;
7240 }
Chris Masonc8b97812008-10-29 14:49:59 -04007241 } else {
7242 map = kmap(page);
7243 read_extent_buffer(leaf, map + pg_offset, ptr,
7244 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007245 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007246 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007247 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007248 copy_size);
7249 }
Chris Masonc8b97812008-10-29 14:49:59 -04007250 kunmap(page);
7251 }
Chris Mason179e29e2007-11-01 11:28:41 -04007252 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007253 }
Chris Masond1310b22008-01-24 16:13:08 -05007254 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007255 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007256 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007257 }
7258not_found:
7259 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007260 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007261 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007262not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007263 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007264insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007265 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007266 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007267 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007268 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7269 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007270 err = -EIO;
7271 goto out;
7272 }
Chris Masond1310b22008-01-24 16:13:08 -05007273
7274 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007275 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007276 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007277 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007278out:
liubo1abe9b82011-03-24 11:18:59 +00007279
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007280 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007281
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007282 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007283 if (err) {
7284 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007285 return ERR_PTR(err);
7286 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007287 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007288 return em;
7289}
7290
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007291struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7292 struct page *page,
7293 size_t pg_offset, u64 start, u64 len,
7294 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007295{
7296 struct extent_map *em;
7297 struct extent_map *hole_em = NULL;
7298 u64 range_start = start;
7299 u64 end;
7300 u64 found;
7301 u64 found_end;
7302 int err = 0;
7303
7304 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7305 if (IS_ERR(em))
7306 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007307 /*
7308 * If our em maps to:
7309 * - a hole or
7310 * - a pre-alloc extent,
7311 * there might actually be delalloc bytes behind it.
7312 */
7313 if (em->block_start != EXTENT_MAP_HOLE &&
7314 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7315 return em;
7316 else
7317 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007318
7319 /* check to see if we've wrapped (len == -1 or similar) */
7320 end = start + len;
7321 if (end < start)
7322 end = (u64)-1;
7323 else
7324 end -= 1;
7325
7326 em = NULL;
7327
7328 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007329 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007330 end, len, EXTENT_DELALLOC, 1);
7331 found_end = range_start + found;
7332 if (found_end < range_start)
7333 found_end = (u64)-1;
7334
7335 /*
7336 * we didn't find anything useful, return
7337 * the original results from get_extent()
7338 */
7339 if (range_start > end || found_end <= start) {
7340 em = hole_em;
7341 hole_em = NULL;
7342 goto out;
7343 }
7344
7345 /* adjust the range_start to make sure it doesn't
7346 * go backwards from the start they passed in
7347 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307348 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007349 found = found_end - range_start;
7350
7351 if (found > 0) {
7352 u64 hole_start = start;
7353 u64 hole_len = len;
7354
David Sterba172ddd62011-04-21 00:48:27 +02007355 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007356 if (!em) {
7357 err = -ENOMEM;
7358 goto out;
7359 }
7360 /*
7361 * when btrfs_get_extent can't find anything it
7362 * returns one huge hole
7363 *
7364 * make sure what it found really fits our range, and
7365 * adjust to make sure it is based on the start from
7366 * the caller
7367 */
7368 if (hole_em) {
7369 u64 calc_end = extent_map_end(hole_em);
7370
7371 if (calc_end <= start || (hole_em->start > end)) {
7372 free_extent_map(hole_em);
7373 hole_em = NULL;
7374 } else {
7375 hole_start = max(hole_em->start, start);
7376 hole_len = calc_end - hole_start;
7377 }
7378 }
7379 em->bdev = NULL;
7380 if (hole_em && range_start > hole_start) {
7381 /* our hole starts before our delalloc, so we
7382 * have to return just the parts of the hole
7383 * that go until the delalloc starts
7384 */
7385 em->len = min(hole_len,
7386 range_start - hole_start);
7387 em->start = hole_start;
7388 em->orig_start = hole_start;
7389 /*
7390 * don't adjust block start at all,
7391 * it is fixed at EXTENT_MAP_HOLE
7392 */
7393 em->block_start = hole_em->block_start;
7394 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007395 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7396 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007397 } else {
7398 em->start = range_start;
7399 em->len = found;
7400 em->orig_start = range_start;
7401 em->block_start = EXTENT_MAP_DELALLOC;
7402 em->block_len = found;
7403 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007404 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007405 return hole_em;
7406 }
7407out:
7408
7409 free_extent_map(hole_em);
7410 if (err) {
7411 free_extent_map(em);
7412 return ERR_PTR(err);
7413 }
7414 return em;
7415}
7416
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007417static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7418 const u64 start,
7419 const u64 len,
7420 const u64 orig_start,
7421 const u64 block_start,
7422 const u64 block_len,
7423 const u64 orig_block_len,
7424 const u64 ram_bytes,
7425 const int type)
7426{
7427 struct extent_map *em = NULL;
7428 int ret;
7429
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007430 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007431 em = create_io_em(inode, start, len, orig_start,
7432 block_start, block_len, orig_block_len,
7433 ram_bytes,
7434 BTRFS_COMPRESS_NONE, /* compress_type */
7435 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007436 if (IS_ERR(em))
7437 goto out;
7438 }
7439 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7440 len, block_len, type);
7441 if (ret) {
7442 if (em) {
7443 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007444 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007445 start + len - 1, 0);
7446 }
7447 em = ERR_PTR(ret);
7448 }
7449 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007450
7451 return em;
7452}
7453
Josef Bacik4b46fce2010-05-23 11:00:55 -04007454static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7455 u64 start, u64 len)
7456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007457 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007458 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007459 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007460 struct btrfs_key ins;
7461 u64 alloc_hint;
7462 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007463
Josef Bacik4b46fce2010-05-23 11:00:55 -04007464 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007465 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007466 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007467 if (ret)
7468 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007469
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007470 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7471 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007472 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007473 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007474 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007475 btrfs_free_reserved_extent(fs_info, ins.objectid,
7476 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007477
Josef Bacik4b46fce2010-05-23 11:00:55 -04007478 return em;
7479}
7480
Chris Mason46bfbb52010-05-26 11:04:10 -04007481/*
7482 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7483 * block must be cow'd
7484 */
Josef Bacik00361582013-08-14 14:02:47 -04007485noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007486 u64 *orig_start, u64 *orig_block_len,
7487 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007488{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007490 struct btrfs_path *path;
7491 int ret;
7492 struct extent_buffer *leaf;
7493 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007494 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007495 struct btrfs_file_extent_item *fi;
7496 struct btrfs_key key;
7497 u64 disk_bytenr;
7498 u64 backref_offset;
7499 u64 extent_end;
7500 u64 num_bytes;
7501 int slot;
7502 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007503 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007504
Chris Mason46bfbb52010-05-26 11:04:10 -04007505 path = btrfs_alloc_path();
7506 if (!path)
7507 return -ENOMEM;
7508
David Sterbaf85b7372017-01-20 14:54:07 +01007509 ret = btrfs_lookup_file_extent(NULL, root, path,
7510 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007511 if (ret < 0)
7512 goto out;
7513
7514 slot = path->slots[0];
7515 if (ret == 1) {
7516 if (slot == 0) {
7517 /* can't find the item, must cow */
7518 ret = 0;
7519 goto out;
7520 }
7521 slot--;
7522 }
7523 ret = 0;
7524 leaf = path->nodes[0];
7525 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007526 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007527 key.type != BTRFS_EXTENT_DATA_KEY) {
7528 /* not our file or wrong item type, must cow */
7529 goto out;
7530 }
7531
7532 if (key.offset > offset) {
7533 /* Wrong offset, must cow */
7534 goto out;
7535 }
7536
7537 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7538 found_type = btrfs_file_extent_type(leaf, fi);
7539 if (found_type != BTRFS_FILE_EXTENT_REG &&
7540 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7541 /* not a regular extent, must cow */
7542 goto out;
7543 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007544
7545 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7546 goto out;
7547
Miao Xiee77751a2013-12-27 21:11:50 +08007548 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7549 if (extent_end <= offset)
7550 goto out;
7551
Chris Mason46bfbb52010-05-26 11:04:10 -04007552 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007553 if (disk_bytenr == 0)
7554 goto out;
7555
7556 if (btrfs_file_extent_compression(leaf, fi) ||
7557 btrfs_file_extent_encryption(leaf, fi) ||
7558 btrfs_file_extent_other_encoding(leaf, fi))
7559 goto out;
7560
Chris Mason46bfbb52010-05-26 11:04:10 -04007561 backref_offset = btrfs_file_extent_offset(leaf, fi);
7562
Josef Bacik7ee9e442013-06-21 16:37:03 -04007563 if (orig_start) {
7564 *orig_start = key.offset - backref_offset;
7565 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7566 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7567 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007569 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007570 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007571
7572 num_bytes = min(offset + *len, extent_end) - offset;
7573 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7574 u64 range_end;
7575
Jeff Mahoneyda170662016-06-15 09:22:56 -04007576 range_end = round_up(offset + num_bytes,
7577 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007578 ret = test_range_bit(io_tree, offset, range_end,
7579 EXTENT_DELALLOC, 0, NULL);
7580 if (ret) {
7581 ret = -EAGAIN;
7582 goto out;
7583 }
7584 }
7585
Josef Bacik1bda19e2013-10-18 12:10:36 -04007586 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007587
7588 /*
7589 * look for other files referencing this extent, if we
7590 * find any we must cow
7591 */
Josef Bacik00361582013-08-14 14:02:47 -04007592
Liu Boe4c3b2d2017-01-30 12:25:28 -08007593 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007594 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007595 if (ret) {
7596 ret = 0;
7597 goto out;
7598 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007599
7600 /*
7601 * adjust disk_bytenr and num_bytes to cover just the bytes
7602 * in this extent we are about to write. If there
7603 * are any csums in that range we have to cow in order
7604 * to keep the csums correct
7605 */
7606 disk_bytenr += backref_offset;
7607 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007608 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7609 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007610 /*
7611 * all of the above have passed, it is safe to overwrite this extent
7612 * without cow
7613 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007614 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007615 ret = 1;
7616out:
7617 btrfs_free_path(path);
7618 return ret;
7619}
7620
Josef Bacikeb838e72012-07-31 16:28:48 -04007621static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7622 struct extent_state **cached_state, int writing)
7623{
7624 struct btrfs_ordered_extent *ordered;
7625 int ret = 0;
7626
7627 while (1) {
7628 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007629 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 /*
7631 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007632 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007633 * extents in this range.
7634 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007635 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007636 lockend - lockstart + 1);
7637
7638 /*
7639 * We need to make sure there are no buffered pages in this
7640 * range either, we could have raced between the invalidate in
7641 * generic_file_direct_write and locking the extent. The
7642 * invalidate needs to happen so that reads after a write do not
7643 * get stale data.
7644 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007645 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007646 (!writing || !filemap_range_has_page(inode->i_mapping,
7647 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007648 break;
7649
7650 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007651 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007652
7653 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007654 /*
7655 * If we are doing a DIO read and the ordered extent we
7656 * found is for a buffered write, we can not wait for it
7657 * to complete and retry, because if we do so we can
7658 * deadlock with concurrent buffered writes on page
7659 * locks. This happens only if our DIO read covers more
7660 * than one extent map, if at this point has already
7661 * created an ordered extent for a previous extent map
7662 * and locked its range in the inode's io tree, and a
7663 * concurrent write against that previous extent map's
7664 * range and this range started (we unlock the ranges
7665 * in the io tree only when the bios complete and
7666 * buffered writes always lock pages before attempting
7667 * to lock range in the io tree).
7668 */
7669 if (writing ||
7670 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7671 btrfs_start_ordered_extent(inode, ordered, 1);
7672 else
7673 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007674 btrfs_put_ordered_extent(ordered);
7675 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007676 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007677 * We could trigger writeback for this range (and wait
7678 * for it to complete) and then invalidate the pages for
7679 * this range (through invalidate_inode_pages2_range()),
7680 * but that can lead us to a deadlock with a concurrent
7681 * call to readpages() (a buffered read or a defrag call
7682 * triggered a readahead) on a page lock due to an
7683 * ordered dio extent we created before but did not have
7684 * yet a corresponding bio submitted (whence it can not
7685 * complete), which makes readpages() wait for that
7686 * ordered extent to complete while holding a lock on
7687 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007688 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007689 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007690 }
7691
Filipe Mananaade77022016-02-18 14:28:55 +00007692 if (ret)
7693 break;
7694
Josef Bacikeb838e72012-07-31 16:28:48 -04007695 cond_resched();
7696 }
7697
7698 return ret;
7699}
7700
Liu Bo6f9994d2017-01-31 07:50:22 -08007701/* The callers of this must take lock_extent() */
7702static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7703 u64 orig_start, u64 block_start,
7704 u64 block_len, u64 orig_block_len,
7705 u64 ram_bytes, int compress_type,
7706 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007707{
7708 struct extent_map_tree *em_tree;
7709 struct extent_map *em;
7710 struct btrfs_root *root = BTRFS_I(inode)->root;
7711 int ret;
7712
Liu Bo6f9994d2017-01-31 07:50:22 -08007713 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7714 type == BTRFS_ORDERED_COMPRESSED ||
7715 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007716 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007717
Josef Bacik69ffb542012-09-11 15:40:07 -04007718 em_tree = &BTRFS_I(inode)->extent_tree;
7719 em = alloc_extent_map();
7720 if (!em)
7721 return ERR_PTR(-ENOMEM);
7722
7723 em->start = start;
7724 em->orig_start = orig_start;
7725 em->len = len;
7726 em->block_len = block_len;
7727 em->block_start = block_start;
7728 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007729 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007730 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007731 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007732 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007733 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007734 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007735 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007736 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7737 em->compress_type = compress_type;
7738 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007739
7740 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007741 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007742 em->start + em->len - 1, 0);
7743 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007744 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007745 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007746 /*
7747 * The caller has taken lock_extent(), who could race with us
7748 * to add em?
7749 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007750 } while (ret == -EEXIST);
7751
7752 if (ret) {
7753 free_extent_map(em);
7754 return ERR_PTR(ret);
7755 }
7756
Liu Bo6f9994d2017-01-31 07:50:22 -08007757 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007758 return em;
7759}
7760
Josef Bacik4b46fce2010-05-23 11:00:55 -04007761static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7762 struct buffer_head *bh_result, int create)
7763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007765 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307767 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007768 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007769 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007772 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007773
Miao Xie172a5042013-02-21 02:48:22 -07007774 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007775 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007776 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007777 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007778
Josef Bacikc3298612012-08-03 16:49:19 -04007779 lockstart = start;
7780 lockend = start + len - 1;
7781
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007782 if (current->journal_info) {
7783 /*
7784 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007785 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007786 * confused.
7787 */
chandan50745b02015-08-28 21:10:13 +05307788 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007789 current->journal_info = NULL;
7790 }
7791
Josef Bacikeb838e72012-07-31 16:28:48 -04007792 /*
7793 * If this errors out it's because we couldn't invalidate pagecache for
7794 * this range and we need to fallback to buffered.
7795 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007796 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7797 create)) {
7798 ret = -ENOTBLK;
7799 goto err;
7800 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007801
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007802 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007803 if (IS_ERR(em)) {
7804 ret = PTR_ERR(em);
7805 goto unlock_err;
7806 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007807
7808 /*
7809 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7810 * io. INLINE is special, and we could probably kludge it in here, but
7811 * it's still buffered so for safety lets just fall back to the generic
7812 * buffered path.
7813 *
7814 * For COMPRESSED we _have_ to read the entire extent in so we can
7815 * decompress it, so there will be buffering required no matter what we
7816 * do, so go ahead and fallback to buffered.
7817 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007818 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007819 * to buffered IO. Don't blame me, this is the price we pay for using
7820 * the generic code.
7821 */
7822 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7823 em->block_start == EXTENT_MAP_INLINE) {
7824 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007825 ret = -ENOTBLK;
7826 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827 }
7828
7829 /* Just a good old fashioned hole, return */
7830 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7831 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7832 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007833 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007834 }
7835
7836 /*
7837 * We don't allocate a new extent in the following cases
7838 *
7839 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7840 * existing extent.
7841 * 2) The extent is marked as PREALLOC. We're good to go here and can
7842 * just use the extent.
7843 *
7844 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007845 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007846 len = min(len, em->len - (start - em->start));
7847 lockstart = start + len;
7848 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007849 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007850
7851 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7852 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7853 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007854 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007855 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007856
7857 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7858 type = BTRFS_ORDERED_PREALLOC;
7859 else
7860 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007861 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007862 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007863
Josef Bacik00361582013-08-14 14:02:47 -04007864 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007865 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007866 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007867 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007868
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007869 em2 = btrfs_create_dio_extent(inode, start, len,
7870 orig_start, block_start,
7871 len, orig_block_len,
7872 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007873 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007874 if (type == BTRFS_ORDERED_PREALLOC) {
7875 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007876 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007877 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007878 if (em2 && IS_ERR(em2)) {
7879 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007880 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007881 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007882 /*
7883 * For inode marked NODATACOW or extent marked PREALLOC,
7884 * use the existing or preallocated extent, so does not
7885 * need to adjust btrfs_space_info's bytes_may_use.
7886 */
7887 btrfs_free_reserved_data_space_noquota(inode,
7888 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007889 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007890 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007891 }
Josef Bacik00361582013-08-14 14:02:47 -04007892
Chris Mason46bfbb52010-05-26 11:04:10 -04007893 /*
7894 * this will cow the extent, reset the len in case we changed
7895 * it above
7896 */
7897 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007898 free_extent_map(em);
7899 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007900 if (IS_ERR(em)) {
7901 ret = PTR_ERR(em);
7902 goto unlock_err;
7903 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007904 len = min(len, em->len - (start - em->start));
7905unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007906 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7907 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007908 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007909 bh_result->b_bdev = em->bdev;
7910 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007911 if (create) {
7912 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7913 set_buffer_new(bh_result);
7914
7915 /*
7916 * Need to update the i_size under the extent lock so buffered
7917 * readers will get the updated i_size when we unlock.
7918 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007919 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007920 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007921
chandan50745b02015-08-28 21:10:13 +05307922 WARN_ON(dio_data->reserve < len);
7923 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007924 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307925 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007926 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007927
Josef Bacikeb838e72012-07-31 16:28:48 -04007928 /*
7929 * In the case of write we need to clear and unlock the entire range,
7930 * in the case of read we need to unlock only the end area that we
7931 * aren't using if there is any left over space.
7932 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007933 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007934 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7935 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007936 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007937 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007938 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007939 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007940
Josef Bacik4b46fce2010-05-23 11:00:55 -04007941 free_extent_map(em);
7942
7943 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007944
7945unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007946 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007947 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007948err:
chandan50745b02015-08-28 21:10:13 +05307949 if (dio_data)
7950 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007951 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007952}
7953
Omar Sandoval58efbc92017-08-22 23:45:59 -07007954static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7955 struct bio *bio,
7956 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007957{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007959 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007960
Mike Christie37226b22016-06-05 14:31:52 -05007961 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007962
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007963 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007964 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007965 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007966
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007967 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007968
Miao Xie8b110e32014-09-12 18:44:03 +08007969 return ret;
7970}
7971
7972static int btrfs_check_dio_repairable(struct inode *inode,
7973 struct bio *failed_bio,
7974 struct io_failure_record *failrec,
7975 int failed_mirror)
7976{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007977 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007978 int num_copies;
7979
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007980 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007981 if (num_copies == 1) {
7982 /*
7983 * we only have a single copy of the data, so don't bother with
7984 * all the retry and error correction code that follows. no
7985 * matter what the error is, it is very likely to persist.
7986 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007987 btrfs_debug(fs_info,
7988 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7989 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007990 return 0;
7991 }
7992
7993 failrec->failed_mirror = failed_mirror;
7994 failrec->this_mirror++;
7995 if (failrec->this_mirror == failed_mirror)
7996 failrec->this_mirror++;
7997
7998 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007999 btrfs_debug(fs_info,
8000 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8001 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008002 return 0;
8003 }
8004
8005 return 1;
8006}
8007
Omar Sandoval58efbc92017-08-22 23:45:59 -07008008static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8009 struct page *page, unsigned int pgoff,
8010 u64 start, u64 end, int failed_mirror,
8011 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008012{
8013 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008014 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8015 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008016 struct bio *bio;
8017 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008018 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008019 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008020 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008021 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08008022 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08008023
Mike Christie37226b22016-06-05 14:31:52 -05008024 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008025
8026 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8027 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008028 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008029
8030 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8031 failed_mirror);
8032 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008033 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008034 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008035 }
8036
Liu Bo17347ce2017-05-15 15:33:27 -07008037 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08008038 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07008039 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08008040 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008041 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008042
8043 isector = start - btrfs_io_bio(failed_bio)->logical;
8044 isector >>= inode->i_sb->s_blocksize_bits;
8045 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308046 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008047 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008048
8049 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008050 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008051 read_mode, failrec->this_mirror, failrec->in_validation);
8052
Omar Sandoval58efbc92017-08-22 23:45:59 -07008053 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8054 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008055 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008056 bio_put(bio);
8057 }
8058
Omar Sandoval58efbc92017-08-22 23:45:59 -07008059 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008060}
8061
8062struct btrfs_retry_complete {
8063 struct completion done;
8064 struct inode *inode;
8065 u64 start;
8066 int uptodate;
8067};
8068
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008069static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008070{
8071 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008072 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008073 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008074 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008075 int i;
8076
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008077 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008078 goto end;
8079
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308080 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008081 io_tree = &BTRFS_I(inode)->io_tree;
8082 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08008083 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308084
Miao Xie8b110e32014-09-12 18:44:03 +08008085 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008086 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008087 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008088 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8089 io_tree, done->start, bvec->bv_page,
8090 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008091end:
8092 complete(&done->done);
8093 bio_put(bio);
8094}
8095
Omar Sandoval58efbc92017-08-22 23:45:59 -07008096static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8097 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008098{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008100 struct bio_vec bvec;
8101 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008102 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008103 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308104 unsigned int pgoff;
8105 u32 sectorsize;
8106 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008107 blk_status_t ret;
8108 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008109
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008111 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308112
Miao Xiec1dc0892014-09-12 18:43:56 +08008113 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008114 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008115 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008116
Liu Bo17347ce2017-05-15 15:33:27 -07008117 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8118 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8119 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308120
8121next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008122 done.uptodate = 0;
8123 done.start = start;
8124 init_completion(&done.done);
8125
Liu Bo17347ce2017-05-15 15:33:27 -07008126 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308127 pgoff, start, start + sectorsize - 1,
8128 io_bio->mirror_num,
8129 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008130 if (ret) {
8131 err = ret;
8132 goto next;
8133 }
Miao Xie8b110e32014-09-12 18:44:03 +08008134
David Sterba9c17f6c2017-07-19 19:26:45 +02008135 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008136
8137 if (!done.uptodate) {
8138 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308139 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008140 }
8141
Liu Bo629ebf42017-05-15 17:20:07 -07008142next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308143 start += sectorsize;
8144
Liu Bo97bf5a52017-04-07 13:11:10 -07008145 nr_sectors--;
8146 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008148 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308149 goto next_block_or_try_again;
8150 }
Miao Xie8b110e32014-09-12 18:44:03 +08008151 }
8152
Liu Bo629ebf42017-05-15 17:20:07 -07008153 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008154}
8155
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008156static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008157{
8158 struct btrfs_retry_complete *done = bio->bi_private;
8159 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008160 struct extent_io_tree *io_tree, *failure_tree;
8161 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008162 struct bio_vec *bvec;
8163 int uptodate;
8164 int ret;
8165 int i;
8166
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008167 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008168 goto end;
8169
8170 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308171
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008173 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308174
Josef Bacik7870d082017-05-05 11:57:15 -04008175 io_tree = &BTRFS_I(inode)->io_tree;
8176 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8177
David Sterbac09abff2017-07-13 18:10:07 +02008178 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008179 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008180 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8181 bvec->bv_offset, done->start,
8182 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008183 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008184 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8185 failure_tree, io_tree, done->start,
8186 bvec->bv_page,
8187 btrfs_ino(BTRFS_I(inode)),
8188 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008189 else
8190 uptodate = 0;
8191 }
8192
8193 done->uptodate = uptodate;
8194end:
8195 complete(&done->done);
8196 bio_put(bio);
8197}
8198
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008199static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8200 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008201{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308202 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008203 struct bio_vec bvec;
8204 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008205 struct btrfs_retry_complete done;
8206 u64 start;
8207 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308208 u32 sectorsize;
8209 int nr_sectors;
8210 unsigned int pgoff;
8211 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008212 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008213 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008214 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008215
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308216 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008217 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308218
Omar Sandoval58efbc92017-08-22 23:45:59 -07008219 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008220 start = io_bio->logical;
8221 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008222 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008223
Liu Bo17347ce2017-05-15 15:33:27 -07008224 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8225 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308226
Liu Bo17347ce2017-05-15 15:33:27 -07008227 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308228next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008229 if (uptodate) {
8230 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8231 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8232 bvec.bv_page, pgoff, start, sectorsize);
8233 if (likely(!ret))
8234 goto next;
8235 }
Miao Xie8b110e32014-09-12 18:44:03 +08008236try_again:
8237 done.uptodate = 0;
8238 done.start = start;
8239 init_completion(&done.done);
8240
Omar Sandoval58efbc92017-08-22 23:45:59 -07008241 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8242 pgoff, start, start + sectorsize - 1,
8243 io_bio->mirror_num, btrfs_retry_endio,
8244 &done);
8245 if (status) {
8246 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008247 goto next;
8248 }
8249
David Sterba9c17f6c2017-07-19 19:26:45 +02008250 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008251
8252 if (!done.uptodate) {
8253 /* We might have another mirror, so try again */
8254 goto try_again;
8255 }
8256next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308257 offset += sectorsize;
8258 start += sectorsize;
8259
8260 ASSERT(nr_sectors);
8261
Liu Bo97bf5a52017-04-07 13:11:10 -07008262 nr_sectors--;
8263 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308264 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008265 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308266 goto next_block;
8267 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008268 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008269
8270 return err;
8271}
8272
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008273static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8274 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008275{
8276 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8277
8278 if (skip_csum) {
8279 if (unlikely(err))
8280 return __btrfs_correct_data_nocsum(inode, io_bio);
8281 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008282 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008283 } else {
8284 return __btrfs_subio_endio_read(inode, io_bio, err);
8285 }
8286}
8287
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008288static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008289{
8290 struct btrfs_dio_private *dip = bio->bi_private;
8291 struct inode *inode = dip->inode;
8292 struct bio *dio_bio;
8293 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008294 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008295
Liu Bo99c4e3b2017-09-15 15:06:51 -06008296 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008297 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008298
Josef Bacik4b46fce2010-05-23 11:00:55 -04008299 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008300 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008301 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302
Josef Bacik4b46fce2010-05-23 11:00:55 -04008303 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008304
Liu Bo99c4e3b2017-09-15 15:06:51 -06008305 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008306 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008307
8308 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008309 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008310 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008311}
8312
Qu Wenruo524272602017-03-08 10:25:52 +08008313static void __endio_write_update_ordered(struct inode *inode,
8314 const u64 offset, const u64 bytes,
8315 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008316{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008317 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008318 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008319 struct btrfs_workqueue *wq;
8320 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008321 u64 ordered_offset = offset;
8322 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008323 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008324
Qu Wenruo524272602017-03-08 10:25:52 +08008325 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8326 wq = fs_info->endio_freespace_worker;
8327 func = btrfs_freespace_write_helper;
8328 } else {
8329 wq = fs_info->endio_write_workers;
8330 func = btrfs_endio_write_helper;
8331 }
8332
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008333 while (ordered_offset < offset + bytes) {
8334 last_offset = ordered_offset;
8335 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8336 &ordered_offset,
8337 ordered_bytes,
8338 uptodate)) {
8339 btrfs_init_work(&ordered->work, func,
8340 finish_ordered_fn,
8341 NULL, NULL);
8342 btrfs_queue_work(wq, &ordered->work);
8343 }
8344 /*
8345 * If btrfs_dec_test_ordered_pending does not find any ordered
8346 * extent in the range, we can exit.
8347 */
8348 if (ordered_offset == last_offset)
8349 return;
8350 /*
8351 * Our bio might span multiple ordered extents. In this case
8352 * we keep goin until we have accounted the whole dio.
8353 */
8354 if (ordered_offset < offset + bytes) {
8355 ordered_bytes = offset + bytes - ordered_offset;
8356 ordered = NULL;
8357 }
Chris Mason163cf092010-11-28 19:56:33 -05008358 }
Filipe Manana14543772015-11-24 16:23:54 +00008359}
8360
8361static void btrfs_endio_direct_write(struct bio *bio)
8362{
8363 struct btrfs_dio_private *dip = bio->bi_private;
8364 struct bio *dio_bio = dip->dio_bio;
8365
Qu Wenruo524272602017-03-08 10:25:52 +08008366 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008367 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008368
Josef Bacik4b46fce2010-05-23 11:00:55 -04008369 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008370
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008371 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008372 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008373 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008374}
8375
David Sterbad0ee3932018-03-08 14:35:48 +01008376static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008377 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008378{
Josef Bacikc6100a42017-05-05 11:57:13 -04008379 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008380 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008381 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008382 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008383 return 0;
8384}
8385
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008386static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008387{
8388 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008389 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008390
Miao Xie8b110e32014-09-12 18:44:03 +08008391 if (err)
8392 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008393 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008394 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8395 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008396 (unsigned long long)bio->bi_iter.bi_sector,
8397 bio->bi_iter.bi_size, err);
8398
8399 if (dip->subio_endio)
8400 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008401
8402 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008403 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008404 * We want to perceive the errors flag being set before
8405 * decrementing the reference count. We don't need a barrier
8406 * since atomic operations with a return value are fully
8407 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008408 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008409 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008410 }
8411
8412 /* if there are more bios still pending for this dio, just exit */
8413 if (!atomic_dec_and_test(&dip->pending_bios))
8414 goto out;
8415
Chris Mason9be33952013-05-17 18:30:14 -04008416 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008417 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008418 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008419 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008420 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008421 }
8422out:
8423 bio_put(bio);
8424}
8425
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008426static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008427 struct btrfs_dio_private *dip,
8428 struct bio *bio,
8429 u64 file_offset)
8430{
8431 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8432 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008433 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008434
8435 /*
8436 * We load all the csum data we need when we submit
8437 * the first bio to reduce the csum tree search and
8438 * contention.
8439 */
8440 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008441 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008442 file_offset);
8443 if (ret)
8444 return ret;
8445 }
8446
8447 if (bio == dip->orig_bio)
8448 return 0;
8449
8450 file_offset -= dip->logical_offset;
8451 file_offset >>= inode->i_sb->s_blocksize_bits;
8452 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8453
8454 return 0;
8455}
8456
David Sterbad0ee3932018-03-08 14:35:48 +01008457static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8458 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008461 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008462 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008463 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008464
Liu Bo4c274bc2017-11-01 17:19:27 -06008465 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008466 if (async_submit)
8467 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8468
Josef Bacik5fd02042012-05-02 14:00:54 -04008469 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008470 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008471 if (ret)
8472 goto err;
8473 }
Miao Xiee65e1532010-11-22 03:04:43 +00008474
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008475 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008476 goto map;
8477
8478 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008479 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8480 file_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01008481 btrfs_submit_bio_start_direct_io,
8482 btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008483 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008484 } else if (write) {
8485 /*
8486 * If we aren't doing async submit, calculate the csum of the
8487 * bio now.
8488 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008489 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008490 if (ret)
8491 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008492 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008493 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008494 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008495 if (ret)
8496 goto err;
8497 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008498map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008499 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008500err:
Miao Xiee65e1532010-11-22 03:04:43 +00008501 return ret;
8502}
8503
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008504static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008505{
8506 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008507 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008508 struct bio *bio;
8509 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008510 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008511 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008512 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008513 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008514 u64 submit_len;
8515 int clone_offset = 0;
8516 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308517 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008518 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008519
Kent Overstreet4f024f32013-10-11 15:44:27 -07008520 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008521 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008522 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8523 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008524 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008525 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008526
Liu Bo725130b2017-05-16 09:51:39 -07008527 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008528 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008529 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008530 goto submit;
8531 }
8532
David Woodhouse53b381b2013-01-29 18:40:14 -05008533 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008534 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008535 async_submit = 0;
8536 else
8537 async_submit = 1;
8538
Liu Bo725130b2017-05-16 09:51:39 -07008539 /* bio split */
8540 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008541 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008542 do {
Liu Bo725130b2017-05-16 09:51:39 -07008543 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008544
Liu Bo725130b2017-05-16 09:51:39 -07008545 /*
8546 * This will never fail as it's passing GPF_NOFS and
8547 * the allocation is backed by btrfs_bioset.
8548 */
Liu Boe4770942017-05-16 10:57:14 -07008549 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008550 clone_len);
8551 bio->bi_private = dip;
8552 bio->bi_end_io = btrfs_end_dio_bio;
8553 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008554
Liu Bo725130b2017-05-16 09:51:39 -07008555 ASSERT(submit_len >= clone_len);
8556 submit_len -= clone_len;
8557 if (submit_len == 0)
8558 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008559
Liu Bo725130b2017-05-16 09:51:39 -07008560 /*
8561 * Increase the count before we submit the bio so we know
8562 * the end IO handler won't happen before we increase the
8563 * count. Otherwise, the dip might get freed before we're
8564 * done setting it up.
8565 */
8566 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008567
David Sterbad0ee3932018-03-08 14:35:48 +01008568 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008569 async_submit);
8570 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008571 bio_put(bio);
8572 atomic_dec(&dip->pending_bios);
8573 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008574 }
Liu Bo725130b2017-05-16 09:51:39 -07008575
8576 clone_offset += clone_len;
8577 start_sector += clone_len >> 9;
8578 file_offset += clone_len;
8579
8580 map_length = submit_len;
8581 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8582 start_sector << 9, &map_length, NULL, 0);
8583 if (ret)
8584 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008585 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008586
Josef Bacik02f57c72011-04-06 14:25:44 -04008587submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008588 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008589 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008590 return 0;
8591
8592 bio_put(bio);
8593out_err:
8594 dip->errors = 1;
8595 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008596 * Before atomic variable goto zero, we must make sure dip->errors is
8597 * perceived to be set. This ordering is ensured by the fact that an
8598 * atomic operations with a return value are fully ordered as per
8599 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008600 */
Miao Xiee65e1532010-11-22 03:04:43 +00008601 if (atomic_dec_and_test(&dip->pending_bios))
8602 bio_io_error(dip->orig_bio);
8603
8604 /* bio_end_io() will handle error, so we needn't return it */
8605 return 0;
8606}
8607
Mike Christie8a4c1e42016-06-05 14:31:50 -05008608static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8609 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008610{
Filipe Manana61de7182015-07-01 12:13:10 +01008611 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008612 struct bio *bio = NULL;
8613 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008614 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008615 int ret = 0;
8616
David Sterba8b6c1d52017-06-02 17:48:13 +02008617 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008618
Miao Xiec1dc0892014-09-12 18:43:56 +08008619 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008620 if (!dip) {
8621 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008622 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008623 }
8624
8625 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008626 dip->inode = inode;
8627 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008628 dip->bytes = dio_bio->bi_iter.bi_size;
8629 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008630 bio->bi_private = dip;
8631 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008632 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008633 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008634 io_bio = btrfs_io_bio(bio);
8635 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008636
Miao Xiec1dc0892014-09-12 18:43:56 +08008637 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008638 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008639 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008640 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008641 dip->subio_endio = btrfs_subio_endio_read;
8642 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008643
Filipe Mananaf28a4922015-12-08 19:23:20 +00008644 /*
8645 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8646 * even if we fail to submit a bio, because in such case we do the
8647 * corresponding error handling below and it must not be done a second
8648 * time by btrfs_direct_IO().
8649 */
8650 if (write) {
8651 struct btrfs_dio_data *dio_data = current->journal_info;
8652
8653 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8654 dip->bytes;
8655 dio_data->unsubmitted_oe_range_start =
8656 dio_data->unsubmitted_oe_range_end;
8657 }
8658
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008659 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008660 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008661 return;
Chris Mason9be33952013-05-17 18:30:14 -04008662
Liu Bo3892ac92017-04-17 15:00:28 -07008663 if (io_bio->end_io)
8664 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008665
Josef Bacik4b46fce2010-05-23 11:00:55 -04008666free_ordered:
8667 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008668 * If we arrived here it means either we failed to submit the dip
8669 * or we either failed to clone the dio_bio or failed to allocate the
8670 * dip. If we cloned the dio_bio and allocated the dip, we can just
8671 * call bio_endio against our io_bio so that we get proper resource
8672 * cleanup if we fail to submit the dip, otherwise, we must do the
8673 * same as btrfs_endio_direct_[write|read] because we can't call these
8674 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008675 */
Liu Bo3892ac92017-04-17 15:00:28 -07008676 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008677 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008678 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008679 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008680 * and all the cleanup and final put for dio_bio (through
8681 * dio_end_io()).
8682 */
8683 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008684 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008685 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008686 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008687 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008688 file_offset,
8689 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008690 false);
Filipe Manana14543772015-11-24 16:23:54 +00008691 else
Filipe Manana61de7182015-07-01 12:13:10 +01008692 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8693 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008694
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008695 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008696 /*
8697 * Releases and cleans up our dio_bio, no need to bio_put()
8698 * nor bio_endio()/bio_io_error() against dio_bio.
8699 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008700 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008701 }
Liu Bo3892ac92017-04-17 15:00:28 -07008702 if (bio)
8703 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008704 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008705}
8706
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008707static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008708 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008709{
8710 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008711 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008712 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008713 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008714
8715 if (offset & blocksize_mask)
8716 goto out;
8717
Al Viro28060d52014-03-22 05:15:17 -04008718 if (iov_iter_alignment(iter) & blocksize_mask)
8719 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008720
Al Viro28060d52014-03-22 05:15:17 -04008721 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008722 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008723 return 0;
8724 /*
8725 * Check to make sure we don't have duplicate iov_base's in this
8726 * iovec, if so return EINVAL, otherwise we'll get csum errors
8727 * when reading back.
8728 */
8729 for (seg = 0; seg < iter->nr_segs; seg++) {
8730 for (i = seg + 1; i < iter->nr_segs; i++) {
8731 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008732 goto out;
8733 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008734 }
8735 retval = 0;
8736out:
8737 return retval;
8738}
Josef Bacikeb838e72012-07-31 16:28:48 -04008739
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008740static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008741{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008742 struct file *file = iocb->ki_filp;
8743 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008744 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308745 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008746 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008747 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008748 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008749 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008750 bool wakeup = true;
8751 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008752 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008753
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008754 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008755 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008756
Jens Axboefe0f07d2015-04-15 17:05:48 -06008757 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008758
Josef Bacik0e267c42013-07-02 10:38:02 -04008759 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008760 * The generic stuff only does filemap_write_and_wait_range, which
8761 * isn't enough if we've written compressed pages to this area, so
8762 * we need to flush the dirty pages again to make absolutely sure
8763 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008764 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008765 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008766 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8767 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008768 filemap_fdatawrite_range(inode->i_mapping, offset,
8769 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008770
Omar Sandoval6f673762015-03-16 04:33:52 -07008771 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008772 /*
8773 * If the write DIO is beyond the EOF, we need update
8774 * the isize, but it is protected by i_mutex. So we can
8775 * not unlock the i_mutex at this case.
8776 */
8777 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008778 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008779 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008780 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008781 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8782 ret = -EAGAIN;
8783 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008784 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008785 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8786 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008787 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008788 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008789
8790 /*
8791 * We need to know how many extents we reserved so that we can
8792 * do the accounting properly if we go over the number we
8793 * originally calculated. Abuse current->journal_info for this.
8794 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008795 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008796 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008797 dio_data.unsubmitted_oe_range_start = (u64)offset;
8798 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308799 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308800 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008801 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8802 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008803 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008804 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8805 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008806 }
8807
Omar Sandoval17f8c842015-03-16 04:33:50 -07008808 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008809 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008810 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008811 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008812 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308813 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008814 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008815 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308816 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008817 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008818 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008819 /*
8820 * On error we might have left some ordered extents
8821 * without submitting corresponding bios for them, so
8822 * cleanup them up to avoid other tasks getting them
8823 * and waiting for them to complete forever.
8824 */
8825 if (dio_data.unsubmitted_oe_range_start <
8826 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008827 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008828 dio_data.unsubmitted_oe_range_start,
8829 dio_data.unsubmitted_oe_range_end -
8830 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008831 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008832 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008833 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008834 offset, count - (size_t)ret, true);
8835 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008836 }
Miao Xie38851cc2013-02-08 07:04:11 +00008837out:
Miao Xie2e60a512013-02-08 07:01:08 +00008838 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008839 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008840 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008841 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008842
Qu Wenruo364ecf32017-02-27 15:10:38 +08008843 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008844 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008845}
8846
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008847#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8848
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008849static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8850 __u64 start, __u64 len)
8851{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008852 int ret;
8853
8854 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8855 if (ret)
8856 return ret;
8857
David Sterba2135fb92017-06-23 04:09:57 +02008858 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008859}
8860
Chris Mason9ebefb182007-06-15 13:50:00 -04008861int btrfs_readpage(struct file *file, struct page *page)
8862{
Chris Masond1310b22008-01-24 16:13:08 -05008863 struct extent_io_tree *tree;
8864 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008865 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008866}
Chris Mason1832a6d2007-12-21 16:27:21 -05008867
Chris Mason39279cc2007-06-12 06:35:45 -04008868static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8869{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008870 struct inode *inode = page->mapping->host;
8871 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008872
8873 if (current->flags & PF_MEMALLOC) {
8874 redirty_page_for_writepage(wbc, page);
8875 unlock_page(page);
8876 return 0;
8877 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008878
8879 /*
8880 * If we are under memory pressure we will call this directly from the
8881 * VM, we need to make sure we have the inode referenced for the ordered
8882 * extent. If not just return like we didn't do anything.
8883 */
8884 if (!igrab(inode)) {
8885 redirty_page_for_writepage(wbc, page);
8886 return AOP_WRITEPAGE_ACTIVATE;
8887 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008888 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008889 btrfs_add_delayed_iput(inode);
8890 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008891}
Chris Mason39279cc2007-06-12 06:35:45 -04008892
Eric Sandeen48a3b632013-04-25 20:41:01 +00008893static int btrfs_writepages(struct address_space *mapping,
8894 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008895{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008896 return extent_writepages(mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008897}
8898
Chris Mason3ab2fb52007-11-08 10:59:22 -05008899static int
8900btrfs_readpages(struct file *file, struct address_space *mapping,
8901 struct list_head *pages, unsigned nr_pages)
8902{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008903 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008904}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008905
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008907{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008908 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008909 if (ret == 1) {
8910 ClearPagePrivate(page);
8911 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008912 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008913 }
8914 return ret;
8915}
Chris Mason39279cc2007-06-12 06:35:45 -04008916
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8918{
Chris Mason98509cf2008-09-11 15:51:43 -04008919 if (PageWriteback(page) || PageDirty(page))
8920 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008921 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922}
8923
Lukas Czernerd47992f2013-05-21 23:17:23 -04008924static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8925 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008926{
Josef Bacik5fd02042012-05-02 14:00:54 -04008927 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008928 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008929 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008930 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008931 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008932 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308933 u64 start;
8934 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008935 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008936
Chris Mason8b62b722009-09-02 16:53:46 -04008937 /*
8938 * we have the page locked, so new writeback can't start,
8939 * and the dirty bit won't be cleared while we are here.
8940 *
8941 * Wait for IO on this page so that we can safely clear
8942 * the PagePrivate2 bit and do ordered accounting
8943 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008944 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008945
Josef Bacik5fd02042012-05-02 14:00:54 -04008946 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947 if (offset) {
8948 btrfs_releasepage(page, GFP_NOFS);
8949 return;
8950 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008951
8952 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008953 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308954again:
8955 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008956 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308957 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008958 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308959 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008960 /*
8961 * IO on this page will never be started, so we need
8962 * to account for any ordered extents now
8963 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008964 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308965 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008966 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008967 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008968 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008969 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008970 /*
8971 * whoever cleared the private bit is responsible
8972 * for the finish_ordered_io
8973 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008974 if (TestClearPagePrivate2(page)) {
8975 struct btrfs_ordered_inode_tree *tree;
8976 u64 new_len;
8977
8978 tree = &BTRFS_I(inode)->ordered_tree;
8979
8980 spin_lock_irq(&tree->lock);
8981 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308982 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008983 if (new_len < ordered->truncated_len)
8984 ordered->truncated_len = new_len;
8985 spin_unlock_irq(&tree->lock);
8986
8987 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308988 start,
8989 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008990 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008991 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008992 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008993 if (!inode_evicting) {
8994 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308995 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008996 &cached_state);
8997 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308998
8999 start = end + 1;
9000 if (start < page_end)
9001 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009002 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009003
Qu Wenruob9d0b382015-09-29 10:35:16 +08009004 /*
9005 * Qgroup reserved space handler
9006 * Page here will be either
9007 * 1) Already written to disk
9008 * In this case, its reserved space is released from data rsv map
9009 * and will be freed by delayed_ref handler finally.
9010 * So even we call qgroup_free_data(), it won't decrease reserved
9011 * space.
9012 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009013 * This means the reserved space should be freed here. However,
9014 * if a truncate invalidates the page (by clearing PageDirty)
9015 * and the page is accounted for while allocating extent
9016 * in btrfs_check_data_free_space() we let delayed_ref to
9017 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009018 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009019 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009020 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009021 if (!inode_evicting) {
9022 clear_extent_bit(tree, page_start, page_end,
9023 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009024 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9025 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009026 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009027
9028 __btrfs_releasepage(page, GFP_NOFS);
9029 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009030
Chris Mason4a096752008-07-21 10:29:44 -04009031 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009032 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009033 ClearPagePrivate(page);
9034 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009035 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009036 }
Chris Mason39279cc2007-06-12 06:35:45 -04009037}
9038
Chris Mason9ebefb182007-06-15 13:50:00 -04009039/*
9040 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9041 * called from a page fault handler when a page is first dirtied. Hence we must
9042 * be careful to check for EOF conditions here. We set the page up correctly
9043 * for a written page which means we get ENOSPC checking when writing into
9044 * holes and correct delalloc and unwritten extent mapping on filesystems that
9045 * support these features.
9046 *
9047 * We are not allowed to take the i_mutex here so we have to play games to
9048 * protect against truncate races as the page could now be beyond EOF. Because
9049 * vmtruncate() writes the inode size before removing pages, once we have the
9050 * page lock we can determine safely if the page is beyond EOF. If it is not
9051 * beyond EOF, then the page is guaranteed safe against truncation until we
9052 * unlock the page.
9053 */
Dave Jiang11bac802017-02-24 14:56:41 -08009054int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009055{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009056 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009057 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009058 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009059 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9060 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009061 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009062 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009063 char *kaddr;
9064 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009065 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009066 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009067 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309068 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009069 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009070 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309071 u64 end;
9072
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009073 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009074
Jan Karab2b5ef52012-06-12 16:20:45 +02009075 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009076 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009077 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309078 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009079
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309080 /*
9081 * Reserving delalloc space after obtaining the page lock can lead to
9082 * deadlock. For example, if a dirty page is locked by this function
9083 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9084 * dirty page write out, then the btrfs_writepage() function could
9085 * end up waiting indefinitely to get a lock on the page currently
9086 * being processed by btrfs_page_mkwrite() function.
9087 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009088 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309089 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009090 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009091 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009092 reserved = 1;
9093 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009094 if (ret) {
9095 if (ret == -ENOMEM)
9096 ret = VM_FAULT_OOM;
9097 else /* -ENOSPC, -EIO, etc */
9098 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009099 if (reserved)
9100 goto out;
9101 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009102 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009103
Nick Piggin56a76f82009-03-31 15:23:23 -07009104 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009105again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009106 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009107 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009108
Chris Mason9ebefb182007-06-15 13:50:00 -04009109 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009110 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009111 /* page got truncated out from underneath us */
9112 goto out_unlock;
9113 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009114 wait_on_page_writeback(page);
9115
David Sterbaff13db42015-12-03 14:30:40 +01009116 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009117 set_page_extent_mapped(page);
9118
Chris Masoneb84ae02008-07-17 13:53:27 -04009119 /*
9120 * we can't set the delalloc bits if there are pending ordered
9121 * extents. Drop our locks and wait for them to finish
9122 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009123 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9124 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009125 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009126 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009127 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009128 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009129 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009130 btrfs_put_ordered_extent(ordered);
9131 goto again;
9132 }
9133
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009134 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009135 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009136 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009137 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309138 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009139 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009140 page_start, PAGE_SIZE - reserved_space,
9141 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309142 }
9143 }
9144
Josef Bacikfbf19082009-10-01 17:10:23 -04009145 /*
Liu Bo54160342016-12-13 12:15:19 -08009146 * page_mkwrite gets called when the page is firstly dirtied after it's
9147 * faulted in, but write(2) could also dirty a page and set delalloc
9148 * bits, thus in this case for space account reason, we still need to
9149 * clear any delalloc bits within this page range since we have to
9150 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009151 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309152 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009153 EXTENT_DIRTY | EXTENT_DELALLOC |
9154 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009155 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009156
Filipe Mananae3b8a482017-11-04 00:16:59 +00009157 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009158 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009159 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009160 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009161 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009162 ret = VM_FAULT_SIGBUS;
9163 goto out_unlock;
9164 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009165 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009166
9167 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009168 if (page_start + PAGE_SIZE > size)
9169 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009170 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009171 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009172
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009173 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009174 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009175 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009176 flush_dcache_page(page);
9177 kunmap(page);
9178 }
Chris Mason247e7432008-07-17 12:53:51 -04009179 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009180 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009181 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009182
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009183 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009184 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009185 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009186
David Sterbae43bbe52017-12-12 21:43:52 +01009187 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009188
9189out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009190 if (!ret) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009191 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009192 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009193 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009194 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009195 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009196 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009197out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009198 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009199 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009200 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009201out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009202 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009203 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009204 return ret;
9205}
9206
Filipe Manana213e8c52018-02-06 20:40:31 +00009207static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009208{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009209 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009210 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009211 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009212 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009213 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009214 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009215 u64 mask = fs_info->sectorsize - 1;
9216 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009217
Filipe Manana213e8c52018-02-06 20:40:31 +00009218 if (!skip_writeback) {
9219 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9220 (u64)-1);
9221 if (ret)
9222 return ret;
9223 }
Chris Mason39279cc2007-06-12 06:35:45 -04009224
Josef Bacikfcb80c22011-05-03 10:40:22 -04009225 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009226 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009227 * 3 things going on here
9228 *
9229 * 1) We need to reserve space for our orphan item and the space to
9230 * delete our orphan item. Lord knows we don't want to have a dangling
9231 * orphan item because we didn't reserve space to remove it.
9232 *
9233 * 2) We need to reserve space to update our inode.
9234 *
9235 * 3) We need to have something to cache all the space that is going to
9236 * be free'd up by the truncate operation, but also have some slack
9237 * space reserved in case it uses space during the truncate (thank you
9238 * very much snapshotting).
9239 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009240 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009241 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009242 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009243 * doesn't end up using space reserved for updating the inode or
9244 * removing the orphan item. We also need to be able to stop the
9245 * transaction and start a new one, which means we need to be able to
9246 * update the inode several times, and we have no idea of knowing how
9247 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009248 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249 * Then there is the orphan item, which does indeed need to be held on
9250 * to for the whole operation, and we need nobody to touch this reserved
9251 * space except the orphan code.
9252 *
9253 * So that leaves us with
9254 *
9255 * 1) root->orphan_block_rsv - for the orphan deletion.
9256 * 2) rsv - for the truncate reservation, which we will steal from the
9257 * transaction reservation.
9258 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9259 * updating the inode.
9260 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009261 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009262 if (!rsv)
9263 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009264 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009265 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009266
Josef Bacik907cbce2011-08-08 13:46:15 -04009267 /*
Josef Bacik07127182011-08-19 10:29:59 -04009268 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009269 * 1 for updating the inode.
9270 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009271 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009272 if (IS_ERR(trans)) {
9273 err = PTR_ERR(trans);
9274 goto out;
9275 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009276
Josef Bacik907cbce2011-08-08 13:46:15 -04009277 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009278 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009279 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009280 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009281
Chris Mason5a3f23d2009-03-31 13:27:11 -04009282 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009283 * So if we truncate and then write and fsync we normally would just
9284 * write the extents that changed, which is a problem if we need to
9285 * first truncate that entire inode. So set this flag so we write out
9286 * all of the extents in the inode to the sync log so we're completely
9287 * safe.
9288 */
9289 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009290 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009291
Yan, Zheng80825102009-11-12 09:35:36 +00009292 while (1) {
9293 ret = btrfs_truncate_inode_items(trans, root, inode,
9294 inode->i_size,
9295 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009296 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009297 if (ret != -ENOSPC && ret != -EAGAIN) {
Omar Sandovald5014732018-05-22 09:47:58 -07009298 if (ret < 0)
9299 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009300 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009301 }
Chris Mason39279cc2007-06-12 06:35:45 -04009302
Yan, Zheng80825102009-11-12 09:35:36 +00009303 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009304 if (ret) {
9305 err = ret;
9306 break;
9307 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009308
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009309 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009310 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009311
9312 trans = btrfs_start_transaction(root, 2);
9313 if (IS_ERR(trans)) {
9314 ret = err = PTR_ERR(trans);
9315 trans = NULL;
9316 break;
9317 }
9318
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009319 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009320 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009321 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009322 BUG_ON(ret); /* shouldn't happen */
9323 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009324 }
9325
Josef Bacikddfae632017-10-19 14:16:02 -04009326 /*
9327 * We can't call btrfs_truncate_block inside a trans handle as we could
9328 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9329 * we've truncated everything except the last little bit, and can do
9330 * btrfs_truncate_block and then update the disk_i_size.
9331 */
9332 if (ret == NEED_TRUNCATE_BLOCK) {
9333 btrfs_end_transaction(trans);
9334 btrfs_btree_balance_dirty(fs_info);
9335
9336 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9337 if (ret)
9338 goto out;
9339 trans = btrfs_start_transaction(root, 1);
9340 if (IS_ERR(trans)) {
9341 ret = PTR_ERR(trans);
9342 goto out;
9343 }
9344 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9345 }
9346
Yan, Zheng80825102009-11-12 09:35:36 +00009347 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009348 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009349 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009350 if (ret)
9351 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009352 }
9353
Chris Mason917c16b2011-11-08 14:49:59 -05009354 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009355 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009356 ret = btrfs_update_inode(trans, root, inode);
9357 if (ret && !err)
9358 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009359
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009360 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009361 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009362 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009363out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009364 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009365
Josef Bacik3893e332011-01-31 16:03:11 -05009366 if (ret && !err)
9367 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009368
Josef Bacik3893e332011-01-31 16:03:11 -05009369 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009370}
9371
Sven Wegener3b963622008-06-09 21:57:42 -04009372/*
Chris Masond352ac62008-09-29 15:18:18 -04009373 * create a new subvolume directory/inode (helper for the ioctl).
9374 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009375int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009376 struct btrfs_root *new_root,
9377 struct btrfs_root *parent_root,
9378 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009379{
Chris Mason39279cc2007-06-12 06:35:45 -04009380 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009381 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009382 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009383
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009384 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9385 new_dirid, new_dirid,
9386 S_IFDIR | (~current_umask() & S_IRWXUGO),
9387 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009388 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009389 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009390 inode->i_op = &btrfs_dir_inode_operations;
9391 inode->i_fop = &btrfs_dir_file_operations;
9392
Miklos Szeredibfe86842011-10-28 14:13:29 +02009393 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009394 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009395 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009396
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009397 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9398 if (err)
9399 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009400 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009401 new_root->root_key.objectid, err);
9402
Yan, Zheng76dda932009-09-21 16:00:26 -04009403 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009404
Yan, Zheng76dda932009-09-21 16:00:26 -04009405 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009406 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009407}
9408
Chris Mason39279cc2007-06-12 06:35:45 -04009409struct inode *btrfs_alloc_inode(struct super_block *sb)
9410{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009411 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009412 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009414
David Sterba712e36c2017-10-31 17:08:27 +01009415 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009416 if (!ei)
9417 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418
9419 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009420 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009421 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009422 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009423 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009424 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009425 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009426 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009427 ei->disk_i_size = 0;
9428 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009429 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009430 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009431 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009432 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009433 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009434
Josef Bacik9e0baf62011-07-15 15:16:44 +00009435 spin_lock_init(&ei->lock);
9436 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009437 if (sb->s_magic != BTRFS_TEST_MAGIC)
9438 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9439 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009440 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009441 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009442 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009443
Miao Xie16cdcec2011-04-22 18:12:22 +08009444 ei->delayed_node = NULL;
9445
chandan r9cc97d62012-07-04 12:48:07 +05309446 ei->i_otime.tv_sec = 0;
9447 ei->i_otime.tv_nsec = 0;
9448
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009449 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009450 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009451 extent_io_tree_init(&ei->io_tree, inode);
9452 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009453 ei->io_tree.track_uptodate = 1;
9454 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009455 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009456 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009457 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009458 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009459 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009460 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009461 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009462 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009463
9464 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009465}
9466
Josef Bacikaaedb552013-10-11 14:44:09 -04009467#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9468void btrfs_test_destroy_inode(struct inode *inode)
9469{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009470 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009471 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9472}
9473#endif
9474
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009475static void btrfs_i_callback(struct rcu_head *head)
9476{
9477 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009478 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9479}
9480
Chris Mason39279cc2007-06-12 06:35:45 -04009481void btrfs_destroy_inode(struct inode *inode)
9482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009484 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009485 struct btrfs_root *root = BTRFS_I(inode)->root;
9486
Al Virob3d9b7a2012-06-09 13:51:19 -04009487 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009488 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009489 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9490 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009491 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009492 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009493 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009494 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009495 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009496
Chris Mason5a3f23d2009-03-31 13:27:11 -04009497 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009498 * This can happen where we create an inode, but somebody else also
9499 * created the same inode and we need to destroy the one we already
9500 * created.
9501 */
9502 if (!root)
9503 goto free;
9504
Josef Bacik8a35d952012-05-23 14:26:42 -04009505 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9506 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009507 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009508 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009509 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009510 }
Josef Bacik7b128762008-07-24 12:17:14 -04009511
Chris Masond3977122009-01-05 21:25:51 -05009512 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009513 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9514 if (!ordered)
9515 break;
9516 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009517 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009518 "found ordered extent %llu %llu on inode cleanup",
9519 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009520 btrfs_remove_ordered_extent(inode, ordered);
9521 btrfs_put_ordered_extent(ordered);
9522 btrfs_put_ordered_extent(ordered);
9523 }
9524 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009525 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009526 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009527 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009528free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009529 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009530}
9531
Al Viro45321ac2010-06-07 13:43:19 -04009532int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009533{
9534 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009535
Naohiro Aota6379ef92013-06-06 09:56:34 +00009536 if (root == NULL)
9537 return 1;
9538
Liu Bofa6ac872013-02-20 14:10:23 +00009539 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009540 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009541 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009542 else
Al Viro45321ac2010-06-07 13:43:19 -04009543 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009544}
9545
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009546static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009547{
9548 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9549
9550 inode_init_once(&ei->vfs_inode);
9551}
9552
David Sterbae67c7182018-02-19 17:24:18 +01009553void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009554{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009555 /*
9556 * Make sure all delayed rcu free inodes are flushed before we
9557 * destroy cache.
9558 */
9559 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009560 kmem_cache_destroy(btrfs_inode_cachep);
9561 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009562 kmem_cache_destroy(btrfs_path_cachep);
9563 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009564}
9565
Liu Bof5c29bd2017-11-02 17:21:50 -06009566int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009567{
David Sterba837e1972012-09-07 03:00:48 -06009568 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009569 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009570 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9571 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009572 if (!btrfs_inode_cachep)
9573 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009574
David Sterba837e1972012-09-07 03:00:48 -06009575 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009576 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009577 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009578 if (!btrfs_trans_handle_cachep)
9579 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009580
David Sterba837e1972012-09-07 03:00:48 -06009581 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009582 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009583 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009584 if (!btrfs_path_cachep)
9585 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009586
David Sterba837e1972012-09-07 03:00:48 -06009587 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009588 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009589 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009590 if (!btrfs_free_space_cachep)
9591 goto fail;
9592
Chris Mason39279cc2007-06-12 06:35:45 -04009593 return 0;
9594fail:
9595 btrfs_destroy_cachep();
9596 return -ENOMEM;
9597}
9598
David Howellsa528d352017-01-31 16:46:22 +00009599static int btrfs_getattr(const struct path *path, struct kstat *stat,
9600 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009601{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009602 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009603 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009604 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009605 u32 bi_flags = BTRFS_I(inode)->flags;
9606
9607 stat->result_mask |= STATX_BTIME;
9608 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9609 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9610 if (bi_flags & BTRFS_INODE_APPEND)
9611 stat->attributes |= STATX_ATTR_APPEND;
9612 if (bi_flags & BTRFS_INODE_COMPRESS)
9613 stat->attributes |= STATX_ATTR_COMPRESSED;
9614 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9615 stat->attributes |= STATX_ATTR_IMMUTABLE;
9616 if (bi_flags & BTRFS_INODE_NODUMP)
9617 stat->attributes |= STATX_ATTR_NODUMP;
9618
9619 stat->attributes_mask |= (STATX_ATTR_APPEND |
9620 STATX_ATTR_COMPRESSED |
9621 STATX_ATTR_IMMUTABLE |
9622 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009623
Chris Mason39279cc2007-06-12 06:35:45 -04009624 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009625 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009626
9627 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009628 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009629 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009630 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009631 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009632 return 0;
9633}
9634
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009635static int btrfs_rename_exchange(struct inode *old_dir,
9636 struct dentry *old_dentry,
9637 struct inode *new_dir,
9638 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009639{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009640 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009641 struct btrfs_trans_handle *trans;
9642 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009643 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 struct inode *new_inode = new_dentry->d_inode;
9645 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009646 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009648 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9649 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 u64 old_idx = 0;
9651 u64 new_idx = 0;
9652 u64 root_objectid;
9653 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009654 bool root_log_pinned = false;
9655 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656
9657 /* we only allow rename subvolume link between subvolumes */
9658 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9659 return -EXDEV;
9660
9661 /* close the race window with snapshot create/destroy ioctl */
9662 if (old_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 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009665 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666
9667 /*
9668 * We want to reserve the absolute worst case amount of items. So if
9669 * both inodes are subvols and we need to unlink them then that would
9670 * require 4 item modifications, but if they are both normal inodes it
9671 * would require 5 item modifications, so we'll assume their normal
9672 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9673 * should cover the worst case number of items we'll modify.
9674 */
9675 trans = btrfs_start_transaction(root, 12);
9676 if (IS_ERR(trans)) {
9677 ret = PTR_ERR(trans);
9678 goto out_notrans;
9679 }
9680
9681 /*
9682 * We need to find a free sequence number both in the source and
9683 * in the destination directory for the exchange.
9684 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009685 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 if (ret)
9687 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009688 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 if (ret)
9690 goto out_fail;
9691
9692 BTRFS_I(old_inode)->dir_index = 0ULL;
9693 BTRFS_I(new_inode)->dir_index = 0ULL;
9694
9695 /* Reference for the source. */
9696 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9697 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009698 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009700 btrfs_pin_log_trans(root);
9701 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 ret = btrfs_insert_inode_ref(trans, dest,
9703 new_dentry->d_name.name,
9704 new_dentry->d_name.len,
9705 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009706 btrfs_ino(BTRFS_I(new_dir)),
9707 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 if (ret)
9709 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 }
9711
9712 /* And now for the dest. */
9713 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9714 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009715 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009717 btrfs_pin_log_trans(dest);
9718 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 ret = btrfs_insert_inode_ref(trans, root,
9720 old_dentry->d_name.name,
9721 old_dentry->d_name.len,
9722 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009723 btrfs_ino(BTRFS_I(old_dir)),
9724 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 if (ret)
9726 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009727 }
9728
9729 /* Update inode version and ctime/mtime. */
9730 inode_inc_iversion(old_dir);
9731 inode_inc_iversion(new_dir);
9732 inode_inc_iversion(old_inode);
9733 inode_inc_iversion(new_inode);
9734 old_dir->i_ctime = old_dir->i_mtime = ctime;
9735 new_dir->i_ctime = new_dir->i_mtime = ctime;
9736 old_inode->i_ctime = ctime;
9737 new_inode->i_ctime = ctime;
9738
9739 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009740 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9741 BTRFS_I(old_inode), 1);
9742 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9743 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009744 }
9745
9746 /* src is a subvolume */
9747 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9748 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9749 ret = btrfs_unlink_subvol(trans, root, old_dir,
9750 root_objectid,
9751 old_dentry->d_name.name,
9752 old_dentry->d_name.len);
9753 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009754 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9755 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756 old_dentry->d_name.name,
9757 old_dentry->d_name.len);
9758 if (!ret)
9759 ret = btrfs_update_inode(trans, root, old_inode);
9760 }
9761 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009762 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009763 goto out_fail;
9764 }
9765
9766 /* dest is a subvolume */
9767 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9768 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9769 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9770 root_objectid,
9771 new_dentry->d_name.name,
9772 new_dentry->d_name.len);
9773 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009774 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9775 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 new_dentry->d_name.name,
9777 new_dentry->d_name.len);
9778 if (!ret)
9779 ret = btrfs_update_inode(trans, dest, new_inode);
9780 }
9781 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009782 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 goto out_fail;
9784 }
9785
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009786 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009787 new_dentry->d_name.name,
9788 new_dentry->d_name.len, 0, old_idx);
9789 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009790 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009791 goto out_fail;
9792 }
9793
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009794 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795 old_dentry->d_name.name,
9796 old_dentry->d_name.len, 0, new_idx);
9797 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009798 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799 goto out_fail;
9800 }
9801
9802 if (old_inode->i_nlink == 1)
9803 BTRFS_I(old_inode)->dir_index = old_idx;
9804 if (new_inode->i_nlink == 1)
9805 BTRFS_I(new_inode)->dir_index = new_idx;
9806
Filipe Manana86e8aa02016-05-05 02:02:27 +01009807 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009809 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9810 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009812 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009813 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009814 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009815 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009816 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9817 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009819 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009820 }
9821out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009822 /*
9823 * If we have pinned a log and an error happened, we unpin tasks
9824 * trying to sync the log and force them to fallback to a transaction
9825 * commit if the log currently contains any of the inodes involved in
9826 * this rename operation (to ensure we do not persist a log with an
9827 * inconsistent state for any of these inodes or leading to any
9828 * inconsistencies when replayed). If the transaction was aborted, the
9829 * abortion reason is propagated to userspace when attempting to commit
9830 * the transaction. If the log does not contain any of these inodes, we
9831 * allow the tasks to sync it.
9832 */
9833 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009834 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9835 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9836 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009837 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009838 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009839 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009840
9841 if (root_log_pinned) {
9842 btrfs_end_log_trans(root);
9843 root_log_pinned = false;
9844 }
9845 if (dest_log_pinned) {
9846 btrfs_end_log_trans(dest);
9847 dest_log_pinned = false;
9848 }
9849 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009850 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851out_notrans:
9852 if (new_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 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009855 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009856
9857 return ret;
9858}
9859
9860static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9861 struct btrfs_root *root,
9862 struct inode *dir,
9863 struct dentry *dentry)
9864{
9865 int ret;
9866 struct inode *inode;
9867 u64 objectid;
9868 u64 index;
9869
9870 ret = btrfs_find_free_ino(root, &objectid);
9871 if (ret)
9872 return ret;
9873
9874 inode = btrfs_new_inode(trans, root, dir,
9875 dentry->d_name.name,
9876 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009877 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009878 objectid,
9879 S_IFCHR | WHITEOUT_MODE,
9880 &index);
9881
9882 if (IS_ERR(inode)) {
9883 ret = PTR_ERR(inode);
9884 return ret;
9885 }
9886
9887 inode->i_op = &btrfs_special_inode_operations;
9888 init_special_inode(inode, inode->i_mode,
9889 WHITEOUT_DEV);
9890
9891 ret = btrfs_init_inode_security(trans, inode, dir,
9892 &dentry->d_name);
9893 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009894 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009895
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009896 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9897 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009898 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009899 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009900
9901 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009902out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009903 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009904 if (ret)
9905 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009906 iput(inode);
9907
Filipe Mananac9901612016-05-05 01:41:57 +01009908 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009909}
9910
Chris Mason39279cc2007-06-12 06:35:45 -04009911static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009912 struct inode *new_dir, struct dentry *new_dentry,
9913 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009915 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009916 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009917 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009918 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9919 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009920 struct inode *new_inode = d_inode(new_dentry);
9921 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009922 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009923 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009924 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009925 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009926 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009927
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009928 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009929 return -EPERM;
9930
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009931 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009932 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009933 return -EXDEV;
9934
Li Zefan33345d012011-04-20 10:31:50 +08009935 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009936 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009937 return -ENOTEMPTY;
9938
Chris Mason39279cc2007-06-12 06:35:45 -04009939 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009940 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009941 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009942
9943
9944 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009945 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009946 new_dentry->d_name.name,
9947 new_dentry->d_name.len);
9948
9949 if (ret) {
9950 if (ret == -EEXIST) {
9951 /* we shouldn't get
9952 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309953 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009954 return ret;
9955 }
9956 } else {
9957 /* maybe -EOVERFLOW */
9958 return ret;
9959 }
9960 }
9961 ret = 0;
9962
Chris Mason5a3f23d2009-03-31 13:27:11 -04009963 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009964 * we're using rename to replace one file with another. Start IO on it
9965 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009966 */
Chris Mason8d875f92014-08-12 10:47:42 -07009967 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009968 filemap_flush(old_inode->i_mapping);
9969
Yan, Zheng76dda932009-09-21 16:00:26 -04009970 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009971 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009972 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009973 /*
9974 * We want to reserve the absolute worst case amount of items. So if
9975 * both inodes are subvols and we need to unlink them then that would
9976 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009977 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009978 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9979 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009980 * If our rename has the whiteout flag, we need more 5 units for the
9981 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9982 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009983 */
Filipe Manana5062af32016-05-05 10:26:26 +01009984 trans_num_items = 11;
9985 if (flags & RENAME_WHITEOUT)
9986 trans_num_items += 5;
9987 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009988 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009989 ret = PTR_ERR(trans);
9990 goto out_notrans;
9991 }
Chris Mason5f39d392007-10-15 16:14:19 -04009992
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009993 if (dest != root)
9994 btrfs_record_root_in_trans(trans, dest);
9995
Nikolay Borisov877574e2017-02-20 13:50:33 +02009996 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009997 if (ret)
9998 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009999
Miao Xie67de1172013-12-26 13:07:06 +080010000 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010001 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010002 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010003 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010004 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010005 btrfs_pin_log_trans(root);
10006 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010007 ret = btrfs_insert_inode_ref(trans, dest,
10008 new_dentry->d_name.name,
10009 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010010 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010011 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010012 if (ret)
10013 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010014 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010015
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010016 inode_inc_iversion(old_dir);
10017 inode_inc_iversion(new_dir);
10018 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010019 old_dir->i_ctime = old_dir->i_mtime =
10020 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010021 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010022
Chris Mason12fcfd22009-03-24 10:24:20 -040010023 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010024 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10025 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010026
Li Zefan33345d012011-04-20 10:31:50 +080010027 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010028 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10029 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10030 old_dentry->d_name.name,
10031 old_dentry->d_name.len);
10032 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010033 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10034 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010035 old_dentry->d_name.name,
10036 old_dentry->d_name.len);
10037 if (!ret)
10038 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010039 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010040 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010041 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010042 goto out_fail;
10043 }
Chris Mason39279cc2007-06-12 06:35:45 -040010044
10045 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010046 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010047 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010048 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010049 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10050 root_objectid = BTRFS_I(new_inode)->location.objectid;
10051 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10052 root_objectid,
10053 new_dentry->d_name.name,
10054 new_dentry->d_name.len);
10055 BUG_ON(new_inode->i_nlink == 0);
10056 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010057 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10058 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010059 new_dentry->d_name.name,
10060 new_dentry->d_name.len);
10061 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010062 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010063 ret = btrfs_orphan_add(trans,
10064 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010065 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010066 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010067 goto out_fail;
10068 }
Chris Mason39279cc2007-06-12 06:35:45 -040010069 }
Josef Bacikaec74772008-07-24 12:12:38 -040010070
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010071 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010072 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010073 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010074 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010075 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010076 goto out_fail;
10077 }
Chris Mason39279cc2007-06-12 06:35:45 -040010078
Miao Xie67de1172013-12-26 13:07:06 +080010079 if (old_inode->i_nlink == 1)
10080 BTRFS_I(old_inode)->dir_index = index;
10081
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010082 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010083 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010084
David Sterbaf85b7372017-01-20 14:54:07 +010010085 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10086 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010087 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010088 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010089 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010090
10091 if (flags & RENAME_WHITEOUT) {
10092 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10093 old_dentry);
10094
10095 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010096 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010097 goto out_fail;
10098 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010099 }
Chris Mason39279cc2007-06-12 06:35:45 -040010100out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010101 /*
10102 * If we have pinned the log and an error happened, we unpin tasks
10103 * trying to sync the log and force them to fallback to a transaction
10104 * commit if the log currently contains any of the inodes involved in
10105 * this rename operation (to ensure we do not persist a log with an
10106 * inconsistent state for any of these inodes or leading to any
10107 * inconsistencies when replayed). If the transaction was aborted, the
10108 * abortion reason is propagated to userspace when attempting to commit
10109 * the transaction. If the log does not contain any of these inodes, we
10110 * allow the tasks to sync it.
10111 */
10112 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010113 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10114 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10115 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010116 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010117 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010118 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010119
10120 btrfs_end_log_trans(root);
10121 log_pinned = false;
10122 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010123 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010124out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010125 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010126 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010127
Chris Mason39279cc2007-06-12 06:35:45 -040010128 return ret;
10129}
10130
Miklos Szeredi80ace852014-07-23 15:15:32 +020010131static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10132 struct inode *new_dir, struct dentry *new_dentry,
10133 unsigned int flags)
10134{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010135 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010136 return -EINVAL;
10137
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010138 if (flags & RENAME_EXCHANGE)
10139 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10140 new_dentry);
10141
10142 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010143}
10144
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010145struct btrfs_delalloc_work {
10146 struct inode *inode;
10147 struct completion completion;
10148 struct list_head list;
10149 struct btrfs_work work;
10150};
10151
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152static void btrfs_run_delalloc_work(struct btrfs_work *work)
10153{
10154 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010155 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010156
10157 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10158 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010159 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010160 filemap_flush(inode->i_mapping);
10161 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10162 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010163 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164
Nikolay Borisov076da912018-04-23 10:54:16 +030010165 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010166 complete(&delalloc_work->completion);
10167}
10168
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010169static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010170{
10171 struct btrfs_delalloc_work *work;
10172
David Sterba100d5702015-12-08 14:39:32 +010010173 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010174 if (!work)
10175 return NULL;
10176
10177 init_completion(&work->completion);
10178 INIT_LIST_HEAD(&work->list);
10179 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010180 WARN_ON_ONCE(!inode);
10181 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10182 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010183
10184 return work;
10185}
10186
Chris Masond352ac62008-09-29 15:18:18 -040010187/*
10188 * some fairly slow code that needs optimization. This walks the list
10189 * of all the inodes with pending delalloc and forces them to disk.
10190 */
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010191static int start_delalloc_inodes(struct btrfs_root *root, int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010192{
Chris Masonea8c2812008-08-04 23:17:27 -040010193 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010194 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010195 struct btrfs_delalloc_work *work, *next;
10196 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010197 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010198 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010199
Miao Xie8ccf6f192012-10-25 09:28:04 +000010200 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010201 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010202
Miao Xie573bfb72014-03-06 13:55:03 +080010203 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010204 spin_lock(&root->delalloc_lock);
10205 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010206 while (!list_empty(&splice)) {
10207 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010208 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010209
Miao Xieeb73c1b2013-05-15 07:48:22 +000010210 list_move_tail(&binode->delalloc_inodes,
10211 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010212 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010213 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010214 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010215 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010216 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010218
Nikolay Borisov076da912018-04-23 10:54:16 +030010219 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010220 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010221 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010222 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010223 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010224 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010225 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010226 btrfs_queue_work(root->fs_info->flush_workers,
10227 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010228 ret++;
10229 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010230 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010231 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010232 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010233 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010234 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010235
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010236out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010237 list_for_each_entry_safe(work, next, &works, list) {
10238 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010239 wait_for_completion(&work->completion);
10240 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010241 }
10242
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010243 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010244 spin_lock(&root->delalloc_lock);
10245 list_splice_tail(&splice, &root->delalloc_inodes);
10246 spin_unlock(&root->delalloc_lock);
10247 }
Miao Xie573bfb72014-03-06 13:55:03 +080010248 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249 return ret;
10250}
10251
Nikolay Borisov76f32e22018-04-23 10:54:14 +030010252int btrfs_start_delalloc_inodes(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010253{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010254 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010255 int ret;
10256
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010257 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010258 return -EROFS;
10259
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010260 ret = start_delalloc_inodes(root, -1);
Miao Xie6c255e62014-03-06 13:55:01 +080010261 if (ret > 0)
10262 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010263 return ret;
10264}
10265
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010266int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010267{
10268 struct btrfs_root *root;
10269 struct list_head splice;
10270 int ret;
10271
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010272 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010273 return -EROFS;
10274
10275 INIT_LIST_HEAD(&splice);
10276
Miao Xie573bfb72014-03-06 13:55:03 +080010277 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010278 spin_lock(&fs_info->delalloc_root_lock);
10279 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010280 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010281 root = list_first_entry(&splice, struct btrfs_root,
10282 delalloc_root);
10283 root = btrfs_grab_fs_root(root);
10284 BUG_ON(!root);
10285 list_move_tail(&root->delalloc_root,
10286 &fs_info->delalloc_roots);
10287 spin_unlock(&fs_info->delalloc_root_lock);
10288
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010289 ret = start_delalloc_inodes(root, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010290 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010291 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010292 goto out;
10293
Miao Xie6c255e62014-03-06 13:55:01 +080010294 if (nr != -1) {
10295 nr -= ret;
10296 WARN_ON(nr < 0);
10297 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010298 spin_lock(&fs_info->delalloc_root_lock);
10299 }
10300 spin_unlock(&fs_info->delalloc_root_lock);
10301
Miao Xie6c255e62014-03-06 13:55:01 +080010302 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010303out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010304 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010305 spin_lock(&fs_info->delalloc_root_lock);
10306 list_splice_tail(&splice, &fs_info->delalloc_roots);
10307 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010308 }
Miao Xie573bfb72014-03-06 13:55:03 +080010309 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010310 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010311}
10312
Chris Mason39279cc2007-06-12 06:35:45 -040010313static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10314 const char *symname)
10315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010316 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010317 struct btrfs_trans_handle *trans;
10318 struct btrfs_root *root = BTRFS_I(dir)->root;
10319 struct btrfs_path *path;
10320 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010321 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010322 int err;
10323 int drop_inode = 0;
10324 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010325 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010326 int name_len;
10327 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010328 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010329 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010330 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010331
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010332 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010333 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010334 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010335
Josef Bacik9ed74f22009-09-11 16:12:44 -040010336 /*
10337 * 2 items for inode item and ref
10338 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010339 * 1 item for updating parent inode item
10340 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010341 * 1 item for xattr if selinux is on
10342 */
Filipe Manana9269d122015-12-31 18:16:29 +000010343 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010344 if (IS_ERR(trans))
10345 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010346
Li Zefan581bb052011-04-20 10:06:11 +080010347 err = btrfs_find_free_ino(root, &objectid);
10348 if (err)
10349 goto out_unlock;
10350
Josef Bacikaec74772008-07-24 12:12:38 -040010351 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010352 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10353 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010354 if (IS_ERR(inode)) {
10355 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010356 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010357 }
Chris Mason39279cc2007-06-12 06:35:45 -040010358
Casey Schauflerad19db72011-12-15 10:09:07 -050010359 /*
10360 * If the active LSM wants to access the inode during
10361 * d_instantiate it needs these. Smack checks to see
10362 * if the filesystem supports xattrs by looking at the
10363 * ops vector.
10364 */
10365 inode->i_fop = &btrfs_file_operations;
10366 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010367 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010368 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10369
10370 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10371 if (err)
10372 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010373
Chris Mason39279cc2007-06-12 06:35:45 -040010374 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010375 if (!path) {
10376 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010377 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010378 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010379 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010380 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010381 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010382 datasize = btrfs_file_extent_calc_inline_size(name_len);
10383 err = btrfs_insert_empty_item(trans, root, path, &key,
10384 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010385 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010386 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010387 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010388 }
Chris Mason5f39d392007-10-15 16:14:19 -040010389 leaf = path->nodes[0];
10390 ei = btrfs_item_ptr(leaf, path->slots[0],
10391 struct btrfs_file_extent_item);
10392 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10393 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010394 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010395 btrfs_set_file_extent_encryption(leaf, ei, 0);
10396 btrfs_set_file_extent_compression(leaf, ei, 0);
10397 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10398 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10399
Chris Mason39279cc2007-06-12 06:35:45 -040010400 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010401 write_extent_buffer(leaf, symname, ptr, name_len);
10402 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010403 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010404
Chris Mason39279cc2007-06-12 06:35:45 -040010405 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010406 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010407 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010408 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010409 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010410 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010411 /*
10412 * Last step, add directory indexes for our symlink inode. This is the
10413 * last step to avoid extra cleanup of these indexes if an error happens
10414 * elsewhere above.
10415 */
10416 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010417 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10418 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010419 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010420 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010421 goto out_unlock_inode;
10422 }
10423
Al Viro1e2e5472018-05-04 08:23:01 -040010424 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010425
10426out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010427 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010428 if (drop_inode) {
10429 inode_dec_link_count(inode);
10430 iput(inode);
10431 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010432 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010433 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010434
10435out_unlock_inode:
10436 drop_inode = 1;
10437 unlock_new_inode(inode);
10438 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010439}
Chris Mason16432982008-04-10 10:23:21 -040010440
Josef Bacik0af3d002010-06-21 14:48:16 -040010441static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10442 u64 start, u64 num_bytes, u64 min_size,
10443 loff_t actual_len, u64 *alloc_hint,
10444 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010446 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010447 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10448 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010449 struct btrfs_root *root = BTRFS_I(inode)->root;
10450 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010451 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010452 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010453 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010454 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010455 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010456 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010457 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010458
Josef Bacik0af3d002010-06-21 14:48:16 -040010459 if (trans)
10460 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010461 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010462 if (own_trans) {
10463 trans = btrfs_start_transaction(root, 3);
10464 if (IS_ERR(trans)) {
10465 ret = PTR_ERR(trans);
10466 break;
10467 }
Yan Zhengd899e052008-10-30 14:25:28 -040010468 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010469
Byongho Leeee221842015-12-15 01:42:10 +090010470 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010471 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010472 /*
10473 * If we are severely fragmented we could end up with really
10474 * small allocations, so if the allocator is returning small
10475 * chunks lets make its job easier by only searching for those
10476 * sized chunks.
10477 */
10478 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010479 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10480 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010481 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010482 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010483 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010484 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010485 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010486 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010487
Josef Bacik0b670dc2015-09-23 17:11:16 -040010488 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010489 ret = insert_reserved_file_extent(trans, inode,
10490 cur_offset, ins.objectid,
10491 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010492 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010493 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010494 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010495 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010496 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010497 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010498 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010499 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010500 break;
10501 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010502
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010503 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010504 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010505
Josef Bacik5dc562c2012-08-17 13:14:17 -040010506 em = alloc_extent_map();
10507 if (!em) {
10508 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10509 &BTRFS_I(inode)->runtime_flags);
10510 goto next;
10511 }
10512
10513 em->start = cur_offset;
10514 em->orig_start = cur_offset;
10515 em->len = ins.offset;
10516 em->block_start = ins.objectid;
10517 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010518 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010519 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010520 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010521 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10522 em->generation = trans->transid;
10523
10524 while (1) {
10525 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010526 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010527 write_unlock(&em_tree->lock);
10528 if (ret != -EEXIST)
10529 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010530 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010531 cur_offset + ins.offset - 1,
10532 0);
10533 }
10534 free_extent_map(em);
10535next:
Yan Zhengd899e052008-10-30 14:25:28 -040010536 num_bytes -= ins.offset;
10537 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010538 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010539
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010540 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010541 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010542 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010543 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010544 (actual_len > inode->i_size) &&
10545 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010546 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010547 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010548 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010549 i_size = cur_offset;
10550 i_size_write(inode, i_size);
10551 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010552 }
10553
Yan Zhengd899e052008-10-30 14:25:28 -040010554 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010555
10556 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010557 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010558 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010559 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010560 break;
10561 }
Yan Zhengd899e052008-10-30 14:25:28 -040010562
Josef Bacik0af3d002010-06-21 14:48:16 -040010563 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010564 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010565 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010566 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010567 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010568 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010569 return ret;
10570}
10571
Josef Bacik0af3d002010-06-21 14:48:16 -040010572int btrfs_prealloc_file_range(struct inode *inode, int mode,
10573 u64 start, u64 num_bytes, u64 min_size,
10574 loff_t actual_len, u64 *alloc_hint)
10575{
10576 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10577 min_size, actual_len, alloc_hint,
10578 NULL);
10579}
10580
10581int btrfs_prealloc_file_range_trans(struct inode *inode,
10582 struct btrfs_trans_handle *trans, int mode,
10583 u64 start, u64 num_bytes, u64 min_size,
10584 loff_t actual_len, u64 *alloc_hint)
10585{
10586 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10587 min_size, actual_len, alloc_hint, trans);
10588}
10589
Chris Masone6dcd2d2008-07-17 12:53:50 -040010590static int btrfs_set_page_dirty(struct page *page)
10591{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010592 return __set_page_dirty_nobuffers(page);
10593}
10594
Al Viro10556cb2011-06-20 19:28:19 -040010595static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010596{
Li Zefanb83cc962010-12-20 16:04:08 +080010597 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010598 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010599
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010600 if (mask & MAY_WRITE &&
10601 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10602 if (btrfs_root_readonly(root))
10603 return -EROFS;
10604 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10605 return -EACCES;
10606 }
Al Viro2830ba72011-06-20 19:16:29 -040010607 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010608}
Chris Mason39279cc2007-06-12 06:35:45 -040010609
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010610static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10611{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010612 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010613 struct btrfs_trans_handle *trans;
10614 struct btrfs_root *root = BTRFS_I(dir)->root;
10615 struct inode *inode = NULL;
10616 u64 objectid;
10617 u64 index;
10618 int ret = 0;
10619
10620 /*
10621 * 5 units required for adding orphan entry
10622 */
10623 trans = btrfs_start_transaction(root, 5);
10624 if (IS_ERR(trans))
10625 return PTR_ERR(trans);
10626
10627 ret = btrfs_find_free_ino(root, &objectid);
10628 if (ret)
10629 goto out;
10630
10631 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010632 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633 if (IS_ERR(inode)) {
10634 ret = PTR_ERR(inode);
10635 inode = NULL;
10636 goto out;
10637 }
10638
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010639 inode->i_fop = &btrfs_file_operations;
10640 inode->i_op = &btrfs_file_inode_operations;
10641
10642 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010643 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10644
Chris Masonb0d5d102014-09-08 13:08:51 -070010645 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10646 if (ret)
10647 goto out_inode;
10648
10649 ret = btrfs_update_inode(trans, root, inode);
10650 if (ret)
10651 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010652 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010653 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010654 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010655
Filipe Manana5762b5c2014-08-01 00:10:32 +010010656 /*
10657 * We set number of links to 0 in btrfs_new_inode(), and here we set
10658 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10659 * through:
10660 *
10661 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10662 */
10663 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010664 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010665 d_tmpfile(dentry, inode);
10666 mark_inode_dirty(inode);
10667
10668out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010669 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010670 if (ret)
10671 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010672 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010673 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010674
10675out_inode:
10676 unlock_new_inode(inode);
10677 goto out;
10678
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010679}
10680
David Sterba20a7db82017-02-17 16:24:29 +010010681__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010682static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010683{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010684 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010685}
10686
Josef Bacikc6100a42017-05-05 11:57:13 -040010687static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10688{
10689 struct inode *inode = private_data;
10690 return btrfs_sb(inode->i_sb);
10691}
10692
10693static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10694 u64 start, u64 end)
10695{
10696 struct inode *inode = private_data;
10697 u64 isize;
10698
10699 isize = i_size_read(inode);
10700 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10701 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10702 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10703 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10704 }
10705}
10706
10707void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10708{
10709 struct inode *inode = private_data;
10710 unsigned long index = start >> PAGE_SHIFT;
10711 unsigned long end_index = end >> PAGE_SHIFT;
10712 struct page *page;
10713
10714 while (index <= end_index) {
10715 page = find_get_page(inode->i_mapping, index);
10716 ASSERT(page); /* Pages should be in the extent_io_tree */
10717 set_page_writeback(page);
10718 put_page(page);
10719 index++;
10720 }
10721}
10722
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010723static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010724 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010725 .lookup = btrfs_lookup,
10726 .create = btrfs_create,
10727 .unlink = btrfs_unlink,
10728 .link = btrfs_link,
10729 .mkdir = btrfs_mkdir,
10730 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010731 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010732 .symlink = btrfs_symlink,
10733 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010734 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010735 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010736 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010737 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010738 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010739 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010740 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010741};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010742static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010743 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010744 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010745 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010746};
Yan, Zheng76dda932009-09-21 16:00:26 -040010747
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010748static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010749 .llseek = generic_file_llseek,
10750 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010751 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010752 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010753 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010754#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010755 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010756#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010757 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010758 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010759};
10760
David Sterba20e55062015-11-19 11:42:28 +010010761static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010762 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010763 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010764 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010765 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010766 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010767 .tree_fs_info = iotree_fs_info,
10768 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010769
10770 /* optional callbacks */
10771 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010772 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010773 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010774 .set_bit_hook = btrfs_set_bit_hook,
10775 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010776 .merge_extent_hook = btrfs_merge_extent_hook,
10777 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010778 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010779};
10780
Chris Mason35054392009-01-21 13:11:13 -050010781/*
10782 * btrfs doesn't support the bmap operation because swapfiles
10783 * use bmap to make a mapping of extents in the file. They assume
10784 * these extents won't change over the life of the file and they
10785 * use the bmap result to do IO directly to the drive.
10786 *
10787 * the btrfs bmap call would return logical addresses that aren't
10788 * suitable for IO and they also will change frequently as COW
10789 * operations happen. So, swapfile + btrfs == corruption.
10790 *
10791 * For now we're avoiding this by dropping bmap.
10792 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010793static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010794 .readpage = btrfs_readpage,
10795 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010796 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010797 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010798 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010799 .invalidatepage = btrfs_invalidatepage,
10800 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010801 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010802 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010803};
10804
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010805static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010806 .readpage = btrfs_readpage,
10807 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010808 .invalidatepage = btrfs_invalidatepage,
10809 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010810};
10811
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010812static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010813 .getattr = btrfs_getattr,
10814 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010815 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010816 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010817 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010818 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010819 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010820 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010821};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010822static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010823 .getattr = btrfs_getattr,
10824 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010825 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010826 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010827 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010828 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010829 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010830};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010831static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010832 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010833 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010834 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010835 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010836 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010837 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010838};
Yan, Zheng76dda932009-09-21 16:00:26 -040010839
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010840const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010841 .d_delete = btrfs_dentry_delete,
10842};