blob: 32613dca13c133596afdc6a059f370c59f53f801 [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 */);
Su Yue090a127a2018-05-30 16:48:56 +08001021 if (IS_ERR(em)) {
1022 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001023 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001024 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001025 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001026
Chris Masone6dcd2d2008-07-17 12:53:50 -04001027 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001028 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001029 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001030 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001031
Yan Zheng17d217f2008-12-12 10:03:38 -05001032 if (root->root_key.objectid ==
1033 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1034 ret = btrfs_reloc_clone_csums(inode, start,
1035 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001036 /*
1037 * Only drop cache here, and process as normal.
1038 *
1039 * We must not allow extent_clear_unlock_delalloc()
1040 * at out_unlock label to free meta of this ordered
1041 * extent, as its meta should be freed by
1042 * btrfs_finish_ordered_io().
1043 *
1044 * So we must continue until @start is increased to
1045 * skip current ordered extent.
1046 */
Josef Bacik00361582013-08-14 14:02:47 -04001047 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001048 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1049 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001050 }
1051
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001052 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001053
Chris Masonc8b97812008-10-29 14:49:59 -04001054 /* we're not doing compressed IO, don't unlock the first
1055 * page (which the caller expects to stay locked), don't
1056 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001057 *
1058 * Do set the Private2 bit so we know this page was properly
1059 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001060 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001061 page_ops = unlock ? PAGE_UNLOCK : 0;
1062 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001063
Josef Bacikc2790a22013-07-29 11:20:47 -04001064 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001065 start + ram_size - 1,
1066 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001067 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001068 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001069 if (num_bytes < cur_alloc_size)
1070 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071 else
Anand Jain3752d222018-02-15 12:29:38 +08001072 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001073 alloc_hint = ins.objectid + ins.offset;
1074 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001075 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001076
1077 /*
1078 * btrfs_reloc_clone_csums() error, since start is increased
1079 * extent_clear_unlock_delalloc() at out_unlock label won't
1080 * free metadata of current ordered extent, we're OK to exit.
1081 */
1082 if (ret)
1083 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001084 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001085out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001086 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001087
Filipe Mananad9f85962014-08-25 10:43:00 +01001088out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001089 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001090out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001091 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001092 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001093out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001094 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1095 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001096 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1097 PAGE_END_WRITEBACK;
1098 /*
1099 * If we reserved an extent for our delalloc range (or a subrange) and
1100 * failed to create the respective ordered extent, then it means that
1101 * when we reserved the extent we decremented the extent's size from
1102 * the data space_info's bytes_may_use counter and incremented the
1103 * space_info's bytes_reserved counter by the same amount. We must make
1104 * sure extent_clear_unlock_delalloc() does not try to decrement again
1105 * the data space_info's bytes_may_use counter, therefore we do not pass
1106 * it the flag EXTENT_CLEAR_DATA_RESV.
1107 */
1108 if (extent_reserved) {
1109 extent_clear_unlock_delalloc(inode, start,
1110 start + cur_alloc_size,
1111 start + cur_alloc_size,
1112 locked_page,
1113 clear_bits,
1114 page_ops);
1115 start += cur_alloc_size;
1116 if (start >= end)
1117 goto out;
1118 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001119 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1120 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 clear_bits | EXTENT_CLEAR_DATA_RESV,
1122 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001123 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001124}
Chris Masonc8b97812008-10-29 14:49:59 -04001125
Chris Mason771ed682008-11-06 22:02:51 -05001126/*
1127 * work queue call back to started compression on a file and pages
1128 */
1129static noinline void async_cow_start(struct btrfs_work *work)
1130{
1131 struct async_cow *async_cow;
1132 int num_added = 0;
1133 async_cow = container_of(work, struct async_cow, work);
1134
1135 compress_file_range(async_cow->inode, async_cow->locked_page,
1136 async_cow->start, async_cow->end, async_cow,
1137 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001138 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001139 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001140 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001141 }
Chris Mason771ed682008-11-06 22:02:51 -05001142}
1143
1144/*
1145 * work queue call back to submit previously compressed pages
1146 */
1147static noinline void async_cow_submit(struct btrfs_work *work)
1148{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001149 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001150 struct async_cow *async_cow;
1151 struct btrfs_root *root;
1152 unsigned long nr_pages;
1153
1154 async_cow = container_of(work, struct async_cow, work);
1155
1156 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001157 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001158 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1159 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001160
David Sterba093258e2018-02-26 16:15:17 +01001161 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001163 5 * SZ_1M)
1164 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001165
Chris Masond3977122009-01-05 21:25:51 -05001166 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001167 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001168}
Chris Masonc8b97812008-10-29 14:49:59 -04001169
Chris Mason771ed682008-11-06 22:02:51 -05001170static noinline void async_cow_free(struct btrfs_work *work)
1171{
1172 struct async_cow *async_cow;
1173 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001174 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001175 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001176 kfree(async_cow);
1177}
1178
1179static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1180 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001181 unsigned long *nr_written,
1182 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001185 struct async_cow *async_cow;
1186 struct btrfs_root *root = BTRFS_I(inode)->root;
1187 unsigned long nr_pages;
1188 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001189
Chris Masona3429ab2009-10-08 12:30:20 -04001190 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001191 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001192 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001193 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001195 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001196 async_cow->root = root;
1197 async_cow->locked_page = locked_page;
1198 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001199 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001200
Wang Shilongf79707b2014-07-17 11:44:09 +08001201 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001202 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001203 cur_end = end;
1204 else
Byongho Leeee221842015-12-15 01:42:10 +09001205 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001206
1207 async_cow->end = cur_end;
1208 INIT_LIST_HEAD(&async_cow->extents);
1209
Liu Bo9e0af232014-08-15 23:36:53 +08001210 btrfs_init_work(&async_cow->work,
1211 btrfs_delalloc_helper,
1212 async_cow_start, async_cow_submit,
1213 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001215 nr_pages = (cur_end - start + PAGE_SIZE) >>
1216 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001218
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001219 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001220
Chris Mason771ed682008-11-06 22:02:51 -05001221 *nr_written += nr_pages;
1222 start = cur_end + 1;
1223 }
1224 *page_started = 1;
1225 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001226}
1227
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001228static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001229 u64 bytenr, u64 num_bytes)
1230{
1231 int ret;
1232 struct btrfs_ordered_sum *sums;
1233 LIST_HEAD(list);
1234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001235 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001236 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001237 if (ret == 0 && list_empty(&list))
1238 return 0;
1239
1240 while (!list_empty(&list)) {
1241 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1242 list_del(&sums->list);
1243 kfree(sums);
1244 }
Liu Bo58113752018-01-31 17:09:13 -07001245 if (ret < 0)
1246 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001247 return 1;
1248}
1249
Chris Masond352ac62008-09-29 15:18:18 -04001250/*
1251 * when nowcow writeback call back. This checks for snapshots or COW copies
1252 * of the extents that exist in the file, and COWs the file as required.
1253 *
1254 * If no cow copies or snapshots exist, we write directly to the existing
1255 * blocks on disk
1256 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001257static noinline int run_delalloc_nocow(struct inode *inode,
1258 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001259 u64 start, u64 end, int *page_started, int force,
1260 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001261{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001263 struct btrfs_root *root = BTRFS_I(inode)->root;
1264 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001265 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001267 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001268 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 u64 cow_start;
1270 u64 cur_offset;
1271 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001272 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 u64 disk_bytenr;
1274 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001275 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001276 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001279 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 int nocow;
1281 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001282 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001283 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
1285 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001286 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001287 extent_clear_unlock_delalloc(inode, start, end, end,
1288 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001289 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001290 EXTENT_DO_ACCOUNTING |
1291 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001292 PAGE_CLEAR_DIRTY |
1293 PAGE_SET_WRITEBACK |
1294 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001295 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001296 }
Li Zefan82d59022011-04-20 10:33:24 +08001297
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001298 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 cow_start = (u64)-1;
1301 cur_offset = start;
1302 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001303 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001305 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1308 leaf = path->nodes[0];
1309 btrfs_item_key_to_cpu(leaf, &found_key,
1310 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001311 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 found_key.type == BTRFS_EXTENT_DATA_KEY)
1313 path->slots[0]--;
1314 }
1315 check_prev = 0;
1316next_slot:
1317 leaf = path->nodes[0];
1318 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1319 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001320 if (ret < 0) {
1321 if (cow_start != (u64)-1)
1322 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001324 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 if (ret > 0)
1326 break;
1327 leaf = path->nodes[0];
1328 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001329
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 nocow = 0;
1331 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
Filipe Manana1d512cb2015-11-09 00:33:58 +00001335 if (found_key.objectid > ino)
1336 break;
1337 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1338 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1339 path->slots[0]++;
1340 goto next_slot;
1341 }
1342 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 found_key.offset > end)
1344 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001345
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 if (found_key.offset > cur_offset) {
1347 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001348 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 goto out_check;
1350 }
Chris Masonc31f8832008-01-08 15:46:31 -05001351
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 fi = btrfs_item_ptr(leaf, path->slots[0],
1353 struct btrfs_file_extent_item);
1354 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001355
Josef Bacikcc95bef2013-04-04 14:31:27 -04001356 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1358 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 extent_end = found_key.offset +
1362 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001363 disk_num_bytes =
1364 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 if (extent_end <= start) {
1366 path->slots[0]++;
1367 goto next_slot;
1368 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001369 if (disk_bytenr == 0)
1370 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (btrfs_file_extent_compression(leaf, fi) ||
1372 btrfs_file_extent_encryption(leaf, fi) ||
1373 btrfs_file_extent_other_encoding(leaf, fi))
1374 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001375 /*
1376 * Do the same check as in btrfs_cross_ref_exist but
1377 * without the unnecessary search.
1378 */
1379 if (btrfs_file_extent_generation(leaf, fi) <=
1380 btrfs_root_last_snapshot(&root->root_item))
1381 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001382 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1383 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001384 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001386 ret = btrfs_cross_ref_exist(root, ino,
1387 found_key.offset -
1388 extent_offset, disk_bytenr);
1389 if (ret) {
1390 /*
1391 * ret could be -EIO if the above fails to read
1392 * metadata.
1393 */
1394 if (ret < 0) {
1395 if (cow_start != (u64)-1)
1396 cur_offset = cow_start;
1397 goto error;
1398 }
1399
1400 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001401 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001402 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001403 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001404 disk_bytenr += cur_offset - found_key.offset;
1405 num_bytes = min(end + 1, extent_end) - cur_offset;
1406 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001407 * if there are pending snapshots for this root,
1408 * we fall into common COW way.
1409 */
1410 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001411 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001412 if (!err)
1413 goto out_check;
1414 }
1415 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001416 * force cow if csum exists in the range.
1417 * this ensure that csum for a given extent are
1418 * either valid or do not exist.
1419 */
Liu Bo58113752018-01-31 17:09:13 -07001420 ret = csum_exist_in_range(fs_info, disk_bytenr,
1421 num_bytes);
1422 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001423 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001424 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001425
1426 /*
1427 * ret could be -EIO if the above fails to read
1428 * metadata.
1429 */
1430 if (ret < 0) {
1431 if (cow_start != (u64)-1)
1432 cur_offset = cow_start;
1433 goto error;
1434 }
1435 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001436 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001437 }
1438 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1439 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001440 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001441 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001442 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 nocow = 1;
1444 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1445 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001446 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001447 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001448 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 } else {
1450 BUG_ON(1);
1451 }
1452out_check:
1453 if (extent_end <= start) {
1454 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001455 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001456 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001457 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001458 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 goto next_slot;
1460 }
1461 if (!nocow) {
1462 if (cow_start == (u64)-1)
1463 cow_start = cur_offset;
1464 cur_offset = extent_end;
1465 if (cur_offset > end)
1466 break;
1467 path->slots[0]++;
1468 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001469 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001470
David Sterbab3b4aa72011-04-21 01:20:15 +02001471 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001472 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001473 ret = cow_file_range(inode, locked_page,
1474 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001475 end, page_started, nr_written, 1,
1476 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001477 if (ret) {
1478 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001479 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001480 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001481 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001482 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001483 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001484 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001486 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001487
Yan Zhengd899e052008-10-30 14:25:28 -04001488 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001489 u64 orig_start = found_key.offset - extent_offset;
1490
1491 em = create_io_em(inode, cur_offset, num_bytes,
1492 orig_start,
1493 disk_bytenr, /* block_start */
1494 num_bytes, /* block_len */
1495 disk_num_bytes, /* orig_block_len */
1496 ram_bytes, BTRFS_COMPRESS_NONE,
1497 BTRFS_ORDERED_PREALLOC);
1498 if (IS_ERR(em)) {
1499 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001500 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001501 if (nocow)
1502 btrfs_dec_nocow_writers(fs_info,
1503 disk_bytenr);
1504 ret = PTR_ERR(em);
1505 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001506 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001507 free_extent_map(em);
1508 }
1509
1510 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001511 type = BTRFS_ORDERED_PREALLOC;
1512 } else {
1513 type = BTRFS_ORDERED_NOCOW;
1514 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001515
1516 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001517 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001518 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001519 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001520 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001521
Yan, Zhengefa56462010-05-16 10:49:59 -04001522 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001523 BTRFS_DATA_RELOC_TREE_OBJECTID)
1524 /*
1525 * Error handled later, as we must prevent
1526 * extent_clear_unlock_delalloc() in error handler
1527 * from freeing metadata of created ordered extent.
1528 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001529 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1530 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001531
Josef Bacikc2790a22013-07-29 11:20:47 -04001532 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001533 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001534 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001535 EXTENT_DELALLOC |
1536 EXTENT_CLEAR_DATA_RESV,
1537 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1538
Wang Shilonge9894fd2014-03-27 11:12:25 +08001539 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001540 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001541 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001542
1543 /*
1544 * btrfs_reloc_clone_csums() error, now we're OK to call error
1545 * handler, as metadata for created ordered extent will only
1546 * be freed by btrfs_finish_ordered_io().
1547 */
1548 if (ret)
1549 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001550 if (cur_offset > end)
1551 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001552 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001553 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001554
Josef Bacik17ca04a2012-05-31 15:58:55 -04001555 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001556 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001557 cur_offset = end;
1558 }
1559
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001561 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1562 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001563 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001565 }
1566
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001567error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001568 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001569 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001570 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001571 EXTENT_DELALLOC | EXTENT_DEFRAG |
1572 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1573 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001574 PAGE_SET_WRITEBACK |
1575 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001576 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001577 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001578}
1579
Wang Shilong47059d92014-07-03 18:22:07 +08001580static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1581{
1582
1583 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1584 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1585 return 0;
1586
1587 /*
1588 * @defrag_bytes is a hint value, no spinlock held here,
1589 * if is not zero, it means the file is defragging.
1590 * Force cow if given extent needs to be defragged.
1591 */
1592 if (BTRFS_I(inode)->defrag_bytes &&
1593 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1594 EXTENT_DEFRAG, 0, NULL))
1595 return 1;
1596
1597 return 0;
1598}
1599
Chris Masond352ac62008-09-29 15:18:18 -04001600/*
1601 * extent_io.c call back to do delayed allocation processing
1602 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001603static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001604 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001605 unsigned long *nr_written,
1606 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001607{
Josef Bacikc6100a42017-05-05 11:57:13 -04001608 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001609 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001610 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001611 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001612
Wang Shilong47059d92014-07-03 18:22:07 +08001613 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001614 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001615 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001616 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001617 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001618 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001619 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001620 ret = cow_file_range(inode, locked_page, start, end, end,
1621 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001622 } else {
1623 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1624 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001625 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001626 page_started, nr_written,
1627 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001628 }
Qu Wenruo524272602017-03-08 10:25:52 +08001629 if (ret)
1630 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001631 return ret;
1632}
1633
Josef Bacikc6100a42017-05-05 11:57:13 -04001634static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001635 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001636{
Josef Bacikc6100a42017-05-05 11:57:13 -04001637 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 u64 size;
1639
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001640 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001641 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001642 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643
Josef Bacikdcab6a32015-02-11 15:08:59 -05001644 size = orig->end - orig->start + 1;
1645 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001646 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001647 u64 new_size;
1648
1649 /*
Josef Bacikba117212015-03-13 15:01:24 -04001650 * See the explanation in btrfs_merge_extent_hook, the same
1651 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652 */
1653 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001654 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001655 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001656 num_extents += count_max_extents(new_size);
1657 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001658 return;
1659 }
1660
Josef Bacik9e0baf62011-07-15 15:16:44 +00001661 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001662 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001663 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001664}
1665
1666/*
1667 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1668 * extents so we can keep track of new extents that are just merged onto old
1669 * extents, such as when we are doing sequential writes, so we can properly
1670 * account for the metadata space we'll need.
1671 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001672static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001673 struct extent_state *new,
1674 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001675{
Josef Bacikc6100a42017-05-05 11:57:13 -04001676 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001677 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001678 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001679
Josef Bacik9ed74f22009-09-11 16:12:44 -04001680 /* not delalloc, ignore it */
1681 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001682 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001683
Josef Bacik8461a3d2015-03-13 15:12:08 -04001684 if (new->start > other->start)
1685 new_size = new->end - other->start + 1;
1686 else
1687 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001688
1689 /* we're not bigger than the max, unreserve the space and go */
1690 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1691 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001692 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001693 spin_unlock(&BTRFS_I(inode)->lock);
1694 return;
1695 }
1696
1697 /*
Josef Bacikba117212015-03-13 15:01:24 -04001698 * We have to add up either side to figure out how many extents were
1699 * accounted for before we merged into one big extent. If the number of
1700 * extents we accounted for is <= the amount we need for the new range
1701 * then we can return, otherwise drop. Think of it like this
1702 *
1703 * [ 4k][MAX_SIZE]
1704 *
1705 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1706 * need 2 outstanding extents, on one side we have 1 and the other side
1707 * we have 1 so they are == and we can return. But in this case
1708 *
1709 * [MAX_SIZE+4k][MAX_SIZE+4k]
1710 *
1711 * Each range on their own accounts for 2 extents, but merged together
1712 * they are only 3 extents worth of accounting, so we need to drop in
1713 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001714 */
Josef Bacikba117212015-03-13 15:01:24 -04001715 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001716 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001717 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001718 num_extents += count_max_extents(old_size);
1719 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001720 return;
1721
Josef Bacik9e0baf62011-07-15 15:16:44 +00001722 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001723 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001724 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001725}
1726
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1728 struct inode *inode)
1729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001730 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1731
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732 spin_lock(&root->delalloc_lock);
1733 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1734 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1735 &root->delalloc_inodes);
1736 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1737 &BTRFS_I(inode)->runtime_flags);
1738 root->nr_delalloc_inodes++;
1739 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 BUG_ON(!list_empty(&root->delalloc_root));
1742 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 &fs_info->delalloc_roots);
1744 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 }
1746 }
1747 spin_unlock(&root->delalloc_lock);
1748}
1749
Nikolay Borisov2b877332018-04-27 12:21:51 +03001750
1751void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1752 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001753{
David Sterba3ffbd682018-06-29 10:56:42 +02001754 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001755
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001756 if (!list_empty(&inode->delalloc_inodes)) {
1757 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001758 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001759 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001760 root->nr_delalloc_inodes--;
1761 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001762 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001764 BUG_ON(list_empty(&root->delalloc_root));
1765 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001766 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001767 }
1768 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001769}
1770
1771static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1772 struct btrfs_inode *inode)
1773{
1774 spin_lock(&root->delalloc_lock);
1775 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001776 spin_unlock(&root->delalloc_lock);
1777}
1778
Chris Masond352ac62008-09-29 15:18:18 -04001779/*
1780 * extent_io.c set_bit_hook, used to track delayed allocation
1781 * bytes in this file, and to maintain the list of inodes that
1782 * have pending delalloc work to be done.
1783 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001784static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001785 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001786{
Josef Bacikc6100a42017-05-05 11:57:13 -04001787 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001788
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1790
Wang Shilong47059d92014-07-03 18:22:07 +08001791 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1792 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001793 /*
1794 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001795 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001796 * bit, which is only set or cleared with irqs on
1797 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001798 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001799 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001800 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001801 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001802 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001803
Josef Bacik8b62f872017-10-19 14:15:55 -04001804 spin_lock(&BTRFS_I(inode)->lock);
1805 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1806 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001807
Josef Bacik6a3891c2015-03-16 17:38:52 -04001808 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001809 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001810 return;
1811
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001812 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1813 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001814 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001815 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001816 if (*bits & EXTENT_DEFRAG)
1817 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001818 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001819 &BTRFS_I(inode)->runtime_flags))
1820 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001821 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001822 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001823
1824 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1825 (*bits & EXTENT_DELALLOC_NEW)) {
1826 spin_lock(&BTRFS_I(inode)->lock);
1827 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1828 state->start;
1829 spin_unlock(&BTRFS_I(inode)->lock);
1830 }
Chris Mason291d6732008-01-29 15:55:23 -05001831}
1832
Chris Masond352ac62008-09-29 15:18:18 -04001833/*
1834 * extent_io.c clear_bit_hook, see set_bit_hook for why
1835 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001836static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001837 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001838 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001839{
Josef Bacikc6100a42017-05-05 11:57:13 -04001840 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001842 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001843 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001844
Filipe Manana4a4b9642017-07-27 19:52:55 +01001845 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1846 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001847 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001848 spin_unlock(&inode->lock);
1849 }
Wang Shilong47059d92014-07-03 18:22:07 +08001850
Chris Mason75eff682008-12-15 15:54:40 -05001851 /*
1852 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001853 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001854 * bit, which is only set or cleared with irqs on
1855 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001856 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001858 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001859
Josef Bacik8b62f872017-10-19 14:15:55 -04001860 spin_lock(&inode->lock);
1861 btrfs_mod_outstanding_extents(inode, -num_extents);
1862 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001863
Josef Bacikb6d08f02013-09-27 14:57:43 -04001864 /*
1865 * We don't reserve metadata space for space cache inodes so we
1866 * don't need to call dellalloc_release_metadata if there is an
1867 * error.
1868 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001869 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001871 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001872
Josef Bacik6a3891c2015-03-16 17:38:52 -04001873 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001874 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001875 return;
1876
Filipe Mananaa315e682017-03-06 23:04:20 +00001877 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1878 do_list && !(state->state & EXTENT_NORESERVE) &&
1879 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001880 btrfs_free_reserved_data_space_noquota(
1881 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001882 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001883
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001884 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1885 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001886 spin_lock(&inode->lock);
1887 inode->delalloc_bytes -= len;
1888 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001889 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001890 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001891 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001892 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001893 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001894
1895 if ((state->state & EXTENT_DELALLOC_NEW) &&
1896 (*bits & EXTENT_DELALLOC_NEW)) {
1897 spin_lock(&inode->lock);
1898 ASSERT(inode->new_delalloc_bytes >= len);
1899 inode->new_delalloc_bytes -= len;
1900 spin_unlock(&inode->lock);
1901 }
Chris Mason291d6732008-01-29 15:55:23 -05001902}
1903
Chris Masond352ac62008-09-29 15:18:18 -04001904/*
1905 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1906 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001907 *
1908 * return 1 if page cannot be merged to bio
1909 * return 0 if page can be merged to bio
1910 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001911 */
Mike Christie81a75f672016-06-05 14:31:54 -05001912int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001913 size_t size, struct bio *bio,
1914 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001916 struct inode *inode = page->mapping->host;
1917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001918 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001919 u64 length = 0;
1920 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001921 int ret;
1922
Chris Mason771ed682008-11-06 22:02:51 -05001923 if (bio_flags & EXTENT_BIO_COMPRESSED)
1924 return 0;
1925
Kent Overstreet4f024f32013-10-11 15:44:27 -07001926 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001927 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001928 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1929 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001930 if (ret < 0)
1931 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001932 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001933 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001934 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001935}
1936
Chris Masond352ac62008-09-29 15:18:18 -04001937/*
1938 * in order to insert checksums into the metadata in large chunks,
1939 * we wait until bio submission time. All the pages in the bio are
1940 * checksummed and sums are attached onto the ordered extent record.
1941 *
1942 * At IO completion time the cums attached on the ordered extent record
1943 * are inserted into the btree
1944 */
David Sterbad0ee3932018-03-08 14:35:48 +01001945static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001946 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001947{
Josef Bacikc6100a42017-05-05 11:57:13 -04001948 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001949 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001950
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001951 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001953 return 0;
1954}
Chris Masone0156402008-04-16 11:15:20 -04001955
Chris Mason4a69a412008-11-06 22:03:00 -05001956/*
1957 * in order to insert checksums into the metadata in large chunks,
1958 * we wait until bio submission time. All the pages in the bio are
1959 * checksummed and sums are attached onto the ordered extent record.
1960 *
1961 * At IO completion time the cums attached on the ordered extent record
1962 * are inserted into the btree
1963 */
David Sterbad0ee3932018-03-08 14:35:48 +01001964static blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001965 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001966{
Josef Bacikc6100a42017-05-05 11:57:13 -04001967 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001968 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001969 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001970
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001971 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001972 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001973 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001974 bio_endio(bio);
1975 }
Stefan Behrens61891922012-11-05 18:51:52 +01001976 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001977}
1978
Chris Masond352ac62008-09-29 15:18:18 -04001979/*
Chris Masoncad321a2008-12-17 14:51:42 -05001980 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001981 * on write, or reading the csums from the tree before a read.
1982 *
1983 * Rules about async/sync submit,
1984 * a) read: sync submit
1985 *
1986 * b) write without checksum: sync submit
1987 *
1988 * c) write with checksum:
1989 * c-1) if bio is issued by fsync: sync submit
1990 * (sync_writers != 0)
1991 *
1992 * c-2) if root is reloc root: sync submit
1993 * (only in case of buffered IO)
1994 *
1995 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001996 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001997static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001998 int mirror_num, unsigned long bio_flags,
1999 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04002000{
Josef Bacikc6100a42017-05-05 11:57:13 -04002001 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002003 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302004 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002005 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002006 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002007 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002008
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002009 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002010
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002011 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302012 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002013
Mike Christie37226b22016-06-05 14:31:52 -05002014 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002015 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002016 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002017 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002018
Chris Masond20f7042008-12-08 16:58:54 -05002019 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002020 ret = btrfs_submit_compressed_read(inode, bio,
2021 mirror_num,
2022 bio_flags);
2023 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002024 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002025 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002026 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002027 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002028 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002029 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002030 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002031 /* csum items have already been cloned */
2032 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2033 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002034 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002035 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2036 bio_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01002037 btrfs_submit_bio_start,
2038 btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002039 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002040 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002041 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002042 if (ret)
2043 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002044 }
2045
Chris Mason0b86a832008-03-24 15:01:56 -04002046mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002047 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002048
2049out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002050 if (ret) {
2051 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002052 bio_endio(bio);
2053 }
Stefan Behrens61891922012-11-05 18:51:52 +01002054 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002055}
Chris Mason6885f302008-02-20 16:11:05 -05002056
Chris Masond352ac62008-09-29 15:18:18 -04002057/*
2058 * given a list of ordered sums record them in the inode. This happens
2059 * at IO completion time based on sums calculated at bio submission time.
2060 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002061static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002062 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002063{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002064 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002065 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002067 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002068 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002069 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002070 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002071 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002072 if (ret)
2073 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002074 }
2075 return 0;
2076}
2077
Josef Bacik2ac55d42010-02-03 19:33:23 +00002078int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002079 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002080 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002081{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002083 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002084 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002085}
2086
Chris Masond352ac62008-09-29 15:18:18 -04002087/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002088struct btrfs_writepage_fixup {
2089 struct page *page;
2090 struct btrfs_work work;
2091};
2092
Christoph Hellwigb2950862008-12-02 09:54:17 -05002093static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002094{
2095 struct btrfs_writepage_fixup *fixup;
2096 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002097 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002098 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002099 struct page *page;
2100 struct inode *inode;
2101 u64 page_start;
2102 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002103 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002104
2105 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2106 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002107again:
Chris Mason247e7432008-07-17 12:53:51 -04002108 lock_page(page);
2109 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2110 ClearPageChecked(page);
2111 goto out_page;
2112 }
2113
2114 inode = page->mapping->host;
2115 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002116 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002117
David Sterbaff13db42015-12-03 14:30:40 +01002118 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002119 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002120
2121 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002122 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002123 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002124
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002125 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002126 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002127 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002128 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002129 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002130 unlock_page(page);
2131 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002132 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002133 goto again;
2134 }
Chris Mason247e7432008-07-17 12:53:51 -04002135
Qu Wenruo364ecf32017-02-27 15:10:38 +08002136 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002137 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002138 if (ret) {
2139 mapping_set_error(page->mapping, ret);
2140 end_extent_writepage(page, ret, page_start, page_end);
2141 ClearPageChecked(page);
2142 goto out;
2143 }
2144
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002145 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2146 &cached_state, 0);
2147 if (ret) {
2148 mapping_set_error(page->mapping, ret);
2149 end_extent_writepage(page, ret, page_start, page_end);
2150 ClearPageChecked(page);
2151 goto out;
2152 }
2153
Chris Mason247e7432008-07-17 12:53:51 -04002154 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002155 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002156 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002157out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002158 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002159 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002160out_page:
2161 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002162 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002163 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002164 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002165}
2166
2167/*
2168 * There are a few paths in the higher layers of the kernel that directly
2169 * set the page dirty bit without asking the filesystem if it is a
2170 * good idea. This causes problems because we want to make sure COW
2171 * properly happens and the data=ordered rules are followed.
2172 *
Chris Masonc8b97812008-10-29 14:49:59 -04002173 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002174 * hasn't been properly setup for IO. We kick off an async process
2175 * to fix it up. The async helper will wait for ordered extents, set
2176 * the delalloc bit and make it safe to write the page.
2177 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002178static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002179{
2180 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002182 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002183
Chris Mason8b62b722009-09-02 16:53:46 -04002184 /* this page is properly in the ordered list */
2185 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002186 return 0;
2187
2188 if (PageChecked(page))
2189 return -EAGAIN;
2190
2191 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2192 if (!fixup)
2193 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002194
Chris Mason247e7432008-07-17 12:53:51 -04002195 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002196 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002197 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2198 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002199 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002200 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002201 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002202}
2203
Yan Zhengd899e052008-10-30 14:25:28 -04002204static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2205 struct inode *inode, u64 file_pos,
2206 u64 disk_bytenr, u64 disk_num_bytes,
2207 u64 num_bytes, u64 ram_bytes,
2208 u8 compression, u8 encryption,
2209 u16 other_encoding, int extent_type)
2210{
2211 struct btrfs_root *root = BTRFS_I(inode)->root;
2212 struct btrfs_file_extent_item *fi;
2213 struct btrfs_path *path;
2214 struct extent_buffer *leaf;
2215 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002216 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002217 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002218 int ret;
2219
2220 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002221 if (!path)
2222 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002223
Chris Masona1ed8352009-09-11 12:27:37 -04002224 /*
2225 * we may be replacing one extent in the tree with another.
2226 * The new extent is pinned in the extent map, and we don't want
2227 * to drop it from the cache until it is completely in the btree.
2228 *
2229 * So, tell btrfs_drop_extents to leave this extent in the cache.
2230 * the caller is expected to unpin it and allow it to be merged
2231 * with the others.
2232 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002233 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2234 file_pos + num_bytes, NULL, 0,
2235 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 if (ret)
2237 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002238
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002239 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002240 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002241 ins.offset = file_pos;
2242 ins.type = BTRFS_EXTENT_DATA_KEY;
2243
2244 path->leave_spinning = 1;
2245 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2246 sizeof(*fi));
2247 if (ret)
2248 goto out;
2249 }
Yan Zhengd899e052008-10-30 14:25:28 -04002250 leaf = path->nodes[0];
2251 fi = btrfs_item_ptr(leaf, path->slots[0],
2252 struct btrfs_file_extent_item);
2253 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2254 btrfs_set_file_extent_type(leaf, fi, extent_type);
2255 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2256 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2257 btrfs_set_file_extent_offset(leaf, fi, 0);
2258 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2259 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2260 btrfs_set_file_extent_compression(leaf, fi, compression);
2261 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2262 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002263
Yan Zhengd899e052008-10-30 14:25:28 -04002264 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002265 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002266
2267 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002268
2269 ins.objectid = disk_bytenr;
2270 ins.offset = disk_num_bytes;
2271 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002272
Qu Wenruo297d7502015-09-08 17:08:37 +08002273 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002274 * Release the reserved range from inode dirty range map, as it is
2275 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002276 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002277 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2278 if (ret < 0)
2279 goto out;
2280 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002281 ret = btrfs_alloc_reserved_file_extent(trans, root,
2282 btrfs_ino(BTRFS_I(inode)),
2283 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002284out:
Yan Zhengd899e052008-10-30 14:25:28 -04002285 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002286
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002288}
2289
Liu Bo38c227d2013-01-29 03:18:40 +00002290/* snapshot-aware defrag */
2291struct sa_defrag_extent_backref {
2292 struct rb_node node;
2293 struct old_sa_defrag_extent *old;
2294 u64 root_id;
2295 u64 inum;
2296 u64 file_pos;
2297 u64 extent_offset;
2298 u64 num_bytes;
2299 u64 generation;
2300};
2301
2302struct old_sa_defrag_extent {
2303 struct list_head list;
2304 struct new_sa_defrag_extent *new;
2305
2306 u64 extent_offset;
2307 u64 bytenr;
2308 u64 offset;
2309 u64 len;
2310 int count;
2311};
2312
2313struct new_sa_defrag_extent {
2314 struct rb_root root;
2315 struct list_head head;
2316 struct btrfs_path *path;
2317 struct inode *inode;
2318 u64 file_pos;
2319 u64 len;
2320 u64 bytenr;
2321 u64 disk_len;
2322 u8 compress_type;
2323};
2324
2325static int backref_comp(struct sa_defrag_extent_backref *b1,
2326 struct sa_defrag_extent_backref *b2)
2327{
2328 if (b1->root_id < b2->root_id)
2329 return -1;
2330 else if (b1->root_id > b2->root_id)
2331 return 1;
2332
2333 if (b1->inum < b2->inum)
2334 return -1;
2335 else if (b1->inum > b2->inum)
2336 return 1;
2337
2338 if (b1->file_pos < b2->file_pos)
2339 return -1;
2340 else if (b1->file_pos > b2->file_pos)
2341 return 1;
2342
2343 /*
2344 * [------------------------------] ===> (a range of space)
2345 * |<--->| |<---->| =============> (fs/file tree A)
2346 * |<---------------------------->| ===> (fs/file tree B)
2347 *
2348 * A range of space can refer to two file extents in one tree while
2349 * refer to only one file extent in another tree.
2350 *
2351 * So we may process a disk offset more than one time(two extents in A)
2352 * and locate at the same extent(one extent in B), then insert two same
2353 * backrefs(both refer to the extent in B).
2354 */
2355 return 0;
2356}
2357
2358static void backref_insert(struct rb_root *root,
2359 struct sa_defrag_extent_backref *backref)
2360{
2361 struct rb_node **p = &root->rb_node;
2362 struct rb_node *parent = NULL;
2363 struct sa_defrag_extent_backref *entry;
2364 int ret;
2365
2366 while (*p) {
2367 parent = *p;
2368 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2369
2370 ret = backref_comp(backref, entry);
2371 if (ret < 0)
2372 p = &(*p)->rb_left;
2373 else
2374 p = &(*p)->rb_right;
2375 }
2376
2377 rb_link_node(&backref->node, parent, p);
2378 rb_insert_color(&backref->node, root);
2379}
2380
2381/*
2382 * Note the backref might has changed, and in this case we just return 0.
2383 */
2384static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2385 void *ctx)
2386{
2387 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002388 struct old_sa_defrag_extent *old = ctx;
2389 struct new_sa_defrag_extent *new = old->new;
2390 struct btrfs_path *path = new->path;
2391 struct btrfs_key key;
2392 struct btrfs_root *root;
2393 struct sa_defrag_extent_backref *backref;
2394 struct extent_buffer *leaf;
2395 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002397 int slot;
2398 int ret;
2399 u64 extent_offset;
2400 u64 num_bytes;
2401
2402 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002403 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002404 return 0;
2405
2406 key.objectid = root_id;
2407 key.type = BTRFS_ROOT_ITEM_KEY;
2408 key.offset = (u64)-1;
2409
Liu Bo38c227d2013-01-29 03:18:40 +00002410 root = btrfs_read_fs_root_no_name(fs_info, &key);
2411 if (IS_ERR(root)) {
2412 if (PTR_ERR(root) == -ENOENT)
2413 return 0;
2414 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002415 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002416 inum, offset, root_id);
2417 return PTR_ERR(root);
2418 }
2419
2420 key.objectid = inum;
2421 key.type = BTRFS_EXTENT_DATA_KEY;
2422 if (offset > (u64)-1 << 32)
2423 key.offset = 0;
2424 else
2425 key.offset = offset;
2426
2427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302428 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002429 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002430 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002431
2432 while (1) {
2433 cond_resched();
2434
2435 leaf = path->nodes[0];
2436 slot = path->slots[0];
2437
2438 if (slot >= btrfs_header_nritems(leaf)) {
2439 ret = btrfs_next_leaf(root, path);
2440 if (ret < 0) {
2441 goto out;
2442 } else if (ret > 0) {
2443 ret = 0;
2444 goto out;
2445 }
2446 continue;
2447 }
2448
2449 path->slots[0]++;
2450
2451 btrfs_item_key_to_cpu(leaf, &key, slot);
2452
2453 if (key.objectid > inum)
2454 goto out;
2455
2456 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2457 continue;
2458
2459 extent = btrfs_item_ptr(leaf, slot,
2460 struct btrfs_file_extent_item);
2461
2462 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2463 continue;
2464
Liu Boe68afa42013-07-01 22:13:26 +08002465 /*
2466 * 'offset' refers to the exact key.offset,
2467 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2468 * (key.offset - extent_offset).
2469 */
2470 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002471 continue;
2472
Liu Boe68afa42013-07-01 22:13:26 +08002473 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002474 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002475
Liu Bo38c227d2013-01-29 03:18:40 +00002476 if (extent_offset >= old->extent_offset + old->offset +
2477 old->len || extent_offset + num_bytes <=
2478 old->extent_offset + old->offset)
2479 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002480 break;
2481 }
2482
2483 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2484 if (!backref) {
2485 ret = -ENOENT;
2486 goto out;
2487 }
2488
2489 backref->root_id = root_id;
2490 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002491 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002492 backref->num_bytes = num_bytes;
2493 backref->extent_offset = extent_offset;
2494 backref->generation = btrfs_file_extent_generation(leaf, extent);
2495 backref->old = old;
2496 backref_insert(&new->root, backref);
2497 old->count++;
2498out:
2499 btrfs_release_path(path);
2500 WARN_ON(ret);
2501 return ret;
2502}
2503
2504static noinline bool record_extent_backrefs(struct btrfs_path *path,
2505 struct new_sa_defrag_extent *new)
2506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002508 struct old_sa_defrag_extent *old, *tmp;
2509 int ret;
2510
2511 new->path = path;
2512
2513 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002514 ret = iterate_inodes_from_logical(old->bytenr +
2515 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002516 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002517 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002518 if (ret < 0 && ret != -ENOENT)
2519 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002520
2521 /* no backref to be processed for this extent */
2522 if (!old->count) {
2523 list_del(&old->list);
2524 kfree(old);
2525 }
2526 }
2527
2528 if (list_empty(&new->head))
2529 return false;
2530
2531 return true;
2532}
2533
2534static int relink_is_mergable(struct extent_buffer *leaf,
2535 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002536 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002537{
Liu Bo116e0022013-08-02 16:30:40 +08002538 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002539 return 0;
2540
2541 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2542 return 0;
2543
Liu Bo116e0022013-08-02 16:30:40 +08002544 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2545 return 0;
2546
2547 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002548 btrfs_file_extent_other_encoding(leaf, fi))
2549 return 0;
2550
2551 return 1;
2552}
2553
2554/*
2555 * Note the backref might has changed, and in this case we just return 0.
2556 */
2557static noinline int relink_extent_backref(struct btrfs_path *path,
2558 struct sa_defrag_extent_backref *prev,
2559 struct sa_defrag_extent_backref *backref)
2560{
2561 struct btrfs_file_extent_item *extent;
2562 struct btrfs_file_extent_item *item;
2563 struct btrfs_ordered_extent *ordered;
2564 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002565 struct btrfs_root *root;
2566 struct btrfs_key key;
2567 struct extent_buffer *leaf;
2568 struct old_sa_defrag_extent *old = backref->old;
2569 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002570 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002571 struct inode *inode;
2572 struct extent_state *cached = NULL;
2573 int ret = 0;
2574 u64 start;
2575 u64 len;
2576 u64 lock_start;
2577 u64 lock_end;
2578 bool merge = false;
2579 int index;
2580
2581 if (prev && prev->root_id == backref->root_id &&
2582 prev->inum == backref->inum &&
2583 prev->file_pos + prev->num_bytes == backref->file_pos)
2584 merge = true;
2585
2586 /* step 1: get root */
2587 key.objectid = backref->root_id;
2588 key.type = BTRFS_ROOT_ITEM_KEY;
2589 key.offset = (u64)-1;
2590
Liu Bo38c227d2013-01-29 03:18:40 +00002591 index = srcu_read_lock(&fs_info->subvol_srcu);
2592
2593 root = btrfs_read_fs_root_no_name(fs_info, &key);
2594 if (IS_ERR(root)) {
2595 srcu_read_unlock(&fs_info->subvol_srcu, index);
2596 if (PTR_ERR(root) == -ENOENT)
2597 return 0;
2598 return PTR_ERR(root);
2599 }
Liu Bo38c227d2013-01-29 03:18:40 +00002600
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002601 if (btrfs_root_readonly(root)) {
2602 srcu_read_unlock(&fs_info->subvol_srcu, index);
2603 return 0;
2604 }
2605
Liu Bo38c227d2013-01-29 03:18:40 +00002606 /* step 2: get inode */
2607 key.objectid = backref->inum;
2608 key.type = BTRFS_INODE_ITEM_KEY;
2609 key.offset = 0;
2610
2611 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2612 if (IS_ERR(inode)) {
2613 srcu_read_unlock(&fs_info->subvol_srcu, index);
2614 return 0;
2615 }
2616
2617 srcu_read_unlock(&fs_info->subvol_srcu, index);
2618
2619 /* step 3: relink backref */
2620 lock_start = backref->file_pos;
2621 lock_end = backref->file_pos + backref->num_bytes - 1;
2622 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002623 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002624
2625 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2626 if (ordered) {
2627 btrfs_put_ordered_extent(ordered);
2628 goto out_unlock;
2629 }
2630
2631 trans = btrfs_join_transaction(root);
2632 if (IS_ERR(trans)) {
2633 ret = PTR_ERR(trans);
2634 goto out_unlock;
2635 }
2636
2637 key.objectid = backref->inum;
2638 key.type = BTRFS_EXTENT_DATA_KEY;
2639 key.offset = backref->file_pos;
2640
2641 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2642 if (ret < 0) {
2643 goto out_free_path;
2644 } else if (ret > 0) {
2645 ret = 0;
2646 goto out_free_path;
2647 }
2648
2649 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2650 struct btrfs_file_extent_item);
2651
2652 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2653 backref->generation)
2654 goto out_free_path;
2655
2656 btrfs_release_path(path);
2657
2658 start = backref->file_pos;
2659 if (backref->extent_offset < old->extent_offset + old->offset)
2660 start += old->extent_offset + old->offset -
2661 backref->extent_offset;
2662
2663 len = min(backref->extent_offset + backref->num_bytes,
2664 old->extent_offset + old->offset + old->len);
2665 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2666
2667 ret = btrfs_drop_extents(trans, root, inode, start,
2668 start + len, 1);
2669 if (ret)
2670 goto out_free_path;
2671again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002672 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002673 key.type = BTRFS_EXTENT_DATA_KEY;
2674 key.offset = start;
2675
Liu Boa09a0a72013-03-11 09:20:58 +00002676 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002677 if (merge) {
2678 struct btrfs_file_extent_item *fi;
2679 u64 extent_len;
2680 struct btrfs_key found_key;
2681
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002682 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002683 if (ret < 0)
2684 goto out_free_path;
2685
2686 path->slots[0]--;
2687 leaf = path->nodes[0];
2688 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2689
2690 fi = btrfs_item_ptr(leaf, path->slots[0],
2691 struct btrfs_file_extent_item);
2692 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2693
Liu Bo116e0022013-08-02 16:30:40 +08002694 if (extent_len + found_key.offset == start &&
2695 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002696 btrfs_set_file_extent_num_bytes(leaf, fi,
2697 extent_len + len);
2698 btrfs_mark_buffer_dirty(leaf);
2699 inode_add_bytes(inode, len);
2700
2701 ret = 1;
2702 goto out_free_path;
2703 } else {
2704 merge = false;
2705 btrfs_release_path(path);
2706 goto again;
2707 }
2708 }
2709
2710 ret = btrfs_insert_empty_item(trans, root, path, &key,
2711 sizeof(*extent));
2712 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002713 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002714 goto out_free_path;
2715 }
2716
2717 leaf = path->nodes[0];
2718 item = btrfs_item_ptr(leaf, path->slots[0],
2719 struct btrfs_file_extent_item);
2720 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2721 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2722 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2723 btrfs_set_file_extent_num_bytes(leaf, item, len);
2724 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2725 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2726 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2727 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2728 btrfs_set_file_extent_encryption(leaf, item, 0);
2729 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2730
2731 btrfs_mark_buffer_dirty(leaf);
2732 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002733 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002734
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002735 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002736 new->disk_len, 0,
2737 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002738 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002739 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002740 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002741 goto out_free_path;
2742 }
2743
2744 ret = 1;
2745out_free_path:
2746 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002747 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002748 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002749out_unlock:
2750 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002751 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002752 iput(inode);
2753 return ret;
2754}
2755
Liu Bo6f519562013-10-29 10:45:05 +08002756static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2757{
2758 struct old_sa_defrag_extent *old, *tmp;
2759
2760 if (!new)
2761 return;
2762
2763 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002764 kfree(old);
2765 }
2766 kfree(new);
2767}
2768
Liu Bo38c227d2013-01-29 03:18:40 +00002769static void relink_file_extents(struct new_sa_defrag_extent *new)
2770{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002771 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002772 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002773 struct sa_defrag_extent_backref *backref;
2774 struct sa_defrag_extent_backref *prev = NULL;
2775 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002776 struct rb_node *node;
2777 int ret;
2778
2779 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002780
2781 path = btrfs_alloc_path();
2782 if (!path)
2783 return;
2784
2785 if (!record_extent_backrefs(path, new)) {
2786 btrfs_free_path(path);
2787 goto out;
2788 }
2789 btrfs_release_path(path);
2790
2791 while (1) {
2792 node = rb_first(&new->root);
2793 if (!node)
2794 break;
2795 rb_erase(node, &new->root);
2796
2797 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2798
2799 ret = relink_extent_backref(path, prev, backref);
2800 WARN_ON(ret < 0);
2801
2802 kfree(prev);
2803
2804 if (ret == 1)
2805 prev = backref;
2806 else
2807 prev = NULL;
2808 cond_resched();
2809 }
2810 kfree(prev);
2811
2812 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002813out:
Liu Bo6f519562013-10-29 10:45:05 +08002814 free_sa_defrag_extent(new);
2815
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002816 atomic_dec(&fs_info->defrag_running);
2817 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002818}
2819
2820static struct new_sa_defrag_extent *
2821record_old_file_extents(struct inode *inode,
2822 struct btrfs_ordered_extent *ordered)
2823{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002825 struct btrfs_root *root = BTRFS_I(inode)->root;
2826 struct btrfs_path *path;
2827 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002828 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002829 struct new_sa_defrag_extent *new;
2830 int ret;
2831
2832 new = kmalloc(sizeof(*new), GFP_NOFS);
2833 if (!new)
2834 return NULL;
2835
2836 new->inode = inode;
2837 new->file_pos = ordered->file_offset;
2838 new->len = ordered->len;
2839 new->bytenr = ordered->start;
2840 new->disk_len = ordered->disk_len;
2841 new->compress_type = ordered->compress_type;
2842 new->root = RB_ROOT;
2843 INIT_LIST_HEAD(&new->head);
2844
2845 path = btrfs_alloc_path();
2846 if (!path)
2847 goto out_kfree;
2848
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002849 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002850 key.type = BTRFS_EXTENT_DATA_KEY;
2851 key.offset = new->file_pos;
2852
2853 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2854 if (ret < 0)
2855 goto out_free_path;
2856 if (ret > 0 && path->slots[0] > 0)
2857 path->slots[0]--;
2858
2859 /* find out all the old extents for the file range */
2860 while (1) {
2861 struct btrfs_file_extent_item *extent;
2862 struct extent_buffer *l;
2863 int slot;
2864 u64 num_bytes;
2865 u64 offset;
2866 u64 end;
2867 u64 disk_bytenr;
2868 u64 extent_offset;
2869
2870 l = path->nodes[0];
2871 slot = path->slots[0];
2872
2873 if (slot >= btrfs_header_nritems(l)) {
2874 ret = btrfs_next_leaf(root, path);
2875 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002876 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002877 else if (ret > 0)
2878 break;
2879 continue;
2880 }
2881
2882 btrfs_item_key_to_cpu(l, &key, slot);
2883
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002884 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002885 break;
2886 if (key.type != BTRFS_EXTENT_DATA_KEY)
2887 break;
2888 if (key.offset >= new->file_pos + new->len)
2889 break;
2890
2891 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2892
2893 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2894 if (key.offset + num_bytes < new->file_pos)
2895 goto next;
2896
2897 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2898 if (!disk_bytenr)
2899 goto next;
2900
2901 extent_offset = btrfs_file_extent_offset(l, extent);
2902
2903 old = kmalloc(sizeof(*old), GFP_NOFS);
2904 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002905 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002906
2907 offset = max(new->file_pos, key.offset);
2908 end = min(new->file_pos + new->len, key.offset + num_bytes);
2909
2910 old->bytenr = disk_bytenr;
2911 old->extent_offset = extent_offset;
2912 old->offset = offset - key.offset;
2913 old->len = end - offset;
2914 old->new = new;
2915 old->count = 0;
2916 list_add_tail(&old->list, &new->head);
2917next:
2918 path->slots[0]++;
2919 cond_resched();
2920 }
2921
2922 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002923 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002924
2925 return new;
2926
Liu Bo38c227d2013-01-29 03:18:40 +00002927out_free_path:
2928 btrfs_free_path(path);
2929out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002930 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002931 return NULL;
2932}
2933
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002934static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002935 u64 start, u64 len)
2936{
2937 struct btrfs_block_group_cache *cache;
2938
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002939 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002940 ASSERT(cache);
2941
2942 spin_lock(&cache->lock);
2943 cache->delalloc_bytes -= len;
2944 spin_unlock(&cache->lock);
2945
2946 btrfs_put_block_group(cache);
2947}
2948
Chris Masond352ac62008-09-29 15:18:18 -04002949/* as ordered data IO finishes, this gets called so we can finish
2950 * an ordered extent if the range of bytes in the file it covers are
2951 * fully written.
2952 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002953static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002954{
Josef Bacik5fd02042012-05-02 14:00:54 -04002955 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002956 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002957 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002958 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002959 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002960 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002961 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002962 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002963 int ret = 0;
2964 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002965 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002966 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002967 bool range_locked = false;
2968 bool clear_new_delalloc_bytes = false;
2969
2970 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2971 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2972 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2973 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002974
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002975 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002976
Josef Bacik5fd02042012-05-02 14:00:54 -04002977 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2978 ret = -EIO;
2979 goto out;
2980 }
2981
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002982 btrfs_free_io_failure_record(BTRFS_I(inode),
2983 ordered_extent->file_offset,
2984 ordered_extent->file_offset +
2985 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002986
Josef Bacik77cef2e2013-08-29 13:57:21 -04002987 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2988 truncated = true;
2989 logical_len = ordered_extent->truncated_len;
2990 /* Truncated the entire extent, don't bother adding */
2991 if (!logical_len)
2992 goto out;
2993 }
2994
Yan, Zhengc2167752009-11-12 09:34:21 +00002995 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002997
2998 /*
2999 * For mwrite(mmap + memset to write) case, we still reserve
3000 * space for NOCOW range.
3001 * As NOCOW won't cause a new delayed ref, just free the space
3002 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003003 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003004 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003005 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3006 if (nolock)
3007 trans = btrfs_join_transaction_nolock(root);
3008 else
3009 trans = btrfs_join_transaction(root);
3010 if (IS_ERR(trans)) {
3011 ret = PTR_ERR(trans);
3012 trans = NULL;
3013 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003014 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003015 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003016 ret = btrfs_update_inode_fallback(trans, root, inode);
3017 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003018 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003019 goto out;
3020 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003022 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003023 lock_extent_bits(io_tree, ordered_extent->file_offset,
3024 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003025 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003026
Liu Bo38c227d2013-01-29 03:18:40 +00003027 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3028 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003029 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003030 if (ret) {
3031 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003032 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003033 /* the inode is shared */
3034 new = record_old_file_extents(inode, ordered_extent);
3035
3036 clear_extent_bit(io_tree, ordered_extent->file_offset,
3037 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003038 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003039 }
3040
Josef Bacik0cb59c92010-07-02 12:14:14 -04003041 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003042 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003043 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003044 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003045 if (IS_ERR(trans)) {
3046 ret = PTR_ERR(trans);
3047 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003048 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 }
Chris Masona79b7d42014-05-22 16:18:52 -07003050
Josef Bacik69fe2d72017-10-19 14:15:57 -04003051 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003052
Chris Masonc8b97812008-10-29 14:49:59 -04003053 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003054 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003055 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003056 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003057 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3058 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003059 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003060 ordered_extent->file_offset,
3061 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003062 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003063 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003064 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003065 ret = insert_reserved_file_extent(trans, inode,
3066 ordered_extent->file_offset,
3067 ordered_extent->start,
3068 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003069 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003070 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003071 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003072 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003073 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003074 ordered_extent->start,
3075 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003076 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003077 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3078 ordered_extent->file_offset, ordered_extent->len,
3079 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003081 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003082 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003084
Nikolay Borisovac01f262018-01-08 10:59:43 +02003085 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3086 if (ret) {
3087 btrfs_abort_transaction(trans, ret);
3088 goto out;
3089 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003090
Josef Bacik6c760c02012-11-09 10:53:21 -05003091 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3092 ret = btrfs_update_inode_fallback(trans, root, inode);
3093 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003094 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003095 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003096 }
3097 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003098out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003099 if (range_locked || clear_new_delalloc_bytes) {
3100 unsigned int clear_bits = 0;
3101
3102 if (range_locked)
3103 clear_bits |= EXTENT_LOCKED;
3104 if (clear_new_delalloc_bytes)
3105 clear_bits |= EXTENT_DELALLOC_NEW;
3106 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3107 ordered_extent->file_offset,
3108 ordered_extent->file_offset +
3109 ordered_extent->len - 1,
3110 clear_bits,
3111 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003112 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003113 }
3114
Miao Xiea698d0752012-09-20 01:51:59 -06003115 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003116 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003117
Josef Bacik77cef2e2013-08-29 13:57:21 -04003118 if (ret || truncated) {
3119 u64 start, end;
3120
3121 if (truncated)
3122 start = ordered_extent->file_offset + logical_len;
3123 else
3124 start = ordered_extent->file_offset;
3125 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003126 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003127
3128 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003129 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003130
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003131 /*
3132 * If the ordered extent had an IOERR or something else went
3133 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003134 * back to the allocator. We only free the extent in the
3135 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003136 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003137 if ((ret || !logical_len) &&
3138 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003139 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003140 btrfs_free_reserved_extent(fs_info,
3141 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003142 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003143 }
3144
3145
Josef Bacik5fd02042012-05-02 14:00:54 -04003146 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003147 * This needs to be done to make sure anybody waiting knows we are done
3148 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003149 */
3150 btrfs_remove_ordered_extent(inode, ordered_extent);
3151
Liu Bo38c227d2013-01-29 03:18:40 +00003152 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003153 if (new) {
3154 if (ret) {
3155 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003156 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003157 } else {
3158 relink_file_extents(new);
3159 }
3160 }
Liu Bo38c227d2013-01-29 03:18:40 +00003161
Chris Masone6dcd2d2008-07-17 12:53:50 -04003162 /* once for us */
3163 btrfs_put_ordered_extent(ordered_extent);
3164 /* once for the tree */
3165 btrfs_put_ordered_extent(ordered_extent);
3166
Ethan Liene73e81b2018-05-28 13:48:20 +08003167 /* Try to release some metadata so we don't get an OOM but don't wait */
3168 btrfs_btree_balance_dirty_nodelay(fs_info);
3169
Josef Bacik5fd02042012-05-02 14:00:54 -04003170 return ret;
3171}
3172
3173static void finish_ordered_fn(struct btrfs_work *work)
3174{
3175 struct btrfs_ordered_extent *ordered_extent;
3176 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3177 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003178}
3179
David Sterbac3988d62017-02-17 15:18:32 +01003180static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003181 struct extent_state *state, int uptodate)
3182{
Josef Bacik5fd02042012-05-02 14:00:54 -04003183 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003185 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003186 struct btrfs_workqueue *wq;
3187 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003188
liubo1abe9b82011-03-24 11:18:59 +00003189 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3190
Chris Mason8b62b722009-09-02 16:53:46 -04003191 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003192 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3193 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003194 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003195
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003196 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003197 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003198 func = btrfs_freespace_write_helper;
3199 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003200 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003201 func = btrfs_endio_write_helper;
3202 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003203
Liu Bo9e0af232014-08-15 23:36:53 +08003204 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3205 NULL);
3206 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003207}
3208
Miao Xiedc380ae2014-09-12 18:43:55 +08003209static int __readpage_endio_check(struct inode *inode,
3210 struct btrfs_io_bio *io_bio,
3211 int icsum, struct page *page,
3212 int pgoff, u64 start, size_t len)
3213{
3214 char *kaddr;
3215 u32 csum_expected;
3216 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003217
3218 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3219
3220 kaddr = kmap_atomic(page);
3221 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003222 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003223 if (csum != csum_expected)
3224 goto zeroit;
3225
3226 kunmap_atomic(kaddr);
3227 return 0;
3228zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003229 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003230 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003231 memset(kaddr + pgoff, 1, len);
3232 flush_dcache_page(page);
3233 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003234 return -EIO;
3235}
3236
Chris Masond352ac62008-09-29 15:18:18 -04003237/*
Chris Masond352ac62008-09-29 15:18:18 -04003238 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003239 * if there's a match, we allow the bio to finish. If not, the code in
3240 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003241 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003242static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3243 u64 phy_offset, struct page *page,
3244 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003245{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003246 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003247 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003248 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003249 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003250
Chris Masond20f7042008-12-08 16:58:54 -05003251 if (PageChecked(page)) {
3252 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003253 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003254 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003255
3256 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003257 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003258
Yan Zheng17d217f2008-12-12 10:03:38 -05003259 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003260 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003261 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003262 return 0;
3263 }
3264
Miao Xiefacc8a222013-07-25 19:22:34 +08003265 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003266 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3267 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003268}
Chris Masonb888db22007-08-27 16:49:44 -04003269
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003270/*
3271 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3272 *
3273 * @inode: The inode we want to perform iput on
3274 *
3275 * This function uses the generic vfs_inode::i_count to track whether we should
3276 * just decrement it (in case it's > 1) or if this is the last iput then link
3277 * the inode to the delayed iput machinery. Delayed iputs are processed at
3278 * transaction commit time/superblock commit/cleaner kthread.
3279 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003280void btrfs_add_delayed_iput(struct inode *inode)
3281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003282 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003283 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003284
3285 if (atomic_add_unless(&inode->i_count, -1, 1))
3286 return;
3287
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003288 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003289 ASSERT(list_empty(&binode->delayed_iput));
3290 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003291 spin_unlock(&fs_info->delayed_iput_lock);
3292}
3293
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003294void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003295{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003296
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003297 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003298 while (!list_empty(&fs_info->delayed_iputs)) {
3299 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003300
David Sterba8089fe62015-11-19 14:15:51 +01003301 inode = list_first_entry(&fs_info->delayed_iputs,
3302 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003303 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003304 spin_unlock(&fs_info->delayed_iput_lock);
3305 iput(&inode->vfs_inode);
3306 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003307 }
David Sterba8089fe62015-11-19 14:15:51 +01003308 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003309}
3310
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003312 * This creates an orphan entry for the given inode in case something goes wrong
3313 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003314 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003315int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003316 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003317{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003319
Omar Sandoval27919062018-05-11 13:13:37 -07003320 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3321 if (ret && ret != -EEXIST) {
3322 btrfs_abort_transaction(trans, ret);
3323 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324 }
3325
Yan, Zhengd68fc572010-05-16 10:49:58 -04003326 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003327}
3328
3329/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003330 * We have done the delete so we can go ahead and remove the orphan item for
3331 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003332 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003333static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003334 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003335{
Omar Sandoval27919062018-05-11 13:13:37 -07003336 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003337}
3338
3339/*
3340 * this cleans up any orphans that may be left on the list from the last use
3341 * of this root.
3342 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003344{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003345 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003346 struct btrfs_path *path;
3347 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003348 struct btrfs_key key, found_key;
3349 struct btrfs_trans_handle *trans;
3350 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003351 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003352 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003353
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003355 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003356
3357 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003358 if (!path) {
3359 ret = -ENOMEM;
3360 goto out;
3361 }
David Sterbae4058b52015-11-27 16:31:35 +01003362 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003363
3364 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003365 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003366 key.offset = (u64)-1;
3367
Josef Bacik7b128762008-07-24 12:17:14 -04003368 while (1) {
3369 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003370 if (ret < 0)
3371 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003372
3373 /*
3374 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003375 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003376 * find the key and see if we have stuff that matches
3377 */
3378 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003379 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003380 if (path->slots[0] == 0)
3381 break;
3382 path->slots[0]--;
3383 }
3384
3385 /* pull out the item */
3386 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3388
3389 /* make sure the item matches what we want */
3390 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3391 break;
David Sterba962a2982014-06-04 18:41:45 +02003392 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003393 break;
3394
3395 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003396 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003397
3398 /*
3399 * this is where we are basically btrfs_lookup, without the
3400 * crossing root thing. we store the inode number in the
3401 * offset of the orphan item.
3402 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003403
3404 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003405 btrfs_err(fs_info,
3406 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003407 ret = -EINVAL;
3408 goto out;
3409 }
3410
3411 last_objectid = found_key.offset;
3412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413 found_key.objectid = found_key.offset;
3414 found_key.type = BTRFS_INODE_ITEM_KEY;
3415 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003416 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303417 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003418 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003419 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003420
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003421 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003422 struct btrfs_root *dead_root;
3423 struct btrfs_fs_info *fs_info = root->fs_info;
3424 int is_dead_root = 0;
3425
3426 /*
3427 * this is an orphan in the tree root. Currently these
3428 * could come from 2 sources:
3429 * a) a snapshot deletion in progress
3430 * b) a free space cache inode
3431 * We need to distinguish those two, as the snapshot
3432 * orphan must not get deleted.
3433 * find_dead_roots already ran before us, so if this
3434 * is a snapshot deletion, we should find the root
3435 * in the dead_roots list
3436 */
3437 spin_lock(&fs_info->trans_lock);
3438 list_for_each_entry(dead_root, &fs_info->dead_roots,
3439 root_list) {
3440 if (dead_root->root_key.objectid ==
3441 found_key.objectid) {
3442 is_dead_root = 1;
3443 break;
3444 }
3445 }
3446 spin_unlock(&fs_info->trans_lock);
3447 if (is_dead_root) {
3448 /* prevent this orphan from being found again */
3449 key.offset = found_key.objectid - 1;
3450 continue;
3451 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003452
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003453 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003454
Josef Bacika8c9e572011-09-21 16:55:59 -04003455 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003456 * If we have an inode with links, there are a couple of
3457 * possibilities. Old kernels (before v3.12) used to create an
3458 * orphan item for truncate indicating that there were possibly
3459 * extent items past i_size that needed to be deleted. In v3.12,
3460 * truncate was changed to update i_size in sync with the extent
3461 * items, but the (useless) orphan item was still created. Since
3462 * v4.18, we don't create the orphan item for truncate at all.
3463 *
3464 * So, this item could mean that we need to do a truncate, but
3465 * only if this filesystem was last used on a pre-v3.12 kernel
3466 * and was not cleanly unmounted. The odds of that are quite
3467 * slim, and it's a pain to do the truncate now, so just delete
3468 * the orphan item.
3469 *
3470 * It's also possible that this orphan item was supposed to be
3471 * deleted but wasn't. The inode number may have been reused,
3472 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003473 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003474 if (ret == -ENOENT || inode->i_nlink) {
3475 if (!ret)
3476 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003477 trans = btrfs_start_transaction(root, 1);
3478 if (IS_ERR(trans)) {
3479 ret = PTR_ERR(trans);
3480 goto out;
3481 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003482 btrfs_debug(fs_info, "auto deleting %Lu",
3483 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003484 ret = btrfs_del_orphan_item(trans, root,
3485 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003486 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003487 if (ret)
3488 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003489 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003490 }
Josef Bacik7b128762008-07-24 12:17:14 -04003491
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003492 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003493
3494 /* this will do delete_inode and everything for us */
3495 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003496 if (ret)
3497 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003498 }
Miao Xie3254c872011-11-10 20:45:05 -05003499 /* release the path since we're done with it */
3500 btrfs_release_path(path);
3501
Yan, Zhengd68fc572010-05-16 10:49:58 -04003502 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3503
Omar Sandovala575cee2018-05-11 13:13:38 -07003504 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003505 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003506 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003507 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003508 }
Josef Bacik7b128762008-07-24 12:17:14 -04003509
3510 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003511 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003512
3513out:
3514 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003515 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003516 btrfs_free_path(path);
3517 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003518}
3519
Chris Masond352ac62008-09-29 15:18:18 -04003520/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003521 * very simple check to peek ahead in the leaf looking for xattrs. If we
3522 * don't find any xattrs, we know there can't be any acls.
3523 *
3524 * slot is the slot the inode is in, objectid is the objectid of the inode
3525 */
3526static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003527 int slot, u64 objectid,
3528 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003529{
3530 u32 nritems = btrfs_header_nritems(leaf);
3531 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003532 static u64 xattr_access = 0;
3533 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003534 int scanned = 0;
3535
Josef Bacikf23b5a52013-06-19 10:16:26 -04003536 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003537 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3538 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3539 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3540 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003541 }
3542
Chris Mason46a53cc2009-04-27 11:47:50 -04003543 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003544 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003545 while (slot < nritems) {
3546 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3547
3548 /* we found a different objectid, there must not be acls */
3549 if (found_key.objectid != objectid)
3550 return 0;
3551
3552 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003553 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003554 if (*first_xattr_slot == -1)
3555 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003556 if (found_key.offset == xattr_access ||
3557 found_key.offset == xattr_default)
3558 return 1;
3559 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003560
3561 /*
3562 * we found a key greater than an xattr key, there can't
3563 * be any acls later on
3564 */
3565 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3566 return 0;
3567
3568 slot++;
3569 scanned++;
3570
3571 /*
3572 * it goes inode, inode backrefs, xattrs, extents,
3573 * so if there are a ton of hard links to an inode there can
3574 * be a lot of backrefs. Don't waste time searching too hard,
3575 * this is just an optimization
3576 */
3577 if (scanned >= 8)
3578 break;
3579 }
3580 /* we hit the end of the leaf before we found an xattr or
3581 * something larger than an xattr. We have to assume the inode
3582 * has acls
3583 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003584 if (*first_xattr_slot == -1)
3585 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003586 return 1;
3587}
3588
3589/*
Chris Masond352ac62008-09-29 15:18:18 -04003590 * read an inode from the btree into the in-memory inode
3591 */
Filipe Manana67710892016-06-06 11:51:25 +01003592static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003594 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003596 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003597 struct btrfs_inode_item *inode_item;
3598 struct btrfs_root *root = BTRFS_I(inode)->root;
3599 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003600 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003601 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003602 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003603 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003604 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003605 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
3607 ret = btrfs_fill_inode(inode, &rdev);
3608 if (!ret)
3609 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003610
3611 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003612 if (!path) {
3613 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003614 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003615 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003616
Chris Mason39279cc2007-06-12 06:35:45 -04003617 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003618
Chris Mason39279cc2007-06-12 06:35:45 -04003619 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003620 if (ret) {
3621 if (ret > 0)
3622 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003623 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003624 }
Chris Mason39279cc2007-06-12 06:35:45 -04003625
Chris Mason5f39d392007-10-15 16:14:19 -04003626 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003627
3628 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003629 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003630
Chris Mason5f39d392007-10-15 16:14:19 -04003631 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3632 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003633 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003634 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003635 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3636 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003637 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003638
David Sterbaa937b972014-12-12 17:39:12 +01003639 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3640 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003641
David Sterbaa937b972014-12-12 17:39:12 +01003642 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3643 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003644
David Sterbaa937b972014-12-12 17:39:12 +01003645 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3646 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003647
chandan r9cc97d62012-07-04 12:48:07 +05303648 BTRFS_I(inode)->i_otime.tv_sec =
3649 btrfs_timespec_sec(leaf, &inode_item->otime);
3650 BTRFS_I(inode)->i_otime.tv_nsec =
3651 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003652
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003653 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003654 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003655 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3656
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003657 inode_set_iversion_queried(inode,
3658 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003659 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003660 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003661 rdev = btrfs_inode_rdev(leaf, inode_item);
3662
Josef Bacikaec74772008-07-24 12:12:38 -04003663 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003664 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003665
3666cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003667 /*
3668 * If we were modified in the current generation and evicted from memory
3669 * and then re-read we need to do a full sync since we don't have any
3670 * idea about which extents were modified before we were evicted from
3671 * cache.
3672 *
3673 * This is required for both inode re-read from disk and delayed inode
3674 * in delayed_nodes_tree.
3675 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003676 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003677 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3678 &BTRFS_I(inode)->runtime_flags);
3679
Filipe Mananabde6c242015-07-24 00:00:19 +01003680 /*
3681 * We don't persist the id of the transaction where an unlink operation
3682 * against the inode was last made. So here we assume the inode might
3683 * have been evicted, and therefore the exact value of last_unlink_trans
3684 * lost, and set it to last_trans to avoid metadata inconsistencies
3685 * between the inode and its parent if the inode is fsync'ed and the log
3686 * replayed. For example, in the scenario:
3687 *
3688 * touch mydir/foo
3689 * ln mydir/foo mydir/bar
3690 * sync
3691 * unlink mydir/bar
3692 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3693 * xfs_io -c fsync mydir/foo
3694 * <power failure>
3695 * mount fs, triggers fsync log replay
3696 *
3697 * We must make sure that when we fsync our inode foo we also log its
3698 * parent inode, otherwise after log replay the parent still has the
3699 * dentry with the "bar" name but our inode foo has a link count of 1
3700 * and doesn't have an inode ref with the name "bar" anymore.
3701 *
3702 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003703 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003704 * transaction commits on fsync if our inode is a directory, or if our
3705 * inode is not a directory, logging its parent unnecessarily.
3706 */
3707 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3708
Miao Xie67de1172013-12-26 13:07:06 +08003709 path->slots[0]++;
3710 if (inode->i_nlink != 1 ||
3711 path->slots[0] >= btrfs_header_nritems(leaf))
3712 goto cache_acl;
3713
3714 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003715 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003716 goto cache_acl;
3717
3718 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3719 if (location.type == BTRFS_INODE_REF_KEY) {
3720 struct btrfs_inode_ref *ref;
3721
3722 ref = (struct btrfs_inode_ref *)ptr;
3723 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3724 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3725 struct btrfs_inode_extref *extref;
3726
3727 extref = (struct btrfs_inode_extref *)ptr;
3728 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3729 extref);
3730 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003731cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003732 /*
3733 * try to precache a NULL acl entry for files that don't have
3734 * any xattrs or acls
3735 */
Li Zefan33345d012011-04-20 10:31:50 +08003736 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003737 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003738 if (first_xattr_slot != -1) {
3739 path->slots[0] = first_xattr_slot;
3740 ret = btrfs_load_inode_props(inode, path);
3741 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003742 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003743 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003744 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003745 root->root_key.objectid, ret);
3746 }
3747 btrfs_free_path(path);
3748
Al Viro72c04902009-06-24 16:58:48 -04003749 if (!maybe_acls)
3750 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003751
Chris Mason39279cc2007-06-12 06:35:45 -04003752 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003753 case S_IFREG:
3754 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003755 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 inode->i_fop = &btrfs_file_operations;
3757 inode->i_op = &btrfs_file_inode_operations;
3758 break;
3759 case S_IFDIR:
3760 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003761 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 break;
3763 case S_IFLNK:
3764 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003765 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003766 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3767 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003768 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003769 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003770 init_special_inode(inode, inode->i_mode, rdev);
3771 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003773
David Sterba7b6a2212018-03-26 18:40:21 +02003774 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003775 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003776
3777make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003778 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003779 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003780 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003781}
3782
Chris Masond352ac62008-09-29 15:18:18 -04003783/*
3784 * given a leaf and an inode, copy the inode fields into the leaf
3785 */
Chris Masone02119d2008-09-05 16:13:11 -04003786static void fill_inode_item(struct btrfs_trans_handle *trans,
3787 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003788 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003789 struct inode *inode)
3790{
Liu Bo51fab692012-12-27 09:01:21 +00003791 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003792
Liu Bo51fab692012-12-27 09:01:21 +00003793 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003794
Liu Bo51fab692012-12-27 09:01:21 +00003795 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3796 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3797 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3798 &token);
3799 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3800 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003801
David Sterbaa937b972014-12-12 17:39:12 +01003802 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003803 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003804 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003805 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
David Sterbaa937b972014-12-12 17:39:12 +01003807 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003808 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003809 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003810 inode->i_mtime.tv_nsec, &token);
3811
David Sterbaa937b972014-12-12 17:39:12 +01003812 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003813 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003814 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003815 inode->i_ctime.tv_nsec, &token);
3816
chandan r9cc97d62012-07-04 12:48:07 +05303817 btrfs_set_token_timespec_sec(leaf, &item->otime,
3818 BTRFS_I(inode)->i_otime.tv_sec, &token);
3819 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3820 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3821
Liu Bo51fab692012-12-27 09:01:21 +00003822 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3823 &token);
3824 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3825 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003826 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3827 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003828 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3829 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3830 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3831 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003832}
3833
Chris Masond352ac62008-09-29 15:18:18 -04003834/*
3835 * copy everything in the in-memory inode into the btree.
3836 */
Chris Mason21151332011-11-10 20:39:08 -05003837static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003838 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003839{
3840 struct btrfs_inode_item *inode_item;
3841 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003842 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003843 int ret;
3844
3845 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003846 if (!path)
3847 return -ENOMEM;
3848
Chris Masonb9473432009-03-13 11:00:37 -04003849 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003850 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3851 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003852 if (ret) {
3853 if (ret > 0)
3854 ret = -ENOENT;
3855 goto failed;
3856 }
3857
Chris Mason5f39d392007-10-15 16:14:19 -04003858 leaf = path->nodes[0];
3859 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003860 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003861
Chris Masone02119d2008-09-05 16:13:11 -04003862 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003863 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003864 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003865 ret = 0;
3866failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003867 btrfs_free_path(path);
3868 return ret;
3869}
3870
Chris Masond352ac62008-09-29 15:18:18 -04003871/*
Chris Mason21151332011-11-10 20:39:08 -05003872 * copy everything in the in-memory inode into the btree.
3873 */
3874noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3875 struct btrfs_root *root, struct inode *inode)
3876{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003877 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003878 int ret;
3879
3880 /*
3881 * If the inode is a free space inode, we can deadlock during commit
3882 * if we put it into the delayed code.
3883 *
3884 * The data relocation inode should also be directly updated
3885 * without delay
3886 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003887 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003888 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003889 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003890 btrfs_update_root_times(trans, root);
3891
Chris Mason21151332011-11-10 20:39:08 -05003892 ret = btrfs_delayed_update_inode(trans, root, inode);
3893 if (!ret)
3894 btrfs_set_inode_last_trans(trans, inode);
3895 return ret;
3896 }
3897
3898 return btrfs_update_inode_item(trans, root, inode);
3899}
3900
Josef Bacikbe6aef62012-10-22 15:43:12 -04003901noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3902 struct btrfs_root *root,
3903 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003904{
3905 int ret;
3906
3907 ret = btrfs_update_inode(trans, root, inode);
3908 if (ret == -ENOSPC)
3909 return btrfs_update_inode_item(trans, root, inode);
3910 return ret;
3911}
3912
3913/*
Chris Masond352ac62008-09-29 15:18:18 -04003914 * unlink helper that gets used here in inode.c and in the tree logging
3915 * recovery code. It remove a link in a directory with a given name, and
3916 * also drops the back refs in the inode to the directory
3917 */
Al Viro92986792011-03-04 17:14:37 +00003918static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3919 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003920 struct btrfs_inode *dir,
3921 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003922 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003923{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003924 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003925 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003926 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003927 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003928 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003929 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003930 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003931 u64 ino = btrfs_ino(inode);
3932 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003933
3934 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003935 if (!path) {
3936 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003937 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003938 }
3939
Chris Masonb9473432009-03-13 11:00:37 -04003940 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003941 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003942 name, name_len, -1);
3943 if (IS_ERR(di)) {
3944 ret = PTR_ERR(di);
3945 goto err;
3946 }
3947 if (!di) {
3948 ret = -ENOENT;
3949 goto err;
3950 }
Chris Mason5f39d392007-10-15 16:14:19 -04003951 leaf = path->nodes[0];
3952 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003954 if (ret)
3955 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003956 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003957
Miao Xie67de1172013-12-26 13:07:06 +08003958 /*
3959 * If we don't have dir index, we have to get it by looking up
3960 * the inode ref, since we get the inode ref, remove it directly,
3961 * it is unnecessary to do delayed deletion.
3962 *
3963 * But if we have dir index, needn't search inode ref to get it.
3964 * Since the inode ref is close to the inode item, it is better
3965 * that we delay to delete it, and just do this deletion when
3966 * we update the inode item.
3967 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003968 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003969 ret = btrfs_delayed_delete_inode_ref(inode);
3970 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003971 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003972 goto skip_backref;
3973 }
3974 }
3975
Li Zefan33345d012011-04-20 10:31:50 +08003976 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3977 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003978 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003979 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003980 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003981 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003982 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003983 goto err;
3984 }
Miao Xie67de1172013-12-26 13:07:06 +08003985skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003986 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003987 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003988 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003989 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003990 }
Chris Mason39279cc2007-06-12 06:35:45 -04003991
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003992 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3993 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003994 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003995 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003996 goto err;
3997 }
Chris Masone02119d2008-09-05 16:13:11 -04003998
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003999 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4000 index);
Chris Mason6418c962010-10-30 07:34:24 -04004001 if (ret == -ENOENT)
4002 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004003 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004004 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004005err:
4006 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004007 if (ret)
4008 goto out;
4009
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004010 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004011 inode_inc_iversion(&inode->vfs_inode);
4012 inode_inc_iversion(&dir->vfs_inode);
4013 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4014 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4015 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004016out:
Chris Mason39279cc2007-06-12 06:35:45 -04004017 return ret;
4018}
4019
Al Viro92986792011-03-04 17:14:37 +00004020int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004022 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004023 const char *name, int name_len)
4024{
4025 int ret;
4026 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4027 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004028 drop_nlink(&inode->vfs_inode);
4029 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004030 }
4031 return ret;
4032}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004033
4034/*
4035 * helper to start transaction for unlink and rmdir.
4036 *
Josef Bacikd52be812013-05-29 14:54:47 -04004037 * unlink and rmdir are special in btrfs, they do not always free space, so
4038 * if we cannot make our reservations the normal way try and see if there is
4039 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4040 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041 */
Josef Bacikd52be812013-05-29 14:54:47 -04004042static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004043{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045
Josef Bacike70bea52011-10-11 14:18:24 -04004046 /*
4047 * 1 for the possible orphan item
4048 * 1 for the dir item
4049 * 1 for the dir index
4050 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004051 * 1 for the inode
4052 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004053 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004054}
4055
Chris Mason39279cc2007-06-12 06:35:45 -04004056static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4057{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004058 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004059 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004060 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004061 int ret;
4062
Josef Bacikd52be812013-05-29 14:54:47 -04004063 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004064 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004065 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004066
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004067 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4068 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004069
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004070 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4071 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4072 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004073 if (ret)
4074 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004075
Yan, Zhenga22285a2010-05-16 10:48:46 -04004076 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004077 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004078 if (ret)
4079 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004080 }
Josef Bacik7b128762008-07-24 12:17:14 -04004081
Tsutomu Itohb5324022011-07-19 07:27:20 +00004082out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004083 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004084 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004085 return ret;
4086}
4087
Misono Tomohirof60a2362018-04-18 11:34:52 +09004088static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089 struct btrfs_root *root,
4090 struct inode *dir, u64 objectid,
4091 const char *name, int name_len)
4092{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004093 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 struct btrfs_path *path;
4095 struct extent_buffer *leaf;
4096 struct btrfs_dir_item *di;
4097 struct btrfs_key key;
4098 u64 index;
4099 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004100 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101
4102 path = btrfs_alloc_path();
4103 if (!path)
4104 return -ENOMEM;
4105
Li Zefan33345d012011-04-20 10:31:50 +08004106 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 if (IS_ERR_OR_NULL(di)) {
4109 if (!di)
4110 ret = -ENOENT;
4111 else
4112 ret = PTR_ERR(di);
4113 goto out;
4114 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115
4116 leaf = path->nodes[0];
4117 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4118 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4119 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004120 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004121 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 goto out;
4123 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004124 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004126 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4127 root->root_key.objectid, dir_ino,
4128 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004130 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004131 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004132 goto out;
4133 }
Li Zefan33345d012011-04-20 10:31:50 +08004134 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 if (IS_ERR_OR_NULL(di)) {
4137 if (!di)
4138 ret = -ENOENT;
4139 else
4140 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004141 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 goto out;
4143 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004144
4145 leaf = path->nodes[0];
4146 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004147 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148 index = key.offset;
4149 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004150 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004152 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004153 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004154 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 goto out;
4156 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004157
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004158 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004159 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004160 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004161 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004163 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004165 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004166 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167}
4168
Misono Tomohiroec42f162018-04-18 11:34:13 +09004169/*
4170 * Helper to check if the subvolume references other subvolumes or if it's
4171 * default.
4172 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004173static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004174{
4175 struct btrfs_fs_info *fs_info = root->fs_info;
4176 struct btrfs_path *path;
4177 struct btrfs_dir_item *di;
4178 struct btrfs_key key;
4179 u64 dir_id;
4180 int ret;
4181
4182 path = btrfs_alloc_path();
4183 if (!path)
4184 return -ENOMEM;
4185
4186 /* Make sure this root isn't set as the default subvol */
4187 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4188 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4189 dir_id, "default", 7, 0);
4190 if (di && !IS_ERR(di)) {
4191 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4192 if (key.objectid == root->root_key.objectid) {
4193 ret = -EPERM;
4194 btrfs_err(fs_info,
4195 "deleting default subvolume %llu is not allowed",
4196 key.objectid);
4197 goto out;
4198 }
4199 btrfs_release_path(path);
4200 }
4201
4202 key.objectid = root->root_key.objectid;
4203 key.type = BTRFS_ROOT_REF_KEY;
4204 key.offset = (u64)-1;
4205
4206 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4207 if (ret < 0)
4208 goto out;
4209 BUG_ON(ret == 0);
4210
4211 ret = 0;
4212 if (path->slots[0] > 0) {
4213 path->slots[0]--;
4214 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4215 if (key.objectid == root->root_key.objectid &&
4216 key.type == BTRFS_ROOT_REF_KEY)
4217 ret = -ENOTEMPTY;
4218 }
4219out:
4220 btrfs_free_path(path);
4221 return ret;
4222}
4223
Nikolay Borisov20a68002018-04-27 14:36:24 +03004224/* Delete all dentries for inodes belonging to the root */
4225static void btrfs_prune_dentries(struct btrfs_root *root)
4226{
4227 struct btrfs_fs_info *fs_info = root->fs_info;
4228 struct rb_node *node;
4229 struct rb_node *prev;
4230 struct btrfs_inode *entry;
4231 struct inode *inode;
4232 u64 objectid = 0;
4233
4234 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4235 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4236
4237 spin_lock(&root->inode_lock);
4238again:
4239 node = root->inode_tree.rb_node;
4240 prev = NULL;
4241 while (node) {
4242 prev = node;
4243 entry = rb_entry(node, struct btrfs_inode, rb_node);
4244
David Sterba37508512018-06-29 10:56:40 +02004245 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004246 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004247 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004248 node = node->rb_right;
4249 else
4250 break;
4251 }
4252 if (!node) {
4253 while (prev) {
4254 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004255 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004256 node = prev;
4257 break;
4258 }
4259 prev = rb_next(prev);
4260 }
4261 }
4262 while (node) {
4263 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004264 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004265 inode = igrab(&entry->vfs_inode);
4266 if (inode) {
4267 spin_unlock(&root->inode_lock);
4268 if (atomic_read(&inode->i_count) > 1)
4269 d_prune_aliases(inode);
4270 /*
4271 * btrfs_drop_inode will have it removed from the inode
4272 * cache when its usage count hits zero.
4273 */
4274 iput(inode);
4275 cond_resched();
4276 spin_lock(&root->inode_lock);
4277 goto again;
4278 }
4279
4280 if (cond_resched_lock(&root->inode_lock))
4281 goto again;
4282
4283 node = rb_next(node);
4284 }
4285 spin_unlock(&root->inode_lock);
4286}
4287
Misono Tomohirof60a2362018-04-18 11:34:52 +09004288int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4289{
4290 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4291 struct btrfs_root *root = BTRFS_I(dir)->root;
4292 struct inode *inode = d_inode(dentry);
4293 struct btrfs_root *dest = BTRFS_I(inode)->root;
4294 struct btrfs_trans_handle *trans;
4295 struct btrfs_block_rsv block_rsv;
4296 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004297 int ret;
4298 int err;
4299
4300 /*
4301 * Don't allow to delete a subvolume with send in progress. This is
4302 * inside the inode lock so the error handling that has to drop the bit
4303 * again is not run concurrently.
4304 */
4305 spin_lock(&dest->root_item_lock);
4306 root_flags = btrfs_root_flags(&dest->root_item);
4307 if (dest->send_in_progress == 0) {
4308 btrfs_set_root_flags(&dest->root_item,
4309 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4310 spin_unlock(&dest->root_item_lock);
4311 } else {
4312 spin_unlock(&dest->root_item_lock);
4313 btrfs_warn(fs_info,
4314 "attempt to delete subvolume %llu during send",
4315 dest->root_key.objectid);
4316 return -EPERM;
4317 }
4318
4319 down_write(&fs_info->subvol_sem);
4320
4321 err = may_destroy_subvol(dest);
4322 if (err)
4323 goto out_up_write;
4324
4325 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4326 /*
4327 * One for dir inode,
4328 * two for dir entries,
4329 * two for root ref/backref.
4330 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004331 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004332 if (err)
4333 goto out_up_write;
4334
4335 trans = btrfs_start_transaction(root, 0);
4336 if (IS_ERR(trans)) {
4337 err = PTR_ERR(trans);
4338 goto out_release;
4339 }
4340 trans->block_rsv = &block_rsv;
4341 trans->bytes_reserved = block_rsv.size;
4342
4343 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4344
4345 ret = btrfs_unlink_subvol(trans, root, dir,
4346 dest->root_key.objectid,
4347 dentry->d_name.name,
4348 dentry->d_name.len);
4349 if (ret) {
4350 err = ret;
4351 btrfs_abort_transaction(trans, ret);
4352 goto out_end_trans;
4353 }
4354
4355 btrfs_record_root_in_trans(trans, dest);
4356
4357 memset(&dest->root_item.drop_progress, 0,
4358 sizeof(dest->root_item.drop_progress));
4359 dest->root_item.drop_level = 0;
4360 btrfs_set_root_refs(&dest->root_item, 0);
4361
4362 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4363 ret = btrfs_insert_orphan_item(trans,
4364 fs_info->tree_root,
4365 dest->root_key.objectid);
4366 if (ret) {
4367 btrfs_abort_transaction(trans, ret);
4368 err = ret;
4369 goto out_end_trans;
4370 }
4371 }
4372
Lu Fengqid1957792018-05-29 15:01:54 +08004373 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004374 BTRFS_UUID_KEY_SUBVOL,
4375 dest->root_key.objectid);
4376 if (ret && ret != -ENOENT) {
4377 btrfs_abort_transaction(trans, ret);
4378 err = ret;
4379 goto out_end_trans;
4380 }
4381 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004382 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004383 dest->root_item.received_uuid,
4384 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4385 dest->root_key.objectid);
4386 if (ret && ret != -ENOENT) {
4387 btrfs_abort_transaction(trans, ret);
4388 err = ret;
4389 goto out_end_trans;
4390 }
4391 }
4392
4393out_end_trans:
4394 trans->block_rsv = NULL;
4395 trans->bytes_reserved = 0;
4396 ret = btrfs_end_transaction(trans);
4397 if (ret && !err)
4398 err = ret;
4399 inode->i_flags |= S_DEAD;
4400out_release:
4401 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4402out_up_write:
4403 up_write(&fs_info->subvol_sem);
4404 if (err) {
4405 spin_lock(&dest->root_item_lock);
4406 root_flags = btrfs_root_flags(&dest->root_item);
4407 btrfs_set_root_flags(&dest->root_item,
4408 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4409 spin_unlock(&dest->root_item_lock);
4410 } else {
4411 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004412 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004413 ASSERT(dest->send_in_progress == 0);
4414
4415 /* the last ref */
4416 if (dest->ino_cache_inode) {
4417 iput(dest->ino_cache_inode);
4418 dest->ino_cache_inode = NULL;
4419 }
4420 }
4421
4422 return err;
4423}
4424
Chris Mason39279cc2007-06-12 06:35:45 -04004425static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4426{
David Howells2b0143b2015-03-17 22:25:59 +00004427 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004428 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004430 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004431 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004432
David Sterbab3ae2442012-09-13 16:04:34 -06004433 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004434 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004435 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004436 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004437
Josef Bacikd52be812013-05-29 14:54:47 -04004438 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004439 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004440 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004441
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004442 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004443 err = btrfs_unlink_subvol(trans, root, dir,
4444 BTRFS_I(inode)->location.objectid,
4445 dentry->d_name.name,
4446 dentry->d_name.len);
4447 goto out;
4448 }
4449
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004450 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004451 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004452 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004453
Filipe Manana44f714d2016-06-06 16:11:13 +01004454 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4455
Chris Mason39279cc2007-06-12 06:35:45 -04004456 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004457 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4458 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4459 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004460 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004461 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004462 /*
4463 * Propagate the last_unlink_trans value of the deleted dir to
4464 * its parent directory. This is to prevent an unrecoverable
4465 * log tree in the case we do something like this:
4466 * 1) create dir foo
4467 * 2) create snapshot under dir foo
4468 * 3) delete the snapshot
4469 * 4) rmdir foo
4470 * 5) mkdir foo
4471 * 6) fsync foo or some file inside foo
4472 */
4473 if (last_unlink_trans >= trans->transid)
4474 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4475 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004476out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004477 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004478 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004479
Chris Mason39279cc2007-06-12 06:35:45 -04004480 return err;
4481}
4482
Chris Mason28f75a02015-02-04 06:59:29 -08004483static int truncate_space_check(struct btrfs_trans_handle *trans,
4484 struct btrfs_root *root,
4485 u64 bytes_deleted)
4486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004488 int ret;
4489
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004490 /*
4491 * This is only used to apply pressure to the enospc system, we don't
4492 * intend to use this reservation at all.
4493 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004494 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004495 bytes_deleted *= fs_info->nodesize;
4496 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004497 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004498 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004499 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004500 trans->transid,
4501 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004502 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004503 }
Chris Mason28f75a02015-02-04 06:59:29 -08004504 return ret;
4505
4506}
4507
Josef Bacikddfae632017-10-19 14:16:02 -04004508/*
4509 * Return this if we need to call truncate_block for the last bit of the
4510 * truncate.
4511 */
4512#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004513
Chris Mason323ac952008-10-01 19:05:46 -04004514/*
Chris Mason39279cc2007-06-12 06:35:45 -04004515 * this can truncate away extent items, csum items and directory items.
4516 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004517 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004518 *
4519 * csum items that cross the new i_size are truncated to the new size
4520 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004521 *
4522 * min_type is the minimum key type to truncate down to. If set to 0, this
4523 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004524 */
Yan, Zheng80825102009-11-12 09:35:36 +00004525int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4526 struct btrfs_root *root,
4527 struct inode *inode,
4528 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004529{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004530 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004531 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004532 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004533 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004534 struct btrfs_key key;
4535 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004536 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004537 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004538 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004539 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004540 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004541 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004542 int found_extent;
4543 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004544 int pending_del_nr = 0;
4545 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004546 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004547 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004548 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004549 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004550 bool be_nice = false;
4551 bool should_throttle = false;
4552 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004553
4554 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004555
Chris Mason28ed1342014-12-17 09:41:04 -08004556 /*
4557 * for non-free space inodes and ref cows, we want to back off from
4558 * time to time
4559 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004560 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004561 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004562 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004563
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004564 path = btrfs_alloc_path();
4565 if (!path)
4566 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004567 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004568
Josef Bacik5dc562c2012-08-17 13:14:17 -04004569 /*
4570 * We want to drop from the next block forward in case this new size is
4571 * not block aligned since we will be keeping the last block of the
4572 * extent just the way it is.
4573 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004574 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004575 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004576 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004577 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004578 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004579
Miao Xie16cdcec2011-04-22 18:12:22 +08004580 /*
4581 * This function is also used to drop the items in the log tree before
4582 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4583 * it is used to drop the loged items. So we shouldn't kill the delayed
4584 * items.
4585 */
4586 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004587 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004588
Li Zefan33345d012011-04-20 10:31:50 +08004589 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004591 key.type = (u8)-1;
4592
Chris Mason85e21ba2008-01-29 15:11:36 -05004593search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004594 /*
4595 * with a 16K leaf size and 128MB extents, you can actually queue
4596 * up a huge file in a single leaf. Most of the time that
4597 * bytes_deleted is > 0, it will be huge by the time we get here
4598 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004599 if (be_nice && bytes_deleted > SZ_32M &&
4600 btrfs_should_end_transaction(trans)) {
4601 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004602 goto out;
4603 }
Chris Masond3977122009-01-05 21:25:51 -05004604
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004605 path->leave_spinning = 1;
4606 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4607 if (ret < 0)
4608 goto out;
4609
Chris Mason85e21ba2008-01-29 15:11:36 -05004610 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004611 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004612 /* there are no items in the tree for us to truncate, we're
4613 * done
4614 */
Yan, Zheng80825102009-11-12 09:35:36 +00004615 if (path->slots[0] == 0)
4616 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004617 path->slots[0]--;
4618 }
4619
Chris Masond3977122009-01-05 21:25:51 -05004620 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004621 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004622 leaf = path->nodes[0];
4623 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004624 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004625
Li Zefan33345d012011-04-20 10:31:50 +08004626 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004627 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004628
Chris Mason85e21ba2008-01-29 15:11:36 -05004629 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004630 break;
4631
Chris Mason5f39d392007-10-15 16:14:19 -04004632 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004633 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004634 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004635 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004636 extent_type = btrfs_file_extent_type(leaf, fi);
4637 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004638 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004639 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004640
4641 trace_btrfs_truncate_show_fi_regular(
4642 BTRFS_I(inode), leaf, fi,
4643 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004644 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004645 item_end += btrfs_file_extent_ram_bytes(leaf,
4646 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004647
4648 trace_btrfs_truncate_show_fi_inline(
4649 BTRFS_I(inode), leaf, fi, path->slots[0],
4650 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004651 }
Yan008630c2007-11-07 13:31:09 -05004652 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004653 }
Yan, Zheng80825102009-11-12 09:35:36 +00004654 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004655 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004656 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004657 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004658 break;
4659 if (found_key.offset >= new_size)
4660 del_item = 1;
4661 else
4662 del_item = 0;
4663 }
Chris Mason39279cc2007-06-12 06:35:45 -04004664 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004665 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004666 if (found_type != BTRFS_EXTENT_DATA_KEY)
4667 goto delete;
4668
4669 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004670 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004671 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004672 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004673 u64 orig_num_bytes =
4674 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004675 extent_num_bytes = ALIGN(new_size -
4676 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004677 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004678 btrfs_set_file_extent_num_bytes(leaf, fi,
4679 extent_num_bytes);
4680 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004681 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004682 if (test_bit(BTRFS_ROOT_REF_COWS,
4683 &root->state) &&
4684 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004685 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004686 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004687 } else {
Chris Masondb945352007-10-15 16:15:53 -04004688 extent_num_bytes =
4689 btrfs_file_extent_disk_num_bytes(leaf,
4690 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004691 extent_offset = found_key.offset -
4692 btrfs_file_extent_offset(leaf, fi);
4693
Chris Mason39279cc2007-06-12 06:35:45 -04004694 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004695 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004696 if (extent_start != 0) {
4697 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004698 if (test_bit(BTRFS_ROOT_REF_COWS,
4699 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004700 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004701 }
4702 }
Chris Mason90692182008-02-08 13:49:28 -05004703 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004704 /*
4705 * we can't truncate inline items that have had
4706 * special encodings
4707 */
4708 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004709 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004710 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4711 btrfs_file_extent_compression(leaf, fi) == 0) {
4712 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004713
Josef Bacikddfae632017-10-19 14:16:02 -04004714 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4715 size = btrfs_file_extent_calc_inline_size(size);
4716 btrfs_truncate_item(root->fs_info, path, size, 1);
4717 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004718 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004719 * We have to bail so the last_size is set to
4720 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004721 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004722 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004723 break;
Chris Mason90692182008-02-08 13:49:28 -05004724 }
Josef Bacikddfae632017-10-19 14:16:02 -04004725
4726 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4727 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004728 }
Chris Mason179e29e2007-11-01 11:28:41 -04004729delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004730 if (del_item)
4731 last_size = found_key.offset;
4732 else
4733 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004734 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004735 if (!pending_del_nr) {
4736 /* no pending yet, add ourselves */
4737 pending_del_slot = path->slots[0];
4738 pending_del_nr = 1;
4739 } else if (pending_del_nr &&
4740 path->slots[0] + 1 == pending_del_slot) {
4741 /* hop on the pending chunk */
4742 pending_del_nr++;
4743 pending_del_slot = path->slots[0];
4744 } else {
Chris Masond3977122009-01-05 21:25:51 -05004745 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004746 }
Chris Mason39279cc2007-06-12 06:35:45 -04004747 } else {
4748 break;
4749 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004750 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004751
Miao Xie27cdeb72014-04-02 19:51:05 +08004752 if (found_extent &&
4753 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004754 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004755 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004756 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004757 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004758 extent_num_bytes, 0,
4759 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004760 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004761 if (ret) {
4762 btrfs_abort_transaction(trans, ret);
4763 break;
4764 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004765 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4766 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004767 trans->delayed_ref_updates * 2,
4768 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004769 if (be_nice) {
4770 if (truncate_space_check(trans, root,
4771 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004772 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004773 }
4774 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004775 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004776 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004777 }
Chris Mason39279cc2007-06-12 06:35:45 -04004778 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004779
Yan, Zheng80825102009-11-12 09:35:36 +00004780 if (found_type == BTRFS_INODE_ITEM_KEY)
4781 break;
4782
4783 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004784 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004785 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004786 if (pending_del_nr) {
4787 ret = btrfs_del_items(trans, root, path,
4788 pending_del_slot,
4789 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004790 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004791 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004792 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004793 }
Yan, Zheng80825102009-11-12 09:35:36 +00004794 pending_del_nr = 0;
4795 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004796 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004797 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004798 unsigned long updates = trans->delayed_ref_updates;
4799 if (updates) {
4800 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004801 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004802 updates * 2);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004803 if (ret)
4804 break;
Josef Bacik12621332015-02-03 07:50:16 -08004805 }
4806 }
Chris Mason28f75a02015-02-04 06:59:29 -08004807 /*
4808 * if we failed to refill our space rsv, bail out
4809 * and let the transaction restart
4810 */
4811 if (should_end) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004812 ret = -EAGAIN;
4813 break;
Chris Mason28f75a02015-02-04 06:59:29 -08004814 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004815 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004816 } else {
4817 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004818 }
Chris Mason39279cc2007-06-12 06:35:45 -04004819 }
Yan, Zheng80825102009-11-12 09:35:36 +00004820out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004821 if (ret >= 0 && pending_del_nr) {
4822 int err;
4823
4824 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004825 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004826 if (err) {
4827 btrfs_abort_transaction(trans, err);
4828 ret = err;
4829 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004830 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004831 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4832 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004833 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004834 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004835 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004836 }
Chris Mason28ed1342014-12-17 09:41:04 -08004837
Chris Mason39279cc2007-06-12 06:35:45 -04004838 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004839
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004840 if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004841 unsigned long updates = trans->delayed_ref_updates;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004842 int err;
4843
Chris Mason28ed1342014-12-17 09:41:04 -08004844 if (updates) {
4845 trans->delayed_ref_updates = 0;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004846 err = btrfs_run_delayed_refs(trans, updates * 2);
4847 if (err)
4848 ret = err;
Chris Mason28ed1342014-12-17 09:41:04 -08004849 }
4850 }
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004851 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004852}
4853
Chris Masona52d9a82007-08-27 16:49:44 -04004854/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004856 * @inode - inode that we're zeroing
4857 * @from - the offset to start zeroing
4858 * @len - the length to zero, 0 to zero the entire range respective to the
4859 * offset
4860 * @front - zero up to the offset instead of from the offset on
4861 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304862 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004863 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004864 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304865int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004866 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004869 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004870 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4871 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004872 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004873 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004874 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004875 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004876 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304877 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004878 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004879 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004880 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304881 u64 block_start;
4882 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004883
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004884 if (IS_ALIGNED(offset, blocksize) &&
4885 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004886 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304887
Josef Bacik8b62f872017-10-19 14:15:55 -04004888 block_start = round_down(from, blocksize);
4889 block_end = block_start + blocksize - 1;
4890
Qu Wenruo364ecf32017-02-27 15:10:38 +08004891 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004892 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004893 if (ret)
4894 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004895
Chris Mason211c17f2008-05-15 09:13:45 -04004896again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004897 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004898 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004899 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004900 block_start, blocksize, true);
4901 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004902 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004903 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004904 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004905
Chris Masona52d9a82007-08-27 16:49:44 -04004906 if (!PageUptodate(page)) {
4907 ret = btrfs_readpage(NULL, page);
4908 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004909 if (page->mapping != mapping) {
4910 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004911 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004912 goto again;
4913 }
Chris Masona52d9a82007-08-27 16:49:44 -04004914 if (!PageUptodate(page)) {
4915 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004916 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004917 }
4918 }
Chris Mason211c17f2008-05-15 09:13:45 -04004919 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004920
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304921 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922 set_page_extent_mapped(page);
4923
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304924 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004925 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304926 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004927 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004928 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004929 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004930 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004931 btrfs_put_ordered_extent(ordered);
4932 goto again;
4933 }
4934
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004936 EXTENT_DIRTY | EXTENT_DELALLOC |
4937 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004938 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004939
Filipe Mananae3b8a482017-11-04 00:16:59 +00004940 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004941 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004942 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304943 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004944 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004945 goto out_unlock;
4946 }
4947
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304948 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004949 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304950 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004951 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004952 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304953 memset(kaddr + (block_start - page_offset(page)),
4954 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004955 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304956 memset(kaddr + (block_start - page_offset(page)) + offset,
4957 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004958 flush_dcache_page(page);
4959 kunmap(page);
4960 }
Chris Mason247e7432008-07-17 12:53:51 -04004961 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004962 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004963 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004964
Chris Mason89642222008-07-24 09:41:53 -04004965out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004966 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004967 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004968 blocksize, true);
4969 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004970 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004971 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004972out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004973 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004974 return ret;
4975}
4976
Josef Bacik16e75492013-10-22 12:18:51 -04004977static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4978 u64 offset, u64 len)
4979{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004980 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004981 struct btrfs_trans_handle *trans;
4982 int ret;
4983
4984 /*
4985 * Still need to make sure the inode looks like it's been updated so
4986 * that any holes get logged if we fsync.
4987 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004988 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4989 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004990 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4991 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4992 return 0;
4993 }
4994
4995 /*
4996 * 1 - for the one we're dropping
4997 * 1 - for the one we're adding
4998 * 1 - for updating the inode.
4999 */
5000 trans = btrfs_start_transaction(root, 3);
5001 if (IS_ERR(trans))
5002 return PTR_ERR(trans);
5003
5004 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5005 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005006 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005007 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005008 return ret;
5009 }
5010
David Sterbaf85b7372017-01-20 14:54:07 +01005011 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5012 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005013 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005014 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005015 else
5016 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005017 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005018 return ret;
5019}
5020
Josef Bacik695a0d02011-03-04 15:46:53 -05005021/*
5022 * This function puts in dummy file extents for the area we're creating a hole
5023 * for. So if we are truncating this file to a larger size we need to insert
5024 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5025 * the range between oldsize and size
5026 */
Josef Bacika41ad392011-01-31 15:30:16 -05005027int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005029 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005030 struct btrfs_root *root = BTRFS_I(inode)->root;
5031 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005032 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005033 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005034 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5036 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005037 u64 last_byte;
5038 u64 cur_offset;
5039 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005040 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005041
Josef Bacika71754f2013-06-17 17:14:39 -04005042 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305043 * If our size started in the middle of a block we need to zero out the
5044 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005045 * expose stale data.
5046 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305047 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005048 if (err)
5049 return err;
5050
Yan Zheng9036c102008-10-30 14:19:41 -04005051 if (size <= hole_start)
5052 return 0;
5053
Yan Zheng9036c102008-10-30 14:19:41 -04005054 while (1) {
5055 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005056
David Sterbaff13db42015-12-03 14:30:40 +01005057 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005058 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005059 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005060 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005061 if (!ordered)
5062 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005063 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005064 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005065 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005066 btrfs_put_ordered_extent(ordered);
5067 }
5068
Yan Zheng9036c102008-10-30 14:19:41 -04005069 cur_offset = hole_start;
5070 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005071 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005072 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005073 if (IS_ERR(em)) {
5074 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005075 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005076 break;
5077 }
Yan Zheng9036c102008-10-30 14:19:41 -04005078 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005079 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005080 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005081 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005082 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005083
Josef Bacik16e75492013-10-22 12:18:51 -04005084 err = maybe_insert_hole(root, inode, cur_offset,
5085 hole_size);
5086 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005087 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005088 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005089 cur_offset + hole_size - 1, 0);
5090 hole_em = alloc_extent_map();
5091 if (!hole_em) {
5092 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5093 &BTRFS_I(inode)->runtime_flags);
5094 goto next;
5095 }
5096 hole_em->start = cur_offset;
5097 hole_em->len = hole_size;
5098 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005099
Josef Bacik5dc562c2012-08-17 13:14:17 -04005100 hole_em->block_start = EXTENT_MAP_HOLE;
5101 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005102 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005103 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005104 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005105 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005106 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005107
5108 while (1) {
5109 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005110 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005111 write_unlock(&em_tree->lock);
5112 if (err != -EEXIST)
5113 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005114 btrfs_drop_extent_cache(BTRFS_I(inode),
5115 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005116 cur_offset +
5117 hole_size - 1, 0);
5118 }
5119 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005120 }
Josef Bacik16e75492013-10-22 12:18:51 -04005121next:
Yan Zheng9036c102008-10-30 14:19:41 -04005122 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005123 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005124 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005125 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005126 break;
5127 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005128 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005129 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005130 return err;
5131}
5132
Eric Sandeen3972f262013-01-12 02:57:22 +00005133static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005134{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005135 struct btrfs_root *root = BTRFS_I(inode)->root;
5136 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005137 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005138 loff_t newsize = attr->ia_size;
5139 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005140 int ret;
5141
Eric Sandeen3972f262013-01-12 02:57:22 +00005142 /*
5143 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5144 * special case where we need to update the times despite not having
5145 * these flags set. For all other operations the VFS set these flags
5146 * explicitly if it wants a timestamp update.
5147 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005148 if (newsize != oldsize) {
5149 inode_inc_iversion(inode);
5150 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5151 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005152 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005153 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005154
Josef Bacika41ad392011-01-31 15:30:16 -05005155 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005156 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005157 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005158 * This is to ensure the snapshot captures a fully consistent
5159 * state of this file - if the snapshot captures this expanding
5160 * truncation, it must capture all writes that happened before
5161 * this truncation.
5162 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005163 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005164 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005165 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005166 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005167 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005168 }
Yan, Zheng80825102009-11-12 09:35:36 +00005169
Miao Xief4a2f4c2011-12-14 20:12:01 -05005170 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005171 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005172 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005173 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005174 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005175
5176 i_size_write(inode, newsize);
5177 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305178 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005179 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005180 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005181 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005182 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005183
Josef Bacika41ad392011-01-31 15:30:16 -05005184 /*
5185 * We're truncating a file that used to have good data down to
5186 * zero. Make sure it gets into the ordered flush list so that
5187 * any new writes get down to disk quickly.
5188 */
5189 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005190 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5191 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005192
Josef Bacika41ad392011-01-31 15:30:16 -05005193 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005194
5195 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005196 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005197 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005198 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005199
Filipe Manana213e8c52018-02-06 20:40:31 +00005200 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005201 if (ret && inode->i_nlink) {
5202 int err;
5203
5204 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005205 * Truncate failed, so fix up the in-memory size. We
5206 * adjusted disk_i_size down as we removed extents, so
5207 * wait for disk_i_size to be stable and then update the
5208 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005209 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005210 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005211 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005212 return err;
5213 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005214 }
Yan, Zheng80825102009-11-12 09:35:36 +00005215 }
5216
Josef Bacika41ad392011-01-31 15:30:16 -05005217 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005218}
5219
Chris Mason39279cc2007-06-12 06:35:45 -04005220static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5221{
David Howells2b0143b2015-03-17 22:25:59 +00005222 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005223 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005224 int err;
5225
Li Zefanb83cc962010-12-20 16:04:08 +08005226 if (btrfs_root_readonly(root))
5227 return -EROFS;
5228
Jan Kara31051c82016-05-26 16:55:18 +02005229 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005230 if (err)
5231 return err;
5232
Chris Mason5a3f23d2009-03-31 13:27:11 -04005233 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005234 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005235 if (err)
5236 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005237 }
Yan Zheng9036c102008-10-30 14:19:41 -04005238
Christoph Hellwig10257742010-06-04 11:30:02 +02005239 if (attr->ia_valid) {
5240 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005241 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005242 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005243
Josef Bacik22c44fe2011-11-30 10:45:38 -05005244 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005245 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005246 }
5247
Chris Mason39279cc2007-06-12 06:35:45 -04005248 return err;
5249}
Chris Mason61295eb2008-01-14 16:24:38 -05005250
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005251/*
5252 * While truncating the inode pages during eviction, we get the VFS calling
5253 * btrfs_invalidatepage() against each page of the inode. This is slow because
5254 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5255 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5256 * extent_state structures over and over, wasting lots of time.
5257 *
5258 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5259 * those expensive operations on a per page basis and do only the ordered io
5260 * finishing, while we release here the extent_map and extent_state structures,
5261 * without the excessive merging and splitting.
5262 */
5263static void evict_inode_truncate_pages(struct inode *inode)
5264{
5265 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5266 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5267 struct rb_node *node;
5268
5269 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005270 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005271
5272 write_lock(&map_tree->lock);
5273 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5274 struct extent_map *em;
5275
5276 node = rb_first(&map_tree->map);
5277 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005278 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5279 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005280 remove_extent_mapping(map_tree, em);
5281 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005282 if (need_resched()) {
5283 write_unlock(&map_tree->lock);
5284 cond_resched();
5285 write_lock(&map_tree->lock);
5286 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005287 }
5288 write_unlock(&map_tree->lock);
5289
Filipe Manana6ca07092015-05-26 00:55:42 +01005290 /*
5291 * Keep looping until we have no more ranges in the io tree.
5292 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005293 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5294 * still in progress (unlocked the pages in the bio but did not yet
5295 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005296 * ranges can still be locked and eviction started because before
5297 * submitting those bios, which are executed by a separate task (work
5298 * queue kthread), inode references (inode->i_count) were not taken
5299 * (which would be dropped in the end io callback of each bio).
5300 * Therefore here we effectively end up waiting for those bios and
5301 * anyone else holding locked ranges without having bumped the inode's
5302 * reference count - if we don't do it, when they access the inode's
5303 * io_tree to unlock a range it may be too late, leading to an
5304 * use-after-free issue.
5305 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005306 spin_lock(&io_tree->lock);
5307 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5308 struct extent_state *state;
5309 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005310 u64 start;
5311 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005312
5313 node = rb_first(&io_tree->state);
5314 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005315 start = state->start;
5316 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005317 spin_unlock(&io_tree->lock);
5318
David Sterbaff13db42015-12-03 14:30:40 +01005319 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005320
5321 /*
5322 * If still has DELALLOC flag, the extent didn't reach disk,
5323 * and its reserved space won't be freed by delayed_ref.
5324 * So we need to free its reserved space here.
5325 * (Refer to comment in btrfs_invalidatepage, case 2)
5326 *
5327 * Note, end is the bytenr of last byte, so we need + 1 here.
5328 */
5329 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005330 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005331
Filipe Manana6ca07092015-05-26 00:55:42 +01005332 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005333 EXTENT_LOCKED | EXTENT_DIRTY |
5334 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005335 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005336
Filipe Manana7064dd52014-08-08 02:47:05 +01005337 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005338 spin_lock(&io_tree->lock);
5339 }
5340 spin_unlock(&io_tree->lock);
5341}
5342
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005343static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5344 struct btrfs_block_rsv *rsv,
5345 u64 min_size)
5346{
5347 struct btrfs_fs_info *fs_info = root->fs_info;
5348 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5349 int failures = 0;
5350
5351 for (;;) {
5352 struct btrfs_trans_handle *trans;
5353 int ret;
5354
5355 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5356 BTRFS_RESERVE_FLUSH_LIMIT);
5357
5358 if (ret && ++failures > 2) {
5359 btrfs_warn(fs_info,
5360 "could not allocate space for a delete; will truncate on mount");
5361 return ERR_PTR(-ENOSPC);
5362 }
5363
5364 trans = btrfs_join_transaction(root);
5365 if (IS_ERR(trans) || !ret)
5366 return trans;
5367
5368 /*
5369 * Try to steal from the global reserve if there is space for
5370 * it.
5371 */
5372 if (!btrfs_check_space_for_delayed_refs(trans, fs_info) &&
5373 !btrfs_block_rsv_migrate(global_rsv, rsv, min_size, 0))
5374 return trans;
5375
5376 /* If not, commit and try again. */
5377 ret = btrfs_commit_transaction(trans);
5378 if (ret)
5379 return ERR_PTR(ret);
5380 }
5381}
5382
Al Virobd555972010-06-07 11:35:40 -04005383void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005384{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005385 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005386 struct btrfs_trans_handle *trans;
5387 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005388 struct btrfs_block_rsv *rsv;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005389 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 int ret;
5391
liubo1abe9b82011-03-24 11:18:59 +00005392 trace_btrfs_inode_evict(inode);
5393
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005394 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005395 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005396 return;
5397 }
5398
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005400
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005401 evict_inode_truncate_pages(inode);
5402
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005403 if (inode->i_nlink &&
5404 ((btrfs_root_refs(&root->root_item) != 0 &&
5405 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005406 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005407 goto no_delete;
5408
Omar Sandoval27919062018-05-11 13:13:37 -07005409 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005410 goto no_delete;
Al Virobd555972010-06-07 11:35:40 -04005411 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005412 if (!special_file(inode->i_mode))
5413 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005414
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005415 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005416
Omar Sandoval7b40b692018-05-11 13:13:33 -07005417 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005418 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005419
Yan, Zheng76dda932009-09-21 16:00:26 -04005420 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005421 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5422 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005423 goto no_delete;
5424 }
5425
Nikolay Borisovaa790212017-01-10 20:35:40 +02005426 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005427 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005428 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005429
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005430 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005431 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005432 goto no_delete;
Josef Bacik4a338542011-08-29 11:01:31 -04005433 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005434 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005435
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005436 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005437
Yan, Zheng80825102009-11-12 09:35:36 +00005438 while (1) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005439 trans = evict_refill_and_join(root, rsv, min_size);
Omar Sandoval27919062018-05-11 13:13:37 -07005440 if (IS_ERR(trans))
5441 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005442
5443 trans->block_rsv = rsv;
5444
Yan, Zhengd68fc572010-05-16 10:49:58 -04005445 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005446 trans->block_rsv = &fs_info->trans_block_rsv;
5447 btrfs_end_transaction(trans);
5448 btrfs_btree_balance_dirty(fs_info);
5449 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5450 goto free_rsv;
5451 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005452 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005453 }
5454
Josef Bacik4ef31a42013-08-13 14:10:08 -04005455 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005456 * Errors here aren't a big deal, it just means we leave orphan items in
5457 * the tree. They will be cleaned up on the next mount. If the inode
5458 * number gets reused, cleanup deletes the orphan item without doing
5459 * anything, and unlink reuses the existing orphan item.
5460 *
5461 * If it turns out that we are dropping too many of these, we might want
5462 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005463 */
Omar Sandoval27919062018-05-11 13:13:37 -07005464 trans = evict_refill_and_join(root, rsv, min_size);
5465 if (!IS_ERR(trans)) {
5466 trans->block_rsv = rsv;
5467 btrfs_orphan_del(trans, BTRFS_I(inode));
5468 trans->block_rsv = &fs_info->trans_block_rsv;
5469 btrfs_end_transaction(trans);
5470 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005471
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005472 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005473 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005474 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005475
Omar Sandoval27919062018-05-11 13:13:37 -07005476free_rsv:
5477 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005478no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005479 /*
5480 * If we didn't successfully delete, the orphan item will still be in
5481 * the tree and we'll retry on the next mount. Again, we might also want
5482 * to retry these periodically in the future.
5483 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005484 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005485 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005486}
5487
5488/*
5489 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005490 * If no dir entries were found, returns -ENOENT.
5491 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005492 */
5493static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5494 struct btrfs_key *location)
5495{
5496 const char *name = dentry->d_name.name;
5497 int namelen = dentry->d_name.len;
5498 struct btrfs_dir_item *di;
5499 struct btrfs_path *path;
5500 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005501 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005502
5503 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005504 if (!path)
5505 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005506
David Sterbaf85b7372017-01-20 14:54:07 +01005507 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5508 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005509 if (!di) {
5510 ret = -ENOENT;
5511 goto out;
5512 }
5513 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005514 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005515 goto out;
5516 }
Chris Masond3977122009-01-05 21:25:51 -05005517
Chris Mason5f39d392007-10-15 16:14:19 -04005518 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005519 if (location->type != BTRFS_INODE_ITEM_KEY &&
5520 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005521 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005522 btrfs_warn(root->fs_info,
5523"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5524 __func__, name, btrfs_ino(BTRFS_I(dir)),
5525 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005526 }
Chris Mason39279cc2007-06-12 06:35:45 -04005527out:
Chris Mason39279cc2007-06-12 06:35:45 -04005528 btrfs_free_path(path);
5529 return ret;
5530}
5531
5532/*
5533 * when we hit a tree root in a directory, the btrfs part of the inode
5534 * needs to be changed to reflect the root directory of the tree root. This
5535 * is kind of like crossing a mount point.
5536 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005537static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538 struct inode *dir,
5539 struct dentry *dentry,
5540 struct btrfs_key *location,
5541 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005542{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543 struct btrfs_path *path;
5544 struct btrfs_root *new_root;
5545 struct btrfs_root_ref *ref;
5546 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005547 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 int ret;
5549 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005550
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 path = btrfs_alloc_path();
5552 if (!path) {
5553 err = -ENOMEM;
5554 goto out;
5555 }
Chris Mason39279cc2007-06-12 06:35:45 -04005556
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005558 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5559 key.type = BTRFS_ROOT_REF_KEY;
5560 key.offset = location->objectid;
5561
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005562 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005563 if (ret) {
5564 if (ret < 0)
5565 err = ret;
5566 goto out;
5567 }
Chris Mason39279cc2007-06-12 06:35:45 -04005568
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005569 leaf = path->nodes[0];
5570 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005571 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005572 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5573 goto out;
5574
5575 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5576 (unsigned long)(ref + 1),
5577 dentry->d_name.len);
5578 if (ret)
5579 goto out;
5580
David Sterbab3b4aa72011-04-21 01:20:15 +02005581 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005582
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005583 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005584 if (IS_ERR(new_root)) {
5585 err = PTR_ERR(new_root);
5586 goto out;
5587 }
5588
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005589 *sub_root = new_root;
5590 location->objectid = btrfs_root_dirid(&new_root->root_item);
5591 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005592 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005593 err = 0;
5594out:
5595 btrfs_free_path(path);
5596 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005597}
5598
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599static void inode_tree_add(struct inode *inode)
5600{
5601 struct btrfs_root *root = BTRFS_I(inode)->root;
5602 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005603 struct rb_node **p;
5604 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005605 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005606 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607
Al Viro1d3382cb2010-10-23 15:19:20 -04005608 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005610 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005612 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 while (*p) {
5614 parent = *p;
5615 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5616
David Sterba37508512018-06-29 10:56:40 +02005617 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005618 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005619 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005620 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621 else {
5622 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005623 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005624 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005625 RB_CLEAR_NODE(parent);
5626 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005627 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005628 }
5629 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005630 rb_link_node(new, parent, p);
5631 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005632 spin_unlock(&root->inode_lock);
5633}
5634
5635static void inode_tree_del(struct inode *inode)
5636{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005639 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005641 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005643 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005644 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005645 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005647 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005648
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005649 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005650 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005651 spin_lock(&root->inode_lock);
5652 empty = RB_EMPTY_ROOT(&root->inode_tree);
5653 spin_unlock(&root->inode_lock);
5654 if (empty)
5655 btrfs_add_dead_root(root);
5656 }
5657}
5658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005659
Chris Masone02119d2008-09-05 16:13:11 -04005660static int btrfs_init_locked_inode(struct inode *inode, void *p)
5661{
5662 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005663 inode->i_ino = args->location->objectid;
5664 memcpy(&BTRFS_I(inode)->location, args->location,
5665 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005666 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005667 return 0;
5668}
5669
5670static int btrfs_find_actor(struct inode *inode, void *opaque)
5671{
5672 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005673 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005674 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005675}
5676
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005677static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005678 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005679 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005680{
5681 struct inode *inode;
5682 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005683 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005684
Chris Mason90d3e592014-01-09 17:28:00 -08005685 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005686 args.root = root;
5687
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005688 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005689 btrfs_init_locked_inode,
5690 (void *)&args);
5691 return inode;
5692}
5693
Balaji Rao1a54ef82008-07-21 02:01:04 +05305694/* Get an inode object given its location and corresponding root.
5695 * Returns in *is_new if the inode was read from disk
5696 */
5697struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005698 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305699{
5700 struct inode *inode;
5701
Chris Mason90d3e592014-01-09 17:28:00 -08005702 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305703 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005704 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305705
5706 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005707 int ret;
5708
5709 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005710 if (!is_bad_inode(inode)) {
5711 inode_tree_add(inode);
5712 unlock_new_inode(inode);
5713 if (new)
5714 *new = 1;
5715 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005716 unlock_new_inode(inode);
5717 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005718 ASSERT(ret < 0);
5719 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005720 }
5721 }
5722
Balaji Rao1a54ef82008-07-21 02:01:04 +05305723 return inode;
5724}
5725
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726static struct inode *new_simple_dir(struct super_block *s,
5727 struct btrfs_key *key,
5728 struct btrfs_root *root)
5729{
5730 struct inode *inode = new_inode(s);
5731
5732 if (!inode)
5733 return ERR_PTR(-ENOMEM);
5734
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005735 BTRFS_I(inode)->root = root;
5736 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005737 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005738
5739 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005740 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005741 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005742 inode->i_fop = &simple_dir_operations;
5743 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005744 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305745 inode->i_atime = inode->i_mtime;
5746 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005747 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005748
5749 return inode;
5750}
5751
Chris Mason3de45862008-11-17 21:02:50 -05005752struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005753{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005754 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005755 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005757 struct btrfs_root *sub_root = root;
5758 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005759 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005760 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005761
5762 if (dentry->d_name.len > BTRFS_NAME_LEN)
5763 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005764
Jeff Layton39e3c952012-11-28 11:30:53 -05005765 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005766 if (ret < 0)
5767 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005768
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005770 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005772 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005774 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005775 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776 &location, &sub_root);
5777 if (ret < 0) {
5778 if (ret != -ENOENT)
5779 inode = ERR_PTR(ret);
5780 else
5781 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5782 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005783 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005785 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005786
Julia Lawall34d19ba2011-01-24 19:55:19 +00005787 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005788 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005789 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005790 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005791 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005792 if (ret) {
5793 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005794 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005795 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005796 }
5797
Chris Mason3de45862008-11-17 21:02:50 -05005798 return inode;
5799}
5800
Nick Pigginfe15ce42011-01-07 17:49:23 +11005801static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005802{
5803 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005804 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005805
Li Zefan848cce02012-02-21 17:04:28 +08005806 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005807 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005808
Li Zefan848cce02012-02-21 17:04:28 +08005809 if (inode) {
5810 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005811 if (btrfs_root_refs(&root->root_item) == 0)
5812 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005813
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005814 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005815 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005816 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005817 return 0;
5818}
5819
Chris Mason3de45862008-11-17 21:02:50 -05005820static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005821 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005822{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005823 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005824
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005825 inode = btrfs_lookup_dentry(dir, dentry);
5826 if (IS_ERR(inode)) {
5827 if (PTR_ERR(inode) == -ENOENT)
5828 inode = NULL;
5829 else
5830 return ERR_CAST(inode);
5831 }
5832
Al Viro41d28bc2014-10-12 22:24:21 -04005833 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005834}
5835
Miao Xie16cdcec2011-04-22 18:12:22 +08005836unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005837 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5838};
5839
Josef Bacik23b5ec72017-07-24 15:14:25 -04005840/*
5841 * All this infrastructure exists because dir_emit can fault, and we are holding
5842 * the tree lock when doing readdir. For now just allocate a buffer and copy
5843 * our information into that, and then dir_emit from the buffer. This is
5844 * similar to what NFS does, only we don't keep the buffer around in pagecache
5845 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5846 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5847 * tree lock.
5848 */
5849static int btrfs_opendir(struct inode *inode, struct file *file)
5850{
5851 struct btrfs_file_private *private;
5852
5853 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5854 if (!private)
5855 return -ENOMEM;
5856 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5857 if (!private->filldir_buf) {
5858 kfree(private);
5859 return -ENOMEM;
5860 }
5861 file->private_data = private;
5862 return 0;
5863}
5864
5865struct dir_entry {
5866 u64 ino;
5867 u64 offset;
5868 unsigned type;
5869 int name_len;
5870};
5871
5872static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5873{
5874 while (entries--) {
5875 struct dir_entry *entry = addr;
5876 char *name = (char *)(entry + 1);
5877
David Sterba92d32172018-04-16 21:10:14 +02005878 ctx->pos = get_unaligned(&entry->offset);
5879 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5880 get_unaligned(&entry->ino),
5881 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005882 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005883 addr += sizeof(struct dir_entry) +
5884 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005885 ctx->pos++;
5886 }
5887 return 0;
5888}
5889
Al Viro9cdda8d2013-05-22 16:48:09 -04005890static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005891{
Al Viro9cdda8d2013-05-22 16:48:09 -04005892 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005893 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005894 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005895 struct btrfs_dir_item *di;
5896 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005897 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005898 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005899 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005900 struct list_head ins_list;
5901 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005902 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005903 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005904 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005905 char *name_ptr;
5906 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005907 int entries = 0;
5908 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005909 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005910 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005911
Al Viro9cdda8d2013-05-22 16:48:09 -04005912 if (!dir_emit_dots(file, ctx))
5913 return 0;
5914
David Woodhouse49593bf2008-08-17 17:08:36 +01005915 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005916 if (!path)
5917 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005918
Josef Bacik23b5ec72017-07-24 15:14:25 -04005919 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005920 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005921
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005922 INIT_LIST_HEAD(&ins_list);
5923 INIT_LIST_HEAD(&del_list);
5924 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005925
Josef Bacik23b5ec72017-07-24 15:14:25 -04005926again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005927 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005928 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005929 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005930
Chris Mason39279cc2007-06-12 06:35:45 -04005931 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5932 if (ret < 0)
5933 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005934
5935 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936 struct dir_entry *entry;
5937
Chris Mason5f39d392007-10-15 16:14:19 -04005938 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005939 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005940 if (slot >= btrfs_header_nritems(leaf)) {
5941 ret = btrfs_next_leaf(root, path);
5942 if (ret < 0)
5943 goto err;
5944 else if (ret > 0)
5945 break;
5946 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005947 }
Chris Mason3de45862008-11-17 21:02:50 -05005948
Chris Mason5f39d392007-10-15 16:14:19 -04005949 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5950
5951 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005952 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005953 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005954 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005955 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005956 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005957 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005958 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005959 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005960 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005961 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5962 PAGE_SIZE) {
5963 btrfs_release_path(path);
5964 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5965 if (ret)
5966 goto nopos;
5967 addr = private->filldir_buf;
5968 entries = 0;
5969 total_len = 0;
5970 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005971 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005972
5973 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005974 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005975 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005976 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5977 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005978 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5979 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005980 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005981 put_unaligned(location.objectid, &entry->ino);
5982 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983 entries++;
5984 addr += sizeof(struct dir_entry) + name_len;
5985 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005986next:
5987 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005988 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005989 btrfs_release_path(path);
5990
5991 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5992 if (ret)
5993 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005994
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005995 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005996 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005997 goto nopos;
5998
Zach Browndb62efb2013-07-11 16:19:42 -07005999 /*
6000 * Stop new entries from being returned after we return the last
6001 * entry.
6002 *
6003 * New directory entries are assigned a strictly increasing
6004 * offset. This means that new entries created during readdir
6005 * are *guaranteed* to be seen in the future by that readdir.
6006 * This has broken buggy programs which operate on names as
6007 * they're returned by readdir. Until we re-use freed offsets
6008 * we have this hack to stop new entries from being returned
6009 * under the assumption that they'll never reach this huge
6010 * offset.
6011 *
6012 * This is being careful not to overflow 32bit loff_t unless the
6013 * last entry requires it because doing so has broken 32bit apps
6014 * in the past.
6015 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006016 if (ctx->pos >= INT_MAX)
6017 ctx->pos = LLONG_MAX;
6018 else
6019 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006020nopos:
6021 ret = 0;
6022err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006023 if (put)
6024 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006025 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006026 return ret;
6027}
6028
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006029int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006030{
6031 struct btrfs_root *root = BTRFS_I(inode)->root;
6032 struct btrfs_trans_handle *trans;
6033 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006034 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006035
Josef Bacik72ac3c02012-05-23 14:13:11 -04006036 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006037 return 0;
6038
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006039 if (btrfs_fs_closing(root->fs_info) &&
6040 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006041 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006042
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006043 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006044 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006045 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006046 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006047 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006048 if (IS_ERR(trans))
6049 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006050 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006051 }
6052 return ret;
6053}
6054
6055/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006056 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006057 * inode changes. But, it is most likely to find the inode in cache.
6058 * FIXME, needs more benchmarking...there are no reasons other than performance
6059 * to keep or drop this code.
6060 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006061static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006062{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006064 struct btrfs_root *root = BTRFS_I(inode)->root;
6065 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006066 int ret;
6067
Josef Bacik72ac3c02012-05-23 14:13:11 -04006068 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006069 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006070
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006071 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006072 if (IS_ERR(trans))
6073 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006074
6075 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006076 if (ret && ret == -ENOSPC) {
6077 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006078 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006079 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006080 if (IS_ERR(trans))
6081 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006082
Chris Mason94b60442010-05-26 11:02:00 -04006083 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006084 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006085 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006086 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006087 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006088
6089 return ret;
6090}
6091
6092/*
6093 * This is a copy of file_update_time. We need this so we can return error on
6094 * ENOSPC for updating the inode in the case of file write and mmap writes.
6095 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006096static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006097 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006098{
Alexander Block2bc5565282012-06-15 09:49:33 +02006099 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006100 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006101
6102 if (btrfs_root_readonly(root))
6103 return -EROFS;
6104
Josef Bacike41f9412012-03-26 09:46:47 -04006105 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006106 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006107 if (flags & S_CTIME)
6108 inode->i_ctime = *now;
6109 if (flags & S_MTIME)
6110 inode->i_mtime = *now;
6111 if (flags & S_ATIME)
6112 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006113 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006114}
6115
Chris Masond352ac62008-09-29 15:18:18 -04006116/*
6117 * find the highest existing sequence number in a directory
6118 * and then set the in-memory index_cnt variable to reflect
6119 * free sequence numbers
6120 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006121static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006122{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006123 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006124 struct btrfs_key key, found_key;
6125 struct btrfs_path *path;
6126 struct extent_buffer *leaf;
6127 int ret;
6128
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006129 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006130 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006131 key.offset = (u64)-1;
6132
6133 path = btrfs_alloc_path();
6134 if (!path)
6135 return -ENOMEM;
6136
6137 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6138 if (ret < 0)
6139 goto out;
6140 /* FIXME: we should be able to handle this */
6141 if (ret == 0)
6142 goto out;
6143 ret = 0;
6144
6145 /*
6146 * MAGIC NUMBER EXPLANATION:
6147 * since we search a directory based on f_pos we have to start at 2
6148 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6149 * else has to start at 2
6150 */
6151 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006152 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006153 goto out;
6154 }
6155
6156 path->slots[0]--;
6157
6158 leaf = path->nodes[0];
6159 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6160
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006161 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006162 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006163 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006164 goto out;
6165 }
6166
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006167 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006168out:
6169 btrfs_free_path(path);
6170 return ret;
6171}
6172
Chris Masond352ac62008-09-29 15:18:18 -04006173/*
6174 * helper to find a free sequence number in a given directory. This current
6175 * code is very simple, later versions will do smarter things in the btree
6176 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006177int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006178{
6179 int ret = 0;
6180
Nikolay Borisov877574e2017-02-20 13:50:33 +02006181 if (dir->index_cnt == (u64)-1) {
6182 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006183 if (ret) {
6184 ret = btrfs_set_inode_index_count(dir);
6185 if (ret)
6186 return ret;
6187 }
Josef Bacikaec74772008-07-24 12:12:38 -04006188 }
6189
Nikolay Borisov877574e2017-02-20 13:50:33 +02006190 *index = dir->index_cnt;
6191 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006192
6193 return ret;
6194}
6195
Chris Masonb0d5d102014-09-08 13:08:51 -07006196static int btrfs_insert_inode_locked(struct inode *inode)
6197{
6198 struct btrfs_iget_args args;
6199 args.location = &BTRFS_I(inode)->location;
6200 args.root = BTRFS_I(inode)->root;
6201
6202 return insert_inode_locked4(inode,
6203 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6204 btrfs_find_actor, &args);
6205}
6206
Anand Jain19aee8d2017-07-18 17:37:05 +08006207/*
6208 * Inherit flags from the parent inode.
6209 *
6210 * Currently only the compression flags and the cow flags are inherited.
6211 */
6212static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6213{
6214 unsigned int flags;
6215
6216 if (!dir)
6217 return;
6218
6219 flags = BTRFS_I(dir)->flags;
6220
6221 if (flags & BTRFS_INODE_NOCOMPRESS) {
6222 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6223 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6224 } else if (flags & BTRFS_INODE_COMPRESS) {
6225 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6226 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6227 }
6228
6229 if (flags & BTRFS_INODE_NODATACOW) {
6230 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6231 if (S_ISREG(inode->i_mode))
6232 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6233 }
6234
David Sterba7b6a2212018-03-26 18:40:21 +02006235 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006236}
6237
Chris Mason39279cc2007-06-12 06:35:45 -04006238static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6239 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006240 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006241 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006242 u64 ref_objectid, u64 objectid,
6243 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006245 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006246 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006247 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006248 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006249 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006250 struct btrfs_inode_ref *ref;
6251 struct btrfs_key key[2];
6252 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006253 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006254 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006255 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006256
Chris Mason5f39d392007-10-15 16:14:19 -04006257 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006258 if (!path)
6259 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006260
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006261 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006262 if (!inode) {
6263 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006264 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006265 }
Chris Mason39279cc2007-06-12 06:35:45 -04006266
Li Zefan581bb052011-04-20 10:06:11 +08006267 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006268 * O_TMPFILE, set link count to 0, so that after this point,
6269 * we fill in an inode item with the correct link count.
6270 */
6271 if (!name)
6272 set_nlink(inode, 0);
6273
6274 /*
Li Zefan581bb052011-04-20 10:06:11 +08006275 * we have to initialize this early, so we can reclaim the inode
6276 * number if we fail afterwards in this function.
6277 */
6278 inode->i_ino = objectid;
6279
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006281 trace_btrfs_inode_request(dir);
6282
Nikolay Borisov877574e2017-02-20 13:50:33 +02006283 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006284 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006285 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006286 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006287 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006288 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006289 } else if (dir) {
6290 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006291 }
6292 /*
6293 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006294 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006295 * number
6296 */
6297 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006298 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006299 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006300 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006301 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006302
Josef Bacik5dc562c2012-08-17 13:14:17 -04006303 /*
6304 * We could have gotten an inode number from somebody who was fsynced
6305 * and then removed in this same transaction, so let's just set full
6306 * sync since it will be a full sync anyway and this will blow away the
6307 * old info in the log.
6308 */
6309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6310
Chris Mason9c583092008-01-29 15:15:18 -05006311 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006312 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006313 key[0].offset = 0;
6314
Chris Mason9c583092008-01-29 15:15:18 -05006315 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006316
6317 if (name) {
6318 /*
6319 * Start new inodes with an inode_ref. This is slightly more
6320 * efficient for small numbers of hard links since they will
6321 * be packed into one item. Extended refs will kick in if we
6322 * add more hard links than can fit in the ref item.
6323 */
6324 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006325 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006326 key[1].offset = ref_objectid;
6327
6328 sizes[1] = name_len + sizeof(*ref);
6329 }
Chris Mason9c583092008-01-29 15:15:18 -05006330
Chris Masonb0d5d102014-09-08 13:08:51 -07006331 location = &BTRFS_I(inode)->location;
6332 location->objectid = objectid;
6333 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006334 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006335
6336 ret = btrfs_insert_inode_locked(inode);
6337 if (ret < 0)
6338 goto fail;
6339
Chris Masonb9473432009-03-13 11:00:37 -04006340 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006341 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006342 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006343 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006344
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006345 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006346 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306347
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006348 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306349 inode->i_atime = inode->i_mtime;
6350 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006351 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306352
Chris Mason5f39d392007-10-15 16:14:19 -04006353 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6354 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006355 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006356 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006357 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006358
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006359 if (name) {
6360 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6361 struct btrfs_inode_ref);
6362 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6363 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6364 ptr = (unsigned long)(ref + 1);
6365 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6366 }
Chris Mason9c583092008-01-29 15:15:18 -05006367
Chris Mason5f39d392007-10-15 16:14:19 -04006368 btrfs_mark_buffer_dirty(path->nodes[0]);
6369 btrfs_free_path(path);
6370
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006371 btrfs_inherit_iflags(inode, dir);
6372
Al Viro569254b2011-07-24 17:08:40 -04006373 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006375 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006376 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006377 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6378 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006379 }
6380
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006381 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006382
6383 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006384 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006385
Alexander Block8ea05e32012-07-25 17:35:53 +02006386 btrfs_update_root_times(trans, root);
6387
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006388 ret = btrfs_inode_inherit_props(trans, inode, dir);
6389 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006390 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006391 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006392 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006393
Chris Mason39279cc2007-06-12 06:35:45 -04006394 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006395
6396fail_unlock:
6397 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006398fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006399 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006400 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006401 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006402 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006403 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006404}
6405
6406static inline u8 btrfs_inode_type(struct inode *inode)
6407{
6408 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6409}
6410
Chris Masond352ac62008-09-29 15:18:18 -04006411/*
6412 * utility function to add 'inode' into 'parent_inode' with
6413 * a give name and a given sequence number.
6414 * if 'add_backref' is true, also insert a backref from the
6415 * inode to the parent directory.
6416 */
Chris Masone02119d2008-09-05 16:13:11 -04006417int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006418 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006419 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006420{
David Sterba3ffbd682018-06-29 10:56:42 +02006421 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006422 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006423 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006424 struct btrfs_root *root = parent_inode->root;
6425 u64 ino = btrfs_ino(inode);
6426 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006427
Li Zefan33345d012011-04-20 10:31:50 +08006428 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006429 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006430 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006431 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006432 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006433 key.offset = 0;
6434 }
Chris Mason39279cc2007-06-12 06:35:45 -04006435
Li Zefan33345d012011-04-20 10:31:50 +08006436 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006437 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6438 root->root_key.objectid, parent_ino,
6439 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006440 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006441 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6442 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006443 }
6444
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006445 /* Nothing to clean up yet */
6446 if (ret)
6447 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006448
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006449 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6450 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006451 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006452 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006453 goto fail_dir_item;
6454 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006455 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006456 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006457 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006458
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006459 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006460 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006461 inode_inc_iversion(&parent_inode->vfs_inode);
6462 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6463 current_time(&parent_inode->vfs_inode);
6464 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006465 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006466 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006467 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006468
6469fail_dir_item:
6470 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6471 u64 local_index;
6472 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006473 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6474 root->root_key.objectid, parent_ino,
6475 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006476
6477 } else if (add_backref) {
6478 u64 local_index;
6479 int err;
6480
6481 err = btrfs_del_inode_ref(trans, root, name, name_len,
6482 ino, parent_ino, &local_index);
6483 }
6484 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006485}
6486
6487static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006488 struct btrfs_inode *dir, struct dentry *dentry,
6489 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006490{
Josef Bacika1b075d2010-11-19 20:36:11 +00006491 int err = btrfs_add_link(trans, dir, inode,
6492 dentry->d_name.name, dentry->d_name.len,
6493 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006494 if (err > 0)
6495 err = -EEXIST;
6496 return err;
6497}
6498
Josef Bacik618e21d2007-07-11 10:18:17 -04006499static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006500 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006501{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006502 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006503 struct btrfs_trans_handle *trans;
6504 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006505 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006506 int err;
6507 int drop_inode = 0;
6508 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006509 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006510
Josef Bacik9ed74f22009-09-11 16:12:44 -04006511 /*
6512 * 2 for inode item and ref
6513 * 2 for dir items
6514 * 1 for xattr if selinux is on
6515 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006516 trans = btrfs_start_transaction(root, 5);
6517 if (IS_ERR(trans))
6518 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006519
Li Zefan581bb052011-04-20 10:06:11 +08006520 err = btrfs_find_free_ino(root, &objectid);
6521 if (err)
6522 goto out_unlock;
6523
Josef Bacikaec74772008-07-24 12:12:38 -04006524 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006525 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6526 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006527 if (IS_ERR(inode)) {
6528 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006529 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006530 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006531
Casey Schauflerad19db72011-12-15 10:09:07 -05006532 /*
6533 * If the active LSM wants to access the inode during
6534 * d_instantiate it needs these. Smack checks to see
6535 * if the filesystem supports xattrs by looking at the
6536 * ops vector.
6537 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006538 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006539 init_special_inode(inode, inode->i_mode, rdev);
6540
6541 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006542 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006543 goto out_unlock_inode;
6544
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006545 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6546 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006547 if (err) {
6548 goto out_unlock_inode;
6549 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006550 btrfs_update_inode(trans, root, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04006551 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006552 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006553
Josef Bacik618e21d2007-07-11 10:18:17 -04006554out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006555 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006556 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006557 if (drop_inode) {
6558 inode_dec_link_count(inode);
6559 iput(inode);
6560 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006561 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006562
6563out_unlock_inode:
6564 drop_inode = 1;
6565 unlock_new_inode(inode);
6566 goto out_unlock;
6567
Josef Bacik618e21d2007-07-11 10:18:17 -04006568}
6569
Chris Mason39279cc2007-06-12 06:35:45 -04006570static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006571 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006572{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006573 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006574 struct btrfs_trans_handle *trans;
6575 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006576 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006577 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006579 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006580 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006581
Josef Bacik9ed74f22009-09-11 16:12:44 -04006582 /*
6583 * 2 for inode item and ref
6584 * 2 for dir items
6585 * 1 for xattr if selinux is on
6586 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006587 trans = btrfs_start_transaction(root, 5);
6588 if (IS_ERR(trans))
6589 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006590
Li Zefan581bb052011-04-20 10:06:11 +08006591 err = btrfs_find_free_ino(root, &objectid);
6592 if (err)
6593 goto out_unlock;
6594
Josef Bacikaec74772008-07-24 12:12:38 -04006595 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006596 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6597 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006598 if (IS_ERR(inode)) {
6599 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006600 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006601 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006602 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006603 /*
6604 * If the active LSM wants to access the inode during
6605 * d_instantiate it needs these. Smack checks to see
6606 * if the filesystem supports xattrs by looking at the
6607 * ops vector.
6608 */
6609 inode->i_fop = &btrfs_file_operations;
6610 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006611 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006612
6613 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6614 if (err)
6615 goto out_unlock_inode;
6616
6617 err = btrfs_update_inode(trans, root, inode);
6618 if (err)
6619 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006620
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006621 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6622 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006623 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006624 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006625
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006626 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006627 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006628
Chris Mason39279cc2007-06-12 06:35:45 -04006629out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006630 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006631 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006632 inode_dec_link_count(inode);
6633 iput(inode);
6634 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006635 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006636 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006637
6638out_unlock_inode:
6639 unlock_new_inode(inode);
6640 goto out_unlock;
6641
Chris Mason39279cc2007-06-12 06:35:45 -04006642}
6643
6644static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6645 struct dentry *dentry)
6646{
Filipe Manana271dba452016-01-05 16:24:05 +00006647 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006648 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006649 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006650 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006651 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006652 int err;
6653 int drop_inode = 0;
6654
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006655 /* do not allow sys_link's with other subvols of the same device */
6656 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006657 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006658
Mark Fashehf1863732012-08-08 11:32:27 -07006659 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006660 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006661
Nikolay Borisov877574e2017-02-20 13:50:33 +02006662 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006663 if (err)
6664 goto fail;
6665
Yan, Zhenga22285a2010-05-16 10:48:46 -04006666 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006667 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006668 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006669 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006670 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006671 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006672 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006673 if (IS_ERR(trans)) {
6674 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006675 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006676 goto fail;
6677 }
Chris Mason5f39d392007-10-15 16:14:19 -04006678
Miao Xie67de1172013-12-26 13:07:06 +08006679 /* There are several dir indexes for this inode, clear the cache. */
6680 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006681 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006682 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006683 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006684 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006685 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006686
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006687 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6688 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006689
Yan, Zhenga5719522009-09-24 09:17:31 -04006690 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006691 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006692 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006693 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006694 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006695 if (err)
6696 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006697 if (inode->i_nlink == 1) {
6698 /*
6699 * If new hard link count is 1, it's a file created
6700 * with open(2) O_TMPFILE flag.
6701 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006702 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006703 if (err)
6704 goto fail;
6705 }
Al Viro08c422c2011-12-23 07:58:13 -05006706 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006707 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006708 }
Chris Mason39279cc2007-06-12 06:35:45 -04006709
Chris Mason1832a6d2007-12-21 16:27:21 -05006710fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006711 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006712 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006713 if (drop_inode) {
6714 inode_dec_link_count(inode);
6715 iput(inode);
6716 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006717 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006718 return err;
6719}
6720
Al Viro18bb1db2011-07-26 01:41:39 -04006721static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006722{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006723 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006724 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006725 struct btrfs_trans_handle *trans;
6726 struct btrfs_root *root = BTRFS_I(dir)->root;
6727 int err = 0;
6728 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006729 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006730 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006731
Josef Bacik9ed74f22009-09-11 16:12:44 -04006732 /*
6733 * 2 items for inode and ref
6734 * 2 items for dir items
6735 * 1 for xattr if selinux is on
6736 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006737 trans = btrfs_start_transaction(root, 5);
6738 if (IS_ERR(trans))
6739 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006740
Li Zefan581bb052011-04-20 10:06:11 +08006741 err = btrfs_find_free_ino(root, &objectid);
6742 if (err)
6743 goto out_fail;
6744
Josef Bacikaec74772008-07-24 12:12:38 -04006745 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006746 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6747 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006748 if (IS_ERR(inode)) {
6749 err = PTR_ERR(inode);
6750 goto out_fail;
6751 }
Chris Mason5f39d392007-10-15 16:14:19 -04006752
Chris Mason39279cc2007-06-12 06:35:45 -04006753 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006754 /* these must be set before we unlock the inode */
6755 inode->i_op = &btrfs_dir_inode_operations;
6756 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006757
Eric Paris2a7dba32011-02-01 11:05:39 -05006758 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006759 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006760 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006761
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006762 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006763 err = btrfs_update_inode(trans, root, inode);
6764 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006765 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006766
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006767 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6768 dentry->d_name.name,
6769 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006770 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006771 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006772
Al Viro1e2e5472018-05-04 08:23:01 -04006773 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006774 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006775
6776out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006777 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006778 if (drop_on_err) {
6779 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006780 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006781 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006782 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006783 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006784
6785out_fail_inode:
6786 unlock_new_inode(inode);
6787 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006788}
6789
Chris Masonc8b97812008-10-29 14:49:59 -04006790static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006791 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006792 size_t pg_offset, u64 extent_offset,
6793 struct btrfs_file_extent_item *item)
6794{
6795 int ret;
6796 struct extent_buffer *leaf = path->nodes[0];
6797 char *tmp;
6798 size_t max_size;
6799 unsigned long inline_size;
6800 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006801 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006802
6803 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006804 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006805 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6806 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006807 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006808 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006809 if (!tmp)
6810 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006811 ptr = btrfs_file_extent_inline_start(item);
6812
6813 read_extent_buffer(leaf, tmp, ptr, inline_size);
6814
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006815 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006816 ret = btrfs_decompress(compress_type, tmp, page,
6817 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006818
6819 /*
6820 * decompression code contains a memset to fill in any space between the end
6821 * of the uncompressed data and the end of max_size in case the decompressed
6822 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6823 * the end of an inline extent and the beginning of the next block, so we
6824 * cover that region here.
6825 */
6826
6827 if (max_size + pg_offset < PAGE_SIZE) {
6828 char *map = kmap(page);
6829 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6830 kunmap(page);
6831 }
Chris Masonc8b97812008-10-29 14:49:59 -04006832 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006833 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006834}
6835
Chris Masond352ac62008-09-29 15:18:18 -04006836/*
6837 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006838 * the ugly parts come from merging extents from the disk with the in-ram
6839 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006840 * where the in-ram extents might be locked pending data=ordered completion.
6841 *
6842 * This also copies inline extents directly into the page.
6843 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006844struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6845 struct page *page,
6846 size_t pg_offset, u64 start, u64 len,
6847 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006848{
David Sterba3ffbd682018-06-29 10:56:42 +02006849 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006850 int ret;
6851 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006852 u64 extent_start = 0;
6853 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006854 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006855 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006856 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006857 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006858 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006859 struct extent_buffer *leaf;
6860 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006861 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006862 struct extent_map_tree *em_tree = &inode->extent_tree;
6863 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006864 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006865
Chris Mason890871b2009-09-02 16:24:52 -04006866 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006867 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006868 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006869 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006870 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006871
Chris Masona52d9a82007-08-27 16:49:44 -04006872 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006873 if (em->start > start || em->start + em->len <= start)
6874 free_extent_map(em);
6875 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006876 free_extent_map(em);
6877 else
6878 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 }
David Sterba172ddd62011-04-21 00:48:27 +02006880 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006881 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006882 err = -ENOMEM;
6883 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006885 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006886 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006887 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006888 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006889 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006890
6891 if (!path) {
6892 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006893 if (!path) {
6894 err = -ENOMEM;
6895 goto out;
6896 }
6897 /*
6898 * Chances are we'll be called again, so go ahead and do
6899 * readahead
6900 */
David Sterbae4058b52015-11-27 16:31:35 +01006901 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006902 }
6903
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006904 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006905 if (ret < 0) {
6906 err = ret;
6907 goto out;
6908 }
6909
6910 if (ret != 0) {
6911 if (path->slots[0] == 0)
6912 goto not_found;
6913 path->slots[0]--;
6914 }
6915
Chris Mason5f39d392007-10-15 16:14:19 -04006916 leaf = path->nodes[0];
6917 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006918 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006919 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006920 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006921 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006922 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006923 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006924 /*
6925 * If we backup past the first extent we want to move forward
6926 * and see if there is an extent in front of us, otherwise we'll
6927 * say there is a hole for our whole search range which can
6928 * cause problems.
6929 */
6930 extent_end = start;
6931 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006932 }
6933
Chris Mason5f39d392007-10-15 16:14:19 -04006934 found_type = btrfs_file_extent_type(leaf, item);
6935 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006936 if (found_type == BTRFS_FILE_EXTENT_REG ||
6937 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006938 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006939 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006940
6941 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6942 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006943 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6944 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006945
6946 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006947 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006948 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006949
6950 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6951 path->slots[0],
6952 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006953 }
Josef Bacik25a50342013-10-14 12:08:38 -04006954next:
Yan Zheng9036c102008-10-30 14:19:41 -04006955 if (start >= extent_end) {
6956 path->slots[0]++;
6957 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6958 ret = btrfs_next_leaf(root, path);
6959 if (ret < 0) {
6960 err = ret;
6961 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006962 }
Yan Zheng9036c102008-10-30 14:19:41 -04006963 if (ret > 0)
6964 goto not_found;
6965 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006966 }
Yan Zheng9036c102008-10-30 14:19:41 -04006967 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6968 if (found_key.objectid != objectid ||
6969 found_key.type != BTRFS_EXTENT_DATA_KEY)
6970 goto not_found;
6971 if (start + len <= found_key.offset)
6972 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006973 if (start > found_key.offset)
6974 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006975 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006976 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006977 em->len = found_key.offset - start;
6978 goto not_found_em;
6979 }
6980
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006981 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006982 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006983
Yan Zhengd899e052008-10-30 14:25:28 -04006984 if (found_type == BTRFS_FILE_EXTENT_REG ||
6985 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006986 goto insert;
6987 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006988 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006989 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006990 size_t size;
6991 size_t extent_offset;
6992 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006993
Filipe Manana7ffbb592014-06-09 03:48:05 +01006994 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006995 goto out;
Yan689f9342007-10-29 11:41:07 -04006996
Qu Wenruoe41ca582018-06-06 15:41:49 +08006997 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006998 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006999 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7000 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007001 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007002 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007003 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007004 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007005 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007006 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007007 if (btrfs_file_extent_compression(leaf, item) !=
7008 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007009 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007010 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007011 if (ret) {
7012 err = ret;
7013 goto out;
7014 }
Chris Masonc8b97812008-10-29 14:49:59 -04007015 } else {
7016 map = kmap(page);
7017 read_extent_buffer(leaf, map + pg_offset, ptr,
7018 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007019 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007020 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007021 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007022 copy_size);
7023 }
Chris Masonc8b97812008-10-29 14:49:59 -04007024 kunmap(page);
7025 }
Chris Mason179e29e2007-11-01 11:28:41 -04007026 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007027 }
Chris Masond1310b22008-01-24 16:13:08 -05007028 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007029 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007030 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007031 }
7032not_found:
7033 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007034 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007035 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007036not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007037 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007038insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007039 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007040 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007041 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007042 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7043 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007044 err = -EIO;
7045 goto out;
7046 }
Chris Masond1310b22008-01-24 16:13:08 -05007047
7048 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007049 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007050 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007051 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007052out:
liubo1abe9b82011-03-24 11:18:59 +00007053
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007054 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007055
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007056 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007057 if (err) {
7058 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007059 return ERR_PTR(err);
7060 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007061 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007062 return em;
7063}
7064
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007065struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7066 struct page *page,
7067 size_t pg_offset, u64 start, u64 len,
7068 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007069{
7070 struct extent_map *em;
7071 struct extent_map *hole_em = NULL;
7072 u64 range_start = start;
7073 u64 end;
7074 u64 found;
7075 u64 found_end;
7076 int err = 0;
7077
7078 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7079 if (IS_ERR(em))
7080 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007081 /*
7082 * If our em maps to:
7083 * - a hole or
7084 * - a pre-alloc extent,
7085 * there might actually be delalloc bytes behind it.
7086 */
7087 if (em->block_start != EXTENT_MAP_HOLE &&
7088 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7089 return em;
7090 else
7091 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007092
7093 /* check to see if we've wrapped (len == -1 or similar) */
7094 end = start + len;
7095 if (end < start)
7096 end = (u64)-1;
7097 else
7098 end -= 1;
7099
7100 em = NULL;
7101
7102 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007103 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007104 end, len, EXTENT_DELALLOC, 1);
7105 found_end = range_start + found;
7106 if (found_end < range_start)
7107 found_end = (u64)-1;
7108
7109 /*
7110 * we didn't find anything useful, return
7111 * the original results from get_extent()
7112 */
7113 if (range_start > end || found_end <= start) {
7114 em = hole_em;
7115 hole_em = NULL;
7116 goto out;
7117 }
7118
7119 /* adjust the range_start to make sure it doesn't
7120 * go backwards from the start they passed in
7121 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307122 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007123 found = found_end - range_start;
7124
7125 if (found > 0) {
7126 u64 hole_start = start;
7127 u64 hole_len = len;
7128
David Sterba172ddd62011-04-21 00:48:27 +02007129 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007130 if (!em) {
7131 err = -ENOMEM;
7132 goto out;
7133 }
7134 /*
7135 * when btrfs_get_extent can't find anything it
7136 * returns one huge hole
7137 *
7138 * make sure what it found really fits our range, and
7139 * adjust to make sure it is based on the start from
7140 * the caller
7141 */
7142 if (hole_em) {
7143 u64 calc_end = extent_map_end(hole_em);
7144
7145 if (calc_end <= start || (hole_em->start > end)) {
7146 free_extent_map(hole_em);
7147 hole_em = NULL;
7148 } else {
7149 hole_start = max(hole_em->start, start);
7150 hole_len = calc_end - hole_start;
7151 }
7152 }
7153 em->bdev = NULL;
7154 if (hole_em && range_start > hole_start) {
7155 /* our hole starts before our delalloc, so we
7156 * have to return just the parts of the hole
7157 * that go until the delalloc starts
7158 */
7159 em->len = min(hole_len,
7160 range_start - hole_start);
7161 em->start = hole_start;
7162 em->orig_start = hole_start;
7163 /*
7164 * don't adjust block start at all,
7165 * it is fixed at EXTENT_MAP_HOLE
7166 */
7167 em->block_start = hole_em->block_start;
7168 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007169 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7170 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007171 } else {
7172 em->start = range_start;
7173 em->len = found;
7174 em->orig_start = range_start;
7175 em->block_start = EXTENT_MAP_DELALLOC;
7176 em->block_len = found;
7177 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007178 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007179 return hole_em;
7180 }
7181out:
7182
7183 free_extent_map(hole_em);
7184 if (err) {
7185 free_extent_map(em);
7186 return ERR_PTR(err);
7187 }
7188 return em;
7189}
7190
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007191static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7192 const u64 start,
7193 const u64 len,
7194 const u64 orig_start,
7195 const u64 block_start,
7196 const u64 block_len,
7197 const u64 orig_block_len,
7198 const u64 ram_bytes,
7199 const int type)
7200{
7201 struct extent_map *em = NULL;
7202 int ret;
7203
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007204 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007205 em = create_io_em(inode, start, len, orig_start,
7206 block_start, block_len, orig_block_len,
7207 ram_bytes,
7208 BTRFS_COMPRESS_NONE, /* compress_type */
7209 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007210 if (IS_ERR(em))
7211 goto out;
7212 }
7213 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7214 len, block_len, type);
7215 if (ret) {
7216 if (em) {
7217 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007218 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007219 start + len - 1, 0);
7220 }
7221 em = ERR_PTR(ret);
7222 }
7223 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007224
7225 return em;
7226}
7227
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7229 u64 start, u64 len)
7230{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007233 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007234 struct btrfs_key ins;
7235 u64 alloc_hint;
7236 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007237
Josef Bacik4b46fce2010-05-23 11:00:55 -04007238 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007239 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007240 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007241 if (ret)
7242 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007243
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007244 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7245 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007246 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007247 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007248 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007249 btrfs_free_reserved_extent(fs_info, ins.objectid,
7250 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007251
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 return em;
7253}
7254
Chris Mason46bfbb52010-05-26 11:04:10 -04007255/*
7256 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7257 * block must be cow'd
7258 */
Josef Bacik00361582013-08-14 14:02:47 -04007259noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007260 u64 *orig_start, u64 *orig_block_len,
7261 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007262{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007264 struct btrfs_path *path;
7265 int ret;
7266 struct extent_buffer *leaf;
7267 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007268 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007269 struct btrfs_file_extent_item *fi;
7270 struct btrfs_key key;
7271 u64 disk_bytenr;
7272 u64 backref_offset;
7273 u64 extent_end;
7274 u64 num_bytes;
7275 int slot;
7276 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007277 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007278
Chris Mason46bfbb52010-05-26 11:04:10 -04007279 path = btrfs_alloc_path();
7280 if (!path)
7281 return -ENOMEM;
7282
David Sterbaf85b7372017-01-20 14:54:07 +01007283 ret = btrfs_lookup_file_extent(NULL, root, path,
7284 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007285 if (ret < 0)
7286 goto out;
7287
7288 slot = path->slots[0];
7289 if (ret == 1) {
7290 if (slot == 0) {
7291 /* can't find the item, must cow */
7292 ret = 0;
7293 goto out;
7294 }
7295 slot--;
7296 }
7297 ret = 0;
7298 leaf = path->nodes[0];
7299 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007300 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007301 key.type != BTRFS_EXTENT_DATA_KEY) {
7302 /* not our file or wrong item type, must cow */
7303 goto out;
7304 }
7305
7306 if (key.offset > offset) {
7307 /* Wrong offset, must cow */
7308 goto out;
7309 }
7310
7311 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7312 found_type = btrfs_file_extent_type(leaf, fi);
7313 if (found_type != BTRFS_FILE_EXTENT_REG &&
7314 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7315 /* not a regular extent, must cow */
7316 goto out;
7317 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007318
7319 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7320 goto out;
7321
Miao Xiee77751a2013-12-27 21:11:50 +08007322 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7323 if (extent_end <= offset)
7324 goto out;
7325
Chris Mason46bfbb52010-05-26 11:04:10 -04007326 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007327 if (disk_bytenr == 0)
7328 goto out;
7329
7330 if (btrfs_file_extent_compression(leaf, fi) ||
7331 btrfs_file_extent_encryption(leaf, fi) ||
7332 btrfs_file_extent_other_encoding(leaf, fi))
7333 goto out;
7334
Ethan Lien78d42952018-05-17 14:58:29 +08007335 /*
7336 * Do the same check as in btrfs_cross_ref_exist but without the
7337 * unnecessary search.
7338 */
7339 if (btrfs_file_extent_generation(leaf, fi) <=
7340 btrfs_root_last_snapshot(&root->root_item))
7341 goto out;
7342
Chris Mason46bfbb52010-05-26 11:04:10 -04007343 backref_offset = btrfs_file_extent_offset(leaf, fi);
7344
Josef Bacik7ee9e442013-06-21 16:37:03 -04007345 if (orig_start) {
7346 *orig_start = key.offset - backref_offset;
7347 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7348 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7349 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007350
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007351 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007352 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007353
7354 num_bytes = min(offset + *len, extent_end) - offset;
7355 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7356 u64 range_end;
7357
Jeff Mahoneyda170662016-06-15 09:22:56 -04007358 range_end = round_up(offset + num_bytes,
7359 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007360 ret = test_range_bit(io_tree, offset, range_end,
7361 EXTENT_DELALLOC, 0, NULL);
7362 if (ret) {
7363 ret = -EAGAIN;
7364 goto out;
7365 }
7366 }
7367
Josef Bacik1bda19e2013-10-18 12:10:36 -04007368 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007369
7370 /*
7371 * look for other files referencing this extent, if we
7372 * find any we must cow
7373 */
Josef Bacik00361582013-08-14 14:02:47 -04007374
Liu Boe4c3b2d2017-01-30 12:25:28 -08007375 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007376 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007377 if (ret) {
7378 ret = 0;
7379 goto out;
7380 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007381
7382 /*
7383 * adjust disk_bytenr and num_bytes to cover just the bytes
7384 * in this extent we are about to write. If there
7385 * are any csums in that range we have to cow in order
7386 * to keep the csums correct
7387 */
7388 disk_bytenr += backref_offset;
7389 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007390 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7391 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007392 /*
7393 * all of the above have passed, it is safe to overwrite this extent
7394 * without cow
7395 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007396 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007397 ret = 1;
7398out:
7399 btrfs_free_path(path);
7400 return ret;
7401}
7402
Josef Bacikeb838e72012-07-31 16:28:48 -04007403static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7404 struct extent_state **cached_state, int writing)
7405{
7406 struct btrfs_ordered_extent *ordered;
7407 int ret = 0;
7408
7409 while (1) {
7410 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007411 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007412 /*
7413 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007414 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007415 * extents in this range.
7416 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007417 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007418 lockend - lockstart + 1);
7419
7420 /*
7421 * We need to make sure there are no buffered pages in this
7422 * range either, we could have raced between the invalidate in
7423 * generic_file_direct_write and locking the extent. The
7424 * invalidate needs to happen so that reads after a write do not
7425 * get stale data.
7426 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007427 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007428 (!writing || !filemap_range_has_page(inode->i_mapping,
7429 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007430 break;
7431
7432 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007433 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007434
7435 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007436 /*
7437 * If we are doing a DIO read and the ordered extent we
7438 * found is for a buffered write, we can not wait for it
7439 * to complete and retry, because if we do so we can
7440 * deadlock with concurrent buffered writes on page
7441 * locks. This happens only if our DIO read covers more
7442 * than one extent map, if at this point has already
7443 * created an ordered extent for a previous extent map
7444 * and locked its range in the inode's io tree, and a
7445 * concurrent write against that previous extent map's
7446 * range and this range started (we unlock the ranges
7447 * in the io tree only when the bios complete and
7448 * buffered writes always lock pages before attempting
7449 * to lock range in the io tree).
7450 */
7451 if (writing ||
7452 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7453 btrfs_start_ordered_extent(inode, ordered, 1);
7454 else
7455 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007456 btrfs_put_ordered_extent(ordered);
7457 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007458 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007459 * We could trigger writeback for this range (and wait
7460 * for it to complete) and then invalidate the pages for
7461 * this range (through invalidate_inode_pages2_range()),
7462 * but that can lead us to a deadlock with a concurrent
7463 * call to readpages() (a buffered read or a defrag call
7464 * triggered a readahead) on a page lock due to an
7465 * ordered dio extent we created before but did not have
7466 * yet a corresponding bio submitted (whence it can not
7467 * complete), which makes readpages() wait for that
7468 * ordered extent to complete while holding a lock on
7469 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007470 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007471 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007472 }
7473
Filipe Mananaade77022016-02-18 14:28:55 +00007474 if (ret)
7475 break;
7476
Josef Bacikeb838e72012-07-31 16:28:48 -04007477 cond_resched();
7478 }
7479
7480 return ret;
7481}
7482
Liu Bo6f9994d2017-01-31 07:50:22 -08007483/* The callers of this must take lock_extent() */
7484static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7485 u64 orig_start, u64 block_start,
7486 u64 block_len, u64 orig_block_len,
7487 u64 ram_bytes, int compress_type,
7488 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007489{
7490 struct extent_map_tree *em_tree;
7491 struct extent_map *em;
7492 struct btrfs_root *root = BTRFS_I(inode)->root;
7493 int ret;
7494
Liu Bo6f9994d2017-01-31 07:50:22 -08007495 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7496 type == BTRFS_ORDERED_COMPRESSED ||
7497 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007498 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007499
Josef Bacik69ffb542012-09-11 15:40:07 -04007500 em_tree = &BTRFS_I(inode)->extent_tree;
7501 em = alloc_extent_map();
7502 if (!em)
7503 return ERR_PTR(-ENOMEM);
7504
7505 em->start = start;
7506 em->orig_start = orig_start;
7507 em->len = len;
7508 em->block_len = block_len;
7509 em->block_start = block_start;
7510 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007511 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007512 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007513 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007514 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007515 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007516 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007517 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007518 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7519 em->compress_type = compress_type;
7520 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007521
7522 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007523 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007524 em->start + em->len - 1, 0);
7525 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007526 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007527 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007528 /*
7529 * The caller has taken lock_extent(), who could race with us
7530 * to add em?
7531 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007532 } while (ret == -EEXIST);
7533
7534 if (ret) {
7535 free_extent_map(em);
7536 return ERR_PTR(ret);
7537 }
7538
Liu Bo6f9994d2017-01-31 07:50:22 -08007539 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007540 return em;
7541}
7542
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007543
7544static int btrfs_get_blocks_direct_read(struct extent_map *em,
7545 struct buffer_head *bh_result,
7546 struct inode *inode,
7547 u64 start, u64 len)
7548{
7549 if (em->block_start == EXTENT_MAP_HOLE ||
7550 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7551 return -ENOENT;
7552
7553 len = min(len, em->len - (start - em->start));
7554
7555 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7556 inode->i_blkbits;
7557 bh_result->b_size = len;
7558 bh_result->b_bdev = em->bdev;
7559 set_buffer_mapped(bh_result);
7560
7561 return 0;
7562}
7563
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007564static int btrfs_get_blocks_direct_write(struct extent_map **map,
7565 struct buffer_head *bh_result,
7566 struct inode *inode,
7567 struct btrfs_dio_data *dio_data,
7568 u64 start, u64 len)
7569{
7570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7571 struct extent_map *em = *map;
7572 int ret = 0;
7573
7574 /*
7575 * We don't allocate a new extent in the following cases
7576 *
7577 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7578 * existing extent.
7579 * 2) The extent is marked as PREALLOC. We're good to go here and can
7580 * just use the extent.
7581 *
7582 */
7583 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7584 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7585 em->block_start != EXTENT_MAP_HOLE)) {
7586 int type;
7587 u64 block_start, orig_start, orig_block_len, ram_bytes;
7588
7589 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7590 type = BTRFS_ORDERED_PREALLOC;
7591 else
7592 type = BTRFS_ORDERED_NOCOW;
7593 len = min(len, em->len - (start - em->start));
7594 block_start = em->block_start + (start - em->start);
7595
7596 if (can_nocow_extent(inode, start, &len, &orig_start,
7597 &orig_block_len, &ram_bytes) == 1 &&
7598 btrfs_inc_nocow_writers(fs_info, block_start)) {
7599 struct extent_map *em2;
7600
7601 em2 = btrfs_create_dio_extent(inode, start, len,
7602 orig_start, block_start,
7603 len, orig_block_len,
7604 ram_bytes, type);
7605 btrfs_dec_nocow_writers(fs_info, block_start);
7606 if (type == BTRFS_ORDERED_PREALLOC) {
7607 free_extent_map(em);
7608 *map = em = em2;
7609 }
7610
7611 if (em2 && IS_ERR(em2)) {
7612 ret = PTR_ERR(em2);
7613 goto out;
7614 }
7615 /*
7616 * For inode marked NODATACOW or extent marked PREALLOC,
7617 * use the existing or preallocated extent, so does not
7618 * need to adjust btrfs_space_info's bytes_may_use.
7619 */
7620 btrfs_free_reserved_data_space_noquota(inode, start,
7621 len);
7622 goto skip_cow;
7623 }
7624 }
7625
7626 /* this will cow the extent */
7627 len = bh_result->b_size;
7628 free_extent_map(em);
7629 *map = em = btrfs_new_extent_direct(inode, start, len);
7630 if (IS_ERR(em)) {
7631 ret = PTR_ERR(em);
7632 goto out;
7633 }
7634
7635 len = min(len, em->len - (start - em->start));
7636
7637skip_cow:
7638 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7639 inode->i_blkbits;
7640 bh_result->b_size = len;
7641 bh_result->b_bdev = em->bdev;
7642 set_buffer_mapped(bh_result);
7643
7644 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7645 set_buffer_new(bh_result);
7646
7647 /*
7648 * Need to update the i_size under the extent lock so buffered
7649 * readers will get the updated i_size when we unlock.
7650 */
7651 if (!dio_data->overwrite && start + len > i_size_read(inode))
7652 i_size_write(inode, start + len);
7653
7654 WARN_ON(dio_data->reserve < len);
7655 dio_data->reserve -= len;
7656 dio_data->unsubmitted_oe_range_end = start + len;
7657 current->journal_info = dio_data;
7658out:
7659 return ret;
7660}
7661
Josef Bacik4b46fce2010-05-23 11:00:55 -04007662static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7663 struct buffer_head *bh_result, int create)
7664{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007665 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007666 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007667 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307668 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007669 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007670 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007673 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007674
Miao Xie172a5042013-02-21 02:48:22 -07007675 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007676 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007677 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007678 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007679
Josef Bacikc3298612012-08-03 16:49:19 -04007680 lockstart = start;
7681 lockend = start + len - 1;
7682
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007683 if (current->journal_info) {
7684 /*
7685 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007686 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007687 * confused.
7688 */
chandan50745b02015-08-28 21:10:13 +05307689 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007690 current->journal_info = NULL;
7691 }
7692
Josef Bacikeb838e72012-07-31 16:28:48 -04007693 /*
7694 * If this errors out it's because we couldn't invalidate pagecache for
7695 * this range and we need to fallback to buffered.
7696 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007697 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7698 create)) {
7699 ret = -ENOTBLK;
7700 goto err;
7701 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007702
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007703 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007704 if (IS_ERR(em)) {
7705 ret = PTR_ERR(em);
7706 goto unlock_err;
7707 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708
7709 /*
7710 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7711 * io. INLINE is special, and we could probably kludge it in here, but
7712 * it's still buffered so for safety lets just fall back to the generic
7713 * buffered path.
7714 *
7715 * For COMPRESSED we _have_ to read the entire extent in so we can
7716 * decompress it, so there will be buffering required no matter what we
7717 * do, so go ahead and fallback to buffered.
7718 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007719 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720 * to buffered IO. Don't blame me, this is the price we pay for using
7721 * the generic code.
7722 */
7723 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7724 em->block_start == EXTENT_MAP_INLINE) {
7725 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007726 ret = -ENOTBLK;
7727 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 }
7729
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007730 if (create) {
7731 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7732 dio_data, start, len);
7733 if (ret < 0)
7734 goto unlock_err;
7735
7736 /* clear and unlock the entire range */
7737 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7738 unlock_bits, 1, 0, &cached_state);
7739 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007740 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7741 start, len);
7742 /* Can be negative only if we read from a hole */
7743 if (ret < 0) {
7744 ret = 0;
7745 free_extent_map(em);
7746 goto unlock_err;
7747 }
7748 /*
7749 * We need to unlock only the end area that we aren't using.
7750 * The rest is going to be unlocked by the endio routine.
7751 */
7752 lockstart = start + bh_result->b_size;
7753 if (lockstart < lockend) {
7754 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7755 lockend, unlock_bits, 1, 0,
7756 &cached_state);
7757 } else {
7758 free_extent_state(cached_state);
7759 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760 }
7761
Josef Bacik4b46fce2010-05-23 11:00:55 -04007762 free_extent_map(em);
7763
7764 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007765
7766unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007767 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007768 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007769err:
chandan50745b02015-08-28 21:10:13 +05307770 if (dio_data)
7771 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007772 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773}
7774
Omar Sandoval58efbc92017-08-22 23:45:59 -07007775static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7776 struct bio *bio,
7777 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007778{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007779 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007780 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007781
Mike Christie37226b22016-06-05 14:31:52 -05007782 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007783
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007784 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007785 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007786 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007788 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007789
Miao Xie8b110e32014-09-12 18:44:03 +08007790 return ret;
7791}
7792
7793static int btrfs_check_dio_repairable(struct inode *inode,
7794 struct bio *failed_bio,
7795 struct io_failure_record *failrec,
7796 int failed_mirror)
7797{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007799 int num_copies;
7800
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007801 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007802 if (num_copies == 1) {
7803 /*
7804 * we only have a single copy of the data, so don't bother with
7805 * all the retry and error correction code that follows. no
7806 * matter what the error is, it is very likely to persist.
7807 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007808 btrfs_debug(fs_info,
7809 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7810 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007811 return 0;
7812 }
7813
7814 failrec->failed_mirror = failed_mirror;
7815 failrec->this_mirror++;
7816 if (failrec->this_mirror == failed_mirror)
7817 failrec->this_mirror++;
7818
7819 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007820 btrfs_debug(fs_info,
7821 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7822 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007823 return 0;
7824 }
7825
7826 return 1;
7827}
7828
Omar Sandoval58efbc92017-08-22 23:45:59 -07007829static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7830 struct page *page, unsigned int pgoff,
7831 u64 start, u64 end, int failed_mirror,
7832 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007833{
7834 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007835 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7836 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007837 struct bio *bio;
7838 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007839 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007840 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007841 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007842 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007843 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007844
Mike Christie37226b22016-06-05 14:31:52 -05007845 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007846
7847 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7848 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007849 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007850
7851 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7852 failed_mirror);
7853 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007854 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007855 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007856 }
7857
Liu Bo17347ce2017-05-15 15:33:27 -07007858 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007859 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007860 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007861 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007862 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007863
7864 isector = start - btrfs_io_bio(failed_bio)->logical;
7865 isector >>= inode->i_sb->s_blocksize_bits;
7866 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307867 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007868 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007869
7870 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007871 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007872 read_mode, failrec->this_mirror, failrec->in_validation);
7873
Omar Sandoval58efbc92017-08-22 23:45:59 -07007874 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7875 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007876 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007877 bio_put(bio);
7878 }
7879
Omar Sandoval58efbc92017-08-22 23:45:59 -07007880 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007881}
7882
7883struct btrfs_retry_complete {
7884 struct completion done;
7885 struct inode *inode;
7886 u64 start;
7887 int uptodate;
7888};
7889
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007890static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007891{
7892 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007893 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007894 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007895 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007896 int i;
7897
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007898 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007899 goto end;
7900
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307901 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007902 io_tree = &BTRFS_I(inode)->io_tree;
7903 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007904 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307905
Miao Xie8b110e32014-09-12 18:44:03 +08007906 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007907 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007908 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007909 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7910 io_tree, done->start, bvec->bv_page,
7911 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007912end:
7913 complete(&done->done);
7914 bio_put(bio);
7915}
7916
Omar Sandoval58efbc92017-08-22 23:45:59 -07007917static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7918 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007919{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307920 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007921 struct bio_vec bvec;
7922 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007923 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307925 unsigned int pgoff;
7926 u32 sectorsize;
7927 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007928 blk_status_t ret;
7929 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007930
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007932 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307933
Miao Xiec1dc0892014-09-12 18:43:56 +08007934 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007935 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007936 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007937
Liu Bo17347ce2017-05-15 15:33:27 -07007938 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7939 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7940 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307941
7942next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007943 done.uptodate = 0;
7944 done.start = start;
7945 init_completion(&done.done);
7946
Liu Bo17347ce2017-05-15 15:33:27 -07007947 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307948 pgoff, start, start + sectorsize - 1,
7949 io_bio->mirror_num,
7950 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007951 if (ret) {
7952 err = ret;
7953 goto next;
7954 }
Miao Xie8b110e32014-09-12 18:44:03 +08007955
David Sterba9c17f6c2017-07-19 19:26:45 +02007956 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007957
7958 if (!done.uptodate) {
7959 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007961 }
7962
Liu Bo629ebf42017-05-15 17:20:07 -07007963next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 start += sectorsize;
7965
Liu Bo97bf5a52017-04-07 13:11:10 -07007966 nr_sectors--;
7967 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307968 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007969 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 goto next_block_or_try_again;
7971 }
Miao Xie8b110e32014-09-12 18:44:03 +08007972 }
7973
Liu Bo629ebf42017-05-15 17:20:07 -07007974 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007975}
7976
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007977static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007978{
7979 struct btrfs_retry_complete *done = bio->bi_private;
7980 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007981 struct extent_io_tree *io_tree, *failure_tree;
7982 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007983 struct bio_vec *bvec;
7984 int uptodate;
7985 int ret;
7986 int i;
7987
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007988 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007989 goto end;
7990
7991 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307993 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007994 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995
Josef Bacik7870d082017-05-05 11:57:15 -04007996 io_tree = &BTRFS_I(inode)->io_tree;
7997 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7998
David Sterbac09abff2017-07-13 18:10:07 +02007999 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008000 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008001 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8002 bvec->bv_offset, done->start,
8003 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008004 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008005 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8006 failure_tree, io_tree, done->start,
8007 bvec->bv_page,
8008 btrfs_ino(BTRFS_I(inode)),
8009 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008010 else
8011 uptodate = 0;
8012 }
8013
8014 done->uptodate = uptodate;
8015end:
8016 complete(&done->done);
8017 bio_put(bio);
8018}
8019
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008020static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8021 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008022{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008024 struct bio_vec bvec;
8025 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008026 struct btrfs_retry_complete done;
8027 u64 start;
8028 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029 u32 sectorsize;
8030 int nr_sectors;
8031 unsigned int pgoff;
8032 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008033 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008034 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008035 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008036
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008038 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308039
Omar Sandoval58efbc92017-08-22 23:45:59 -07008040 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008041 start = io_bio->logical;
8042 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008043 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008044
Liu Bo17347ce2017-05-15 15:33:27 -07008045 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8046 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047
Liu Bo17347ce2017-05-15 15:33:27 -07008048 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008050 if (uptodate) {
8051 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8052 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8053 bvec.bv_page, pgoff, start, sectorsize);
8054 if (likely(!ret))
8055 goto next;
8056 }
Miao Xie8b110e32014-09-12 18:44:03 +08008057try_again:
8058 done.uptodate = 0;
8059 done.start = start;
8060 init_completion(&done.done);
8061
Omar Sandoval58efbc92017-08-22 23:45:59 -07008062 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8063 pgoff, start, start + sectorsize - 1,
8064 io_bio->mirror_num, btrfs_retry_endio,
8065 &done);
8066 if (status) {
8067 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008068 goto next;
8069 }
8070
David Sterba9c17f6c2017-07-19 19:26:45 +02008071 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008072
8073 if (!done.uptodate) {
8074 /* We might have another mirror, so try again */
8075 goto try_again;
8076 }
8077next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 offset += sectorsize;
8079 start += sectorsize;
8080
8081 ASSERT(nr_sectors);
8082
Liu Bo97bf5a52017-04-07 13:11:10 -07008083 nr_sectors--;
8084 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308085 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008086 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308087 goto next_block;
8088 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008089 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008090
8091 return err;
8092}
8093
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008094static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8095 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008096{
8097 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8098
8099 if (skip_csum) {
8100 if (unlikely(err))
8101 return __btrfs_correct_data_nocsum(inode, io_bio);
8102 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008103 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008104 } else {
8105 return __btrfs_subio_endio_read(inode, io_bio, err);
8106 }
8107}
8108
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008109static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008110{
8111 struct btrfs_dio_private *dip = bio->bi_private;
8112 struct inode *inode = dip->inode;
8113 struct bio *dio_bio;
8114 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008115 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008116
Liu Bo99c4e3b92017-09-15 15:06:51 -06008117 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008118 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008119
Josef Bacik4b46fce2010-05-23 11:00:55 -04008120 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008121 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008122 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008123
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008125
Liu Bo99c4e3b92017-09-15 15:06:51 -06008126 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008127 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008128
8129 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008130 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008131 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008132}
8133
Qu Wenruo524272602017-03-08 10:25:52 +08008134static void __endio_write_update_ordered(struct inode *inode,
8135 const u64 offset, const u64 bytes,
8136 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008137{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008138 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008139 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008140 struct btrfs_workqueue *wq;
8141 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008142 u64 ordered_offset = offset;
8143 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008144 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008145
Qu Wenruo524272602017-03-08 10:25:52 +08008146 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8147 wq = fs_info->endio_freespace_worker;
8148 func = btrfs_freespace_write_helper;
8149 } else {
8150 wq = fs_info->endio_write_workers;
8151 func = btrfs_endio_write_helper;
8152 }
8153
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008154 while (ordered_offset < offset + bytes) {
8155 last_offset = ordered_offset;
8156 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8157 &ordered_offset,
8158 ordered_bytes,
8159 uptodate)) {
8160 btrfs_init_work(&ordered->work, func,
8161 finish_ordered_fn,
8162 NULL, NULL);
8163 btrfs_queue_work(wq, &ordered->work);
8164 }
8165 /*
8166 * If btrfs_dec_test_ordered_pending does not find any ordered
8167 * extent in the range, we can exit.
8168 */
8169 if (ordered_offset == last_offset)
8170 return;
8171 /*
8172 * Our bio might span multiple ordered extents. In this case
8173 * we keep goin until we have accounted the whole dio.
8174 */
8175 if (ordered_offset < offset + bytes) {
8176 ordered_bytes = offset + bytes - ordered_offset;
8177 ordered = NULL;
8178 }
Chris Mason163cf092010-11-28 19:56:33 -05008179 }
Filipe Manana14543772015-11-24 16:23:54 +00008180}
8181
8182static void btrfs_endio_direct_write(struct bio *bio)
8183{
8184 struct btrfs_dio_private *dip = bio->bi_private;
8185 struct bio *dio_bio = dip->dio_bio;
8186
Qu Wenruo524272602017-03-08 10:25:52 +08008187 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008188 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008189
Josef Bacik4b46fce2010-05-23 11:00:55 -04008190 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008191
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008192 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008193 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008194 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195}
8196
David Sterbad0ee3932018-03-08 14:35:48 +01008197static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008198 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008199{
Josef Bacikc6100a42017-05-05 11:57:13 -04008200 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008201 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008202 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008203 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008204 return 0;
8205}
8206
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008207static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008208{
8209 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008210 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008211
Miao Xie8b110e32014-09-12 18:44:03 +08008212 if (err)
8213 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008214 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008215 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8216 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008217 (unsigned long long)bio->bi_iter.bi_sector,
8218 bio->bi_iter.bi_size, err);
8219
8220 if (dip->subio_endio)
8221 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008222
8223 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008224 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008225 * We want to perceive the errors flag being set before
8226 * decrementing the reference count. We don't need a barrier
8227 * since atomic operations with a return value are fully
8228 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008229 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008230 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008231 }
8232
8233 /* if there are more bios still pending for this dio, just exit */
8234 if (!atomic_dec_and_test(&dip->pending_bios))
8235 goto out;
8236
Chris Mason9be33952013-05-17 18:30:14 -04008237 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008238 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008239 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008240 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008241 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008242 }
8243out:
8244 bio_put(bio);
8245}
8246
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008247static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008248 struct btrfs_dio_private *dip,
8249 struct bio *bio,
8250 u64 file_offset)
8251{
8252 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8253 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008254 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008255
8256 /*
8257 * We load all the csum data we need when we submit
8258 * the first bio to reduce the csum tree search and
8259 * contention.
8260 */
8261 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008262 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008263 file_offset);
8264 if (ret)
8265 return ret;
8266 }
8267
8268 if (bio == dip->orig_bio)
8269 return 0;
8270
8271 file_offset -= dip->logical_offset;
8272 file_offset >>= inode->i_sb->s_blocksize_bits;
8273 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8274
8275 return 0;
8276}
8277
David Sterbad0ee3932018-03-08 14:35:48 +01008278static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8279 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008282 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008283 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008284 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008285
Liu Bo4c274bc2017-11-01 17:19:27 -06008286 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008287 if (async_submit)
8288 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8289
Josef Bacik5fd02042012-05-02 14:00:54 -04008290 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008291 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008292 if (ret)
8293 goto err;
8294 }
Miao Xiee65e1532010-11-22 03:04:43 +00008295
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008296 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008297 goto map;
8298
8299 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008300 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8301 file_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01008302 btrfs_submit_bio_start_direct_io,
8303 btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008304 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008305 } else if (write) {
8306 /*
8307 * If we aren't doing async submit, calculate the csum of the
8308 * bio now.
8309 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008310 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008311 if (ret)
8312 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008313 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008314 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008315 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008316 if (ret)
8317 goto err;
8318 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008319map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008320 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008321err:
Miao Xiee65e1532010-11-22 03:04:43 +00008322 return ret;
8323}
8324
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008325static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008326{
8327 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008329 struct bio *bio;
8330 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008331 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008332 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008333 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008334 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008335 u64 submit_len;
8336 int clone_offset = 0;
8337 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308338 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008339 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008340
Kent Overstreet4f024f32013-10-11 15:44:27 -07008341 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008342 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008343 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8344 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008345 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008346 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008347
Liu Bo725130b2017-05-16 09:51:39 -07008348 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008349 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008350 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008351 goto submit;
8352 }
8353
David Woodhouse53b381b2013-01-29 18:40:14 -05008354 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008355 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008356 async_submit = 0;
8357 else
8358 async_submit = 1;
8359
Liu Bo725130b2017-05-16 09:51:39 -07008360 /* bio split */
8361 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008362 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008363 do {
Liu Bo725130b2017-05-16 09:51:39 -07008364 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008365
Liu Bo725130b2017-05-16 09:51:39 -07008366 /*
8367 * This will never fail as it's passing GPF_NOFS and
8368 * the allocation is backed by btrfs_bioset.
8369 */
Liu Boe4770942017-05-16 10:57:14 -07008370 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008371 clone_len);
8372 bio->bi_private = dip;
8373 bio->bi_end_io = btrfs_end_dio_bio;
8374 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008375
Liu Bo725130b2017-05-16 09:51:39 -07008376 ASSERT(submit_len >= clone_len);
8377 submit_len -= clone_len;
8378 if (submit_len == 0)
8379 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008380
Liu Bo725130b2017-05-16 09:51:39 -07008381 /*
8382 * Increase the count before we submit the bio so we know
8383 * the end IO handler won't happen before we increase the
8384 * count. Otherwise, the dip might get freed before we're
8385 * done setting it up.
8386 */
8387 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008388
David Sterbad0ee3932018-03-08 14:35:48 +01008389 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008390 async_submit);
8391 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008392 bio_put(bio);
8393 atomic_dec(&dip->pending_bios);
8394 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008395 }
Liu Bo725130b2017-05-16 09:51:39 -07008396
8397 clone_offset += clone_len;
8398 start_sector += clone_len >> 9;
8399 file_offset += clone_len;
8400
8401 map_length = submit_len;
8402 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8403 start_sector << 9, &map_length, NULL, 0);
8404 if (ret)
8405 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008406 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008407
Josef Bacik02f57c72011-04-06 14:25:44 -04008408submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008409 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008410 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008411 return 0;
8412
8413 bio_put(bio);
8414out_err:
8415 dip->errors = 1;
8416 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008417 * Before atomic variable goto zero, we must make sure dip->errors is
8418 * perceived to be set. This ordering is ensured by the fact that an
8419 * atomic operations with a return value are fully ordered as per
8420 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008421 */
Miao Xiee65e1532010-11-22 03:04:43 +00008422 if (atomic_dec_and_test(&dip->pending_bios))
8423 bio_io_error(dip->orig_bio);
8424
8425 /* bio_end_io() will handle error, so we needn't return it */
8426 return 0;
8427}
8428
Mike Christie8a4c1e42016-06-05 14:31:50 -05008429static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8430 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008431{
Filipe Manana61de7182015-07-01 12:13:10 +01008432 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008433 struct bio *bio = NULL;
8434 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008435 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008436 int ret = 0;
8437
David Sterba8b6c1d52017-06-02 17:48:13 +02008438 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008439
Miao Xiec1dc0892014-09-12 18:43:56 +08008440 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008441 if (!dip) {
8442 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008443 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008444 }
8445
8446 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008447 dip->inode = inode;
8448 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008449 dip->bytes = dio_bio->bi_iter.bi_size;
8450 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008451 bio->bi_private = dip;
8452 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008453 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008454 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008455 io_bio = btrfs_io_bio(bio);
8456 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008457
Miao Xiec1dc0892014-09-12 18:43:56 +08008458 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008459 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008460 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008461 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008462 dip->subio_endio = btrfs_subio_endio_read;
8463 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008464
Filipe Mananaf28a4922015-12-08 19:23:20 +00008465 /*
8466 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8467 * even if we fail to submit a bio, because in such case we do the
8468 * corresponding error handling below and it must not be done a second
8469 * time by btrfs_direct_IO().
8470 */
8471 if (write) {
8472 struct btrfs_dio_data *dio_data = current->journal_info;
8473
8474 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8475 dip->bytes;
8476 dio_data->unsubmitted_oe_range_start =
8477 dio_data->unsubmitted_oe_range_end;
8478 }
8479
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008480 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008481 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008482 return;
Chris Mason9be33952013-05-17 18:30:14 -04008483
Liu Bo3892ac92017-04-17 15:00:28 -07008484 if (io_bio->end_io)
8485 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008486
Josef Bacik4b46fce2010-05-23 11:00:55 -04008487free_ordered:
8488 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008489 * If we arrived here it means either we failed to submit the dip
8490 * or we either failed to clone the dio_bio or failed to allocate the
8491 * dip. If we cloned the dio_bio and allocated the dip, we can just
8492 * call bio_endio against our io_bio so that we get proper resource
8493 * cleanup if we fail to submit the dip, otherwise, we must do the
8494 * same as btrfs_endio_direct_[write|read] because we can't call these
8495 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008496 */
Liu Bo3892ac92017-04-17 15:00:28 -07008497 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008498 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008499 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008500 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008501 * and all the cleanup and final put for dio_bio (through
8502 * dio_end_io()).
8503 */
8504 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008505 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008506 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008507 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008508 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008509 file_offset,
8510 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008511 false);
Filipe Manana14543772015-11-24 16:23:54 +00008512 else
Filipe Manana61de7182015-07-01 12:13:10 +01008513 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8514 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008515
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008516 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008517 /*
8518 * Releases and cleans up our dio_bio, no need to bio_put()
8519 * nor bio_endio()/bio_io_error() against dio_bio.
8520 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008521 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522 }
Liu Bo3892ac92017-04-17 15:00:28 -07008523 if (bio)
8524 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008525 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008526}
8527
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008528static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008529 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008530{
8531 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008532 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008533 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008534 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008535
8536 if (offset & blocksize_mask)
8537 goto out;
8538
Al Viro28060d52014-03-22 05:15:17 -04008539 if (iov_iter_alignment(iter) & blocksize_mask)
8540 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008541
Al Viro28060d52014-03-22 05:15:17 -04008542 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008543 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008544 return 0;
8545 /*
8546 * Check to make sure we don't have duplicate iov_base's in this
8547 * iovec, if so return EINVAL, otherwise we'll get csum errors
8548 * when reading back.
8549 */
8550 for (seg = 0; seg < iter->nr_segs; seg++) {
8551 for (i = seg + 1; i < iter->nr_segs; i++) {
8552 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008553 goto out;
8554 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008555 }
8556 retval = 0;
8557out:
8558 return retval;
8559}
Josef Bacikeb838e72012-07-31 16:28:48 -04008560
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008561static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008562{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008563 struct file *file = iocb->ki_filp;
8564 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308566 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008567 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008568 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008569 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008570 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008571 bool wakeup = true;
8572 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008573 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008574
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008575 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008576 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008577
Jens Axboefe0f07d2015-04-15 17:05:48 -06008578 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008579
Josef Bacik0e267c42013-07-02 10:38:02 -04008580 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008581 * The generic stuff only does filemap_write_and_wait_range, which
8582 * isn't enough if we've written compressed pages to this area, so
8583 * we need to flush the dirty pages again to make absolutely sure
8584 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008585 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008586 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008587 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8588 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008589 filemap_fdatawrite_range(inode->i_mapping, offset,
8590 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008591
Omar Sandoval6f673762015-03-16 04:33:52 -07008592 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008593 /*
8594 * If the write DIO is beyond the EOF, we need update
8595 * the isize, but it is protected by i_mutex. So we can
8596 * not unlock the i_mutex at this case.
8597 */
8598 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008599 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008600 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008601 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008602 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8603 ret = -EAGAIN;
8604 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008605 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008606 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8607 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008608 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008609 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008610
8611 /*
8612 * We need to know how many extents we reserved so that we can
8613 * do the accounting properly if we go over the number we
8614 * originally calculated. Abuse current->journal_info for this.
8615 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008616 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008617 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008618 dio_data.unsubmitted_oe_range_start = (u64)offset;
8619 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308620 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308621 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008622 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8623 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008624 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008625 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8626 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008627 }
8628
Omar Sandoval17f8c842015-03-16 04:33:50 -07008629 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008630 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008631 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008632 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008633 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308634 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008635 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008636 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308637 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008638 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008639 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008640 /*
8641 * On error we might have left some ordered extents
8642 * without submitting corresponding bios for them, so
8643 * cleanup them up to avoid other tasks getting them
8644 * and waiting for them to complete forever.
8645 */
8646 if (dio_data.unsubmitted_oe_range_start <
8647 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008648 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008649 dio_data.unsubmitted_oe_range_start,
8650 dio_data.unsubmitted_oe_range_end -
8651 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008652 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008653 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008654 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008655 offset, count - (size_t)ret, true);
8656 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008657 }
Miao Xie38851cc2013-02-08 07:04:11 +00008658out:
Miao Xie2e60a512013-02-08 07:01:08 +00008659 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008660 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008661 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008662 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008663
Qu Wenruo364ecf32017-02-27 15:10:38 +08008664 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008665 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008666}
8667
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008668#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8669
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008670static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8671 __u64 start, __u64 len)
8672{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008673 int ret;
8674
8675 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8676 if (ret)
8677 return ret;
8678
David Sterba2135fb92017-06-23 04:09:57 +02008679 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008680}
8681
Chris Mason9ebefb182007-06-15 13:50:00 -04008682int btrfs_readpage(struct file *file, struct page *page)
8683{
Chris Masond1310b22008-01-24 16:13:08 -05008684 struct extent_io_tree *tree;
8685 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008686 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008687}
Chris Mason1832a6d2007-12-21 16:27:21 -05008688
Chris Mason39279cc2007-06-12 06:35:45 -04008689static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8690{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008691 struct inode *inode = page->mapping->host;
8692 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008693
8694 if (current->flags & PF_MEMALLOC) {
8695 redirty_page_for_writepage(wbc, page);
8696 unlock_page(page);
8697 return 0;
8698 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008699
8700 /*
8701 * If we are under memory pressure we will call this directly from the
8702 * VM, we need to make sure we have the inode referenced for the ordered
8703 * extent. If not just return like we didn't do anything.
8704 */
8705 if (!igrab(inode)) {
8706 redirty_page_for_writepage(wbc, page);
8707 return AOP_WRITEPAGE_ACTIVATE;
8708 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008709 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008710 btrfs_add_delayed_iput(inode);
8711 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008712}
Chris Mason39279cc2007-06-12 06:35:45 -04008713
Eric Sandeen48a3b632013-04-25 20:41:01 +00008714static int btrfs_writepages(struct address_space *mapping,
8715 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008716{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008717 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008718}
8719
Chris Mason3ab2fb52007-11-08 10:59:22 -05008720static int
8721btrfs_readpages(struct file *file, struct address_space *mapping,
8722 struct list_head *pages, unsigned nr_pages)
8723{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008724 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008725}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008726
Chris Masone6dcd2d2008-07-17 12:53:50 -04008727static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008728{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008729 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008730 if (ret == 1) {
8731 ClearPagePrivate(page);
8732 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008733 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008734 }
8735 return ret;
8736}
Chris Mason39279cc2007-06-12 06:35:45 -04008737
Chris Masone6dcd2d2008-07-17 12:53:50 -04008738static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8739{
Chris Mason98509cf2008-09-11 15:51:43 -04008740 if (PageWriteback(page) || PageDirty(page))
8741 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008742 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008743}
8744
Lukas Czernerd47992f2013-05-21 23:17:23 -04008745static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8746 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008747{
Josef Bacik5fd02042012-05-02 14:00:54 -04008748 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008749 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008750 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008751 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008752 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008753 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308754 u64 start;
8755 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008756 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008757
Chris Mason8b62b722009-09-02 16:53:46 -04008758 /*
8759 * we have the page locked, so new writeback can't start,
8760 * and the dirty bit won't be cleared while we are here.
8761 *
8762 * Wait for IO on this page so that we can safely clear
8763 * the PagePrivate2 bit and do ordered accounting
8764 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008765 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008766
Josef Bacik5fd02042012-05-02 14:00:54 -04008767 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008768 if (offset) {
8769 btrfs_releasepage(page, GFP_NOFS);
8770 return;
8771 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008772
8773 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008774 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308775again:
8776 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008777 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308778 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008779 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308780 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008781 /*
8782 * IO on this page will never be started, so we need
8783 * to account for any ordered extents now
8784 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008785 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308786 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008787 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008788 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008789 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008790 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008791 /*
8792 * whoever cleared the private bit is responsible
8793 * for the finish_ordered_io
8794 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008795 if (TestClearPagePrivate2(page)) {
8796 struct btrfs_ordered_inode_tree *tree;
8797 u64 new_len;
8798
8799 tree = &BTRFS_I(inode)->ordered_tree;
8800
8801 spin_lock_irq(&tree->lock);
8802 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308803 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008804 if (new_len < ordered->truncated_len)
8805 ordered->truncated_len = new_len;
8806 spin_unlock_irq(&tree->lock);
8807
8808 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308809 start,
8810 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008811 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008812 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008814 if (!inode_evicting) {
8815 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308816 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008817 &cached_state);
8818 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308819
8820 start = end + 1;
8821 if (start < page_end)
8822 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008824
Qu Wenruob9d0b382015-09-29 10:35:16 +08008825 /*
8826 * Qgroup reserved space handler
8827 * Page here will be either
8828 * 1) Already written to disk
8829 * In this case, its reserved space is released from data rsv map
8830 * and will be freed by delayed_ref handler finally.
8831 * So even we call qgroup_free_data(), it won't decrease reserved
8832 * space.
8833 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008834 * This means the reserved space should be freed here. However,
8835 * if a truncate invalidates the page (by clearing PageDirty)
8836 * and the page is accounted for while allocating extent
8837 * in btrfs_check_data_free_space() we let delayed_ref to
8838 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008839 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008840 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008841 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008842 if (!inode_evicting) {
8843 clear_extent_bit(tree, page_start, page_end,
8844 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008845 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8846 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008847 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008848
8849 __btrfs_releasepage(page, GFP_NOFS);
8850 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008851
Chris Mason4a096752008-07-21 10:29:44 -04008852 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008853 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008854 ClearPagePrivate(page);
8855 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008856 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008857 }
Chris Mason39279cc2007-06-12 06:35:45 -04008858}
8859
Chris Mason9ebefb182007-06-15 13:50:00 -04008860/*
8861 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8862 * called from a page fault handler when a page is first dirtied. Hence we must
8863 * be careful to check for EOF conditions here. We set the page up correctly
8864 * for a written page which means we get ENOSPC checking when writing into
8865 * holes and correct delalloc and unwritten extent mapping on filesystems that
8866 * support these features.
8867 *
8868 * We are not allowed to take the i_mutex here so we have to play games to
8869 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008870 * truncate_setsize() writes the inode size before removing pages, once we have
8871 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008872 * beyond EOF, then the page is guaranteed safe against truncation until we
8873 * unlock the page.
8874 */
Souptick Joardera528a242018-06-06 19:54:44 +05308875vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008876{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008877 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008878 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008880 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8881 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008882 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008883 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008884 char *kaddr;
8885 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008886 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308887 vm_fault_t ret;
8888 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008889 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308890 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008891 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008892 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308893 u64 end;
8894
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008895 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008896
Jan Karab2b5ef52012-06-12 16:20:45 +02008897 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008898 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008899 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308900 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008901
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308902 /*
8903 * Reserving delalloc space after obtaining the page lock can lead to
8904 * deadlock. For example, if a dirty page is locked by this function
8905 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8906 * dirty page write out, then the btrfs_writepage() function could
8907 * end up waiting indefinitely to get a lock on the page currently
8908 * being processed by btrfs_page_mkwrite() function.
8909 */
Souptick Joardera528a242018-06-06 19:54:44 +05308910 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308911 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308912 if (!ret2) {
8913 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008914 reserved = 1;
8915 }
Souptick Joardera528a242018-06-06 19:54:44 +05308916 if (ret2) {
8917 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008918 if (reserved)
8919 goto out;
8920 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008921 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008922
Nick Piggin56a76f82009-03-31 15:23:23 -07008923 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008924again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008925 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008926 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008927
Chris Mason9ebefb182007-06-15 13:50:00 -04008928 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008929 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008930 /* page got truncated out from underneath us */
8931 goto out_unlock;
8932 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008933 wait_on_page_writeback(page);
8934
David Sterbaff13db42015-12-03 14:30:40 +01008935 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008936 set_page_extent_mapped(page);
8937
Chris Masoneb84ae02008-07-17 13:53:27 -04008938 /*
8939 * we can't set the delalloc bits if there are pending ordered
8940 * extents. Drop our locks and wait for them to finish
8941 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008942 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8943 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008944 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008945 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008946 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008948 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008949 btrfs_put_ordered_extent(ordered);
8950 goto again;
8951 }
8952
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008953 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008954 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008955 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008956 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308957 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008958 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008959 page_start, PAGE_SIZE - reserved_space,
8960 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308961 }
8962 }
8963
Josef Bacikfbf19082009-10-01 17:10:23 -04008964 /*
Liu Bo54160342016-12-13 12:15:19 -08008965 * page_mkwrite gets called when the page is firstly dirtied after it's
8966 * faulted in, but write(2) could also dirty a page and set delalloc
8967 * bits, thus in this case for space account reason, we still need to
8968 * clear any delalloc bits within this page range since we have to
8969 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008970 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308971 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008972 EXTENT_DIRTY | EXTENT_DELALLOC |
8973 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008974 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008975
Souptick Joardera528a242018-06-06 19:54:44 +05308976 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008977 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308978 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008979 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008980 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008981 ret = VM_FAULT_SIGBUS;
8982 goto out_unlock;
8983 }
Souptick Joardera528a242018-06-06 19:54:44 +05308984 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008985
8986 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008987 if (page_start + PAGE_SIZE > size)
8988 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008989 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008990 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008991
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008992 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008993 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008994 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995 flush_dcache_page(page);
8996 kunmap(page);
8997 }
Chris Mason247e7432008-07-17 12:53:51 -04008998 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008999 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009000 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009001
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009002 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009003 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009004 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009005
David Sterbae43bbe52017-12-12 21:43:52 +01009006 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009007
Souptick Joardera528a242018-06-06 19:54:44 +05309008 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009009 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009010 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009011 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009012 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009013 }
Chris Mason717beb92018-06-25 10:03:41 -07009014
9015out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009016 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009017out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009018 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009019 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009020 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009021out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009022 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009023 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009024 return ret;
9025}
9026
Filipe Manana213e8c52018-02-06 20:40:31 +00009027static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009029 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009030 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009031 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009032 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009033 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009034 u64 mask = fs_info->sectorsize - 1;
9035 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009036
Filipe Manana213e8c52018-02-06 20:40:31 +00009037 if (!skip_writeback) {
9038 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9039 (u64)-1);
9040 if (ret)
9041 return ret;
9042 }
Chris Mason39279cc2007-06-12 06:35:45 -04009043
Josef Bacikfcb80c22011-05-03 10:40:22 -04009044 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009045 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9046 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009047 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009048 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009049 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009050 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009051 * be free'd up by the truncate operation, but also have some slack
9052 * space reserved in case it uses space during the truncate (thank you
9053 * very much snapshotting).
9054 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009055 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009056 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009057 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009058 * doesn't end up using space reserved for updating the inode. We also
9059 * need to be able to stop the transaction and start a new one, which
9060 * means we need to be able to update the inode several times, and we
9061 * have no idea of knowing how many times that will be, so we can't just
9062 * reserve 1 item for the entirety of the operation, so that has to be
9063 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 *
9065 * So that leaves us with
9066 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009067 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009068 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009069 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009070 * updating the inode.
9071 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009072 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009073 if (!rsv)
9074 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009075 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009076 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009077
Josef Bacik907cbce2011-08-08 13:46:15 -04009078 /*
Josef Bacik07127182011-08-19 10:29:59 -04009079 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009080 * 1 for updating the inode.
9081 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009082 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009083 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009084 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009085 goto out;
9086 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009087
Josef Bacik907cbce2011-08-08 13:46:15 -04009088 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009089 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009090 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009091 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009092
Chris Mason5a3f23d2009-03-31 13:27:11 -04009093 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009094 * So if we truncate and then write and fsync we normally would just
9095 * write the extents that changed, which is a problem if we need to
9096 * first truncate that entire inode. So set this flag so we write out
9097 * all of the extents in the inode to the sync log so we're completely
9098 * safe.
9099 */
9100 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009101 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009102
Yan, Zheng80825102009-11-12 09:35:36 +00009103 while (1) {
9104 ret = btrfs_truncate_inode_items(trans, root, inode,
9105 inode->i_size,
9106 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009107 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009108 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009109 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009110
Yan, Zheng80825102009-11-12 09:35:36 +00009111 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009112 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009113 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009114
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009115 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009116 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009117
9118 trans = btrfs_start_transaction(root, 2);
9119 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009120 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009121 trans = NULL;
9122 break;
9123 }
9124
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009125 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009126 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009127 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009128 BUG_ON(ret); /* shouldn't happen */
9129 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009130 }
9131
Josef Bacikddfae632017-10-19 14:16:02 -04009132 /*
9133 * We can't call btrfs_truncate_block inside a trans handle as we could
9134 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9135 * we've truncated everything except the last little bit, and can do
9136 * btrfs_truncate_block and then update the disk_i_size.
9137 */
9138 if (ret == NEED_TRUNCATE_BLOCK) {
9139 btrfs_end_transaction(trans);
9140 btrfs_btree_balance_dirty(fs_info);
9141
9142 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9143 if (ret)
9144 goto out;
9145 trans = btrfs_start_transaction(root, 1);
9146 if (IS_ERR(trans)) {
9147 ret = PTR_ERR(trans);
9148 goto out;
9149 }
9150 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9151 }
9152
Chris Mason917c16b2011-11-08 14:49:59 -05009153 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009154 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009155
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009156 trans->block_rsv = &fs_info->trans_block_rsv;
9157 ret2 = btrfs_update_inode(trans, root, inode);
9158 if (ret2 && !ret)
9159 ret = ret2;
9160
9161 ret2 = btrfs_end_transaction(trans);
9162 if (ret2 && !ret)
9163 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009164 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009165 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009166out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009167 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009168
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009169 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009170}
9171
Sven Wegener3b963622008-06-09 21:57:42 -04009172/*
Chris Masond352ac62008-09-29 15:18:18 -04009173 * create a new subvolume directory/inode (helper for the ioctl).
9174 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009175int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009176 struct btrfs_root *new_root,
9177 struct btrfs_root *parent_root,
9178 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009179{
Chris Mason39279cc2007-06-12 06:35:45 -04009180 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009181 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009182 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009183
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009184 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9185 new_dirid, new_dirid,
9186 S_IFDIR | (~current_umask() & S_IRWXUGO),
9187 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009188 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009189 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009190 inode->i_op = &btrfs_dir_inode_operations;
9191 inode->i_fop = &btrfs_dir_file_operations;
9192
Miklos Szeredibfe86842011-10-28 14:13:29 +02009193 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009194 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009195 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009196
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009197 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9198 if (err)
9199 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009200 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009201 new_root->root_key.objectid, err);
9202
Yan, Zheng76dda932009-09-21 16:00:26 -04009203 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009204
Yan, Zheng76dda932009-09-21 16:00:26 -04009205 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009206 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009207}
9208
Chris Mason39279cc2007-06-12 06:35:45 -04009209struct inode *btrfs_alloc_inode(struct super_block *sb)
9210{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009211 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009212 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009213 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009214
David Sterba712e36c2017-10-31 17:08:27 +01009215 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009216 if (!ei)
9217 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009218
9219 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009220 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009221 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009222 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009223 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009224 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009225 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009226 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009227 ei->disk_i_size = 0;
9228 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009229 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009230 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009231 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009232 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009233 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009234
Josef Bacik9e0baf62011-07-15 15:16:44 +00009235 spin_lock_init(&ei->lock);
9236 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009237 if (sb->s_magic != BTRFS_TEST_MAGIC)
9238 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9239 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009240 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009241 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009242 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009243
Miao Xie16cdcec2011-04-22 18:12:22 +08009244 ei->delayed_node = NULL;
9245
chandan r9cc97d62012-07-04 12:48:07 +05309246 ei->i_otime.tv_sec = 0;
9247 ei->i_otime.tv_nsec = 0;
9248
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009249 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009250 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009251 extent_io_tree_init(&ei->io_tree, inode);
9252 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009253 ei->io_tree.track_uptodate = 1;
9254 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009255 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009256 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009257 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009258 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009259 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009260 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009261 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009262 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009263
9264 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009265}
9266
Josef Bacikaaedb552013-10-11 14:44:09 -04009267#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9268void btrfs_test_destroy_inode(struct inode *inode)
9269{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009270 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009271 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9272}
9273#endif
9274
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009275static void btrfs_i_callback(struct rcu_head *head)
9276{
9277 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009278 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9279}
9280
Chris Mason39279cc2007-06-12 06:35:45 -04009281void btrfs_destroy_inode(struct inode *inode)
9282{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009284 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009285 struct btrfs_root *root = BTRFS_I(inode)->root;
9286
Al Virob3d9b7a2012-06-09 13:51:19 -04009287 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009288 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009289 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9290 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009291 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009292 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009293 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009294 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009295 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009296
Chris Mason5a3f23d2009-03-31 13:27:11 -04009297 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009298 * This can happen where we create an inode, but somebody else also
9299 * created the same inode and we need to destroy the one we already
9300 * created.
9301 */
9302 if (!root)
9303 goto free;
9304
Chris Masond3977122009-01-05 21:25:51 -05009305 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009306 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9307 if (!ordered)
9308 break;
9309 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009310 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009311 "found ordered extent %llu %llu on inode cleanup",
9312 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009313 btrfs_remove_ordered_extent(inode, ordered);
9314 btrfs_put_ordered_extent(ordered);
9315 btrfs_put_ordered_extent(ordered);
9316 }
9317 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009318 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009319 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009320 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009321free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009322 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009323}
9324
Al Viro45321ac2010-06-07 13:43:19 -04009325int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009326{
9327 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009328
Naohiro Aota6379ef92013-06-06 09:56:34 +00009329 if (root == NULL)
9330 return 1;
9331
Liu Bofa6ac872013-02-20 14:10:23 +00009332 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009333 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009334 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009335 else
Al Viro45321ac2010-06-07 13:43:19 -04009336 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009337}
9338
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009339static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009340{
9341 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9342
9343 inode_init_once(&ei->vfs_inode);
9344}
9345
David Sterbae67c7182018-02-19 17:24:18 +01009346void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009347{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009348 /*
9349 * Make sure all delayed rcu free inodes are flushed before we
9350 * destroy cache.
9351 */
9352 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009353 kmem_cache_destroy(btrfs_inode_cachep);
9354 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009355 kmem_cache_destroy(btrfs_path_cachep);
9356 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009357}
9358
Liu Bof5c29bd2017-11-02 17:21:50 -06009359int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009360{
David Sterba837e1972012-09-07 03:00:48 -06009361 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009362 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009363 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9364 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009365 if (!btrfs_inode_cachep)
9366 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009367
David Sterba837e1972012-09-07 03:00:48 -06009368 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009369 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009370 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009371 if (!btrfs_trans_handle_cachep)
9372 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009373
David Sterba837e1972012-09-07 03:00:48 -06009374 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009375 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009376 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009377 if (!btrfs_path_cachep)
9378 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009379
David Sterba837e1972012-09-07 03:00:48 -06009380 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009381 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009382 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009383 if (!btrfs_free_space_cachep)
9384 goto fail;
9385
Chris Mason39279cc2007-06-12 06:35:45 -04009386 return 0;
9387fail:
9388 btrfs_destroy_cachep();
9389 return -ENOMEM;
9390}
9391
David Howellsa528d352017-01-31 16:46:22 +00009392static int btrfs_getattr(const struct path *path, struct kstat *stat,
9393 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009394{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009395 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009396 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009397 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009398 u32 bi_flags = BTRFS_I(inode)->flags;
9399
9400 stat->result_mask |= STATX_BTIME;
9401 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9402 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9403 if (bi_flags & BTRFS_INODE_APPEND)
9404 stat->attributes |= STATX_ATTR_APPEND;
9405 if (bi_flags & BTRFS_INODE_COMPRESS)
9406 stat->attributes |= STATX_ATTR_COMPRESSED;
9407 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9408 stat->attributes |= STATX_ATTR_IMMUTABLE;
9409 if (bi_flags & BTRFS_INODE_NODUMP)
9410 stat->attributes |= STATX_ATTR_NODUMP;
9411
9412 stat->attributes_mask |= (STATX_ATTR_APPEND |
9413 STATX_ATTR_COMPRESSED |
9414 STATX_ATTR_IMMUTABLE |
9415 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009416
Chris Mason39279cc2007-06-12 06:35:45 -04009417 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009418 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009419
9420 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009421 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009422 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009423 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009424 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009425 return 0;
9426}
9427
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009428static int btrfs_rename_exchange(struct inode *old_dir,
9429 struct dentry *old_dentry,
9430 struct inode *new_dir,
9431 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009433 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009434 struct btrfs_trans_handle *trans;
9435 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009436 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009437 struct inode *new_inode = new_dentry->d_inode;
9438 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009439 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009440 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009441 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9442 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009443 u64 old_idx = 0;
9444 u64 new_idx = 0;
9445 u64 root_objectid;
9446 int ret;
Filipe Mananac5b4a502018-06-11 19:24:16 +01009447 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009448 bool root_log_pinned = false;
9449 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450
9451 /* we only allow rename subvolume link between subvolumes */
9452 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9453 return -EXDEV;
9454
9455 /* close the race window with snapshot create/destroy ioctl */
9456 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009457 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009458 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009459 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460
9461 /*
9462 * We want to reserve the absolute worst case amount of items. So if
9463 * both inodes are subvols and we need to unlink them then that would
9464 * require 4 item modifications, but if they are both normal inodes it
9465 * would require 5 item modifications, so we'll assume their normal
9466 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9467 * should cover the worst case number of items we'll modify.
9468 */
9469 trans = btrfs_start_transaction(root, 12);
9470 if (IS_ERR(trans)) {
9471 ret = PTR_ERR(trans);
9472 goto out_notrans;
9473 }
9474
9475 /*
9476 * We need to find a free sequence number both in the source and
9477 * in the destination directory for the exchange.
9478 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009479 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 if (ret)
9481 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009482 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009483 if (ret)
9484 goto out_fail;
9485
9486 BTRFS_I(old_inode)->dir_index = 0ULL;
9487 BTRFS_I(new_inode)->dir_index = 0ULL;
9488
9489 /* Reference for the source. */
9490 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9491 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009492 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009493 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009494 btrfs_pin_log_trans(root);
9495 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009496 ret = btrfs_insert_inode_ref(trans, dest,
9497 new_dentry->d_name.name,
9498 new_dentry->d_name.len,
9499 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009500 btrfs_ino(BTRFS_I(new_dir)),
9501 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 if (ret)
9503 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009504 }
9505
9506 /* And now for the dest. */
9507 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9508 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009509 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009511 btrfs_pin_log_trans(dest);
9512 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009513 ret = btrfs_insert_inode_ref(trans, root,
9514 old_dentry->d_name.name,
9515 old_dentry->d_name.len,
9516 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009517 btrfs_ino(BTRFS_I(old_dir)),
9518 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 if (ret)
9520 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009521 }
9522
9523 /* Update inode version and ctime/mtime. */
9524 inode_inc_iversion(old_dir);
9525 inode_inc_iversion(new_dir);
9526 inode_inc_iversion(old_inode);
9527 inode_inc_iversion(new_inode);
9528 old_dir->i_ctime = old_dir->i_mtime = ctime;
9529 new_dir->i_ctime = new_dir->i_mtime = ctime;
9530 old_inode->i_ctime = ctime;
9531 new_inode->i_ctime = ctime;
9532
9533 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009534 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9535 BTRFS_I(old_inode), 1);
9536 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9537 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 }
9539
9540 /* src is a subvolume */
9541 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9542 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9543 ret = btrfs_unlink_subvol(trans, root, old_dir,
9544 root_objectid,
9545 old_dentry->d_name.name,
9546 old_dentry->d_name.len);
9547 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009548 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9549 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 old_dentry->d_name.name,
9551 old_dentry->d_name.len);
9552 if (!ret)
9553 ret = btrfs_update_inode(trans, root, old_inode);
9554 }
9555 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009556 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 goto out_fail;
9558 }
9559
9560 /* dest is a subvolume */
9561 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9562 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9563 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9564 root_objectid,
9565 new_dentry->d_name.name,
9566 new_dentry->d_name.len);
9567 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009568 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9569 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 new_dentry->d_name.name,
9571 new_dentry->d_name.len);
9572 if (!ret)
9573 ret = btrfs_update_inode(trans, dest, new_inode);
9574 }
9575 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009576 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009577 goto out_fail;
9578 }
9579
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009580 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 new_dentry->d_name.name,
9582 new_dentry->d_name.len, 0, old_idx);
9583 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009584 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009585 goto out_fail;
9586 }
9587
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009588 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 old_dentry->d_name.name,
9590 old_dentry->d_name.len, 0, new_idx);
9591 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009592 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593 goto out_fail;
9594 }
9595
9596 if (old_inode->i_nlink == 1)
9597 BTRFS_I(old_inode)->dir_index = old_idx;
9598 if (new_inode->i_nlink == 1)
9599 BTRFS_I(new_inode)->dir_index = new_idx;
9600
Filipe Manana86e8aa02016-05-05 02:02:27 +01009601 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009603 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9604 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009606 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009608 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009610 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9611 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009613 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 }
9615out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009616 /*
9617 * If we have pinned a log and an error happened, we unpin tasks
9618 * trying to sync the log and force them to fallback to a transaction
9619 * commit if the log currently contains any of the inodes involved in
9620 * this rename operation (to ensure we do not persist a log with an
9621 * inconsistent state for any of these inodes or leading to any
9622 * inconsistencies when replayed). If the transaction was aborted, the
9623 * abortion reason is propagated to userspace when attempting to commit
9624 * the transaction. If the log does not contain any of these inodes, we
9625 * allow the tasks to sync it.
9626 */
9627 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009628 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9629 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9630 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009631 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009632 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009633 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009634
9635 if (root_log_pinned) {
9636 btrfs_end_log_trans(root);
9637 root_log_pinned = false;
9638 }
9639 if (dest_log_pinned) {
9640 btrfs_end_log_trans(dest);
9641 dest_log_pinned = false;
9642 }
9643 }
Filipe Mananac5b4a502018-06-11 19:24:16 +01009644 ret2 = btrfs_end_transaction(trans);
9645 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009646out_notrans:
9647 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009648 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009650 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651
9652 return ret;
9653}
9654
9655static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9656 struct btrfs_root *root,
9657 struct inode *dir,
9658 struct dentry *dentry)
9659{
9660 int ret;
9661 struct inode *inode;
9662 u64 objectid;
9663 u64 index;
9664
9665 ret = btrfs_find_free_ino(root, &objectid);
9666 if (ret)
9667 return ret;
9668
9669 inode = btrfs_new_inode(trans, root, dir,
9670 dentry->d_name.name,
9671 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009672 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 objectid,
9674 S_IFCHR | WHITEOUT_MODE,
9675 &index);
9676
9677 if (IS_ERR(inode)) {
9678 ret = PTR_ERR(inode);
9679 return ret;
9680 }
9681
9682 inode->i_op = &btrfs_special_inode_operations;
9683 init_special_inode(inode, inode->i_mode,
9684 WHITEOUT_DEV);
9685
9686 ret = btrfs_init_inode_security(trans, inode, dir,
9687 &dentry->d_name);
9688 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009689 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009691 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9692 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009694 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695
9696 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009697out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009699 if (ret)
9700 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009701 iput(inode);
9702
Filipe Mananac9901612016-05-05 01:41:57 +01009703 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704}
9705
Chris Mason39279cc2007-06-12 06:35:45 -04009706static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 struct inode *new_dir, struct dentry *new_dentry,
9708 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009710 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009711 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009712 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009713 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9714 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009715 struct inode *new_inode = d_inode(new_dentry);
9716 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009717 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009718 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009719 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009720 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009721 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009722
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009723 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009724 return -EPERM;
9725
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009726 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009727 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009728 return -EXDEV;
9729
Li Zefan33345d012011-04-20 10:31:50 +08009730 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009731 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009732 return -ENOTEMPTY;
9733
Chris Mason39279cc2007-06-12 06:35:45 -04009734 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009735 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009736 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009737
9738
9739 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009740 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009741 new_dentry->d_name.name,
9742 new_dentry->d_name.len);
9743
9744 if (ret) {
9745 if (ret == -EEXIST) {
9746 /* we shouldn't get
9747 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309748 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009749 return ret;
9750 }
9751 } else {
9752 /* maybe -EOVERFLOW */
9753 return ret;
9754 }
9755 }
9756 ret = 0;
9757
Chris Mason5a3f23d2009-03-31 13:27:11 -04009758 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009759 * we're using rename to replace one file with another. Start IO on it
9760 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009761 */
Chris Mason8d875f92014-08-12 10:47:42 -07009762 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009763 filemap_flush(old_inode->i_mapping);
9764
Yan, Zheng76dda932009-09-21 16:00:26 -04009765 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009766 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009767 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009768 /*
9769 * We want to reserve the absolute worst case amount of items. So if
9770 * both inodes are subvols and we need to unlink them then that would
9771 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009772 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009773 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9774 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009775 * If our rename has the whiteout flag, we need more 5 units for the
9776 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9777 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009778 */
Filipe Manana5062af32016-05-05 10:26:26 +01009779 trans_num_items = 11;
9780 if (flags & RENAME_WHITEOUT)
9781 trans_num_items += 5;
9782 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009783 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009784 ret = PTR_ERR(trans);
9785 goto out_notrans;
9786 }
Chris Mason5f39d392007-10-15 16:14:19 -04009787
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009788 if (dest != root)
9789 btrfs_record_root_in_trans(trans, dest);
9790
Nikolay Borisov877574e2017-02-20 13:50:33 +02009791 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009792 if (ret)
9793 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009794
Miao Xie67de1172013-12-26 13:07:06 +08009795 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009796 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009797 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009798 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009799 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009800 btrfs_pin_log_trans(root);
9801 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009802 ret = btrfs_insert_inode_ref(trans, dest,
9803 new_dentry->d_name.name,
9804 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009805 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009806 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009807 if (ret)
9808 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009809 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009810
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009811 inode_inc_iversion(old_dir);
9812 inode_inc_iversion(new_dir);
9813 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009814 old_dir->i_ctime = old_dir->i_mtime =
9815 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009816 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009817
Chris Mason12fcfd22009-03-24 10:24:20 -04009818 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009819 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9820 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009821
Li Zefan33345d012011-04-20 10:31:50 +08009822 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009823 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9824 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9825 old_dentry->d_name.name,
9826 old_dentry->d_name.len);
9827 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009828 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9829 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009830 old_dentry->d_name.name,
9831 old_dentry->d_name.len);
9832 if (!ret)
9833 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009834 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009835 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009836 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009837 goto out_fail;
9838 }
Chris Mason39279cc2007-06-12 06:35:45 -04009839
9840 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009841 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009842 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009843 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009844 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9845 root_objectid = BTRFS_I(new_inode)->location.objectid;
9846 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9847 root_objectid,
9848 new_dentry->d_name.name,
9849 new_dentry->d_name.len);
9850 BUG_ON(new_inode->i_nlink == 0);
9851 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009852 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9853 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009854 new_dentry->d_name.name,
9855 new_dentry->d_name.len);
9856 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009857 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009858 ret = btrfs_orphan_add(trans,
9859 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009860 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009861 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009862 goto out_fail;
9863 }
Chris Mason39279cc2007-06-12 06:35:45 -04009864 }
Josef Bacikaec74772008-07-24 12:12:38 -04009865
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009866 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009867 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009868 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009869 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009870 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009871 goto out_fail;
9872 }
Chris Mason39279cc2007-06-12 06:35:45 -04009873
Miao Xie67de1172013-12-26 13:07:06 +08009874 if (old_inode->i_nlink == 1)
9875 BTRFS_I(old_inode)->dir_index = index;
9876
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009877 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009878 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009879
David Sterbaf85b7372017-01-20 14:54:07 +01009880 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9881 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009882 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009883 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009884 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885
9886 if (flags & RENAME_WHITEOUT) {
9887 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9888 old_dentry);
9889
9890 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009891 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009892 goto out_fail;
9893 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009894 }
Chris Mason39279cc2007-06-12 06:35:45 -04009895out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009896 /*
9897 * If we have pinned the log and an error happened, we unpin tasks
9898 * trying to sync the log and force them to fallback to a transaction
9899 * commit if the log currently contains any of the inodes involved in
9900 * this rename operation (to ensure we do not persist a log with an
9901 * inconsistent state for any of these inodes or leading to any
9902 * inconsistencies when replayed). If the transaction was aborted, the
9903 * abortion reason is propagated to userspace when attempting to commit
9904 * the transaction. If the log does not contain any of these inodes, we
9905 * allow the tasks to sync it.
9906 */
9907 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009908 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9909 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9910 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009911 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009912 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009913 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009914
9915 btrfs_end_log_trans(root);
9916 log_pinned = false;
9917 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009918 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009919out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009920 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009921 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009922
Chris Mason39279cc2007-06-12 06:35:45 -04009923 return ret;
9924}
9925
Miklos Szeredi80ace852014-07-23 15:15:32 +02009926static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9927 struct inode *new_dir, struct dentry *new_dentry,
9928 unsigned int flags)
9929{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009930 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009931 return -EINVAL;
9932
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009933 if (flags & RENAME_EXCHANGE)
9934 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9935 new_dentry);
9936
9937 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009938}
9939
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009940struct btrfs_delalloc_work {
9941 struct inode *inode;
9942 struct completion completion;
9943 struct list_head list;
9944 struct btrfs_work work;
9945};
9946
Miao Xie8ccf6f192012-10-25 09:28:04 +00009947static void btrfs_run_delalloc_work(struct btrfs_work *work)
9948{
9949 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009950 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009951
9952 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9953 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009954 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009955 filemap_flush(inode->i_mapping);
9956 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9957 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009958 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009959
Nikolay Borisov076da912018-04-23 10:54:16 +03009960 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009961 complete(&delalloc_work->completion);
9962}
9963
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009964static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009965{
9966 struct btrfs_delalloc_work *work;
9967
David Sterba100d5702015-12-08 14:39:32 +01009968 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009969 if (!work)
9970 return NULL;
9971
9972 init_completion(&work->completion);
9973 INIT_LIST_HEAD(&work->list);
9974 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009975 WARN_ON_ONCE(!inode);
9976 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9977 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009978
9979 return work;
9980}
9981
Chris Masond352ac62008-09-29 15:18:18 -04009982/*
9983 * some fairly slow code that needs optimization. This walks the list
9984 * of all the inodes with pending delalloc and forces them to disk.
9985 */
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009986static int start_delalloc_inodes(struct btrfs_root *root, int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009987{
Chris Masonea8c2812008-08-04 23:17:27 -04009988 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009989 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009990 struct btrfs_delalloc_work *work, *next;
9991 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009992 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009993 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009994
Miao Xie8ccf6f192012-10-25 09:28:04 +00009995 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009996 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009997
Miao Xie573bfb72014-03-06 13:55:03 +08009998 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009999 spin_lock(&root->delalloc_lock);
10000 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010001 while (!list_empty(&splice)) {
10002 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010003 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010004
Miao Xieeb73c1b2013-05-15 07:48:22 +000010005 list_move_tail(&binode->delalloc_inodes,
10006 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010007 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010008 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010010 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010011 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010012 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010013
Nikolay Borisov076da912018-04-23 10:54:16 +030010014 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010015 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010016 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010017 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010018 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010019 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010020 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010021 btrfs_queue_work(root->fs_info->flush_workers,
10022 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010023 ret++;
10024 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010025 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010026 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010027 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010028 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010029 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010030
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010031out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010032 list_for_each_entry_safe(work, next, &works, list) {
10033 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010034 wait_for_completion(&work->completion);
10035 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010036 }
10037
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010038 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 spin_lock(&root->delalloc_lock);
10040 list_splice_tail(&splice, &root->delalloc_inodes);
10041 spin_unlock(&root->delalloc_lock);
10042 }
Miao Xie573bfb72014-03-06 13:55:03 +080010043 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010044 return ret;
10045}
10046
Nikolay Borisov76f32e22018-04-23 10:54:14 +030010047int btrfs_start_delalloc_inodes(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010048{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010049 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 int ret;
10051
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010052 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010053 return -EROFS;
10054
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010055 ret = start_delalloc_inodes(root, -1);
Miao Xie6c255e62014-03-06 13:55:01 +080010056 if (ret > 0)
10057 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010058 return ret;
10059}
10060
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010061int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010062{
10063 struct btrfs_root *root;
10064 struct list_head splice;
10065 int ret;
10066
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010067 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 return -EROFS;
10069
10070 INIT_LIST_HEAD(&splice);
10071
Miao Xie573bfb72014-03-06 13:55:03 +080010072 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010073 spin_lock(&fs_info->delalloc_root_lock);
10074 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010075 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010076 root = list_first_entry(&splice, struct btrfs_root,
10077 delalloc_root);
10078 root = btrfs_grab_fs_root(root);
10079 BUG_ON(!root);
10080 list_move_tail(&root->delalloc_root,
10081 &fs_info->delalloc_roots);
10082 spin_unlock(&fs_info->delalloc_root_lock);
10083
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010084 ret = start_delalloc_inodes(root, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010085 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010086 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087 goto out;
10088
Miao Xie6c255e62014-03-06 13:55:01 +080010089 if (nr != -1) {
10090 nr -= ret;
10091 WARN_ON(nr < 0);
10092 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 spin_lock(&fs_info->delalloc_root_lock);
10094 }
10095 spin_unlock(&fs_info->delalloc_root_lock);
10096
Miao Xie6c255e62014-03-06 13:55:01 +080010097 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010098out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010099 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010100 spin_lock(&fs_info->delalloc_root_lock);
10101 list_splice_tail(&splice, &fs_info->delalloc_roots);
10102 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010103 }
Miao Xie573bfb72014-03-06 13:55:03 +080010104 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010105 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010106}
10107
Chris Mason39279cc2007-06-12 06:35:45 -040010108static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10109 const char *symname)
10110{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010111 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010112 struct btrfs_trans_handle *trans;
10113 struct btrfs_root *root = BTRFS_I(dir)->root;
10114 struct btrfs_path *path;
10115 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010116 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010117 int err;
10118 int drop_inode = 0;
10119 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010120 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010121 int name_len;
10122 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010123 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010124 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010125 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010126
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010127 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010128 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010129 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010130
Josef Bacik9ed74f22009-09-11 16:12:44 -040010131 /*
10132 * 2 items for inode item and ref
10133 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010134 * 1 item for updating parent inode item
10135 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010136 * 1 item for xattr if selinux is on
10137 */
Filipe Manana9269d122015-12-31 18:16:29 +000010138 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010139 if (IS_ERR(trans))
10140 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010141
Li Zefan581bb052011-04-20 10:06:11 +080010142 err = btrfs_find_free_ino(root, &objectid);
10143 if (err)
10144 goto out_unlock;
10145
Josef Bacikaec74772008-07-24 12:12:38 -040010146 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010147 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10148 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010149 if (IS_ERR(inode)) {
10150 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010151 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010152 }
Chris Mason39279cc2007-06-12 06:35:45 -040010153
Casey Schauflerad19db72011-12-15 10:09:07 -050010154 /*
10155 * If the active LSM wants to access the inode during
10156 * d_instantiate it needs these. Smack checks to see
10157 * if the filesystem supports xattrs by looking at the
10158 * ops vector.
10159 */
10160 inode->i_fop = &btrfs_file_operations;
10161 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010162 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010163 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10164
10165 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10166 if (err)
10167 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010168
Chris Mason39279cc2007-06-12 06:35:45 -040010169 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010170 if (!path) {
10171 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010172 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010173 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010174 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010175 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010176 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010177 datasize = btrfs_file_extent_calc_inline_size(name_len);
10178 err = btrfs_insert_empty_item(trans, root, path, &key,
10179 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010180 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010181 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010182 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010183 }
Chris Mason5f39d392007-10-15 16:14:19 -040010184 leaf = path->nodes[0];
10185 ei = btrfs_item_ptr(leaf, path->slots[0],
10186 struct btrfs_file_extent_item);
10187 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10188 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010189 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010190 btrfs_set_file_extent_encryption(leaf, ei, 0);
10191 btrfs_set_file_extent_compression(leaf, ei, 0);
10192 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10193 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10194
Chris Mason39279cc2007-06-12 06:35:45 -040010195 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010196 write_extent_buffer(leaf, symname, ptr, name_len);
10197 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010198 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010199
Chris Mason39279cc2007-06-12 06:35:45 -040010200 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010201 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010202 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010203 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010204 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010205 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010206 /*
10207 * Last step, add directory indexes for our symlink inode. This is the
10208 * last step to avoid extra cleanup of these indexes if an error happens
10209 * elsewhere above.
10210 */
10211 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010212 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10213 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010214 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010215 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010216 goto out_unlock_inode;
10217 }
10218
Al Viro1e2e5472018-05-04 08:23:01 -040010219 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010220
10221out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010222 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010223 if (drop_inode) {
10224 inode_dec_link_count(inode);
10225 iput(inode);
10226 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010227 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010228 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010229
10230out_unlock_inode:
10231 drop_inode = 1;
10232 unlock_new_inode(inode);
10233 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010234}
Chris Mason16432982008-04-10 10:23:21 -040010235
Josef Bacik0af3d002010-06-21 14:48:16 -040010236static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10237 u64 start, u64 num_bytes, u64 min_size,
10238 loff_t actual_len, u64 *alloc_hint,
10239 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010242 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10243 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010244 struct btrfs_root *root = BTRFS_I(inode)->root;
10245 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010246 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010247 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010248 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010249 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010250 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010251 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010252 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010253
Josef Bacik0af3d002010-06-21 14:48:16 -040010254 if (trans)
10255 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010256 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010257 if (own_trans) {
10258 trans = btrfs_start_transaction(root, 3);
10259 if (IS_ERR(trans)) {
10260 ret = PTR_ERR(trans);
10261 break;
10262 }
Yan Zhengd899e052008-10-30 14:25:28 -040010263 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010264
Byongho Leeee221842015-12-15 01:42:10 +090010265 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010266 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010267 /*
10268 * If we are severely fragmented we could end up with really
10269 * small allocations, so if the allocator is returning small
10270 * chunks lets make its job easier by only searching for those
10271 * sized chunks.
10272 */
10273 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010274 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10275 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010276 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010277 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010278 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010279 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010280 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010281 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010282
Josef Bacik0b670dc2015-09-23 17:11:16 -040010283 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010284 ret = insert_reserved_file_extent(trans, inode,
10285 cur_offset, ins.objectid,
10286 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010287 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010288 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010289 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010290 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010291 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010292 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010293 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010294 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010295 break;
10296 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010297
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010298 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010299 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010300
Josef Bacik5dc562c2012-08-17 13:14:17 -040010301 em = alloc_extent_map();
10302 if (!em) {
10303 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10304 &BTRFS_I(inode)->runtime_flags);
10305 goto next;
10306 }
10307
10308 em->start = cur_offset;
10309 em->orig_start = cur_offset;
10310 em->len = ins.offset;
10311 em->block_start = ins.objectid;
10312 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010313 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010314 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010315 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010316 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10317 em->generation = trans->transid;
10318
10319 while (1) {
10320 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010321 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010322 write_unlock(&em_tree->lock);
10323 if (ret != -EEXIST)
10324 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010325 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010326 cur_offset + ins.offset - 1,
10327 0);
10328 }
10329 free_extent_map(em);
10330next:
Yan Zhengd899e052008-10-30 14:25:28 -040010331 num_bytes -= ins.offset;
10332 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010333 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010335 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010336 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010337 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010338 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010339 (actual_len > inode->i_size) &&
10340 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010341 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010342 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010343 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010344 i_size = cur_offset;
10345 i_size_write(inode, i_size);
10346 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010347 }
10348
Yan Zhengd899e052008-10-30 14:25:28 -040010349 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010350
10351 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010352 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010353 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010354 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010355 break;
10356 }
Yan Zhengd899e052008-10-30 14:25:28 -040010357
Josef Bacik0af3d002010-06-21 14:48:16 -040010358 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010359 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010360 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010361 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010362 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010363 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010364 return ret;
10365}
10366
Josef Bacik0af3d002010-06-21 14:48:16 -040010367int btrfs_prealloc_file_range(struct inode *inode, int mode,
10368 u64 start, u64 num_bytes, u64 min_size,
10369 loff_t actual_len, u64 *alloc_hint)
10370{
10371 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10372 min_size, actual_len, alloc_hint,
10373 NULL);
10374}
10375
10376int btrfs_prealloc_file_range_trans(struct inode *inode,
10377 struct btrfs_trans_handle *trans, int mode,
10378 u64 start, u64 num_bytes, u64 min_size,
10379 loff_t actual_len, u64 *alloc_hint)
10380{
10381 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10382 min_size, actual_len, alloc_hint, trans);
10383}
10384
Chris Masone6dcd2d2008-07-17 12:53:50 -040010385static int btrfs_set_page_dirty(struct page *page)
10386{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010387 return __set_page_dirty_nobuffers(page);
10388}
10389
Al Viro10556cb22011-06-20 19:28:19 -040010390static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010391{
Li Zefanb83cc962010-12-20 16:04:08 +080010392 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010393 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010394
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010395 if (mask & MAY_WRITE &&
10396 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10397 if (btrfs_root_readonly(root))
10398 return -EROFS;
10399 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10400 return -EACCES;
10401 }
Al Viro2830ba72011-06-20 19:16:29 -040010402 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010403}
Chris Mason39279cc2007-06-12 06:35:45 -040010404
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010405static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10406{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010407 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010408 struct btrfs_trans_handle *trans;
10409 struct btrfs_root *root = BTRFS_I(dir)->root;
10410 struct inode *inode = NULL;
10411 u64 objectid;
10412 u64 index;
10413 int ret = 0;
10414
10415 /*
10416 * 5 units required for adding orphan entry
10417 */
10418 trans = btrfs_start_transaction(root, 5);
10419 if (IS_ERR(trans))
10420 return PTR_ERR(trans);
10421
10422 ret = btrfs_find_free_ino(root, &objectid);
10423 if (ret)
10424 goto out;
10425
10426 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010427 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010428 if (IS_ERR(inode)) {
10429 ret = PTR_ERR(inode);
10430 inode = NULL;
10431 goto out;
10432 }
10433
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010434 inode->i_fop = &btrfs_file_operations;
10435 inode->i_op = &btrfs_file_inode_operations;
10436
10437 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010438 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10439
Chris Masonb0d5d102014-09-08 13:08:51 -070010440 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10441 if (ret)
10442 goto out_inode;
10443
10444 ret = btrfs_update_inode(trans, root, inode);
10445 if (ret)
10446 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010447 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010448 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010449 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010450
Filipe Manana5762b5c2014-08-01 00:10:32 +010010451 /*
10452 * We set number of links to 0 in btrfs_new_inode(), and here we set
10453 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10454 * through:
10455 *
10456 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10457 */
10458 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010459 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010460 d_tmpfile(dentry, inode);
10461 mark_inode_dirty(inode);
10462
10463out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010464 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010465 if (ret)
10466 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010467 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010469
10470out_inode:
10471 unlock_new_inode(inode);
10472 goto out;
10473
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010474}
10475
David Sterba20a7db82017-02-17 16:24:29 +010010476__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010477static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010478{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010479 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010480}
10481
Josef Bacikc6100a42017-05-05 11:57:13 -040010482static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10483{
10484 struct inode *inode = private_data;
10485 return btrfs_sb(inode->i_sb);
10486}
10487
10488static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10489 u64 start, u64 end)
10490{
10491 struct inode *inode = private_data;
10492 u64 isize;
10493
10494 isize = i_size_read(inode);
10495 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10496 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10497 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10498 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10499 }
10500}
10501
10502void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10503{
10504 struct inode *inode = private_data;
10505 unsigned long index = start >> PAGE_SHIFT;
10506 unsigned long end_index = end >> PAGE_SHIFT;
10507 struct page *page;
10508
10509 while (index <= end_index) {
10510 page = find_get_page(inode->i_mapping, index);
10511 ASSERT(page); /* Pages should be in the extent_io_tree */
10512 set_page_writeback(page);
10513 put_page(page);
10514 index++;
10515 }
10516}
10517
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010518static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010519 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010520 .lookup = btrfs_lookup,
10521 .create = btrfs_create,
10522 .unlink = btrfs_unlink,
10523 .link = btrfs_link,
10524 .mkdir = btrfs_mkdir,
10525 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010526 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010527 .symlink = btrfs_symlink,
10528 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010529 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010530 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010531 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010532 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010533 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010534 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010535 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010536};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010537static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010538 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010539 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010540 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010541};
Yan, Zheng76dda932009-09-21 16:00:26 -040010542
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010543static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010544 .llseek = generic_file_llseek,
10545 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010546 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010547 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010548 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010549#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010550 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010551#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010552 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010553 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010554};
10555
David Sterba20e55062015-11-19 11:42:28 +010010556static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010557 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010558 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010559 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010560 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010561 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010562 .tree_fs_info = iotree_fs_info,
10563 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010564
10565 /* optional callbacks */
10566 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010567 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010568 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010569 .set_bit_hook = btrfs_set_bit_hook,
10570 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010571 .merge_extent_hook = btrfs_merge_extent_hook,
10572 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010573 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010574};
10575
Chris Mason35054392009-01-21 13:11:13 -050010576/*
10577 * btrfs doesn't support the bmap operation because swapfiles
10578 * use bmap to make a mapping of extents in the file. They assume
10579 * these extents won't change over the life of the file and they
10580 * use the bmap result to do IO directly to the drive.
10581 *
10582 * the btrfs bmap call would return logical addresses that aren't
10583 * suitable for IO and they also will change frequently as COW
10584 * operations happen. So, swapfile + btrfs == corruption.
10585 *
10586 * For now we're avoiding this by dropping bmap.
10587 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010588static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010589 .readpage = btrfs_readpage,
10590 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010591 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010592 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010593 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010594 .invalidatepage = btrfs_invalidatepage,
10595 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010596 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010597 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010598};
10599
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010600static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010601 .readpage = btrfs_readpage,
10602 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010603 .invalidatepage = btrfs_invalidatepage,
10604 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010605};
10606
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010607static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010608 .getattr = btrfs_getattr,
10609 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010610 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010611 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010612 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010613 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010614 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010615 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010616};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010617static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010618 .getattr = btrfs_getattr,
10619 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010620 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010621 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010622 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010623 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010624 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010625};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010626static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010627 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010628 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010629 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010630 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010631 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010632 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010633};
Yan, Zheng76dda932009-09-21 16:00:26 -040010634
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010635const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010636 .d_delete = btrfs_dentry_delete,
10637};