blob: 76245323a7c8abed0db0280fe775fdb7694cb53e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050046#include <linux/iversion.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000062#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080063#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080064#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040065
66struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080067 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040068 struct btrfs_root *root;
69};
70
Filipe Mananaf28a4922015-12-08 19:23:20 +000071struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000072 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080075 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000076};
77
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070078static const struct inode_operations btrfs_dir_inode_operations;
79static const struct inode_operations btrfs_symlink_inode_operations;
80static const struct inode_operations btrfs_dir_ro_inode_operations;
81static const struct inode_operations btrfs_special_inode_operations;
82static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070083static const struct address_space_operations btrfs_aops;
84static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070085static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010086static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040087
88static struct kmem_cache *btrfs_inode_cachep;
89struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Qu Wenruo524272602017-03-08 10:25:52 +0800118static void __endio_write_update_ordered(struct inode *inode,
119 const u64 offset, const u64 bytes,
120 const bool uptodate);
121
122/*
123 * Cleanup all submitted ordered extents in specified range to handle errors
124 * from the fill_dellaloc() callback.
125 *
126 * NOTE: caller must ensure that when an error happens, it can not call
127 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
128 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
129 * to be released, which we want to happen only when finishing the ordered
130 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
131 * fill_delalloc() callback already does proper cleanup for the first page of
132 * the range, that is, it invokes the callback writepage_end_io_hook() for the
133 * range of the first page.
134 */
135static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
136 const u64 offset,
137 const u64 bytes)
138{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900139 unsigned long index = offset >> PAGE_SHIFT;
140 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
141 struct page *page;
142
143 while (index <= end_index) {
144 page = find_get_page(inode->i_mapping, index);
145 index++;
146 if (!page)
147 continue;
148 ClearPagePrivate2(page);
149 put_page(page);
150 }
Qu Wenruo524272602017-03-08 10:25:52 +0800151 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
152 bytes - PAGE_SIZE, false);
153}
154
Eric Sandeen48a3b632013-04-25 20:41:01 +0000155static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400156
Josef Bacik6a3891c2015-03-16 17:38:52 -0400157#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
158void btrfs_test_inode_set_ops(struct inode *inode)
159{
160 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
161}
162#endif
163
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000164static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500165 struct inode *inode, struct inode *dir,
166 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500167{
168 int err;
169
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000170 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500171 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500172 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500173 return err;
174}
175
Chris Masond352ac62008-09-29 15:18:18 -0400176/*
Chris Masonc8b97812008-10-29 14:49:59 -0400177 * this does all the hard work for inserting an inline extent into
178 * the btree. The caller should have done a btrfs_drop_extents so that
179 * no overlapping inline items exist in the btree
180 */
Chris Mason40f76582014-05-21 13:35:51 -0700181static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000182 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 struct btrfs_root *root, struct inode *inode,
184 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400186 struct page **compressed_pages)
187{
Chris Masonc8b97812008-10-29 14:49:59 -0400188 struct extent_buffer *leaf;
189 struct page *page = NULL;
190 char *kaddr;
191 unsigned long ptr;
192 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400193 int ret;
194 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400195 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400196
Li Zefanfe3f5662011-03-28 08:30:38 +0000197 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400198 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400199
Chris Masonc8b97812008-10-29 14:49:59 -0400200 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201
202 if (!extent_inserted) {
203 struct btrfs_key key;
204 size_t datasize;
205
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200206 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000207 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200208 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000209
210 datasize = btrfs_file_extent_calc_inline_size(cur_size);
211 path->leave_spinning = 1;
212 ret = btrfs_insert_empty_item(trans, root, path, &key,
213 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200214 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000215 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 }
217 leaf = path->nodes[0];
218 ei = btrfs_item_ptr(leaf, path->slots[0],
219 struct btrfs_file_extent_item);
220 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
221 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
222 btrfs_set_file_extent_encryption(leaf, ei, 0);
223 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
224 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
225 ptr = btrfs_file_extent_inline_start(ei);
226
Li Zefan261507a02010-12-17 14:21:50 +0800227 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400228 struct page *cpage;
229 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500230 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400231 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500232 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400234
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400238
239 i++;
240 ptr += cur_size;
241 compressed_size -= cur_size;
242 }
243 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800244 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 } else {
246 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300247 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400248 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800249 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300250 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400251 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800252 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300253 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400254 }
255 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000256 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Yan, Zhengc2167752009-11-12 09:34:21 +0000258 /*
259 * we're an inline extent, so nobody can
260 * extend the file past i_size without locking
261 * a page we already have locked.
262 *
263 * We must do any isize and inode updates
264 * before we unlock the pages. Otherwise we
265 * could end up racing with unlink.
266 */
Chris Masonc8b97812008-10-29 14:49:59 -0400267 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000269
Chris Masonc8b97812008-10-29 14:49:59 -0400270fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200271 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400272}
273
274
275/*
276 * conditionally insert an inline extent into the file. This
277 * does the checks required to make sure the data is small enough
278 * to fit as an inline extent.
279 */
Josef Bacik00361582013-08-14 14:02:47 -0400280static noinline int cow_file_range_inline(struct btrfs_root *root,
281 struct inode *inode, u64 start,
282 u64 end, size_t compressed_size,
283 int compress_type,
284 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400287 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 isize = i_size_read(inode);
289 u64 actual_end = min(end + 1, isize);
290 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400291 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400292 u64 data_len = inline_len;
293 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000294 struct btrfs_path *path;
295 int extent_inserted = 0;
296 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400297
298 if (compressed_size)
299 data_len = compressed_size;
300
301 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 actual_end > fs_info->sectorsize ||
303 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400304 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400305 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400306 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400307 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400308 return 1;
309 }
310
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 path = btrfs_alloc_path();
312 if (!path)
313 return -ENOMEM;
314
Josef Bacik00361582013-08-14 14:02:47 -0400315 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000316 if (IS_ERR(trans)) {
317 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400318 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000319 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400320 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400321
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000322 if (compressed_size && compressed_pages)
323 extent_item_size = btrfs_file_extent_calc_inline_size(
324 compressed_size);
325 else
326 extent_item_size = btrfs_file_extent_calc_inline_size(
327 inline_len);
328
329 ret = __btrfs_drop_extents(trans, root, inode, path,
330 start, aligned_end, NULL,
331 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400332 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400333 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400334 goto out;
335 }
Chris Masonc8b97812008-10-29 14:49:59 -0400336
337 if (isize > actual_end)
338 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000339 ret = insert_inline_extent(trans, path, extent_inserted,
340 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400341 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000342 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400343 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400344 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400345 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400346 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400347 ret = 1;
348 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100349 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400350
Josef Bacikbdc20e62013-02-28 13:23:38 -0500351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200352 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400353out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800354 /*
355 * Don't forget to free the reserved space, as for inlined extent
356 * it won't count as data extent, free them directly here.
357 * And at reserve time, it's always aligned to page size, so
358 * just free one page here.
359 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800360 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000361 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400362 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400363 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400364}
365
Chris Mason771ed682008-11-06 22:02:51 -0500366struct async_extent {
367 u64 start;
368 u64 ram_size;
369 u64 compressed_size;
370 struct page **pages;
371 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800372 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct list_head list;
374};
375
376struct async_cow {
377 struct inode *inode;
378 struct btrfs_root *root;
379 struct page *locked_page;
380 u64 start;
381 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600382 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500383 struct list_head extents;
384 struct btrfs_work work;
385};
386
387static noinline int add_async_extent(struct async_cow *cow,
388 u64 start, u64 ram_size,
389 u64 compressed_size,
390 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800391 unsigned long nr_pages,
392 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500393{
394 struct async_extent *async_extent;
395
396 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100397 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500398 async_extent->start = start;
399 async_extent->ram_size = ram_size;
400 async_extent->compressed_size = compressed_size;
401 async_extent->pages = pages;
402 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800403 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500404 list_add_tail(&async_extent->list, &cow->extents);
405 return 0;
406}
407
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300408static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800409{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800411
412 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400413 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800414 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200415 /* defrag ioctl */
416 if (BTRFS_I(inode)->defrag_compress)
417 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800418 /* bad compression ratios */
419 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
420 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400421 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800422 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200423 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300424 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800425 return 0;
426}
427
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200428static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800429 u64 start, u64 end, u64 num_bytes, u64 small_write)
430{
431 /* If this is a small write inside eof, kick off a defrag */
432 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200433 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800434 btrfs_add_inode_defrag(NULL, inode);
435}
436
Chris Masonc8b97812008-10-29 14:49:59 -0400437/*
Chris Mason771ed682008-11-06 22:02:51 -0500438 * we create compressed extents in two phases. The first
439 * phase compresses a range of pages that have already been
440 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400441 *
Chris Mason771ed682008-11-06 22:02:51 -0500442 * This is done inside an ordered work queue, and the compression
443 * is spread across many cpus. The actual IO submission is step
444 * two, and the ordered work queue takes care of making sure that
445 * happens in the same order things were put onto the queue by
446 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400447 *
Chris Mason771ed682008-11-06 22:02:51 -0500448 * If this code finds it can't get good compression, it puts an
449 * entry onto the work queue to write the uncompressed bytes. This
450 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300451 * are written in the same order that the flusher thread sent them
452 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400453 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100454static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500455 struct page *locked_page,
456 u64 start, u64 end,
457 struct async_cow *async_cow,
458 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400461 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400463 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500464 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400465 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400466 struct page **pages = NULL;
467 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400468 unsigned long total_compressed = 0;
469 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400470 int i;
471 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400473 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400474
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200475 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
476 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400477
Chris Mason42dc7ba2008-12-15 11:44:56 -0500478 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400479again:
480 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300481 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100482 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
483 nr_pages = min_t(unsigned long, nr_pages,
484 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400485
Chris Masonf03d9301f2009-02-04 09:31:06 -0500486 /*
487 * we don't want to send crud past the end of i_size through
488 * compression, that's just a waste of CPU time. So, if the
489 * end of the file is before the start of our current
490 * requested range of bytes, we bail out to the uncompressed
491 * cleanup code that can deal with all of this.
492 *
493 * It isn't really the fastest way to fix things, but this is a
494 * very uncommon corner.
495 */
496 if (actual_end <= start)
497 goto cleanup_and_bail_uncompressed;
498
Chris Masonc8b97812008-10-29 14:49:59 -0400499 total_compressed = actual_end - start;
500
Shilong Wang4bcbb332014-10-07 18:44:35 -0400501 /*
502 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400503 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400504 */
505 if (total_compressed <= blocksize &&
506 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
507 goto cleanup_and_bail_uncompressed;
508
David Sterba069eac72017-02-14 19:36:54 +0100509 total_compressed = min_t(unsigned long, total_compressed,
510 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400511 total_in = 0;
512 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400513
Chris Mason771ed682008-11-06 22:02:51 -0500514 /*
515 * we do compression for mount -o compress and when the
516 * inode has not been flagged as nocompress. This flag can
517 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300519 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400520 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100521 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800522 if (!pages) {
523 /* just bail out to the uncompressed code */
524 goto cont;
525 }
Chris Mason179e29e2007-11-01 11:28:41 -0400526
David Sterbaeec63c62017-07-17 19:41:31 +0200527 if (BTRFS_I(inode)->defrag_compress)
528 compress_type = BTRFS_I(inode)->defrag_compress;
529 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200530 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800531
Chris Mason4adaa612013-03-26 13:07:00 -0400532 /*
533 * we need to call clear_page_dirty_for_io on each
534 * page in the range. Otherwise applications with the file
535 * mmap'd can wander in and change the page contents while
536 * we are compressing them.
537 *
538 * If the compression fails for any reason, we set the pages
539 * dirty again later on.
540 */
541 extent_range_clear_dirty_for_io(inode, start, end);
542 redirty = 1;
David Sterbaf51d2b52017-09-15 17:36:57 +0200543
544 /* Compression level is applied here and only here */
545 ret = btrfs_compress_pages(
546 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800547 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100548 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100549 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800550 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100551 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400552
553 if (!ret) {
554 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300555 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100556 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400557 char *kaddr;
558
559 /* zero the tail end of the last page, we might be
560 * sending it down to disk
561 */
562 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800563 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400564 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300565 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800566 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 }
568 will_compress = 1;
569 }
570 }
Li Zefan560f7d72011-09-08 10:22:01 +0800571cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400572 if (start == 0) {
573 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300574 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400575 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500576 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400577 */
Josef Bacik00361582013-08-14 14:02:47 -0400578 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800579 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400580 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500581 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400582 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000583 total_compressed,
584 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400585 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100586 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400587 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400588 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
589 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100590 unsigned long page_error_op;
591
Filipe Mananae6eb4312014-10-10 10:45:12 +0100592 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400593
Chris Mason771ed682008-11-06 22:02:51 -0500594 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100595 * inline extent creation worked or returned error,
596 * we don't need to create any more async work items.
597 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400598 *
599 * We use DO_ACCOUNTING here because we need the
600 * delalloc_release_metadata to be done _after_ we drop
601 * our outstanding extent for clearing delalloc for this
602 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500603 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800604 extent_clear_unlock_delalloc(inode, start, end, end,
605 NULL, clear_flags,
606 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400607 PAGE_CLEAR_DIRTY |
608 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100609 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400610 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400611 goto free_pages_out;
612 }
613 }
614
615 if (will_compress) {
616 /*
617 * we aren't doing an inline extent round the compressed size
618 * up to a block size boundary so the allocator does sane
619 * things
620 */
Qu Wenruofda28322013-02-26 08:10:22 +0000621 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400622
623 /*
624 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300625 * win, compare the page count read with the blocks on disk,
626 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400627 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300628 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300629 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700630 *num_added += 1;
631
632 /*
633 * The async work queues will take care of doing actual
634 * allocation on disk for these compressed pages, and
635 * will submit them to the elevator.
636 */
Timofey Titovets11708622017-10-03 18:06:01 +0300637 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100638 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700639 compress_type);
640
Timofey Titovets11708622017-10-03 18:06:01 +0300641 if (start + total_in < end) {
642 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 pages = NULL;
644 cond_resched();
645 goto again;
646 }
647 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400648 }
649 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700650 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400651 /*
652 * the compression code ran but failed to make things smaller,
653 * free any pages it allocated and our page pointer array
654 */
David Sterba4d3a8002017-02-14 19:04:07 +0100655 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400656 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300657 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400658 }
659 kfree(pages);
660 pages = NULL;
661 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100662 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400663
664 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400665 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200666 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500667 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500668 }
Chris Masonc8b97812008-10-29 14:49:59 -0400669 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500670cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700671 /*
672 * No compression, but we still need to write the pages in the file
673 * we've been given so far. redirty the locked page if it corresponds
674 * to our extent and set things up for the async work queue to run
675 * cow_file_range to do the normal delalloc dance.
676 */
677 if (page_offset(locked_page) >= start &&
678 page_offset(locked_page) <= end)
679 __set_page_dirty_nobuffers(locked_page);
680 /* unlocked later on in the async handlers */
681
682 if (redirty)
683 extent_range_redirty_for_io(inode, start, end);
684 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
685 BTRFS_COMPRESS_NONE);
686 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500687
Filipe Mananac44f6492014-10-09 21:15:44 +0100688 return;
Chris Mason771ed682008-11-06 22:02:51 -0500689
690free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100691 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500692 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300693 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500694 }
Chris Masond3977122009-01-05 21:25:51 -0500695 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500696}
Chris Mason771ed682008-11-06 22:02:51 -0500697
Filipe Manana40ae8372014-10-06 22:14:24 +0100698static void free_async_extent_pages(struct async_extent *async_extent)
699{
700 int i;
701
702 if (!async_extent->pages)
703 return;
704
705 for (i = 0; i < async_extent->nr_pages; i++) {
706 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100708 }
709 kfree(async_extent->pages);
710 async_extent->nr_pages = 0;
711 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500712}
713
714/*
715 * phase two of compressed writeback. This is the ordered portion
716 * of the code, which only gets called in the order the work was
717 * queued. We walk all the async extents created by compress_file_range
718 * and send them down to the disk.
719 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100720static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500721 struct async_cow *async_cow)
722{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400723 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500724 struct async_extent *async_extent;
725 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500726 struct btrfs_key ins;
727 struct extent_map *em;
728 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500729 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500730 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500731
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500732again:
Chris Masond3977122009-01-05 21:25:51 -0500733 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500734 async_extent = list_entry(async_cow->extents.next,
735 struct async_extent, list);
736 list_del(&async_extent->list);
737
738 io_tree = &BTRFS_I(inode)->io_tree;
739
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500740retry:
Chris Mason771ed682008-11-06 22:02:51 -0500741 /* did the compression code fall back to uncompressed IO? */
742 if (!async_extent->pages) {
743 int page_started = 0;
744 unsigned long nr_written = 0;
745
746 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000747 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100748 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500749
750 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500751 ret = cow_file_range(inode, async_cow->locked_page,
752 async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800755 async_extent->start +
756 async_extent->ram_size - 1,
757 &page_started, &nr_written, 0,
758 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500759
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100760 /* JDM XXX */
761
Chris Mason771ed682008-11-06 22:02:51 -0500762 /*
763 * if page_started, cow_file_range inserted an
764 * inline extent and took care of all the unlocking
765 * and IO for us. Otherwise, we need to submit
766 * all those pages down to the drive.
767 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500768 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500769 extent_write_locked_range(io_tree,
770 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500771 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500772 async_extent->ram_size - 1,
773 btrfs_get_extent,
774 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775 else if (ret)
776 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500777 kfree(async_extent);
778 cond_resched();
779 continue;
780 }
781
782 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100783 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500784
Wang Xiaoguang18513092016-07-25 15:51:40 +0800785 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500786 async_extent->compressed_size,
787 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800788 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500789 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100790 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400792 if (ret == -ENOSPC) {
793 unlock_extent(io_tree, async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800796
797 /*
798 * we need to redirty the pages if we decide to
799 * fallback to uncompressed IO, otherwise we
800 * will not submit these pages down to lower
801 * layers.
802 */
803 extent_range_redirty_for_io(inode,
804 async_extent->start,
805 async_extent->start +
806 async_extent->ram_size - 1);
807
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100808 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400809 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500810 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500811 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000812 /*
813 * here we're doing allocation and writeback of the
814 * compressed pages
815 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800816 em = create_io_em(inode, async_extent->start,
817 async_extent->ram_size, /* len */
818 async_extent->start, /* orig_start */
819 ins.objectid, /* block_start */
820 ins.offset, /* block_len */
821 ins.offset, /* orig_block_len */
822 async_extent->ram_size, /* ram_bytes */
823 async_extent->compress_type,
824 BTRFS_ORDERED_COMPRESSED);
825 if (IS_ERR(em))
826 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500827 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800828 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829
Li Zefan261507a02010-12-17 14:21:50 +0800830 ret = btrfs_add_ordered_extent_compress(inode,
831 async_extent->start,
832 ins.objectid,
833 async_extent->ram_size,
834 ins.offset,
835 BTRFS_ORDERED_COMPRESSED,
836 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100837 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200838 btrfs_drop_extent_cache(BTRFS_I(inode),
839 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 async_extent->start +
841 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500842 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100843 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400844 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500845
Chris Mason771ed682008-11-06 22:02:51 -0500846 /*
847 * clear dirty, set writeback and unlock the pages.
848 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400849 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400850 async_extent->start +
851 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800852 async_extent->start +
853 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400854 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
855 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400856 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200857 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500858 async_extent->start,
859 async_extent->ram_size,
860 ins.objectid,
861 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600862 async_extent->nr_pages,
863 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
865 struct page *p = async_extent->pages[0];
866 const u64 start = async_extent->start;
867 const u64 end = start + async_extent->ram_size - 1;
868
869 p->mapping = inode->i_mapping;
870 tree->ops->writepage_end_io_hook(p, start, end,
871 NULL, 0);
872 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800873 extent_clear_unlock_delalloc(inode, start, end, end,
874 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100875 PAGE_END_WRITEBACK |
876 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100877 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100878 }
Chris Mason771ed682008-11-06 22:02:51 -0500879 alloc_hint = ins.objectid + ins.offset;
880 kfree(async_extent);
881 cond_resched();
882 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100883 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500884out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400885 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400886 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100887out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400888 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500889 async_extent->start +
890 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800891 async_extent->start +
892 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400893 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100894 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400895 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
896 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100897 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
898 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100899 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500901 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500902}
903
Josef Bacik4b46fce2010-05-23 11:00:55 -0400904static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
905 u64 num_bytes)
906{
907 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
908 struct extent_map *em;
909 u64 alloc_hint = 0;
910
911 read_lock(&em_tree->lock);
912 em = search_extent_mapping(em_tree, start, num_bytes);
913 if (em) {
914 /*
915 * if block start isn't an actual block number then find the
916 * first block in this inode and use that as a hint. If that
917 * block is also bogus then just don't worry about it.
918 */
919 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
920 free_extent_map(em);
921 em = search_extent_mapping(em_tree, 0, 0);
922 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
923 alloc_hint = em->block_start;
924 if (em)
925 free_extent_map(em);
926 } else {
927 alloc_hint = em->block_start;
928 free_extent_map(em);
929 }
930 }
931 read_unlock(&em_tree->lock);
932
933 return alloc_hint;
934}
935
Chris Mason771ed682008-11-06 22:02:51 -0500936/*
937 * when extent_io.c finds a delayed allocation range in the file,
938 * the call backs end up in this code. The basic idea is to
939 * allocate extents on disk for the range, and create ordered data structs
940 * in ram to track those extents.
941 *
942 * locked_page is the page that writepage had locked already. We use
943 * it to make sure we don't do extra locks or unlocks.
944 *
945 * *page_started is set to one if we unlock locked_page and do everything
946 * required to start IO on it. It may be clean and already done with
947 * IO when we return.
948 */
Josef Bacik00361582013-08-14 14:02:47 -0400949static noinline int cow_file_range(struct inode *inode,
950 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800951 u64 start, u64 end, u64 delalloc_end,
952 int *page_started, unsigned long *nr_written,
953 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500954{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400955 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400956 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500957 u64 alloc_hint = 0;
958 u64 num_bytes;
959 unsigned long ram_size;
960 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000961 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400962 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500963 struct btrfs_key ins;
964 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000965 unsigned clear_bits;
966 unsigned long page_ops;
967 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500968 int ret = 0;
969
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200970 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400971 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500972 ret = -EINVAL;
973 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 }
Chris Mason771ed682008-11-06 22:02:51 -0500975
Qu Wenruofda28322013-02-26 08:10:22 +0000976 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500977 num_bytes = max(blocksize, num_bytes);
978 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500979
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200980 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400981
Chris Mason771ed682008-11-06 22:02:51 -0500982 if (start == 0) {
983 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800984 ret = cow_file_range_inline(root, inode, start, end, 0,
985 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500986 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400987 /*
988 * We use DO_ACCOUNTING here because we need the
989 * delalloc_release_metadata to be run _after_ we drop
990 * our outstanding extent for clearing delalloc for this
991 * range.
992 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800993 extent_clear_unlock_delalloc(inode, start, end,
994 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400995 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400996 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
997 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
999 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001000 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001001 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001002 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001003 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001006 }
1007 }
Chris Masonc8b97812008-10-29 14:49:59 -04001008
1009 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001010 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001011
Josef Bacik4b46fce2010-05-23 11:00:55 -04001012 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001013 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1014 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001015
Chris Masond3977122009-01-05 21:25:51 -05001016 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001017 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001018 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001019 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001020 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001021 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001022 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001023 cur_alloc_size = ins.offset;
1024 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001025
Chris Mason771ed682008-11-06 22:02:51 -05001026 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001027 em = create_io_em(inode, start, ins.offset, /* len */
1028 start, /* orig_start */
1029 ins.objectid, /* block_start */
1030 ins.offset, /* block_len */
1031 ins.offset, /* orig_block_len */
1032 ram_size, /* ram_bytes */
1033 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001034 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001035 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001036 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001037 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038
Chris Masone6dcd2d2008-07-17 12:53:50 -04001039 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001040 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001041 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001042 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001043
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 if (root->root_key.objectid ==
1045 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1046 ret = btrfs_reloc_clone_csums(inode, start,
1047 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001048 /*
1049 * Only drop cache here, and process as normal.
1050 *
1051 * We must not allow extent_clear_unlock_delalloc()
1052 * at out_unlock label to free meta of this ordered
1053 * extent, as its meta should be freed by
1054 * btrfs_finish_ordered_io().
1055 *
1056 * So we must continue until @start is increased to
1057 * skip current ordered extent.
1058 */
Josef Bacik00361582013-08-14 14:02:47 -04001059 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001060 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1061 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001062 }
1063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001064 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001065
Chris Masonc8b97812008-10-29 14:49:59 -04001066 /* we're not doing compressed IO, don't unlock the first
1067 * page (which the caller expects to stay locked), don't
1068 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001069 *
1070 * Do set the Private2 bit so we know this page was properly
1071 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001072 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001073 page_ops = unlock ? PAGE_UNLOCK : 0;
1074 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001075
Josef Bacikc2790a22013-07-29 11:20:47 -04001076 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001077 start + ram_size - 1,
1078 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001080 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001081 if (disk_num_bytes < cur_alloc_size)
1082 disk_num_bytes = 0;
1083 else
1084 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001085 num_bytes -= cur_alloc_size;
1086 alloc_hint = ins.objectid + ins.offset;
1087 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001088 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001089
1090 /*
1091 * btrfs_reloc_clone_csums() error, since start is increased
1092 * extent_clear_unlock_delalloc() at out_unlock label won't
1093 * free metadata of current ordered extent, we're OK to exit.
1094 */
1095 if (ret)
1096 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001097 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001098out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001099 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001100
Filipe Mananad9f85962014-08-25 10:43:00 +01001101out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001102 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001103out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001104 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001105 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001107 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1108 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001109 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1110 PAGE_END_WRITEBACK;
1111 /*
1112 * If we reserved an extent for our delalloc range (or a subrange) and
1113 * failed to create the respective ordered extent, then it means that
1114 * when we reserved the extent we decremented the extent's size from
1115 * the data space_info's bytes_may_use counter and incremented the
1116 * space_info's bytes_reserved counter by the same amount. We must make
1117 * sure extent_clear_unlock_delalloc() does not try to decrement again
1118 * the data space_info's bytes_may_use counter, therefore we do not pass
1119 * it the flag EXTENT_CLEAR_DATA_RESV.
1120 */
1121 if (extent_reserved) {
1122 extent_clear_unlock_delalloc(inode, start,
1123 start + cur_alloc_size,
1124 start + cur_alloc_size,
1125 locked_page,
1126 clear_bits,
1127 page_ops);
1128 start += cur_alloc_size;
1129 if (start >= end)
1130 goto out;
1131 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001132 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1133 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001134 clear_bits | EXTENT_CLEAR_DATA_RESV,
1135 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001136 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001137}
Chris Masonc8b97812008-10-29 14:49:59 -04001138
Chris Mason771ed682008-11-06 22:02:51 -05001139/*
1140 * work queue call back to started compression on a file and pages
1141 */
1142static noinline void async_cow_start(struct btrfs_work *work)
1143{
1144 struct async_cow *async_cow;
1145 int num_added = 0;
1146 async_cow = container_of(work, struct async_cow, work);
1147
1148 compress_file_range(async_cow->inode, async_cow->locked_page,
1149 async_cow->start, async_cow->end, async_cow,
1150 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001151 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001152 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001153 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001154 }
Chris Mason771ed682008-11-06 22:02:51 -05001155}
1156
1157/*
1158 * work queue call back to submit previously compressed pages
1159 */
1160static noinline void async_cow_submit(struct btrfs_work *work)
1161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001163 struct async_cow *async_cow;
1164 struct btrfs_root *root;
1165 unsigned long nr_pages;
1166
1167 async_cow = container_of(work, struct async_cow, work);
1168
1169 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001170 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001171 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1172 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001173
David Sterbaee863952015-02-16 19:41:40 +01001174 /*
1175 * atomic_sub_return implies a barrier for waitqueue_active
1176 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001177 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001178 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001179 waitqueue_active(&fs_info->async_submit_wait))
1180 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001181
Chris Masond3977122009-01-05 21:25:51 -05001182 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001183 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001184}
Chris Masonc8b97812008-10-29 14:49:59 -04001185
Chris Mason771ed682008-11-06 22:02:51 -05001186static noinline void async_cow_free(struct btrfs_work *work)
1187{
1188 struct async_cow *async_cow;
1189 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001190 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001191 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001192 kfree(async_cow);
1193}
1194
1195static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1196 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001197 unsigned long *nr_written,
1198 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001200 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001201 struct async_cow *async_cow;
1202 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 unsigned long nr_pages;
1204 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001205
Chris Masona3429ab2009-10-08 12:30:20 -04001206 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1207 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001208 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001209 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001210 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001211 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001212 async_cow->root = root;
1213 async_cow->locked_page = locked_page;
1214 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001215 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001216
Wang Shilongf79707b2014-07-17 11:44:09 +08001217 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001218 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001219 cur_end = end;
1220 else
Byongho Leeee221842015-12-15 01:42:10 +09001221 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001222
1223 async_cow->end = cur_end;
1224 INIT_LIST_HEAD(&async_cow->extents);
1225
Liu Bo9e0af232014-08-15 23:36:53 +08001226 btrfs_init_work(&async_cow->work,
1227 btrfs_delalloc_helper,
1228 async_cow_start, async_cow_submit,
1229 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001230
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 nr_pages = (cur_end - start + PAGE_SIZE) >>
1232 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001233 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001235 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001236
Chris Mason771ed682008-11-06 22:02:51 -05001237 *nr_written += nr_pages;
1238 start = cur_end + 1;
1239 }
1240 *page_started = 1;
1241 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001242}
1243
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001244static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001245 u64 bytenr, u64 num_bytes)
1246{
1247 int ret;
1248 struct btrfs_ordered_sum *sums;
1249 LIST_HEAD(list);
1250
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001251 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001252 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 if (ret == 0 && list_empty(&list))
1254 return 0;
1255
1256 while (!list_empty(&list)) {
1257 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1258 list_del(&sums->list);
1259 kfree(sums);
1260 }
1261 return 1;
1262}
1263
Chris Masond352ac62008-09-29 15:18:18 -04001264/*
1265 * when nowcow writeback call back. This checks for snapshots or COW copies
1266 * of the extents that exist in the file, and COWs the file as required.
1267 *
1268 * If no cow copies or snapshots exist, we write directly to the existing
1269 * blocks on disk
1270 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001271static noinline int run_delalloc_nocow(struct inode *inode,
1272 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001273 u64 start, u64 end, int *page_started, int force,
1274 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001277 struct btrfs_root *root = BTRFS_I(inode)->root;
1278 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001279 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001281 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001282 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 u64 cow_start;
1284 u64 cur_offset;
1285 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 u64 disk_bytenr;
1288 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001289 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001290 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001292 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001293 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int nocow;
1295 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001296 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001297 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001298
1299 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001300 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001301 extent_clear_unlock_delalloc(inode, start, end, end,
1302 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001303 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001304 EXTENT_DO_ACCOUNTING |
1305 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 PAGE_CLEAR_DIRTY |
1307 PAGE_SET_WRITEBACK |
1308 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001309 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001310 }
Li Zefan82d59022011-04-20 10:33:24 +08001311
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001312 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001313
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 cow_start = (u64)-1;
1315 cur_offset = start;
1316 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001317 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001319 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001320 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1322 leaf = path->nodes[0];
1323 btrfs_item_key_to_cpu(leaf, &found_key,
1324 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001325 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 found_key.type == BTRFS_EXTENT_DATA_KEY)
1327 path->slots[0]--;
1328 }
1329 check_prev = 0;
1330next_slot:
1331 leaf = path->nodes[0];
1332 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1333 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001334 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001335 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 if (ret > 0)
1337 break;
1338 leaf = path->nodes[0];
1339 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001340
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 nocow = 0;
1342 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001343 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001345
Filipe Manana1d512cb2015-11-09 00:33:58 +00001346 if (found_key.objectid > ino)
1347 break;
1348 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1349 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1350 path->slots[0]++;
1351 goto next_slot;
1352 }
1353 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 found_key.offset > end)
1355 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001356
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 if (found_key.offset > cur_offset) {
1358 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001359 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 goto out_check;
1361 }
Chris Masonc31f8832008-01-08 15:46:31 -05001362
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 fi = btrfs_item_ptr(leaf, path->slots[0],
1364 struct btrfs_file_extent_item);
1365 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001366
Josef Bacikcc95bef2013-04-04 14:31:27 -04001367 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001368 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1369 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001371 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 extent_end = found_key.offset +
1373 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001374 disk_num_bytes =
1375 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 if (extent_end <= start) {
1377 path->slots[0]++;
1378 goto next_slot;
1379 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001380 if (disk_bytenr == 0)
1381 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001382 if (btrfs_file_extent_compression(leaf, fi) ||
1383 btrfs_file_extent_encryption(leaf, fi) ||
1384 btrfs_file_extent_other_encoding(leaf, fi))
1385 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001386 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1387 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001388 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001389 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001390 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001391 found_key.offset -
1392 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001393 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001395 disk_bytenr += cur_offset - found_key.offset;
1396 num_bytes = min(end + 1, extent_end) - cur_offset;
1397 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001398 * if there are pending snapshots for this root,
1399 * we fall into common COW way.
1400 */
1401 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001402 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 if (!err)
1404 goto out_check;
1405 }
1406 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001407 * force cow if csum exists in the range.
1408 * this ensure that csum for a given extent are
1409 * either valid or do not exist.
1410 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001411 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001412 num_bytes)) {
1413 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001414 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001415 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001416 }
1417 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1418 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001419 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001420 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001421 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001422 nocow = 1;
1423 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1424 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001425 btrfs_file_extent_inline_len(leaf,
1426 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001427 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001428 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 } else {
1430 BUG_ON(1);
1431 }
1432out_check:
1433 if (extent_end <= start) {
1434 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001435 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001436 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001437 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001438 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 goto next_slot;
1440 }
1441 if (!nocow) {
1442 if (cow_start == (u64)-1)
1443 cow_start = cur_offset;
1444 cur_offset = extent_end;
1445 if (cur_offset > end)
1446 break;
1447 path->slots[0]++;
1448 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001449 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001450
David Sterbab3b4aa72011-04-21 01:20:15 +02001451 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001453 ret = cow_file_range(inode, locked_page,
1454 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001455 end, page_started, nr_written, 1,
1456 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001457 if (ret) {
1458 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001459 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001460 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001461 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001462 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001463 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001464 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001465 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001466 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001467
Yan Zhengd899e052008-10-30 14:25:28 -04001468 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001469 u64 orig_start = found_key.offset - extent_offset;
1470
1471 em = create_io_em(inode, cur_offset, num_bytes,
1472 orig_start,
1473 disk_bytenr, /* block_start */
1474 num_bytes, /* block_len */
1475 disk_num_bytes, /* orig_block_len */
1476 ram_bytes, BTRFS_COMPRESS_NONE,
1477 BTRFS_ORDERED_PREALLOC);
1478 if (IS_ERR(em)) {
1479 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001480 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001481 if (nocow)
1482 btrfs_dec_nocow_writers(fs_info,
1483 disk_bytenr);
1484 ret = PTR_ERR(em);
1485 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001486 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001487 free_extent_map(em);
1488 }
1489
1490 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001491 type = BTRFS_ORDERED_PREALLOC;
1492 } else {
1493 type = BTRFS_ORDERED_NOCOW;
1494 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001495
1496 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001497 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001498 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001499 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001500 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001501
Yan, Zhengefa56462010-05-16 10:49:59 -04001502 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001503 BTRFS_DATA_RELOC_TREE_OBJECTID)
1504 /*
1505 * Error handled later, as we must prevent
1506 * extent_clear_unlock_delalloc() in error handler
1507 * from freeing metadata of created ordered extent.
1508 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001509 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1510 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001511
Josef Bacikc2790a22013-07-29 11:20:47 -04001512 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001513 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001514 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001515 EXTENT_DELALLOC |
1516 EXTENT_CLEAR_DATA_RESV,
1517 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1518
Wang Shilonge9894fd2014-03-27 11:12:25 +08001519 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001520 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001522
1523 /*
1524 * btrfs_reloc_clone_csums() error, now we're OK to call error
1525 * handler, as metadata for created ordered extent will only
1526 * be freed by btrfs_finish_ordered_io().
1527 */
1528 if (ret)
1529 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001530 if (cur_offset > end)
1531 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001532 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001533 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001534
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001536 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001537 cur_offset = end;
1538 }
1539
Yan Zheng80ff3852008-10-30 14:20:02 -04001540 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001541 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1542 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001543 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 }
1546
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001547error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001548 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001549 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001550 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001551 EXTENT_DELALLOC | EXTENT_DEFRAG |
1552 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1553 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001554 PAGE_SET_WRITEBACK |
1555 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001556 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001557 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001558}
1559
Wang Shilong47059d92014-07-03 18:22:07 +08001560static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1561{
1562
1563 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1564 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1565 return 0;
1566
1567 /*
1568 * @defrag_bytes is a hint value, no spinlock held here,
1569 * if is not zero, it means the file is defragging.
1570 * Force cow if given extent needs to be defragged.
1571 */
1572 if (BTRFS_I(inode)->defrag_bytes &&
1573 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1574 EXTENT_DEFRAG, 0, NULL))
1575 return 1;
1576
1577 return 0;
1578}
1579
Chris Masond352ac62008-09-29 15:18:18 -04001580/*
1581 * extent_io.c call back to do delayed allocation processing
1582 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001583static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001584 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001585 unsigned long *nr_written,
1586 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001587{
Josef Bacikc6100a42017-05-05 11:57:13 -04001588 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001589 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001590 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001591 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001592
Wang Shilong47059d92014-07-03 18:22:07 +08001593 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001594 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001595 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001596 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001597 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001598 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001599 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001600 ret = cow_file_range(inode, locked_page, start, end, end,
1601 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001602 } else {
1603 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1604 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001605 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001606 page_started, nr_written,
1607 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001608 }
Qu Wenruo524272602017-03-08 10:25:52 +08001609 if (ret)
1610 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001611 return ret;
1612}
1613
Josef Bacikc6100a42017-05-05 11:57:13 -04001614static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001615 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001616{
Josef Bacikc6100a42017-05-05 11:57:13 -04001617 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 u64 size;
1619
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001620 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001621 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001622 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001623
Josef Bacikdcab6a32015-02-11 15:08:59 -05001624 size = orig->end - orig->start + 1;
1625 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001626 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 u64 new_size;
1628
1629 /*
Josef Bacikba117212015-03-13 15:01:24 -04001630 * See the explanation in btrfs_merge_extent_hook, the same
1631 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001632 */
1633 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001634 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001635 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001636 num_extents += count_max_extents(new_size);
1637 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 return;
1639 }
1640
Josef Bacik9e0baf62011-07-15 15:16:44 +00001641 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001642 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001643 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644}
1645
1646/*
1647 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1648 * extents so we can keep track of new extents that are just merged onto old
1649 * extents, such as when we are doing sequential writes, so we can properly
1650 * account for the metadata space we'll need.
1651 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001652static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001653 struct extent_state *new,
1654 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001655{
Josef Bacikc6100a42017-05-05 11:57:13 -04001656 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001657 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001658 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659
Josef Bacik9ed74f22009-09-11 16:12:44 -04001660 /* not delalloc, ignore it */
1661 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001662 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663
Josef Bacik8461a3d2015-03-13 15:12:08 -04001664 if (new->start > other->start)
1665 new_size = new->end - other->start + 1;
1666 else
1667 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001668
1669 /* we're not bigger than the max, unreserve the space and go */
1670 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1671 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001672 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673 spin_unlock(&BTRFS_I(inode)->lock);
1674 return;
1675 }
1676
1677 /*
Josef Bacikba117212015-03-13 15:01:24 -04001678 * We have to add up either side to figure out how many extents were
1679 * accounted for before we merged into one big extent. If the number of
1680 * extents we accounted for is <= the amount we need for the new range
1681 * then we can return, otherwise drop. Think of it like this
1682 *
1683 * [ 4k][MAX_SIZE]
1684 *
1685 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1686 * need 2 outstanding extents, on one side we have 1 and the other side
1687 * we have 1 so they are == and we can return. But in this case
1688 *
1689 * [MAX_SIZE+4k][MAX_SIZE+4k]
1690 *
1691 * Each range on their own accounts for 2 extents, but merged together
1692 * they are only 3 extents worth of accounting, so we need to drop in
1693 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001694 */
Josef Bacikba117212015-03-13 15:01:24 -04001695 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001696 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001697 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001698 num_extents += count_max_extents(old_size);
1699 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001700 return;
1701
Josef Bacik9e0baf62011-07-15 15:16:44 +00001702 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001703 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001704 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001705}
1706
Miao Xieeb73c1b2013-05-15 07:48:22 +00001707static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1708 struct inode *inode)
1709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1711
Miao Xieeb73c1b2013-05-15 07:48:22 +00001712 spin_lock(&root->delalloc_lock);
1713 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1714 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1715 &root->delalloc_inodes);
1716 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1717 &BTRFS_I(inode)->runtime_flags);
1718 root->nr_delalloc_inodes++;
1719 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001720 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001721 BUG_ON(!list_empty(&root->delalloc_root));
1722 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 &fs_info->delalloc_roots);
1724 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725 }
1726 }
1727 spin_unlock(&root->delalloc_lock);
1728}
1729
1730static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001731 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001736 if (!list_empty(&inode->delalloc_inodes)) {
1737 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740 root->nr_delalloc_inodes--;
1741 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001742 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001743 BUG_ON(list_empty(&root->delalloc_root));
1744 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 }
1747 }
1748 spin_unlock(&root->delalloc_lock);
1749}
1750
Chris Masond352ac62008-09-29 15:18:18 -04001751/*
1752 * extent_io.c set_bit_hook, used to track delayed allocation
1753 * bytes in this file, and to maintain the list of inodes that
1754 * have pending delalloc work to be done.
1755 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001756static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001757 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001758{
Josef Bacikc6100a42017-05-05 11:57:13 -04001759 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001760
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001761 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1762
Wang Shilong47059d92014-07-03 18:22:07 +08001763 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1764 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001765 /*
1766 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001767 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001768 * bit, which is only set or cleared with irqs on
1769 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001771 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001772 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001773 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001774 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001775
Josef Bacik8b62f872017-10-19 14:15:55 -04001776 spin_lock(&BTRFS_I(inode)->lock);
1777 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1778 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001779
Josef Bacik6a3891c2015-03-16 17:38:52 -04001780 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001781 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001782 return;
1783
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001784 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1785 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001786 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001787 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001788 if (*bits & EXTENT_DEFRAG)
1789 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001790 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001791 &BTRFS_I(inode)->runtime_flags))
1792 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001793 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001794 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001795
1796 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1797 (*bits & EXTENT_DELALLOC_NEW)) {
1798 spin_lock(&BTRFS_I(inode)->lock);
1799 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1800 state->start;
1801 spin_unlock(&BTRFS_I(inode)->lock);
1802 }
Chris Mason291d6732008-01-29 15:55:23 -05001803}
1804
Chris Masond352ac62008-09-29 15:18:18 -04001805/*
1806 * extent_io.c clear_bit_hook, see set_bit_hook for why
1807 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001808static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001809 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001810 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001811{
Josef Bacikc6100a42017-05-05 11:57:13 -04001812 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001813 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001814 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001815 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001816
Filipe Manana4a4b9642017-07-27 19:52:55 +01001817 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1818 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001819 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001820 spin_unlock(&inode->lock);
1821 }
Wang Shilong47059d92014-07-03 18:22:07 +08001822
Chris Mason75eff682008-12-15 15:54:40 -05001823 /*
1824 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001825 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001826 * bit, which is only set or cleared with irqs on
1827 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001828 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001829 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001830 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001831
Josef Bacik8b62f872017-10-19 14:15:55 -04001832 spin_lock(&inode->lock);
1833 btrfs_mod_outstanding_extents(inode, -num_extents);
1834 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001835
Josef Bacikb6d08f02013-09-27 14:57:43 -04001836 /*
1837 * We don't reserve metadata space for space cache inodes so we
1838 * don't need to call dellalloc_release_metadata if there is an
1839 * error.
1840 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001841 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001843 btrfs_delalloc_release_metadata(inode, len);
1844
Josef Bacik6a3891c2015-03-16 17:38:52 -04001845 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001846 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001847 return;
1848
Filipe Mananaa315e682017-03-06 23:04:20 +00001849 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1850 do_list && !(state->state & EXTENT_NORESERVE) &&
1851 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001852 btrfs_free_reserved_data_space_noquota(
1853 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001854 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001855
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001856 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1857 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001858 spin_lock(&inode->lock);
1859 inode->delalloc_bytes -= len;
1860 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001861 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001862 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001863 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001864 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001865 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001866
1867 if ((state->state & EXTENT_DELALLOC_NEW) &&
1868 (*bits & EXTENT_DELALLOC_NEW)) {
1869 spin_lock(&inode->lock);
1870 ASSERT(inode->new_delalloc_bytes >= len);
1871 inode->new_delalloc_bytes -= len;
1872 spin_unlock(&inode->lock);
1873 }
Chris Mason291d6732008-01-29 15:55:23 -05001874}
1875
Chris Masond352ac62008-09-29 15:18:18 -04001876/*
1877 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1878 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001879 *
1880 * return 1 if page cannot be merged to bio
1881 * return 0 if page can be merged to bio
1882 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001883 */
Mike Christie81a75f672016-06-05 14:31:54 -05001884int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001885 size_t size, struct bio *bio,
1886 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 struct inode *inode = page->mapping->host;
1889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001890 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001891 u64 length = 0;
1892 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001893 int ret;
1894
Chris Mason771ed682008-11-06 22:02:51 -05001895 if (bio_flags & EXTENT_BIO_COMPRESSED)
1896 return 0;
1897
Kent Overstreet4f024f32013-10-11 15:44:27 -07001898 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001899 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001900 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1901 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001902 if (ret < 0)
1903 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001904 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001905 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001906 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001907}
1908
Chris Masond352ac62008-09-29 15:18:18 -04001909/*
1910 * in order to insert checksums into the metadata in large chunks,
1911 * we wait until bio submission time. All the pages in the bio are
1912 * checksummed and sums are attached onto the ordered extent record.
1913 *
1914 * At IO completion time the cums attached on the ordered extent record
1915 * are inserted into the btree
1916 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001917static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001918 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001919 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001920{
Josef Bacikc6100a42017-05-05 11:57:13 -04001921 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001922 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001924 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001925 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001926 return 0;
1927}
Chris Masone0156402008-04-16 11:15:20 -04001928
Chris Mason4a69a412008-11-06 22:03:00 -05001929/*
1930 * in order to insert checksums into the metadata in large chunks,
1931 * we wait until bio submission time. All the pages in the bio are
1932 * checksummed and sums are attached onto the ordered extent record.
1933 *
1934 * At IO completion time the cums attached on the ordered extent record
1935 * are inserted into the btree
1936 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001937static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001938 int mirror_num, unsigned long bio_flags,
1939 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001940{
Josef Bacikc6100a42017-05-05 11:57:13 -04001941 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001942 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001943 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001944
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001945 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001946 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001947 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001948 bio_endio(bio);
1949 }
Stefan Behrens61891922012-11-05 18:51:52 +01001950 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001951}
1952
Chris Masond352ac62008-09-29 15:18:18 -04001953/*
Chris Masoncad321a2008-12-17 14:51:42 -05001954 * extent_io.c submission hook. This does the right thing for csum calculation
1955 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001956 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001957static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001958 int mirror_num, unsigned long bio_flags,
1959 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001960{
Josef Bacikc6100a42017-05-05 11:57:13 -04001961 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001963 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301964 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001965 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001966 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001967 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001968
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001969 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001970
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001971 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301972 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001973
Mike Christie37226b22016-06-05 14:31:52 -05001974 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001975 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001976 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001977 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001978
Chris Masond20f7042008-12-08 16:58:54 -05001979 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001980 ret = btrfs_submit_compressed_read(inode, bio,
1981 mirror_num,
1982 bio_flags);
1983 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001984 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001985 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001986 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001987 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001988 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001989 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001990 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001991 /* csum items have already been cloned */
1992 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1993 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001994 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001995 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1996 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001997 __btrfs_submit_bio_start,
1998 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001999 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002000 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002001 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002002 if (ret)
2003 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002004 }
2005
Chris Mason0b86a832008-03-24 15:01:56 -04002006mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002007 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002008
2009out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002010 if (ret) {
2011 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002012 bio_endio(bio);
2013 }
Stefan Behrens61891922012-11-05 18:51:52 +01002014 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002015}
Chris Mason6885f302008-02-20 16:11:05 -05002016
Chris Masond352ac62008-09-29 15:18:18 -04002017/*
2018 * given a list of ordered sums record them in the inode. This happens
2019 * at IO completion time based on sums calculated at bio submission time.
2020 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002021static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002022 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002024 struct btrfs_ordered_sum *sum;
2025
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002026 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002027 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002028 btrfs_csum_file_blocks(trans,
2029 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002030 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002031 }
2032 return 0;
2033}
2034
Josef Bacik2ac55d42010-02-03 19:33:23 +00002035int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002036 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002037 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002038{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002039 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002040 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002041 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002042}
2043
Chris Masond352ac62008-09-29 15:18:18 -04002044/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002045struct btrfs_writepage_fixup {
2046 struct page *page;
2047 struct btrfs_work work;
2048};
2049
Christoph Hellwigb2950862008-12-02 09:54:17 -05002050static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002051{
2052 struct btrfs_writepage_fixup *fixup;
2053 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002054 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002055 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002056 struct page *page;
2057 struct inode *inode;
2058 u64 page_start;
2059 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002060 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002061
2062 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2063 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002064again:
Chris Mason247e7432008-07-17 12:53:51 -04002065 lock_page(page);
2066 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2067 ClearPageChecked(page);
2068 goto out_page;
2069 }
2070
2071 inode = page->mapping->host;
2072 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002074
David Sterbaff13db42015-12-03 14:30:40 +01002075 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002076 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002077
2078 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002079 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002080 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002081
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002082 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002083 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002084 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002085 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2086 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002087 unlock_page(page);
2088 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002089 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002090 goto again;
2091 }
Chris Mason247e7432008-07-17 12:53:51 -04002092
Qu Wenruo364ecf32017-02-27 15:10:38 +08002093 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002094 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002095 if (ret) {
2096 mapping_set_error(page->mapping, ret);
2097 end_extent_writepage(page, ret, page_start, page_end);
2098 ClearPageChecked(page);
2099 goto out;
2100 }
2101
Filipe Mananae3b8a482017-11-04 00:16:59 +00002102 btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002103 0);
Chris Mason247e7432008-07-17 12:53:51 -04002104 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002105 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002106 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002107out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002108 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2109 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002110out_page:
2111 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002112 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002113 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002114 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002115}
2116
2117/*
2118 * There are a few paths in the higher layers of the kernel that directly
2119 * set the page dirty bit without asking the filesystem if it is a
2120 * good idea. This causes problems because we want to make sure COW
2121 * properly happens and the data=ordered rules are followed.
2122 *
Chris Masonc8b97812008-10-29 14:49:59 -04002123 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002124 * hasn't been properly setup for IO. We kick off an async process
2125 * to fix it up. The async helper will wait for ordered extents, set
2126 * the delalloc bit and make it safe to write the page.
2127 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002128static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002129{
2130 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002131 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002132 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002133
Chris Mason8b62b722009-09-02 16:53:46 -04002134 /* this page is properly in the ordered list */
2135 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002136 return 0;
2137
2138 if (PageChecked(page))
2139 return -EAGAIN;
2140
2141 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2142 if (!fixup)
2143 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002144
Chris Mason247e7432008-07-17 12:53:51 -04002145 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002146 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002147 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2148 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002149 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002150 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002151 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002152}
2153
Yan Zhengd899e052008-10-30 14:25:28 -04002154static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2155 struct inode *inode, u64 file_pos,
2156 u64 disk_bytenr, u64 disk_num_bytes,
2157 u64 num_bytes, u64 ram_bytes,
2158 u8 compression, u8 encryption,
2159 u16 other_encoding, int extent_type)
2160{
2161 struct btrfs_root *root = BTRFS_I(inode)->root;
2162 struct btrfs_file_extent_item *fi;
2163 struct btrfs_path *path;
2164 struct extent_buffer *leaf;
2165 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002166 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002167 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002168 int ret;
2169
2170 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002171 if (!path)
2172 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002173
Chris Masona1ed8352009-09-11 12:27:37 -04002174 /*
2175 * we may be replacing one extent in the tree with another.
2176 * The new extent is pinned in the extent map, and we don't want
2177 * to drop it from the cache until it is completely in the btree.
2178 *
2179 * So, tell btrfs_drop_extents to leave this extent in the cache.
2180 * the caller is expected to unpin it and allow it to be merged
2181 * with the others.
2182 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002183 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2184 file_pos + num_bytes, NULL, 0,
2185 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002186 if (ret)
2187 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002188
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002189 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002190 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002191 ins.offset = file_pos;
2192 ins.type = BTRFS_EXTENT_DATA_KEY;
2193
2194 path->leave_spinning = 1;
2195 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2196 sizeof(*fi));
2197 if (ret)
2198 goto out;
2199 }
Yan Zhengd899e052008-10-30 14:25:28 -04002200 leaf = path->nodes[0];
2201 fi = btrfs_item_ptr(leaf, path->slots[0],
2202 struct btrfs_file_extent_item);
2203 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2204 btrfs_set_file_extent_type(leaf, fi, extent_type);
2205 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2206 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2207 btrfs_set_file_extent_offset(leaf, fi, 0);
2208 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2209 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2210 btrfs_set_file_extent_compression(leaf, fi, compression);
2211 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2212 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002213
Yan Zhengd899e052008-10-30 14:25:28 -04002214 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002215 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002216
2217 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002218
2219 ins.objectid = disk_bytenr;
2220 ins.offset = disk_num_bytes;
2221 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002222
Qu Wenruo297d7502015-09-08 17:08:37 +08002223 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002224 * Release the reserved range from inode dirty range map, as it is
2225 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002226 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002227 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2228 if (ret < 0)
2229 goto out;
2230 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002231 ret = btrfs_alloc_reserved_file_extent(trans, root,
2232 btrfs_ino(BTRFS_I(inode)),
2233 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234out:
Yan Zhengd899e052008-10-30 14:25:28 -04002235 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002236
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002237 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002238}
2239
Liu Bo38c227d2013-01-29 03:18:40 +00002240/* snapshot-aware defrag */
2241struct sa_defrag_extent_backref {
2242 struct rb_node node;
2243 struct old_sa_defrag_extent *old;
2244 u64 root_id;
2245 u64 inum;
2246 u64 file_pos;
2247 u64 extent_offset;
2248 u64 num_bytes;
2249 u64 generation;
2250};
2251
2252struct old_sa_defrag_extent {
2253 struct list_head list;
2254 struct new_sa_defrag_extent *new;
2255
2256 u64 extent_offset;
2257 u64 bytenr;
2258 u64 offset;
2259 u64 len;
2260 int count;
2261};
2262
2263struct new_sa_defrag_extent {
2264 struct rb_root root;
2265 struct list_head head;
2266 struct btrfs_path *path;
2267 struct inode *inode;
2268 u64 file_pos;
2269 u64 len;
2270 u64 bytenr;
2271 u64 disk_len;
2272 u8 compress_type;
2273};
2274
2275static int backref_comp(struct sa_defrag_extent_backref *b1,
2276 struct sa_defrag_extent_backref *b2)
2277{
2278 if (b1->root_id < b2->root_id)
2279 return -1;
2280 else if (b1->root_id > b2->root_id)
2281 return 1;
2282
2283 if (b1->inum < b2->inum)
2284 return -1;
2285 else if (b1->inum > b2->inum)
2286 return 1;
2287
2288 if (b1->file_pos < b2->file_pos)
2289 return -1;
2290 else if (b1->file_pos > b2->file_pos)
2291 return 1;
2292
2293 /*
2294 * [------------------------------] ===> (a range of space)
2295 * |<--->| |<---->| =============> (fs/file tree A)
2296 * |<---------------------------->| ===> (fs/file tree B)
2297 *
2298 * A range of space can refer to two file extents in one tree while
2299 * refer to only one file extent in another tree.
2300 *
2301 * So we may process a disk offset more than one time(two extents in A)
2302 * and locate at the same extent(one extent in B), then insert two same
2303 * backrefs(both refer to the extent in B).
2304 */
2305 return 0;
2306}
2307
2308static void backref_insert(struct rb_root *root,
2309 struct sa_defrag_extent_backref *backref)
2310{
2311 struct rb_node **p = &root->rb_node;
2312 struct rb_node *parent = NULL;
2313 struct sa_defrag_extent_backref *entry;
2314 int ret;
2315
2316 while (*p) {
2317 parent = *p;
2318 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2319
2320 ret = backref_comp(backref, entry);
2321 if (ret < 0)
2322 p = &(*p)->rb_left;
2323 else
2324 p = &(*p)->rb_right;
2325 }
2326
2327 rb_link_node(&backref->node, parent, p);
2328 rb_insert_color(&backref->node, root);
2329}
2330
2331/*
2332 * Note the backref might has changed, and in this case we just return 0.
2333 */
2334static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2335 void *ctx)
2336{
2337 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002338 struct old_sa_defrag_extent *old = ctx;
2339 struct new_sa_defrag_extent *new = old->new;
2340 struct btrfs_path *path = new->path;
2341 struct btrfs_key key;
2342 struct btrfs_root *root;
2343 struct sa_defrag_extent_backref *backref;
2344 struct extent_buffer *leaf;
2345 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002346 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002347 int slot;
2348 int ret;
2349 u64 extent_offset;
2350 u64 num_bytes;
2351
2352 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002353 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002354 return 0;
2355
2356 key.objectid = root_id;
2357 key.type = BTRFS_ROOT_ITEM_KEY;
2358 key.offset = (u64)-1;
2359
Liu Bo38c227d2013-01-29 03:18:40 +00002360 root = btrfs_read_fs_root_no_name(fs_info, &key);
2361 if (IS_ERR(root)) {
2362 if (PTR_ERR(root) == -ENOENT)
2363 return 0;
2364 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002365 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002366 inum, offset, root_id);
2367 return PTR_ERR(root);
2368 }
2369
2370 key.objectid = inum;
2371 key.type = BTRFS_EXTENT_DATA_KEY;
2372 if (offset > (u64)-1 << 32)
2373 key.offset = 0;
2374 else
2375 key.offset = offset;
2376
2377 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302378 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002379 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002380 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002381
2382 while (1) {
2383 cond_resched();
2384
2385 leaf = path->nodes[0];
2386 slot = path->slots[0];
2387
2388 if (slot >= btrfs_header_nritems(leaf)) {
2389 ret = btrfs_next_leaf(root, path);
2390 if (ret < 0) {
2391 goto out;
2392 } else if (ret > 0) {
2393 ret = 0;
2394 goto out;
2395 }
2396 continue;
2397 }
2398
2399 path->slots[0]++;
2400
2401 btrfs_item_key_to_cpu(leaf, &key, slot);
2402
2403 if (key.objectid > inum)
2404 goto out;
2405
2406 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2407 continue;
2408
2409 extent = btrfs_item_ptr(leaf, slot,
2410 struct btrfs_file_extent_item);
2411
2412 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2413 continue;
2414
Liu Boe68afa42013-07-01 22:13:26 +08002415 /*
2416 * 'offset' refers to the exact key.offset,
2417 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2418 * (key.offset - extent_offset).
2419 */
2420 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002421 continue;
2422
Liu Boe68afa42013-07-01 22:13:26 +08002423 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002424 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002425
Liu Bo38c227d2013-01-29 03:18:40 +00002426 if (extent_offset >= old->extent_offset + old->offset +
2427 old->len || extent_offset + num_bytes <=
2428 old->extent_offset + old->offset)
2429 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002430 break;
2431 }
2432
2433 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2434 if (!backref) {
2435 ret = -ENOENT;
2436 goto out;
2437 }
2438
2439 backref->root_id = root_id;
2440 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002441 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002442 backref->num_bytes = num_bytes;
2443 backref->extent_offset = extent_offset;
2444 backref->generation = btrfs_file_extent_generation(leaf, extent);
2445 backref->old = old;
2446 backref_insert(&new->root, backref);
2447 old->count++;
2448out:
2449 btrfs_release_path(path);
2450 WARN_ON(ret);
2451 return ret;
2452}
2453
2454static noinline bool record_extent_backrefs(struct btrfs_path *path,
2455 struct new_sa_defrag_extent *new)
2456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002458 struct old_sa_defrag_extent *old, *tmp;
2459 int ret;
2460
2461 new->path = path;
2462
2463 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002464 ret = iterate_inodes_from_logical(old->bytenr +
2465 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002466 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002467 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002468 if (ret < 0 && ret != -ENOENT)
2469 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002470
2471 /* no backref to be processed for this extent */
2472 if (!old->count) {
2473 list_del(&old->list);
2474 kfree(old);
2475 }
2476 }
2477
2478 if (list_empty(&new->head))
2479 return false;
2480
2481 return true;
2482}
2483
2484static int relink_is_mergable(struct extent_buffer *leaf,
2485 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002486 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002487{
Liu Bo116e0022013-08-02 16:30:40 +08002488 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002489 return 0;
2490
2491 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2492 return 0;
2493
Liu Bo116e0022013-08-02 16:30:40 +08002494 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2495 return 0;
2496
2497 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002498 btrfs_file_extent_other_encoding(leaf, fi))
2499 return 0;
2500
2501 return 1;
2502}
2503
2504/*
2505 * Note the backref might has changed, and in this case we just return 0.
2506 */
2507static noinline int relink_extent_backref(struct btrfs_path *path,
2508 struct sa_defrag_extent_backref *prev,
2509 struct sa_defrag_extent_backref *backref)
2510{
2511 struct btrfs_file_extent_item *extent;
2512 struct btrfs_file_extent_item *item;
2513 struct btrfs_ordered_extent *ordered;
2514 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002515 struct btrfs_root *root;
2516 struct btrfs_key key;
2517 struct extent_buffer *leaf;
2518 struct old_sa_defrag_extent *old = backref->old;
2519 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002520 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002521 struct inode *inode;
2522 struct extent_state *cached = NULL;
2523 int ret = 0;
2524 u64 start;
2525 u64 len;
2526 u64 lock_start;
2527 u64 lock_end;
2528 bool merge = false;
2529 int index;
2530
2531 if (prev && prev->root_id == backref->root_id &&
2532 prev->inum == backref->inum &&
2533 prev->file_pos + prev->num_bytes == backref->file_pos)
2534 merge = true;
2535
2536 /* step 1: get root */
2537 key.objectid = backref->root_id;
2538 key.type = BTRFS_ROOT_ITEM_KEY;
2539 key.offset = (u64)-1;
2540
Liu Bo38c227d2013-01-29 03:18:40 +00002541 index = srcu_read_lock(&fs_info->subvol_srcu);
2542
2543 root = btrfs_read_fs_root_no_name(fs_info, &key);
2544 if (IS_ERR(root)) {
2545 srcu_read_unlock(&fs_info->subvol_srcu, index);
2546 if (PTR_ERR(root) == -ENOENT)
2547 return 0;
2548 return PTR_ERR(root);
2549 }
Liu Bo38c227d2013-01-29 03:18:40 +00002550
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002551 if (btrfs_root_readonly(root)) {
2552 srcu_read_unlock(&fs_info->subvol_srcu, index);
2553 return 0;
2554 }
2555
Liu Bo38c227d2013-01-29 03:18:40 +00002556 /* step 2: get inode */
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_INODE_ITEM_KEY;
2559 key.offset = 0;
2560
2561 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2562 if (IS_ERR(inode)) {
2563 srcu_read_unlock(&fs_info->subvol_srcu, index);
2564 return 0;
2565 }
2566
2567 srcu_read_unlock(&fs_info->subvol_srcu, index);
2568
2569 /* step 3: relink backref */
2570 lock_start = backref->file_pos;
2571 lock_end = backref->file_pos + backref->num_bytes - 1;
2572 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002573 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002574
2575 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2576 if (ordered) {
2577 btrfs_put_ordered_extent(ordered);
2578 goto out_unlock;
2579 }
2580
2581 trans = btrfs_join_transaction(root);
2582 if (IS_ERR(trans)) {
2583 ret = PTR_ERR(trans);
2584 goto out_unlock;
2585 }
2586
2587 key.objectid = backref->inum;
2588 key.type = BTRFS_EXTENT_DATA_KEY;
2589 key.offset = backref->file_pos;
2590
2591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2592 if (ret < 0) {
2593 goto out_free_path;
2594 } else if (ret > 0) {
2595 ret = 0;
2596 goto out_free_path;
2597 }
2598
2599 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2600 struct btrfs_file_extent_item);
2601
2602 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2603 backref->generation)
2604 goto out_free_path;
2605
2606 btrfs_release_path(path);
2607
2608 start = backref->file_pos;
2609 if (backref->extent_offset < old->extent_offset + old->offset)
2610 start += old->extent_offset + old->offset -
2611 backref->extent_offset;
2612
2613 len = min(backref->extent_offset + backref->num_bytes,
2614 old->extent_offset + old->offset + old->len);
2615 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2616
2617 ret = btrfs_drop_extents(trans, root, inode, start,
2618 start + len, 1);
2619 if (ret)
2620 goto out_free_path;
2621again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002622 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002623 key.type = BTRFS_EXTENT_DATA_KEY;
2624 key.offset = start;
2625
Liu Boa09a0a72013-03-11 09:20:58 +00002626 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002627 if (merge) {
2628 struct btrfs_file_extent_item *fi;
2629 u64 extent_len;
2630 struct btrfs_key found_key;
2631
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002632 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002633 if (ret < 0)
2634 goto out_free_path;
2635
2636 path->slots[0]--;
2637 leaf = path->nodes[0];
2638 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2639
2640 fi = btrfs_item_ptr(leaf, path->slots[0],
2641 struct btrfs_file_extent_item);
2642 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2643
Liu Bo116e0022013-08-02 16:30:40 +08002644 if (extent_len + found_key.offset == start &&
2645 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002646 btrfs_set_file_extent_num_bytes(leaf, fi,
2647 extent_len + len);
2648 btrfs_mark_buffer_dirty(leaf);
2649 inode_add_bytes(inode, len);
2650
2651 ret = 1;
2652 goto out_free_path;
2653 } else {
2654 merge = false;
2655 btrfs_release_path(path);
2656 goto again;
2657 }
2658 }
2659
2660 ret = btrfs_insert_empty_item(trans, root, path, &key,
2661 sizeof(*extent));
2662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002663 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002664 goto out_free_path;
2665 }
2666
2667 leaf = path->nodes[0];
2668 item = btrfs_item_ptr(leaf, path->slots[0],
2669 struct btrfs_file_extent_item);
2670 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2671 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2672 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2673 btrfs_set_file_extent_num_bytes(leaf, item, len);
2674 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2675 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2676 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2677 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2678 btrfs_set_file_extent_encryption(leaf, item, 0);
2679 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2680
2681 btrfs_mark_buffer_dirty(leaf);
2682 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002683 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002684
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002685 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002686 new->disk_len, 0,
2687 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002688 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002691 goto out_free_path;
2692 }
2693
2694 ret = 1;
2695out_free_path:
2696 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002697 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002698 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002699out_unlock:
2700 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2701 &cached, GFP_NOFS);
2702 iput(inode);
2703 return ret;
2704}
2705
Liu Bo6f519562013-10-29 10:45:05 +08002706static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2707{
2708 struct old_sa_defrag_extent *old, *tmp;
2709
2710 if (!new)
2711 return;
2712
2713 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002714 kfree(old);
2715 }
2716 kfree(new);
2717}
2718
Liu Bo38c227d2013-01-29 03:18:40 +00002719static void relink_file_extents(struct new_sa_defrag_extent *new)
2720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002721 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002723 struct sa_defrag_extent_backref *backref;
2724 struct sa_defrag_extent_backref *prev = NULL;
2725 struct inode *inode;
2726 struct btrfs_root *root;
2727 struct rb_node *node;
2728 int ret;
2729
2730 inode = new->inode;
2731 root = BTRFS_I(inode)->root;
2732
2733 path = btrfs_alloc_path();
2734 if (!path)
2735 return;
2736
2737 if (!record_extent_backrefs(path, new)) {
2738 btrfs_free_path(path);
2739 goto out;
2740 }
2741 btrfs_release_path(path);
2742
2743 while (1) {
2744 node = rb_first(&new->root);
2745 if (!node)
2746 break;
2747 rb_erase(node, &new->root);
2748
2749 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2750
2751 ret = relink_extent_backref(path, prev, backref);
2752 WARN_ON(ret < 0);
2753
2754 kfree(prev);
2755
2756 if (ret == 1)
2757 prev = backref;
2758 else
2759 prev = NULL;
2760 cond_resched();
2761 }
2762 kfree(prev);
2763
2764 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002765out:
Liu Bo6f519562013-10-29 10:45:05 +08002766 free_sa_defrag_extent(new);
2767
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002768 atomic_dec(&fs_info->defrag_running);
2769 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002770}
2771
2772static struct new_sa_defrag_extent *
2773record_old_file_extents(struct inode *inode,
2774 struct btrfs_ordered_extent *ordered)
2775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002776 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002777 struct btrfs_root *root = BTRFS_I(inode)->root;
2778 struct btrfs_path *path;
2779 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002780 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002781 struct new_sa_defrag_extent *new;
2782 int ret;
2783
2784 new = kmalloc(sizeof(*new), GFP_NOFS);
2785 if (!new)
2786 return NULL;
2787
2788 new->inode = inode;
2789 new->file_pos = ordered->file_offset;
2790 new->len = ordered->len;
2791 new->bytenr = ordered->start;
2792 new->disk_len = ordered->disk_len;
2793 new->compress_type = ordered->compress_type;
2794 new->root = RB_ROOT;
2795 INIT_LIST_HEAD(&new->head);
2796
2797 path = btrfs_alloc_path();
2798 if (!path)
2799 goto out_kfree;
2800
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002801 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002802 key.type = BTRFS_EXTENT_DATA_KEY;
2803 key.offset = new->file_pos;
2804
2805 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2806 if (ret < 0)
2807 goto out_free_path;
2808 if (ret > 0 && path->slots[0] > 0)
2809 path->slots[0]--;
2810
2811 /* find out all the old extents for the file range */
2812 while (1) {
2813 struct btrfs_file_extent_item *extent;
2814 struct extent_buffer *l;
2815 int slot;
2816 u64 num_bytes;
2817 u64 offset;
2818 u64 end;
2819 u64 disk_bytenr;
2820 u64 extent_offset;
2821
2822 l = path->nodes[0];
2823 slot = path->slots[0];
2824
2825 if (slot >= btrfs_header_nritems(l)) {
2826 ret = btrfs_next_leaf(root, path);
2827 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002828 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002829 else if (ret > 0)
2830 break;
2831 continue;
2832 }
2833
2834 btrfs_item_key_to_cpu(l, &key, slot);
2835
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002836 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002837 break;
2838 if (key.type != BTRFS_EXTENT_DATA_KEY)
2839 break;
2840 if (key.offset >= new->file_pos + new->len)
2841 break;
2842
2843 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2844
2845 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2846 if (key.offset + num_bytes < new->file_pos)
2847 goto next;
2848
2849 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2850 if (!disk_bytenr)
2851 goto next;
2852
2853 extent_offset = btrfs_file_extent_offset(l, extent);
2854
2855 old = kmalloc(sizeof(*old), GFP_NOFS);
2856 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002857 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002858
2859 offset = max(new->file_pos, key.offset);
2860 end = min(new->file_pos + new->len, key.offset + num_bytes);
2861
2862 old->bytenr = disk_bytenr;
2863 old->extent_offset = extent_offset;
2864 old->offset = offset - key.offset;
2865 old->len = end - offset;
2866 old->new = new;
2867 old->count = 0;
2868 list_add_tail(&old->list, &new->head);
2869next:
2870 path->slots[0]++;
2871 cond_resched();
2872 }
2873
2874 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002876
2877 return new;
2878
Liu Bo38c227d2013-01-29 03:18:40 +00002879out_free_path:
2880 btrfs_free_path(path);
2881out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002882 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002883 return NULL;
2884}
2885
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002886static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002887 u64 start, u64 len)
2888{
2889 struct btrfs_block_group_cache *cache;
2890
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002892 ASSERT(cache);
2893
2894 spin_lock(&cache->lock);
2895 cache->delalloc_bytes -= len;
2896 spin_unlock(&cache->lock);
2897
2898 btrfs_put_block_group(cache);
2899}
2900
Chris Masond352ac62008-09-29 15:18:18 -04002901/* as ordered data IO finishes, this gets called so we can finish
2902 * an ordered extent if the range of bytes in the file it covers are
2903 * fully written.
2904 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002905static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002906{
Josef Bacik5fd02042012-05-02 14:00:54 -04002907 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002908 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002909 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002910 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002911 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002912 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002913 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002914 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002915 int ret = 0;
2916 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002917 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002918 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002919 bool range_locked = false;
2920 bool clear_new_delalloc_bytes = false;
2921
2922 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2923 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2924 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2925 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002927 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002928
Josef Bacik5fd02042012-05-02 14:00:54 -04002929 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2930 ret = -EIO;
2931 goto out;
2932 }
2933
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002934 btrfs_free_io_failure_record(BTRFS_I(inode),
2935 ordered_extent->file_offset,
2936 ordered_extent->file_offset +
2937 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002938
Josef Bacik77cef2e2013-08-29 13:57:21 -04002939 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2940 truncated = true;
2941 logical_len = ordered_extent->truncated_len;
2942 /* Truncated the entire extent, don't bother adding */
2943 if (!logical_len)
2944 goto out;
2945 }
2946
Yan, Zhengc2167752009-11-12 09:34:21 +00002947 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002948 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002949
2950 /*
2951 * For mwrite(mmap + memset to write) case, we still reserve
2952 * space for NOCOW range.
2953 * As NOCOW won't cause a new delayed ref, just free the space
2954 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002955 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002956 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002957 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2958 if (nolock)
2959 trans = btrfs_join_transaction_nolock(root);
2960 else
2961 trans = btrfs_join_transaction(root);
2962 if (IS_ERR(trans)) {
2963 ret = PTR_ERR(trans);
2964 trans = NULL;
2965 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002966 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002967 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002968 ret = btrfs_update_inode_fallback(trans, root, inode);
2969 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002970 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002971 goto out;
2972 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002974 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002975 lock_extent_bits(io_tree, ordered_extent->file_offset,
2976 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002977 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978
Liu Bo38c227d2013-01-29 03:18:40 +00002979 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2980 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002981 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002982 if (ret) {
2983 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002984 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002985 /* the inode is shared */
2986 new = record_old_file_extents(inode, ordered_extent);
2987
2988 clear_extent_bit(io_tree, ordered_extent->file_offset,
2989 ordered_extent->file_offset + ordered_extent->len - 1,
2990 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2991 }
2992
Josef Bacik0cb59c92010-07-02 12:14:14 -04002993 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002994 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002995 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002996 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002997 if (IS_ERR(trans)) {
2998 ret = PTR_ERR(trans);
2999 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003000 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003001 }
Chris Masona79b7d42014-05-22 16:18:52 -07003002
Josef Bacik69fe2d72017-10-19 14:15:57 -04003003 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003004
Chris Masonc8b97812008-10-29 14:49:59 -04003005 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003006 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003007 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003008 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003009 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3010 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003011 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003012 ordered_extent->file_offset,
3013 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003014 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003015 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003016 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003017 ret = insert_reserved_file_extent(trans, inode,
3018 ordered_extent->file_offset,
3019 ordered_extent->start,
3020 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003021 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003022 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003023 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003024 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003025 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003026 ordered_extent->start,
3027 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003028 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003029 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3030 ordered_extent->file_offset, ordered_extent->len,
3031 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003033 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003034 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003035 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003036
David Sterbadf9f6282017-02-10 19:35:37 +01003037 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003038
Josef Bacik6c760c02012-11-09 10:53:21 -05003039 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3040 ret = btrfs_update_inode_fallback(trans, root, inode);
3041 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003042 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003043 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003044 }
3045 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003046out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003047 if (range_locked || clear_new_delalloc_bytes) {
3048 unsigned int clear_bits = 0;
3049
3050 if (range_locked)
3051 clear_bits |= EXTENT_LOCKED;
3052 if (clear_new_delalloc_bytes)
3053 clear_bits |= EXTENT_DELALLOC_NEW;
3054 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3055 ordered_extent->file_offset,
3056 ordered_extent->file_offset +
3057 ordered_extent->len - 1,
3058 clear_bits,
3059 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3060 0, &cached_state, GFP_NOFS);
3061 }
3062
Miao Xiea698d0752012-09-20 01:51:59 -06003063 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003064 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003065
Josef Bacik77cef2e2013-08-29 13:57:21 -04003066 if (ret || truncated) {
3067 u64 start, end;
3068
3069 if (truncated)
3070 start = ordered_extent->file_offset + logical_len;
3071 else
3072 start = ordered_extent->file_offset;
3073 end = ordered_extent->file_offset + ordered_extent->len - 1;
3074 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3075
3076 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003077 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003078
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003079 /*
3080 * If the ordered extent had an IOERR or something else went
3081 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003082 * back to the allocator. We only free the extent in the
3083 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003084 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003085 if ((ret || !logical_len) &&
3086 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003087 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003088 btrfs_free_reserved_extent(fs_info,
3089 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003090 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003091 }
3092
3093
Josef Bacik5fd02042012-05-02 14:00:54 -04003094 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003095 * This needs to be done to make sure anybody waiting knows we are done
3096 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003097 */
3098 btrfs_remove_ordered_extent(inode, ordered_extent);
3099
Liu Bo38c227d2013-01-29 03:18:40 +00003100 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003101 if (new) {
3102 if (ret) {
3103 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003104 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003105 } else {
3106 relink_file_extents(new);
3107 }
3108 }
Liu Bo38c227d2013-01-29 03:18:40 +00003109
Chris Masone6dcd2d2008-07-17 12:53:50 -04003110 /* once for us */
3111 btrfs_put_ordered_extent(ordered_extent);
3112 /* once for the tree */
3113 btrfs_put_ordered_extent(ordered_extent);
3114
Josef Bacik5fd02042012-05-02 14:00:54 -04003115 return ret;
3116}
3117
3118static void finish_ordered_fn(struct btrfs_work *work)
3119{
3120 struct btrfs_ordered_extent *ordered_extent;
3121 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3122 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123}
3124
David Sterbac3988d62017-02-17 15:18:32 +01003125static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003126 struct extent_state *state, int uptodate)
3127{
Josef Bacik5fd02042012-05-02 14:00:54 -04003128 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003130 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003131 struct btrfs_workqueue *wq;
3132 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003133
liubo1abe9b82011-03-24 11:18:59 +00003134 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3135
Chris Mason8b62b722009-09-02 16:53:46 -04003136 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003137 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3138 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003139 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003140
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003141 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003143 func = btrfs_freespace_write_helper;
3144 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003145 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003146 func = btrfs_endio_write_helper;
3147 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003148
Liu Bo9e0af232014-08-15 23:36:53 +08003149 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3150 NULL);
3151 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003152}
3153
Miao Xiedc380ae2014-09-12 18:43:55 +08003154static int __readpage_endio_check(struct inode *inode,
3155 struct btrfs_io_bio *io_bio,
3156 int icsum, struct page *page,
3157 int pgoff, u64 start, size_t len)
3158{
3159 char *kaddr;
3160 u32 csum_expected;
3161 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003162
3163 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3164
3165 kaddr = kmap_atomic(page);
3166 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003167 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003168 if (csum != csum_expected)
3169 goto zeroit;
3170
3171 kunmap_atomic(kaddr);
3172 return 0;
3173zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003174 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003175 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003176 memset(kaddr + pgoff, 1, len);
3177 flush_dcache_page(page);
3178 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003179 return -EIO;
3180}
3181
Chris Masond352ac62008-09-29 15:18:18 -04003182/*
Chris Masond352ac62008-09-29 15:18:18 -04003183 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003184 * if there's a match, we allow the bio to finish. If not, the code in
3185 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003186 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003187static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3188 u64 phy_offset, struct page *page,
3189 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003190{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003191 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003192 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003193 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003194 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003195
Chris Masond20f7042008-12-08 16:58:54 -05003196 if (PageChecked(page)) {
3197 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003198 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003199 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003200
3201 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003202 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003203
Yan Zheng17d217f2008-12-12 10:03:38 -05003204 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003205 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003206 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003207 return 0;
3208 }
3209
Miao Xiefacc8a222013-07-25 19:22:34 +08003210 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003211 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3212 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003213}
Chris Masonb888db22007-08-27 16:49:44 -04003214
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003215void btrfs_add_delayed_iput(struct inode *inode)
3216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003217 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003218 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219
3220 if (atomic_add_unless(&inode->i_count, -1, 1))
3221 return;
3222
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003223 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003224 if (binode->delayed_iput_count == 0) {
3225 ASSERT(list_empty(&binode->delayed_iput));
3226 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3227 } else {
3228 binode->delayed_iput_count++;
3229 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003230 spin_unlock(&fs_info->delayed_iput_lock);
3231}
3232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003233void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003234{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003237 while (!list_empty(&fs_info->delayed_iputs)) {
3238 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003239
David Sterba8089fe62015-11-19 14:15:51 +01003240 inode = list_first_entry(&fs_info->delayed_iputs,
3241 struct btrfs_inode, delayed_iput);
3242 if (inode->delayed_iput_count) {
3243 inode->delayed_iput_count--;
3244 list_move_tail(&inode->delayed_iput,
3245 &fs_info->delayed_iputs);
3246 } else {
3247 list_del_init(&inode->delayed_iput);
3248 }
3249 spin_unlock(&fs_info->delayed_iput_lock);
3250 iput(&inode->vfs_inode);
3251 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003252 }
David Sterba8089fe62015-11-19 14:15:51 +01003253 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254}
3255
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003257 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 * files in the subvolume, it removes orphan item and frees block_rsv
3259 * structure.
3260 */
3261void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3262 struct btrfs_root *root)
3263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003264 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003265 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 int ret;
3267
Josef Bacik8a35d952012-05-23 14:26:42 -04003268 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3270 return;
3271
Josef Bacik90290e12011-12-02 15:44:12 -05003272 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003273 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003274 spin_unlock(&root->orphan_lock);
3275 return;
3276 }
3277
3278 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3279 spin_unlock(&root->orphan_lock);
3280 return;
3281 }
3282
3283 block_rsv = root->orphan_block_rsv;
3284 root->orphan_block_rsv = NULL;
3285 spin_unlock(&root->orphan_lock);
3286
Miao Xie27cdeb72014-04-02 19:51:05 +08003287 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003289 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003291 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003292 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003293 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003294 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3295 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 }
3297
Josef Bacik90290e12011-12-02 15:44:12 -05003298 if (block_rsv) {
3299 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003300 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 }
3302}
3303
3304/*
Josef Bacik7b128762008-07-24 12:17:14 -04003305 * This creates an orphan entry for the given inode in case something goes
3306 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 *
3308 * NOTE: caller of this function should reserve 5 units of metadata for
3309 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003310 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003311int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3312 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003313{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003314 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3315 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 struct btrfs_block_rsv *block_rsv = NULL;
3317 int reserve = 0;
3318 int insert = 0;
3319 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003320
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003322 block_rsv = btrfs_alloc_block_rsv(fs_info,
3323 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003324 if (!block_rsv)
3325 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003326 }
3327
Yan, Zhengd68fc572010-05-16 10:49:58 -04003328 spin_lock(&root->orphan_lock);
3329 if (!root->orphan_block_rsv) {
3330 root->orphan_block_rsv = block_rsv;
3331 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003332 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 block_rsv = NULL;
3334 }
Josef Bacik7b128762008-07-24 12:17:14 -04003335
Josef Bacik8a35d952012-05-23 14:26:42 -04003336 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003337 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338#if 0
3339 /*
3340 * For proper ENOSPC handling, we should do orphan
3341 * cleanup when mounting. But this introduces backward
3342 * compatibility issue.
3343 */
3344 if (!xchg(&root->orphan_item_inserted, 1))
3345 insert = 2;
3346 else
3347 insert = 1;
3348#endif
3349 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003350 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 }
Josef Bacik7b128762008-07-24 12:17:14 -04003352
Josef Bacik72ac3c02012-05-23 14:13:11 -04003353 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003354 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003356 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003357
Yan, Zhengd68fc572010-05-16 10:49:58 -04003358 /* grab metadata reservation from transaction handle */
3359 if (reserve) {
3360 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003361 ASSERT(!ret);
3362 if (ret) {
3363 atomic_dec(&root->orphan_inodes);
3364 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003365 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003366 if (insert)
3367 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003368 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003369 return ret;
3370 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003371 }
3372
3373 /* insert an orphan item to track this unlinked/truncated file */
3374 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003375 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003376 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003377 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003378 if (reserve) {
3379 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003380 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003381 btrfs_orphan_release_metadata(inode);
3382 }
3383 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003384 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003385 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003386 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003387 return ret;
3388 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003389 }
3390 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003391 }
3392
3393 /* insert an orphan item to track subvolume contains orphan files */
3394 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003395 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003396 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003397 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003398 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003399 return ret;
3400 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003401 }
3402 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003403}
3404
3405/*
3406 * We have done the truncate/delete so we can go ahead and remove the orphan
3407 * item for this particular inode.
3408 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003409static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003410 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003411{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003412 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 int delete_item = 0;
3414 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003415 int ret = 0;
3416
Yan, Zhengd68fc572010-05-16 10:49:58 -04003417 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003418 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003419 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003420 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003421
Josef Bacik72ac3c02012-05-23 14:13:11 -04003422 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003423 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003424 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003425 spin_unlock(&root->orphan_lock);
3426
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003427 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003428 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003429 if (trans)
3430 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003431 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003432 }
Josef Bacik7b128762008-07-24 12:17:14 -04003433
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003434 if (release_rsv)
3435 btrfs_orphan_release_metadata(inode);
3436
Josef Bacik4ef31a42013-08-13 14:10:08 -04003437 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003438}
3439
3440/*
3441 * this cleans up any orphans that may be left on the list from the last use
3442 * of this root.
3443 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003444int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003446 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003447 struct btrfs_path *path;
3448 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003449 struct btrfs_key key, found_key;
3450 struct btrfs_trans_handle *trans;
3451 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003452 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003453 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3454
Yan, Zhengd68fc572010-05-16 10:49:58 -04003455 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003456 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003457
3458 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003459 if (!path) {
3460 ret = -ENOMEM;
3461 goto out;
3462 }
David Sterbae4058b52015-11-27 16:31:35 +01003463 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003464
3465 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003466 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003467 key.offset = (u64)-1;
3468
Josef Bacik7b128762008-07-24 12:17:14 -04003469 while (1) {
3470 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003471 if (ret < 0)
3472 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003473
3474 /*
3475 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003476 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003477 * find the key and see if we have stuff that matches
3478 */
3479 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003480 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003481 if (path->slots[0] == 0)
3482 break;
3483 path->slots[0]--;
3484 }
3485
3486 /* pull out the item */
3487 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003488 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3489
3490 /* make sure the item matches what we want */
3491 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3492 break;
David Sterba962a2982014-06-04 18:41:45 +02003493 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003494 break;
3495
3496 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003497 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003498
3499 /*
3500 * this is where we are basically btrfs_lookup, without the
3501 * crossing root thing. we store the inode number in the
3502 * offset of the orphan item.
3503 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003504
3505 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003506 btrfs_err(fs_info,
3507 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003508 ret = -EINVAL;
3509 goto out;
3510 }
3511
3512 last_objectid = found_key.offset;
3513
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003514 found_key.objectid = found_key.offset;
3515 found_key.type = BTRFS_INODE_ITEM_KEY;
3516 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003517 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303518 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003519 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003520 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003521
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003522 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003523 struct btrfs_root *dead_root;
3524 struct btrfs_fs_info *fs_info = root->fs_info;
3525 int is_dead_root = 0;
3526
3527 /*
3528 * this is an orphan in the tree root. Currently these
3529 * could come from 2 sources:
3530 * a) a snapshot deletion in progress
3531 * b) a free space cache inode
3532 * We need to distinguish those two, as the snapshot
3533 * orphan must not get deleted.
3534 * find_dead_roots already ran before us, so if this
3535 * is a snapshot deletion, we should find the root
3536 * in the dead_roots list
3537 */
3538 spin_lock(&fs_info->trans_lock);
3539 list_for_each_entry(dead_root, &fs_info->dead_roots,
3540 root_list) {
3541 if (dead_root->root_key.objectid ==
3542 found_key.objectid) {
3543 is_dead_root = 1;
3544 break;
3545 }
3546 }
3547 spin_unlock(&fs_info->trans_lock);
3548 if (is_dead_root) {
3549 /* prevent this orphan from being found again */
3550 key.offset = found_key.objectid - 1;
3551 continue;
3552 }
3553 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003554 /*
3555 * Inode is already gone but the orphan item is still there,
3556 * kill the orphan item.
3557 */
Filipe Manana67710892016-06-06 11:51:25 +01003558 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003559 trans = btrfs_start_transaction(root, 1);
3560 if (IS_ERR(trans)) {
3561 ret = PTR_ERR(trans);
3562 goto out;
3563 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003564 btrfs_debug(fs_info, "auto deleting %Lu",
3565 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003566 ret = btrfs_del_orphan_item(trans, root,
3567 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003568 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003569 if (ret)
3570 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003571 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003572 }
Josef Bacik7b128762008-07-24 12:17:14 -04003573
Josef Bacik7b128762008-07-24 12:17:14 -04003574 /*
3575 * add this inode to the orphan list so btrfs_orphan_del does
3576 * the proper thing when we hit it
3577 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003578 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3579 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003580 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003581
Josef Bacik7b128762008-07-24 12:17:14 -04003582 /* if we have links, this was a truncate, lets do that */
3583 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303584 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003585 iput(inode);
3586 continue;
3587 }
Josef Bacik7b128762008-07-24 12:17:14 -04003588 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003589
3590 /* 1 for the orphan item deletion. */
3591 trans = btrfs_start_transaction(root, 1);
3592 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003593 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003594 ret = PTR_ERR(trans);
3595 goto out;
3596 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003597 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003598 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003599 if (ret) {
3600 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003601 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003602 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003603
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003604 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003605 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003606 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003607 } else {
3608 nr_unlink++;
3609 }
3610
3611 /* this will do delete_inode and everything for us */
3612 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003613 if (ret)
3614 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003615 }
Miao Xie3254c872011-11-10 20:45:05 -05003616 /* release the path since we're done with it */
3617 btrfs_release_path(path);
3618
Yan, Zhengd68fc572010-05-16 10:49:58 -04003619 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3620
3621 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003622 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003623 (u64)-1);
3624
Miao Xie27cdeb72014-04-02 19:51:05 +08003625 if (root->orphan_block_rsv ||
3626 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003627 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003628 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003629 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003630 }
Josef Bacik7b128762008-07-24 12:17:14 -04003631
3632 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003633 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003634 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003635 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003636
3637out:
3638 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003639 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003640 btrfs_free_path(path);
3641 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003642}
3643
Chris Masond352ac62008-09-29 15:18:18 -04003644/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003645 * very simple check to peek ahead in the leaf looking for xattrs. If we
3646 * don't find any xattrs, we know there can't be any acls.
3647 *
3648 * slot is the slot the inode is in, objectid is the objectid of the inode
3649 */
3650static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003651 int slot, u64 objectid,
3652 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003653{
3654 u32 nritems = btrfs_header_nritems(leaf);
3655 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003656 static u64 xattr_access = 0;
3657 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003658 int scanned = 0;
3659
Josef Bacikf23b5a52013-06-19 10:16:26 -04003660 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003661 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3662 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3663 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3664 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003665 }
3666
Chris Mason46a53cc2009-04-27 11:47:50 -04003667 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003668 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003669 while (slot < nritems) {
3670 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3671
3672 /* we found a different objectid, there must not be acls */
3673 if (found_key.objectid != objectid)
3674 return 0;
3675
3676 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003677 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003678 if (*first_xattr_slot == -1)
3679 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003680 if (found_key.offset == xattr_access ||
3681 found_key.offset == xattr_default)
3682 return 1;
3683 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003684
3685 /*
3686 * we found a key greater than an xattr key, there can't
3687 * be any acls later on
3688 */
3689 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3690 return 0;
3691
3692 slot++;
3693 scanned++;
3694
3695 /*
3696 * it goes inode, inode backrefs, xattrs, extents,
3697 * so if there are a ton of hard links to an inode there can
3698 * be a lot of backrefs. Don't waste time searching too hard,
3699 * this is just an optimization
3700 */
3701 if (scanned >= 8)
3702 break;
3703 }
3704 /* we hit the end of the leaf before we found an xattr or
3705 * something larger than an xattr. We have to assume the inode
3706 * has acls
3707 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003708 if (*first_xattr_slot == -1)
3709 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 return 1;
3711}
3712
3713/*
Chris Masond352ac62008-09-29 15:18:18 -04003714 * read an inode from the btree into the in-memory inode
3715 */
Filipe Manana67710892016-06-06 11:51:25 +01003716static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003717{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003718 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003719 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003720 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003721 struct btrfs_inode_item *inode_item;
3722 struct btrfs_root *root = BTRFS_I(inode)->root;
3723 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003724 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003725 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003726 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003727 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003728 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003729 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003730
3731 ret = btrfs_fill_inode(inode, &rdev);
3732 if (!ret)
3733 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003734
3735 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003736 if (!path) {
3737 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003738 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003739 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003740
Chris Mason39279cc2007-06-12 06:35:45 -04003741 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003742
Chris Mason39279cc2007-06-12 06:35:45 -04003743 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003744 if (ret) {
3745 if (ret > 0)
3746 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003747 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003748 }
Chris Mason39279cc2007-06-12 06:35:45 -04003749
Chris Mason5f39d392007-10-15 16:14:19 -04003750 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003751
3752 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003753 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003754
Chris Mason5f39d392007-10-15 16:14:19 -04003755 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3756 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003757 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003758 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003759 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3760 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003761 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003762
David Sterbaa937b972014-12-12 17:39:12 +01003763 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3764 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003765
David Sterbaa937b972014-12-12 17:39:12 +01003766 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3767 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003768
David Sterbaa937b972014-12-12 17:39:12 +01003769 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3770 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003771
chandan r9cc97d62012-07-04 12:48:07 +05303772 BTRFS_I(inode)->i_otime.tv_sec =
3773 btrfs_timespec_sec(leaf, &inode_item->otime);
3774 BTRFS_I(inode)->i_otime.tv_nsec =
3775 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003777 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003778 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003779 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3780
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003781 inode_set_iversion_queried(inode,
3782 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003783 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003784 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003785 rdev = btrfs_inode_rdev(leaf, inode_item);
3786
Josef Bacikaec74772008-07-24 12:12:38 -04003787 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003788 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003789
3790cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003791 /*
3792 * If we were modified in the current generation and evicted from memory
3793 * and then re-read we need to do a full sync since we don't have any
3794 * idea about which extents were modified before we were evicted from
3795 * cache.
3796 *
3797 * This is required for both inode re-read from disk and delayed inode
3798 * in delayed_nodes_tree.
3799 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003800 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003801 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3802 &BTRFS_I(inode)->runtime_flags);
3803
Filipe Mananabde6c242015-07-24 00:00:19 +01003804 /*
3805 * We don't persist the id of the transaction where an unlink operation
3806 * against the inode was last made. So here we assume the inode might
3807 * have been evicted, and therefore the exact value of last_unlink_trans
3808 * lost, and set it to last_trans to avoid metadata inconsistencies
3809 * between the inode and its parent if the inode is fsync'ed and the log
3810 * replayed. For example, in the scenario:
3811 *
3812 * touch mydir/foo
3813 * ln mydir/foo mydir/bar
3814 * sync
3815 * unlink mydir/bar
3816 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3817 * xfs_io -c fsync mydir/foo
3818 * <power failure>
3819 * mount fs, triggers fsync log replay
3820 *
3821 * We must make sure that when we fsync our inode foo we also log its
3822 * parent inode, otherwise after log replay the parent still has the
3823 * dentry with the "bar" name but our inode foo has a link count of 1
3824 * and doesn't have an inode ref with the name "bar" anymore.
3825 *
3826 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003827 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003828 * transaction commits on fsync if our inode is a directory, or if our
3829 * inode is not a directory, logging its parent unnecessarily.
3830 */
3831 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3832
Miao Xie67de1172013-12-26 13:07:06 +08003833 path->slots[0]++;
3834 if (inode->i_nlink != 1 ||
3835 path->slots[0] >= btrfs_header_nritems(leaf))
3836 goto cache_acl;
3837
3838 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003839 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003840 goto cache_acl;
3841
3842 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3843 if (location.type == BTRFS_INODE_REF_KEY) {
3844 struct btrfs_inode_ref *ref;
3845
3846 ref = (struct btrfs_inode_ref *)ptr;
3847 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3848 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3849 struct btrfs_inode_extref *extref;
3850
3851 extref = (struct btrfs_inode_extref *)ptr;
3852 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3853 extref);
3854 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003855cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003856 /*
3857 * try to precache a NULL acl entry for files that don't have
3858 * any xattrs or acls
3859 */
Li Zefan33345d012011-04-20 10:31:50 +08003860 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003861 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003862 if (first_xattr_slot != -1) {
3863 path->slots[0] = first_xattr_slot;
3864 ret = btrfs_load_inode_props(inode, path);
3865 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003866 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003867 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003868 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003869 root->root_key.objectid, ret);
3870 }
3871 btrfs_free_path(path);
3872
Al Viro72c04902009-06-24 16:58:48 -04003873 if (!maybe_acls)
3874 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003875
Chris Mason39279cc2007-06-12 06:35:45 -04003876 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003877 case S_IFREG:
3878 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003879 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003880 inode->i_fop = &btrfs_file_operations;
3881 inode->i_op = &btrfs_file_inode_operations;
3882 break;
3883 case S_IFDIR:
3884 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003885 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003886 break;
3887 case S_IFLNK:
3888 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003889 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003890 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3891 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003892 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003893 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003894 init_special_inode(inode, inode->i_mode, rdev);
3895 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003896 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003897
3898 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003899 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003900
3901make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003902 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003903 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003904 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003905}
3906
Chris Masond352ac62008-09-29 15:18:18 -04003907/*
3908 * given a leaf and an inode, copy the inode fields into the leaf
3909 */
Chris Masone02119d2008-09-05 16:13:11 -04003910static void fill_inode_item(struct btrfs_trans_handle *trans,
3911 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003912 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003913 struct inode *inode)
3914{
Liu Bo51fab692012-12-27 09:01:21 +00003915 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003916
Liu Bo51fab692012-12-27 09:01:21 +00003917 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003918
Liu Bo51fab692012-12-27 09:01:21 +00003919 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3920 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3921 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3922 &token);
3923 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3924 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003925
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003928 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003929 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003930
David Sterbaa937b972014-12-12 17:39:12 +01003931 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003932 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003933 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003934 inode->i_mtime.tv_nsec, &token);
3935
David Sterbaa937b972014-12-12 17:39:12 +01003936 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003937 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003938 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003939 inode->i_ctime.tv_nsec, &token);
3940
chandan r9cc97d62012-07-04 12:48:07 +05303941 btrfs_set_token_timespec_sec(leaf, &item->otime,
3942 BTRFS_I(inode)->i_otime.tv_sec, &token);
3943 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3944 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3945
Liu Bo51fab692012-12-27 09:01:21 +00003946 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3947 &token);
3948 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3949 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003950 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3951 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003952 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3953 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3954 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3955 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003956}
3957
Chris Masond352ac62008-09-29 15:18:18 -04003958/*
3959 * copy everything in the in-memory inode into the btree.
3960 */
Chris Mason21151332011-11-10 20:39:08 -05003961static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003962 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003963{
3964 struct btrfs_inode_item *inode_item;
3965 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003966 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003967 int ret;
3968
3969 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003970 if (!path)
3971 return -ENOMEM;
3972
Chris Masonb9473432009-03-13 11:00:37 -04003973 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003974 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3975 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003976 if (ret) {
3977 if (ret > 0)
3978 ret = -ENOENT;
3979 goto failed;
3980 }
3981
Chris Mason5f39d392007-10-15 16:14:19 -04003982 leaf = path->nodes[0];
3983 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003984 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003985
Chris Masone02119d2008-09-05 16:13:11 -04003986 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003987 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003988 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003989 ret = 0;
3990failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003991 btrfs_free_path(path);
3992 return ret;
3993}
3994
Chris Masond352ac62008-09-29 15:18:18 -04003995/*
Chris Mason21151332011-11-10 20:39:08 -05003996 * copy everything in the in-memory inode into the btree.
3997 */
3998noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3999 struct btrfs_root *root, struct inode *inode)
4000{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004001 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004002 int ret;
4003
4004 /*
4005 * If the inode is a free space inode, we can deadlock during commit
4006 * if we put it into the delayed code.
4007 *
4008 * The data relocation inode should also be directly updated
4009 * without delay
4010 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004011 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004012 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004013 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004014 btrfs_update_root_times(trans, root);
4015
Chris Mason21151332011-11-10 20:39:08 -05004016 ret = btrfs_delayed_update_inode(trans, root, inode);
4017 if (!ret)
4018 btrfs_set_inode_last_trans(trans, inode);
4019 return ret;
4020 }
4021
4022 return btrfs_update_inode_item(trans, root, inode);
4023}
4024
Josef Bacikbe6aef62012-10-22 15:43:12 -04004025noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4026 struct btrfs_root *root,
4027 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004028{
4029 int ret;
4030
4031 ret = btrfs_update_inode(trans, root, inode);
4032 if (ret == -ENOSPC)
4033 return btrfs_update_inode_item(trans, root, inode);
4034 return ret;
4035}
4036
4037/*
Chris Masond352ac62008-09-29 15:18:18 -04004038 * unlink helper that gets used here in inode.c and in the tree logging
4039 * recovery code. It remove a link in a directory with a given name, and
4040 * also drops the back refs in the inode to the directory
4041 */
Al Viro92986792011-03-04 17:14:37 +00004042static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4043 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004044 struct btrfs_inode *dir,
4045 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004046 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004047{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004048 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004050 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004051 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004052 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004053 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004054 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004055 u64 ino = btrfs_ino(inode);
4056 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004057
4058 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004059 if (!path) {
4060 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004061 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004062 }
4063
Chris Masonb9473432009-03-13 11:00:37 -04004064 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004065 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004066 name, name_len, -1);
4067 if (IS_ERR(di)) {
4068 ret = PTR_ERR(di);
4069 goto err;
4070 }
4071 if (!di) {
4072 ret = -ENOENT;
4073 goto err;
4074 }
Chris Mason5f39d392007-10-15 16:14:19 -04004075 leaf = path->nodes[0];
4076 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004077 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004078 if (ret)
4079 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004080 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004081
Miao Xie67de1172013-12-26 13:07:06 +08004082 /*
4083 * If we don't have dir index, we have to get it by looking up
4084 * the inode ref, since we get the inode ref, remove it directly,
4085 * it is unnecessary to do delayed deletion.
4086 *
4087 * But if we have dir index, needn't search inode ref to get it.
4088 * Since the inode ref is close to the inode item, it is better
4089 * that we delay to delete it, and just do this deletion when
4090 * we update the inode item.
4091 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004092 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004093 ret = btrfs_delayed_delete_inode_ref(inode);
4094 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004095 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004096 goto skip_backref;
4097 }
4098 }
4099
Li Zefan33345d012011-04-20 10:31:50 +08004100 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4101 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004102 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004103 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004104 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004105 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004106 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004107 goto err;
4108 }
Miao Xie67de1172013-12-26 13:07:06 +08004109skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004110 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004112 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004113 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004114 }
Chris Mason39279cc2007-06-12 06:35:45 -04004115
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004116 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4117 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004119 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004120 goto err;
4121 }
Chris Masone02119d2008-09-05 16:13:11 -04004122
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004123 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4124 index);
Chris Mason6418c962010-10-30 07:34:24 -04004125 if (ret == -ENOENT)
4126 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004127 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004128 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004129err:
4130 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004131 if (ret)
4132 goto out;
4133
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004134 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004135 inode_inc_iversion(&inode->vfs_inode);
4136 inode_inc_iversion(&dir->vfs_inode);
4137 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4138 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4139 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004140out:
Chris Mason39279cc2007-06-12 06:35:45 -04004141 return ret;
4142}
4143
Al Viro92986792011-03-04 17:14:37 +00004144int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4145 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004146 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004147 const char *name, int name_len)
4148{
4149 int ret;
4150 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4151 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004152 drop_nlink(&inode->vfs_inode);
4153 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004154 }
4155 return ret;
4156}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004157
4158/*
4159 * helper to start transaction for unlink and rmdir.
4160 *
Josef Bacikd52be812013-05-29 14:54:47 -04004161 * unlink and rmdir are special in btrfs, they do not always free space, so
4162 * if we cannot make our reservations the normal way try and see if there is
4163 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4164 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165 */
Josef Bacikd52be812013-05-29 14:54:47 -04004166static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004167{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004168 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004169
Josef Bacike70bea52011-10-11 14:18:24 -04004170 /*
4171 * 1 for the possible orphan item
4172 * 1 for the dir item
4173 * 1 for the dir index
4174 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004175 * 1 for the inode
4176 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004177 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178}
4179
Chris Mason39279cc2007-06-12 06:35:45 -04004180static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4181{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004182 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004183 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004184 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004185 int ret;
4186
Josef Bacikd52be812013-05-29 14:54:47 -04004187 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004188 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004189 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004190
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004191 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4192 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004193
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004194 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4195 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4196 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004197 if (ret)
4198 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004199
Yan, Zhenga22285a2010-05-16 10:48:46 -04004200 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004201 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004202 if (ret)
4203 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204 }
Josef Bacik7b128762008-07-24 12:17:14 -04004205
Tsutomu Itohb5324022011-07-19 07:27:20 +00004206out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004207 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004208 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004209 return ret;
4210}
4211
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4213 struct btrfs_root *root,
4214 struct inode *dir, u64 objectid,
4215 const char *name, int name_len)
4216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218 struct btrfs_path *path;
4219 struct extent_buffer *leaf;
4220 struct btrfs_dir_item *di;
4221 struct btrfs_key key;
4222 u64 index;
4223 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004224 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004225
4226 path = btrfs_alloc_path();
4227 if (!path)
4228 return -ENOMEM;
4229
Li Zefan33345d012011-04-20 10:31:50 +08004230 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004231 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 if (IS_ERR_OR_NULL(di)) {
4233 if (!di)
4234 ret = -ENOENT;
4235 else
4236 ret = PTR_ERR(di);
4237 goto out;
4238 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004239
4240 leaf = path->nodes[0];
4241 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4242 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4243 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004244 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004245 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004246 goto out;
4247 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004248 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004249
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004250 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4251 root->root_key.objectid, dir_ino,
4252 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004255 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004256 goto out;
4257 }
Li Zefan33345d012011-04-20 10:31:50 +08004258 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004260 if (IS_ERR_OR_NULL(di)) {
4261 if (!di)
4262 ret = -ENOENT;
4263 else
4264 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004265 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 goto out;
4267 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268
4269 leaf = path->nodes[0];
4270 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004271 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004272 index = key.offset;
4273 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004274 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004276 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004278 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004279 goto out;
4280 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004281
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004282 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004283 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004284 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004285 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004287 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004289 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004291}
4292
Chris Mason39279cc2007-06-12 06:35:45 -04004293static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4294{
David Howells2b0143b2015-03-17 22:25:59 +00004295 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004296 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004298 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004299 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004300
David Sterbab3ae2442012-09-13 16:04:34 -06004301 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004302 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004303 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004304 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004305
Josef Bacikd52be812013-05-29 14:54:47 -04004306 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004307 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004308 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004309
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004310 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004311 err = btrfs_unlink_subvol(trans, root, dir,
4312 BTRFS_I(inode)->location.objectid,
4313 dentry->d_name.name,
4314 dentry->d_name.len);
4315 goto out;
4316 }
4317
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004318 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004319 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004320 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004321
Filipe Manana44f714d2016-06-06 16:11:13 +01004322 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4323
Chris Mason39279cc2007-06-12 06:35:45 -04004324 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004325 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4326 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4327 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004328 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004329 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004330 /*
4331 * Propagate the last_unlink_trans value of the deleted dir to
4332 * its parent directory. This is to prevent an unrecoverable
4333 * log tree in the case we do something like this:
4334 * 1) create dir foo
4335 * 2) create snapshot under dir foo
4336 * 3) delete the snapshot
4337 * 4) rmdir foo
4338 * 5) mkdir foo
4339 * 6) fsync foo or some file inside foo
4340 */
4341 if (last_unlink_trans >= trans->transid)
4342 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4343 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004344out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004345 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004346 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004347
Chris Mason39279cc2007-06-12 06:35:45 -04004348 return err;
4349}
4350
Chris Mason28f75a02015-02-04 06:59:29 -08004351static int truncate_space_check(struct btrfs_trans_handle *trans,
4352 struct btrfs_root *root,
4353 u64 bytes_deleted)
4354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004355 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004356 int ret;
4357
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004358 /*
4359 * This is only used to apply pressure to the enospc system, we don't
4360 * intend to use this reservation at all.
4361 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004362 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004363 bytes_deleted *= fs_info->nodesize;
4364 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004365 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004366 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004367 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004368 trans->transid,
4369 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004370 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004371 }
Chris Mason28f75a02015-02-04 06:59:29 -08004372 return ret;
4373
4374}
4375
Josef Bacikddfae632017-10-19 14:16:02 -04004376/*
4377 * Return this if we need to call truncate_block for the last bit of the
4378 * truncate.
4379 */
4380#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004381
Chris Mason323ac952008-10-01 19:05:46 -04004382/*
Chris Mason39279cc2007-06-12 06:35:45 -04004383 * this can truncate away extent items, csum items and directory items.
4384 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004385 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004386 *
4387 * csum items that cross the new i_size are truncated to the new size
4388 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004389 *
4390 * min_type is the minimum key type to truncate down to. If set to 0, this
4391 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004392 */
Yan, Zheng80825102009-11-12 09:35:36 +00004393int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4394 struct btrfs_root *root,
4395 struct inode *inode,
4396 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004397{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004398 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004399 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004400 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004401 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004402 struct btrfs_key key;
4403 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004404 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004405 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004406 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004407 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004408 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004409 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004410 int found_extent;
4411 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004412 int pending_del_nr = 0;
4413 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004414 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004415 int ret;
4416 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004417 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004418 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004419 bool be_nice = false;
4420 bool should_throttle = false;
4421 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004422
4423 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004424
Chris Mason28ed1342014-12-17 09:41:04 -08004425 /*
4426 * for non-free space inodes and ref cows, we want to back off from
4427 * time to time
4428 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004429 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004430 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004431 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004432
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004433 path = btrfs_alloc_path();
4434 if (!path)
4435 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004436 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004437
Josef Bacik5dc562c2012-08-17 13:14:17 -04004438 /*
4439 * We want to drop from the next block forward in case this new size is
4440 * not block aligned since we will be keeping the last block of the
4441 * extent just the way it is.
4442 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004443 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004444 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004445 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004446 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004447 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004448
Miao Xie16cdcec2011-04-22 18:12:22 +08004449 /*
4450 * This function is also used to drop the items in the log tree before
4451 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4452 * it is used to drop the loged items. So we shouldn't kill the delayed
4453 * items.
4454 */
4455 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004456 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004457
Li Zefan33345d012011-04-20 10:31:50 +08004458 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004459 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004460 key.type = (u8)-1;
4461
Chris Mason85e21ba2008-01-29 15:11:36 -05004462search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004463 /*
4464 * with a 16K leaf size and 128MB extents, you can actually queue
4465 * up a huge file in a single leaf. Most of the time that
4466 * bytes_deleted is > 0, it will be huge by the time we get here
4467 */
Byongho Leeee221842015-12-15 01:42:10 +09004468 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004469 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004470 err = -EAGAIN;
4471 goto error;
4472 }
4473 }
4474
4475
Chris Masonb9473432009-03-13 11:00:37 -04004476 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004477 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004478 if (ret < 0) {
4479 err = ret;
4480 goto out;
4481 }
Chris Masond3977122009-01-05 21:25:51 -05004482
Chris Mason85e21ba2008-01-29 15:11:36 -05004483 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004484 /* there are no items in the tree for us to truncate, we're
4485 * done
4486 */
Yan, Zheng80825102009-11-12 09:35:36 +00004487 if (path->slots[0] == 0)
4488 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004489 path->slots[0]--;
4490 }
4491
Chris Masond3977122009-01-05 21:25:51 -05004492 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004493 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004494 leaf = path->nodes[0];
4495 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004496 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004497
Li Zefan33345d012011-04-20 10:31:50 +08004498 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004499 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004500
Chris Mason85e21ba2008-01-29 15:11:36 -05004501 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004502 break;
4503
Chris Mason5f39d392007-10-15 16:14:19 -04004504 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004505 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004506 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004507 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004508 extent_type = btrfs_file_extent_type(leaf, fi);
4509 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004510 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004511 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004512
4513 trace_btrfs_truncate_show_fi_regular(
4514 BTRFS_I(inode), leaf, fi,
4515 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004516 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004517 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004518 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004519
4520 trace_btrfs_truncate_show_fi_inline(
4521 BTRFS_I(inode), leaf, fi, path->slots[0],
4522 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004523 }
Yan008630c2007-11-07 13:31:09 -05004524 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004525 }
Yan, Zheng80825102009-11-12 09:35:36 +00004526 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004527 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004528 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004529 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004530 break;
4531 if (found_key.offset >= new_size)
4532 del_item = 1;
4533 else
4534 del_item = 0;
4535 }
Chris Mason39279cc2007-06-12 06:35:45 -04004536 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004537 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004538 if (found_type != BTRFS_EXTENT_DATA_KEY)
4539 goto delete;
4540
4541 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004542 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004543 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004544 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004545 u64 orig_num_bytes =
4546 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004547 extent_num_bytes = ALIGN(new_size -
4548 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004549 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004550 btrfs_set_file_extent_num_bytes(leaf, fi,
4551 extent_num_bytes);
4552 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004553 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004554 if (test_bit(BTRFS_ROOT_REF_COWS,
4555 &root->state) &&
4556 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004557 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004558 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004559 } else {
Chris Masondb945352007-10-15 16:15:53 -04004560 extent_num_bytes =
4561 btrfs_file_extent_disk_num_bytes(leaf,
4562 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004563 extent_offset = found_key.offset -
4564 btrfs_file_extent_offset(leaf, fi);
4565
Chris Mason39279cc2007-06-12 06:35:45 -04004566 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004567 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004568 if (extent_start != 0) {
4569 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004570 if (test_bit(BTRFS_ROOT_REF_COWS,
4571 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004572 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004573 }
4574 }
Chris Mason90692182008-02-08 13:49:28 -05004575 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004576 /*
4577 * we can't truncate inline items that have had
4578 * special encodings
4579 */
4580 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004581 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004582 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4583 btrfs_file_extent_compression(leaf, fi) == 0) {
4584 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004585
Josef Bacikddfae632017-10-19 14:16:02 -04004586 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4587 size = btrfs_file_extent_calc_inline_size(size);
4588 btrfs_truncate_item(root->fs_info, path, size, 1);
4589 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004590 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004591 * We have to bail so the last_size is set to
4592 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004593 */
Josef Bacikddfae632017-10-19 14:16:02 -04004594 err = NEED_TRUNCATE_BLOCK;
4595 break;
Chris Mason90692182008-02-08 13:49:28 -05004596 }
Josef Bacikddfae632017-10-19 14:16:02 -04004597
4598 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4599 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
Chris Mason179e29e2007-11-01 11:28:41 -04004601delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004602 if (del_item)
4603 last_size = found_key.offset;
4604 else
4605 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004606 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004607 if (!pending_del_nr) {
4608 /* no pending yet, add ourselves */
4609 pending_del_slot = path->slots[0];
4610 pending_del_nr = 1;
4611 } else if (pending_del_nr &&
4612 path->slots[0] + 1 == pending_del_slot) {
4613 /* hop on the pending chunk */
4614 pending_del_nr++;
4615 pending_del_slot = path->slots[0];
4616 } else {
Chris Masond3977122009-01-05 21:25:51 -05004617 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 }
Chris Mason39279cc2007-06-12 06:35:45 -04004619 } else {
4620 break;
4621 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004622 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004623
Miao Xie27cdeb72014-04-02 19:51:05 +08004624 if (found_extent &&
4625 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004626 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004627 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004628 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004629 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004630 extent_num_bytes, 0,
4631 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004632 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004633 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004634 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4635 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004636 trans->delayed_ref_updates * 2,
4637 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004638 if (be_nice) {
4639 if (truncate_space_check(trans, root,
4640 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004641 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004642 }
4643 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004644 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004645 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004646 }
Chris Mason39279cc2007-06-12 06:35:45 -04004647 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004648
Yan, Zheng80825102009-11-12 09:35:36 +00004649 if (found_type == BTRFS_INODE_ITEM_KEY)
4650 break;
4651
4652 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004653 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004654 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004655 if (pending_del_nr) {
4656 ret = btrfs_del_items(trans, root, path,
4657 pending_del_slot,
4658 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004659 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004660 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004661 goto error;
4662 }
Yan, Zheng80825102009-11-12 09:35:36 +00004663 pending_del_nr = 0;
4664 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004665 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004666 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004667 unsigned long updates = trans->delayed_ref_updates;
4668 if (updates) {
4669 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004670 ret = btrfs_run_delayed_refs(trans,
4671 fs_info,
4672 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004673 if (ret && !err)
4674 err = ret;
4675 }
4676 }
Chris Mason28f75a02015-02-04 06:59:29 -08004677 /*
4678 * if we failed to refill our space rsv, bail out
4679 * and let the transaction restart
4680 */
4681 if (should_end) {
4682 err = -EAGAIN;
4683 goto error;
4684 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004685 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004686 } else {
4687 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004688 }
Chris Mason39279cc2007-06-12 06:35:45 -04004689 }
Yan, Zheng80825102009-11-12 09:35:36 +00004690out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004691 if (pending_del_nr) {
4692 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4693 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004694 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004695 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004696 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004697error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004698 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4699 ASSERT(last_size >= new_size);
4700 if (!err && last_size > new_size)
4701 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004702 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004703 }
Chris Mason28ed1342014-12-17 09:41:04 -08004704
Chris Mason39279cc2007-06-12 06:35:45 -04004705 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004706
Byongho Leeee221842015-12-15 01:42:10 +09004707 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004708 unsigned long updates = trans->delayed_ref_updates;
4709 if (updates) {
4710 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004711 ret = btrfs_run_delayed_refs(trans, fs_info,
4712 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004713 if (ret && !err)
4714 err = ret;
4715 }
4716 }
Yan, Zheng80825102009-11-12 09:35:36 +00004717 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004718}
4719
Chris Masona52d9a82007-08-27 16:49:44 -04004720/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304721 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004722 * @inode - inode that we're zeroing
4723 * @from - the offset to start zeroing
4724 * @len - the length to zero, 0 to zero the entire range respective to the
4725 * offset
4726 * @front - zero up to the offset instead of from the offset on
4727 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004729 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004730 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304731int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004732 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004734 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004735 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004736 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4737 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004738 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004739 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004741 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004742 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004744 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004745 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004746 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 u64 block_start;
4748 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004749
Josef Bacik2aaa6652012-08-29 14:27:18 -04004750 if ((offset & (blocksize - 1)) == 0 &&
4751 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004752 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753
Josef Bacik8b62f872017-10-19 14:15:55 -04004754 block_start = round_down(from, blocksize);
4755 block_end = block_start + blocksize - 1;
4756
Qu Wenruo364ecf32017-02-27 15:10:38 +08004757 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004758 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004759 if (ret)
4760 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004761
Chris Mason211c17f2008-05-15 09:13:45 -04004762again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004763 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004764 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004765 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004766 block_start, blocksize);
4767 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004768 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004769 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004770 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004771
Chris Masona52d9a82007-08-27 16:49:44 -04004772 if (!PageUptodate(page)) {
4773 ret = btrfs_readpage(NULL, page);
4774 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004775 if (page->mapping != mapping) {
4776 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004777 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004778 goto again;
4779 }
Chris Masona52d9a82007-08-27 16:49:44 -04004780 if (!PageUptodate(page)) {
4781 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004782 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004783 }
4784 }
Chris Mason211c17f2008-05-15 09:13:45 -04004785 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004786
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004788 set_page_extent_mapped(page);
4789
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004791 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004793 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004794 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004795 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004796 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797 btrfs_put_ordered_extent(ordered);
4798 goto again;
4799 }
4800
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004802 EXTENT_DIRTY | EXTENT_DELALLOC |
4803 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004804 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004805
Filipe Mananae3b8a482017-11-04 00:16:59 +00004806 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004807 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004808 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304809 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004810 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004811 goto out_unlock;
4812 }
4813
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004815 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004817 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004818 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 memset(kaddr + (block_start - page_offset(page)),
4820 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004821 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 memset(kaddr + (block_start - page_offset(page)) + offset,
4823 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004824 flush_dcache_page(page);
4825 kunmap(page);
4826 }
Chris Mason247e7432008-07-17 12:53:51 -04004827 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004828 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004830 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004831
Chris Mason89642222008-07-24 09:41:53 -04004832out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004833 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004834 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304835 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004836 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004837 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004838 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004839out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004840 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004841 return ret;
4842}
4843
Josef Bacik16e75492013-10-22 12:18:51 -04004844static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4845 u64 offset, u64 len)
4846{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004847 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004848 struct btrfs_trans_handle *trans;
4849 int ret;
4850
4851 /*
4852 * Still need to make sure the inode looks like it's been updated so
4853 * that any holes get logged if we fsync.
4854 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004855 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4856 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004857 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4858 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4859 return 0;
4860 }
4861
4862 /*
4863 * 1 - for the one we're dropping
4864 * 1 - for the one we're adding
4865 * 1 - for updating the inode.
4866 */
4867 trans = btrfs_start_transaction(root, 3);
4868 if (IS_ERR(trans))
4869 return PTR_ERR(trans);
4870
4871 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4872 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004873 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004874 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004875 return ret;
4876 }
4877
David Sterbaf85b7372017-01-20 14:54:07 +01004878 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4879 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004880 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004881 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004882 else
4883 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004884 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004885 return ret;
4886}
4887
Josef Bacik695a0d02011-03-04 15:46:53 -05004888/*
4889 * This function puts in dummy file extents for the area we're creating a hole
4890 * for. So if we are truncating this file to a larger size we need to insert
4891 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4892 * the range between oldsize and size
4893 */
Josef Bacika41ad392011-01-31 15:30:16 -05004894int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004895{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004897 struct btrfs_root *root = BTRFS_I(inode)->root;
4898 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004899 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004900 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004901 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004902 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4903 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004904 u64 last_byte;
4905 u64 cur_offset;
4906 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004907 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004908
Josef Bacika71754f2013-06-17 17:14:39 -04004909 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304910 * If our size started in the middle of a block we need to zero out the
4911 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004912 * expose stale data.
4913 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304914 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004915 if (err)
4916 return err;
4917
Yan Zheng9036c102008-10-30 14:19:41 -04004918 if (size <= hole_start)
4919 return 0;
4920
Yan Zheng9036c102008-10-30 14:19:41 -04004921 while (1) {
4922 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004923
David Sterbaff13db42015-12-03 14:30:40 +01004924 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004925 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004926 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004927 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004928 if (!ordered)
4929 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004930 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4931 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004932 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004933 btrfs_put_ordered_extent(ordered);
4934 }
4935
Yan Zheng9036c102008-10-30 14:19:41 -04004936 cur_offset = hole_start;
4937 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004938 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004939 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004940 if (IS_ERR(em)) {
4941 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004942 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004943 break;
4944 }
Yan Zheng9036c102008-10-30 14:19:41 -04004945 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004946 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004947 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004948 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004949 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004950
Josef Bacik16e75492013-10-22 12:18:51 -04004951 err = maybe_insert_hole(root, inode, cur_offset,
4952 hole_size);
4953 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004954 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004955 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004956 cur_offset + hole_size - 1, 0);
4957 hole_em = alloc_extent_map();
4958 if (!hole_em) {
4959 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4960 &BTRFS_I(inode)->runtime_flags);
4961 goto next;
4962 }
4963 hole_em->start = cur_offset;
4964 hole_em->len = hole_size;
4965 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004966
Josef Bacik5dc562c2012-08-17 13:14:17 -04004967 hole_em->block_start = EXTENT_MAP_HOLE;
4968 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004969 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004970 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004971 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004972 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004973 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004974
4975 while (1) {
4976 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004977 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004978 write_unlock(&em_tree->lock);
4979 if (err != -EEXIST)
4980 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004981 btrfs_drop_extent_cache(BTRFS_I(inode),
4982 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004983 cur_offset +
4984 hole_size - 1, 0);
4985 }
4986 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004987 }
Josef Bacik16e75492013-10-22 12:18:51 -04004988next:
Yan Zheng9036c102008-10-30 14:19:41 -04004989 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004990 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004991 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004992 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004993 break;
4994 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004995 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004996 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4997 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004998 return err;
4999}
5000
Eric Sandeen3972f262013-01-12 02:57:22 +00005001static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005002{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005003 struct btrfs_root *root = BTRFS_I(inode)->root;
5004 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005005 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005006 loff_t newsize = attr->ia_size;
5007 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005008 int ret;
5009
Eric Sandeen3972f262013-01-12 02:57:22 +00005010 /*
5011 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5012 * special case where we need to update the times despite not having
5013 * these flags set. For all other operations the VFS set these flags
5014 * explicitly if it wants a timestamp update.
5015 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005016 if (newsize != oldsize) {
5017 inode_inc_iversion(inode);
5018 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5019 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005020 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005021 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005022
Josef Bacika41ad392011-01-31 15:30:16 -05005023 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005024 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005025 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005026 * This is to ensure the snapshot captures a fully consistent
5027 * state of this file - if the snapshot captures this expanding
5028 * truncation, it must capture all writes that happened before
5029 * this truncation.
5030 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005031 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005032 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005033 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005034 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005035 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005036 }
Yan, Zheng80825102009-11-12 09:35:36 +00005037
Miao Xief4a2f4c2011-12-14 20:12:01 -05005038 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005039 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005040 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005041 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005042 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005043
5044 i_size_write(inode, newsize);
5045 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305046 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005047 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005048 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005049 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005050 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005051
Josef Bacika41ad392011-01-31 15:30:16 -05005052 /*
5053 * We're truncating a file that used to have good data down to
5054 * zero. Make sure it gets into the ordered flush list so that
5055 * any new writes get down to disk quickly.
5056 */
5057 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005058 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5059 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005060
Josef Bacikf3fe8202013-01-07 17:03:21 -05005061 /*
5062 * 1 for the orphan item we're going to add
5063 * 1 for the orphan item deletion.
5064 */
5065 trans = btrfs_start_transaction(root, 2);
5066 if (IS_ERR(trans))
5067 return PTR_ERR(trans);
5068
5069 /*
5070 * We need to do this in case we fail at _any_ point during the
5071 * actual truncate. Once we do the truncate_setsize we could
5072 * invalidate pages which forces any outstanding ordered io to
5073 * be instantly completed which will give us extents that need
5074 * to be truncated. If we fail to get an orphan inode down we
5075 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005076 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005077 * will be consistent.
5078 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005079 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005080 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005081 if (ret)
5082 return ret;
5083
Josef Bacika41ad392011-01-31 15:30:16 -05005084 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5085 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005086
5087 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005088 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005089 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005090 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005091
Josef Bacika41ad392011-01-31 15:30:16 -05005092 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005093 if (ret && inode->i_nlink) {
5094 int err;
5095
Liu Bo19fd2df2016-12-01 13:01:02 -08005096 /* To get a stable disk_i_size */
5097 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5098 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005099 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005100 return err;
5101 }
5102
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005103 /*
5104 * failed to truncate, disk_i_size is only adjusted down
5105 * as we remove extents, so it should represent the true
5106 * size of the inode, so reset the in memory size and
5107 * delete our orphan entry.
5108 */
5109 trans = btrfs_join_transaction(root);
5110 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005111 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005112 return ret;
5113 }
5114 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005115 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005116 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005117 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005118 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005119 }
Yan, Zheng80825102009-11-12 09:35:36 +00005120 }
5121
Josef Bacika41ad392011-01-31 15:30:16 -05005122 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005123}
5124
Chris Mason39279cc2007-06-12 06:35:45 -04005125static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5126{
David Howells2b0143b2015-03-17 22:25:59 +00005127 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005128 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 int err;
5130
Li Zefanb83cc962010-12-20 16:04:08 +08005131 if (btrfs_root_readonly(root))
5132 return -EROFS;
5133
Jan Kara31051c82016-05-26 16:55:18 +02005134 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005135 if (err)
5136 return err;
5137
Chris Mason5a3f23d2009-03-31 13:27:11 -04005138 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005139 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005140 if (err)
5141 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005142 }
Yan Zheng9036c102008-10-30 14:19:41 -04005143
Christoph Hellwig10257742010-06-04 11:30:02 +02005144 if (attr->ia_valid) {
5145 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005146 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005147 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005148
Josef Bacik22c44fe2011-11-30 10:45:38 -05005149 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005150 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005151 }
5152
Chris Mason39279cc2007-06-12 06:35:45 -04005153 return err;
5154}
Chris Mason61295eb2008-01-14 16:24:38 -05005155
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005156/*
5157 * While truncating the inode pages during eviction, we get the VFS calling
5158 * btrfs_invalidatepage() against each page of the inode. This is slow because
5159 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5160 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5161 * extent_state structures over and over, wasting lots of time.
5162 *
5163 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5164 * those expensive operations on a per page basis and do only the ordered io
5165 * finishing, while we release here the extent_map and extent_state structures,
5166 * without the excessive merging and splitting.
5167 */
5168static void evict_inode_truncate_pages(struct inode *inode)
5169{
5170 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5171 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5172 struct rb_node *node;
5173
5174 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005175 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005176
5177 write_lock(&map_tree->lock);
5178 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5179 struct extent_map *em;
5180
5181 node = rb_first(&map_tree->map);
5182 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005183 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5184 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005185 remove_extent_mapping(map_tree, em);
5186 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005187 if (need_resched()) {
5188 write_unlock(&map_tree->lock);
5189 cond_resched();
5190 write_lock(&map_tree->lock);
5191 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192 }
5193 write_unlock(&map_tree->lock);
5194
Filipe Manana6ca07092015-05-26 00:55:42 +01005195 /*
5196 * Keep looping until we have no more ranges in the io tree.
5197 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005198 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5199 * still in progress (unlocked the pages in the bio but did not yet
5200 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005201 * ranges can still be locked and eviction started because before
5202 * submitting those bios, which are executed by a separate task (work
5203 * queue kthread), inode references (inode->i_count) were not taken
5204 * (which would be dropped in the end io callback of each bio).
5205 * Therefore here we effectively end up waiting for those bios and
5206 * anyone else holding locked ranges without having bumped the inode's
5207 * reference count - if we don't do it, when they access the inode's
5208 * io_tree to unlock a range it may be too late, leading to an
5209 * use-after-free issue.
5210 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005211 spin_lock(&io_tree->lock);
5212 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5213 struct extent_state *state;
5214 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005215 u64 start;
5216 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217
5218 node = rb_first(&io_tree->state);
5219 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 start = state->start;
5221 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 spin_unlock(&io_tree->lock);
5223
David Sterbaff13db42015-12-03 14:30:40 +01005224 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005225
5226 /*
5227 * If still has DELALLOC flag, the extent didn't reach disk,
5228 * and its reserved space won't be freed by delayed_ref.
5229 * So we need to free its reserved space here.
5230 * (Refer to comment in btrfs_invalidatepage, case 2)
5231 *
5232 * Note, end is the bytenr of last byte, so we need + 1 here.
5233 */
5234 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005235 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005236
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005238 EXTENT_LOCKED | EXTENT_DIRTY |
5239 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5240 EXTENT_DEFRAG, 1, 1,
5241 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242
Filipe Manana7064dd52014-08-08 02:47:05 +01005243 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005244 spin_lock(&io_tree->lock);
5245 }
5246 spin_unlock(&io_tree->lock);
5247}
5248
Al Virobd555972010-06-07 11:35:40 -04005249void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005252 struct btrfs_trans_handle *trans;
5253 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005254 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005255 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005256 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005257 int ret;
5258
liubo1abe9b82011-03-24 11:18:59 +00005259 trace_btrfs_inode_evict(inode);
5260
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005261 if (!root) {
5262 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5263 return;
5264 }
5265
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005266 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005267
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005268 evict_inode_truncate_pages(inode);
5269
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005270 if (inode->i_nlink &&
5271 ((btrfs_root_refs(&root->root_item) != 0 &&
5272 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005273 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005274 goto no_delete;
5275
Chris Mason39279cc2007-06-12 06:35:45 -04005276 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005277 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005278 goto no_delete;
5279 }
Al Virobd555972010-06-07 11:35:40 -04005280 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005281 if (!special_file(inode->i_mode))
5282 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005283
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005284 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005285
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005286 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005287 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005288 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005289 goto no_delete;
5290 }
5291
Yan, Zheng76dda932009-09-21 16:00:26 -04005292 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005293 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5294 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005295 goto no_delete;
5296 }
5297
Nikolay Borisovaa790212017-01-10 20:35:40 +02005298 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005299 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005300 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005301 goto no_delete;
5302 }
5303
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005304 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005305 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005306 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005307 goto no_delete;
5308 }
Josef Bacik4a338542011-08-29 11:01:31 -04005309 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005310 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005311 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005312
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005313 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005314
Josef Bacik4289a662011-08-05 13:22:24 -04005315 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005316 * This is a bit simpler than btrfs_truncate since we've already
5317 * reserved our space for our orphan item in the unlink, so we just
5318 * need to reserve some slack space in case we add bytes and update
5319 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005320 */
Yan, Zheng80825102009-11-12 09:35:36 +00005321 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005322 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5323 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005324
Josef Bacik726c35f2011-09-26 15:46:06 -04005325 /*
5326 * Try and steal from the global reserve since we will
5327 * likely not use this space anyway, we want to try as
5328 * hard as possible to get this to work.
5329 */
5330 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005331 steal_from_global++;
5332 else
5333 steal_from_global = 0;
5334 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005335
Josef Bacik3bce8762015-02-24 12:35:51 -08005336 /*
5337 * steal_from_global == 0: we reserved stuff, hooray!
5338 * steal_from_global == 1: we didn't reserve stuff, boo!
5339 * steal_from_global == 2: we've committed, still not a lot of
5340 * room but maybe we'll have room in the global reserve this
5341 * time.
5342 * steal_from_global == 3: abandon all hope!
5343 */
5344 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005345 btrfs_warn(fs_info,
5346 "Could not get space for a delete, will truncate on mount %d",
5347 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005348 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005349 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005350 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005351 }
5352
Miao Xie0e8c36a2012-12-19 06:59:51 +00005353 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005354 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005355 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005356 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005357 goto no_delete;
5358 }
5359
Josef Bacik3bce8762015-02-24 12:35:51 -08005360 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005361 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005362 * sure there is room to do it, if not we need to commit and try
5363 * again.
5364 */
5365 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005366 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005367 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005368 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005369 else
5370 ret = -ENOSPC;
5371 }
5372
5373 /*
5374 * Couldn't steal from the global reserve, we have too much
5375 * pending stuff built up, commit the transaction and try it
5376 * again.
5377 */
5378 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005379 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005380 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005381 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005382 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005383 goto no_delete;
5384 }
5385 continue;
5386 } else {
5387 steal_from_global = 0;
5388 }
5389
Josef Bacik4289a662011-08-05 13:22:24 -04005390 trans->block_rsv = rsv;
5391
Yan, Zhengd68fc572010-05-16 10:49:58 -04005392 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005393 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005394 break;
5395
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005396 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005397 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005398 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005399 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005400 }
5401
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005402 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005403
Josef Bacik4ef31a42013-08-13 14:10:08 -04005404 /*
5405 * Errors here aren't a big deal, it just means we leave orphan items
5406 * in the tree. They will be cleaned up on the next mount.
5407 */
Yan, Zheng80825102009-11-12 09:35:36 +00005408 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005409 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005410 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005411 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005412 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005413 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005415 trans->block_rsv = &fs_info->trans_block_rsv;
5416 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005417 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005418 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005419
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005420 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005421 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005422no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005423 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005424 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005425}
5426
5427/*
5428 * this returns the key found in the dir entry in the location pointer.
5429 * If no dir entries were found, location->objectid is 0.
5430 */
5431static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5432 struct btrfs_key *location)
5433{
5434 const char *name = dentry->d_name.name;
5435 int namelen = dentry->d_name.len;
5436 struct btrfs_dir_item *di;
5437 struct btrfs_path *path;
5438 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005439 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005440
5441 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005442 if (!path)
5443 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005444
David Sterbaf85b7372017-01-20 14:54:07 +01005445 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5446 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005447 if (IS_ERR(di))
5448 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005449
David Sterbac7040052011-04-19 18:00:01 +02005450 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005451 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005452
Chris Mason5f39d392007-10-15 16:14:19 -04005453 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005454 if (location->type != BTRFS_INODE_ITEM_KEY &&
5455 location->type != BTRFS_ROOT_ITEM_KEY) {
5456 btrfs_warn(root->fs_info,
5457"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5458 __func__, name, btrfs_ino(BTRFS_I(dir)),
5459 location->objectid, location->type, location->offset);
5460 goto out_err;
5461 }
Chris Mason39279cc2007-06-12 06:35:45 -04005462out:
Chris Mason39279cc2007-06-12 06:35:45 -04005463 btrfs_free_path(path);
5464 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005465out_err:
5466 location->objectid = 0;
5467 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005468}
5469
5470/*
5471 * when we hit a tree root in a directory, the btrfs part of the inode
5472 * needs to be changed to reflect the root directory of the tree root. This
5473 * is kind of like crossing a mount point.
5474 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005475static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005476 struct inode *dir,
5477 struct dentry *dentry,
5478 struct btrfs_key *location,
5479 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005480{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 struct btrfs_path *path;
5482 struct btrfs_root *new_root;
5483 struct btrfs_root_ref *ref;
5484 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005485 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 int ret;
5487 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005488
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 path = btrfs_alloc_path();
5490 if (!path) {
5491 err = -ENOMEM;
5492 goto out;
5493 }
Chris Mason39279cc2007-06-12 06:35:45 -04005494
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005496 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5497 key.type = BTRFS_ROOT_REF_KEY;
5498 key.offset = location->objectid;
5499
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005500 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 if (ret) {
5502 if (ret < 0)
5503 err = ret;
5504 goto out;
5505 }
Chris Mason39279cc2007-06-12 06:35:45 -04005506
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 leaf = path->nodes[0];
5508 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005509 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5511 goto out;
5512
5513 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5514 (unsigned long)(ref + 1),
5515 dentry->d_name.len);
5516 if (ret)
5517 goto out;
5518
David Sterbab3b4aa72011-04-21 01:20:15 +02005519 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005521 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 if (IS_ERR(new_root)) {
5523 err = PTR_ERR(new_root);
5524 goto out;
5525 }
5526
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 *sub_root = new_root;
5528 location->objectid = btrfs_root_dirid(&new_root->root_item);
5529 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005530 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 err = 0;
5532out:
5533 btrfs_free_path(path);
5534 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005535}
5536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537static void inode_tree_add(struct inode *inode)
5538{
5539 struct btrfs_root *root = BTRFS_I(inode)->root;
5540 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005541 struct rb_node **p;
5542 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005543 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005544 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545
Al Viro1d3382cb2010-10-23 15:19:20 -04005546 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005547 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005548 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005549 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005550 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 while (*p) {
5552 parent = *p;
5553 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5554
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005555 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005556 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005557 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005558 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 else {
5560 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005561 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005562 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005563 RB_CLEAR_NODE(parent);
5564 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005565 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566 }
5567 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005568 rb_link_node(new, parent, p);
5569 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 spin_unlock(&root->inode_lock);
5571}
5572
5573static void inode_tree_del(struct inode *inode)
5574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005575 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005577 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005579 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005583 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005585 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005586
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005587 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005588 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005589 spin_lock(&root->inode_lock);
5590 empty = RB_EMPTY_ROOT(&root->inode_tree);
5591 spin_unlock(&root->inode_lock);
5592 if (empty)
5593 btrfs_add_dead_root(root);
5594 }
5595}
5596
Jeff Mahoney143bede2012-03-01 14:56:26 +01005597void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005598{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005599 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005600 struct rb_node *node;
5601 struct rb_node *prev;
5602 struct btrfs_inode *entry;
5603 struct inode *inode;
5604 u64 objectid = 0;
5605
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005606 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005607 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005608
5609 spin_lock(&root->inode_lock);
5610again:
5611 node = root->inode_tree.rb_node;
5612 prev = NULL;
5613 while (node) {
5614 prev = node;
5615 entry = rb_entry(node, struct btrfs_inode, rb_node);
5616
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005617 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005619 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005620 node = node->rb_right;
5621 else
5622 break;
5623 }
5624 if (!node) {
5625 while (prev) {
5626 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005627 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005628 node = prev;
5629 break;
5630 }
5631 prev = rb_next(prev);
5632 }
5633 }
5634 while (node) {
5635 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005636 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005637 inode = igrab(&entry->vfs_inode);
5638 if (inode) {
5639 spin_unlock(&root->inode_lock);
5640 if (atomic_read(&inode->i_count) > 1)
5641 d_prune_aliases(inode);
5642 /*
Al Viro45321ac2010-06-07 13:43:19 -04005643 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005644 * the inode cache when its usage count
5645 * hits zero.
5646 */
5647 iput(inode);
5648 cond_resched();
5649 spin_lock(&root->inode_lock);
5650 goto again;
5651 }
5652
5653 if (cond_resched_lock(&root->inode_lock))
5654 goto again;
5655
5656 node = rb_next(node);
5657 }
5658 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005659}
5660
Chris Masone02119d2008-09-05 16:13:11 -04005661static int btrfs_init_locked_inode(struct inode *inode, void *p)
5662{
5663 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005664 inode->i_ino = args->location->objectid;
5665 memcpy(&BTRFS_I(inode)->location, args->location,
5666 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005667 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 return 0;
5669}
5670
5671static int btrfs_find_actor(struct inode *inode, void *opaque)
5672{
5673 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005674 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005675 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005676}
5677
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005679 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005681{
5682 struct inode *inode;
5683 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005684 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005685
Chris Mason90d3e592014-01-09 17:28:00 -08005686 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005687 args.root = root;
5688
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005689 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005690 btrfs_init_locked_inode,
5691 (void *)&args);
5692 return inode;
5693}
5694
Balaji Rao1a54ef82008-07-21 02:01:04 +05305695/* Get an inode object given its location and corresponding root.
5696 * Returns in *is_new if the inode was read from disk
5697 */
5698struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005699 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305700{
5701 struct inode *inode;
5702
Chris Mason90d3e592014-01-09 17:28:00 -08005703 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305704 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005705 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305706
5707 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005708 int ret;
5709
5710 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005711 if (!is_bad_inode(inode)) {
5712 inode_tree_add(inode);
5713 unlock_new_inode(inode);
5714 if (new)
5715 *new = 1;
5716 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005717 unlock_new_inode(inode);
5718 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005719 ASSERT(ret < 0);
5720 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005721 }
5722 }
5723
Balaji Rao1a54ef82008-07-21 02:01:04 +05305724 return inode;
5725}
5726
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005727static struct inode *new_simple_dir(struct super_block *s,
5728 struct btrfs_key *key,
5729 struct btrfs_root *root)
5730{
5731 struct inode *inode = new_inode(s);
5732
5733 if (!inode)
5734 return ERR_PTR(-ENOMEM);
5735
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736 BTRFS_I(inode)->root = root;
5737 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005738 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005739
5740 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005741 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005742 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 inode->i_fop = &simple_dir_operations;
5744 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005745 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305746 inode->i_atime = inode->i_mtime;
5747 inode->i_ctime = inode->i_mtime;
5748 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749
5750 return inode;
5751}
5752
Chris Mason3de45862008-11-17 21:02:50 -05005753struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005754{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005755 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005756 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005758 struct btrfs_root *sub_root = root;
5759 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005760 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005761 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005762
5763 if (dentry->d_name.len > BTRFS_NAME_LEN)
5764 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005765
Jeff Layton39e3c952012-11-28 11:30:53 -05005766 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005767 if (ret < 0)
5768 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005769
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005770 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005771 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005772
5773 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005774 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005775 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005776 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005777
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005778 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005779 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005780 &location, &sub_root);
5781 if (ret < 0) {
5782 if (ret != -ENOENT)
5783 inode = ERR_PTR(ret);
5784 else
5785 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5786 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005787 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005788 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005789 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005790
Julia Lawall34d19ba2011-01-24 19:55:19 +00005791 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005793 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005794 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005795 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005796 if (ret) {
5797 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005798 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005799 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005800 }
5801
Chris Mason3de45862008-11-17 21:02:50 -05005802 return inode;
5803}
5804
Nick Pigginfe15ce42011-01-07 17:49:23 +11005805static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005806{
5807 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005808 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005809
Li Zefan848cce02012-02-21 17:04:28 +08005810 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005811 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005812
Li Zefan848cce02012-02-21 17:04:28 +08005813 if (inode) {
5814 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005815 if (btrfs_root_refs(&root->root_item) == 0)
5816 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005817
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005818 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005819 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005820 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005821 return 0;
5822}
5823
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005824static void btrfs_dentry_release(struct dentry *dentry)
5825{
Daeseok Youn944a4512014-04-14 15:37:02 +09005826 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005827}
5828
Chris Mason3de45862008-11-17 21:02:50 -05005829static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005830 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005831{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005832 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005833
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005834 inode = btrfs_lookup_dentry(dir, dentry);
5835 if (IS_ERR(inode)) {
5836 if (PTR_ERR(inode) == -ENOENT)
5837 inode = NULL;
5838 else
5839 return ERR_CAST(inode);
5840 }
5841
Al Viro41d28bc2014-10-12 22:24:21 -04005842 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005843}
5844
Miao Xie16cdcec2011-04-22 18:12:22 +08005845unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005846 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5847};
5848
Josef Bacik23b5ec72017-07-24 15:14:25 -04005849/*
5850 * All this infrastructure exists because dir_emit can fault, and we are holding
5851 * the tree lock when doing readdir. For now just allocate a buffer and copy
5852 * our information into that, and then dir_emit from the buffer. This is
5853 * similar to what NFS does, only we don't keep the buffer around in pagecache
5854 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5855 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5856 * tree lock.
5857 */
5858static int btrfs_opendir(struct inode *inode, struct file *file)
5859{
5860 struct btrfs_file_private *private;
5861
5862 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5863 if (!private)
5864 return -ENOMEM;
5865 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5866 if (!private->filldir_buf) {
5867 kfree(private);
5868 return -ENOMEM;
5869 }
5870 file->private_data = private;
5871 return 0;
5872}
5873
5874struct dir_entry {
5875 u64 ino;
5876 u64 offset;
5877 unsigned type;
5878 int name_len;
5879};
5880
5881static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5882{
5883 while (entries--) {
5884 struct dir_entry *entry = addr;
5885 char *name = (char *)(entry + 1);
5886
5887 ctx->pos = entry->offset;
5888 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5889 entry->type))
5890 return 1;
5891 addr += sizeof(struct dir_entry) + entry->name_len;
5892 ctx->pos++;
5893 }
5894 return 0;
5895}
5896
Al Viro9cdda8d2013-05-22 16:48:09 -04005897static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005898{
Al Viro9cdda8d2013-05-22 16:48:09 -04005899 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005900 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005901 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005902 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005903 struct btrfs_dir_item *di;
5904 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005905 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005906 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005907 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005908 struct list_head ins_list;
5909 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005910 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005911 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005913 char *name_ptr;
5914 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005915 int entries = 0;
5916 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005917 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005918 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005919
Al Viro9cdda8d2013-05-22 16:48:09 -04005920 if (!dir_emit_dots(file, ctx))
5921 return 0;
5922
David Woodhouse49593bf2008-08-17 17:08:36 +01005923 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005924 if (!path)
5925 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005926
Josef Bacik23b5ec72017-07-24 15:14:25 -04005927 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005928 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005929
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005930 INIT_LIST_HEAD(&ins_list);
5931 INIT_LIST_HEAD(&del_list);
5932 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005933
Josef Bacik23b5ec72017-07-24 15:14:25 -04005934again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005935 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005936 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005937 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005938
Chris Mason39279cc2007-06-12 06:35:45 -04005939 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5940 if (ret < 0)
5941 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005942
5943 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005944 struct dir_entry *entry;
5945
Chris Mason5f39d392007-10-15 16:14:19 -04005946 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005947 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005948 if (slot >= btrfs_header_nritems(leaf)) {
5949 ret = btrfs_next_leaf(root, path);
5950 if (ret < 0)
5951 goto err;
5952 else if (ret > 0)
5953 break;
5954 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005955 }
Chris Mason3de45862008-11-17 21:02:50 -05005956
Chris Mason5f39d392007-10-15 16:14:19 -04005957 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5958
5959 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005960 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005961 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005962 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005963 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005964 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005965 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005966 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005967 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005968 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005969 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005970
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005971 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005972 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5973 PAGE_SIZE) {
5974 btrfs_release_path(path);
5975 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5976 if (ret)
5977 goto nopos;
5978 addr = private->filldir_buf;
5979 entries = 0;
5980 total_len = 0;
5981 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005982 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983
5984 entry = addr;
5985 entry->name_len = name_len;
5986 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005987 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5988 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005989 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005990 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005991 entry->ino = location.objectid;
5992 entry->offset = found_key.offset;
5993 entries++;
5994 addr += sizeof(struct dir_entry) + name_len;
5995 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005996next:
5997 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005998 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005999 btrfs_release_path(path);
6000
6001 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6002 if (ret)
6003 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006004
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006005 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006006 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006007 goto nopos;
6008
Zach Browndb62efb2013-07-11 16:19:42 -07006009 /*
6010 * Stop new entries from being returned after we return the last
6011 * entry.
6012 *
6013 * New directory entries are assigned a strictly increasing
6014 * offset. This means that new entries created during readdir
6015 * are *guaranteed* to be seen in the future by that readdir.
6016 * This has broken buggy programs which operate on names as
6017 * they're returned by readdir. Until we re-use freed offsets
6018 * we have this hack to stop new entries from being returned
6019 * under the assumption that they'll never reach this huge
6020 * offset.
6021 *
6022 * This is being careful not to overflow 32bit loff_t unless the
6023 * last entry requires it because doing so has broken 32bit apps
6024 * in the past.
6025 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006026 if (ctx->pos >= INT_MAX)
6027 ctx->pos = LLONG_MAX;
6028 else
6029 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006030nopos:
6031 ret = 0;
6032err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006033 if (put)
6034 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006035 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006036 return ret;
6037}
6038
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006039int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006040{
6041 struct btrfs_root *root = BTRFS_I(inode)->root;
6042 struct btrfs_trans_handle *trans;
6043 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006044 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006045
Josef Bacik72ac3c02012-05-23 14:13:11 -04006046 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006047 return 0;
6048
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006049 if (btrfs_fs_closing(root->fs_info) &&
6050 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006051 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006052
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006053 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006054 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006055 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006056 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006057 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006058 if (IS_ERR(trans))
6059 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006060 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006061 }
6062 return ret;
6063}
6064
6065/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006066 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006067 * inode changes. But, it is most likely to find the inode in cache.
6068 * FIXME, needs more benchmarking...there are no reasons other than performance
6069 * to keep or drop this code.
6070 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006071static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006072{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006073 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006074 struct btrfs_root *root = BTRFS_I(inode)->root;
6075 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006076 int ret;
6077
Josef Bacik72ac3c02012-05-23 14:13:11 -04006078 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006079 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006080
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006081 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006082 if (IS_ERR(trans))
6083 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006084
6085 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006086 if (ret && ret == -ENOSPC) {
6087 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006088 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006089 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006090 if (IS_ERR(trans))
6091 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006092
Chris Mason94b60442010-05-26 11:02:00 -04006093 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006094 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006095 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006096 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006097 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006098
6099 return ret;
6100}
6101
6102/*
6103 * This is a copy of file_update_time. We need this so we can return error on
6104 * ENOSPC for updating the inode in the case of file write and mmap writes.
6105 */
Josef Bacike41f9412012-03-26 09:46:47 -04006106static int btrfs_update_time(struct inode *inode, struct timespec *now,
6107 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006108{
Alexander Block2bc5565282012-06-15 09:49:33 +02006109 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006110 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006111
6112 if (btrfs_root_readonly(root))
6113 return -EROFS;
6114
Josef Bacike41f9412012-03-26 09:46:47 -04006115 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006116 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006117 if (flags & S_CTIME)
6118 inode->i_ctime = *now;
6119 if (flags & S_MTIME)
6120 inode->i_mtime = *now;
6121 if (flags & S_ATIME)
6122 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006123 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006124}
6125
Chris Masond352ac62008-09-29 15:18:18 -04006126/*
6127 * find the highest existing sequence number in a directory
6128 * and then set the in-memory index_cnt variable to reflect
6129 * free sequence numbers
6130 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006131static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006132{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006133 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006134 struct btrfs_key key, found_key;
6135 struct btrfs_path *path;
6136 struct extent_buffer *leaf;
6137 int ret;
6138
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006139 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006140 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006141 key.offset = (u64)-1;
6142
6143 path = btrfs_alloc_path();
6144 if (!path)
6145 return -ENOMEM;
6146
6147 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6148 if (ret < 0)
6149 goto out;
6150 /* FIXME: we should be able to handle this */
6151 if (ret == 0)
6152 goto out;
6153 ret = 0;
6154
6155 /*
6156 * MAGIC NUMBER EXPLANATION:
6157 * since we search a directory based on f_pos we have to start at 2
6158 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6159 * else has to start at 2
6160 */
6161 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006162 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006163 goto out;
6164 }
6165
6166 path->slots[0]--;
6167
6168 leaf = path->nodes[0];
6169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6170
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006171 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006172 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006173 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006174 goto out;
6175 }
6176
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006177 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006178out:
6179 btrfs_free_path(path);
6180 return ret;
6181}
6182
Chris Masond352ac62008-09-29 15:18:18 -04006183/*
6184 * helper to find a free sequence number in a given directory. This current
6185 * code is very simple, later versions will do smarter things in the btree
6186 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006187int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006188{
6189 int ret = 0;
6190
Nikolay Borisov877574e2017-02-20 13:50:33 +02006191 if (dir->index_cnt == (u64)-1) {
6192 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006193 if (ret) {
6194 ret = btrfs_set_inode_index_count(dir);
6195 if (ret)
6196 return ret;
6197 }
Josef Bacikaec74772008-07-24 12:12:38 -04006198 }
6199
Nikolay Borisov877574e2017-02-20 13:50:33 +02006200 *index = dir->index_cnt;
6201 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006202
6203 return ret;
6204}
6205
Chris Masonb0d5d102014-09-08 13:08:51 -07006206static int btrfs_insert_inode_locked(struct inode *inode)
6207{
6208 struct btrfs_iget_args args;
6209 args.location = &BTRFS_I(inode)->location;
6210 args.root = BTRFS_I(inode)->root;
6211
6212 return insert_inode_locked4(inode,
6213 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6214 btrfs_find_actor, &args);
6215}
6216
Anand Jain19aee8d2017-07-18 17:37:05 +08006217/*
6218 * Inherit flags from the parent inode.
6219 *
6220 * Currently only the compression flags and the cow flags are inherited.
6221 */
6222static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6223{
6224 unsigned int flags;
6225
6226 if (!dir)
6227 return;
6228
6229 flags = BTRFS_I(dir)->flags;
6230
6231 if (flags & BTRFS_INODE_NOCOMPRESS) {
6232 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6233 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6234 } else if (flags & BTRFS_INODE_COMPRESS) {
6235 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6236 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6237 }
6238
6239 if (flags & BTRFS_INODE_NODATACOW) {
6240 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6241 if (S_ISREG(inode->i_mode))
6242 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6243 }
6244
6245 btrfs_update_iflags(inode);
6246}
6247
Chris Mason39279cc2007-06-12 06:35:45 -04006248static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6249 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006250 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006251 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006252 u64 ref_objectid, u64 objectid,
6253 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006255 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006256 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006257 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006258 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006259 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006260 struct btrfs_inode_ref *ref;
6261 struct btrfs_key key[2];
6262 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006263 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006264 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006265 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006266
Chris Mason5f39d392007-10-15 16:14:19 -04006267 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006268 if (!path)
6269 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006270
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006271 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006272 if (!inode) {
6273 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006274 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006275 }
Chris Mason39279cc2007-06-12 06:35:45 -04006276
Li Zefan581bb052011-04-20 10:06:11 +08006277 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006278 * O_TMPFILE, set link count to 0, so that after this point,
6279 * we fill in an inode item with the correct link count.
6280 */
6281 if (!name)
6282 set_nlink(inode, 0);
6283
6284 /*
Li Zefan581bb052011-04-20 10:06:11 +08006285 * we have to initialize this early, so we can reclaim the inode
6286 * number if we fail afterwards in this function.
6287 */
6288 inode->i_ino = objectid;
6289
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006290 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006291 trace_btrfs_inode_request(dir);
6292
Nikolay Borisov877574e2017-02-20 13:50:33 +02006293 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006294 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006295 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006296 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006297 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006298 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 } else if (dir) {
6300 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006301 }
6302 /*
6303 * index_cnt is ignored for everything but a dir,
6304 * btrfs_get_inode_index_count has an explanation for the magic
6305 * number
6306 */
6307 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006308 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006309 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006310 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006311 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006312
Josef Bacik5dc562c2012-08-17 13:14:17 -04006313 /*
6314 * We could have gotten an inode number from somebody who was fsynced
6315 * and then removed in this same transaction, so let's just set full
6316 * sync since it will be a full sync anyway and this will blow away the
6317 * old info in the log.
6318 */
6319 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6320
Chris Mason9c583092008-01-29 15:15:18 -05006321 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006322 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006323 key[0].offset = 0;
6324
Chris Mason9c583092008-01-29 15:15:18 -05006325 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006326
6327 if (name) {
6328 /*
6329 * Start new inodes with an inode_ref. This is slightly more
6330 * efficient for small numbers of hard links since they will
6331 * be packed into one item. Extended refs will kick in if we
6332 * add more hard links than can fit in the ref item.
6333 */
6334 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006335 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006336 key[1].offset = ref_objectid;
6337
6338 sizes[1] = name_len + sizeof(*ref);
6339 }
Chris Mason9c583092008-01-29 15:15:18 -05006340
Chris Masonb0d5d102014-09-08 13:08:51 -07006341 location = &BTRFS_I(inode)->location;
6342 location->objectid = objectid;
6343 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006344 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006345
6346 ret = btrfs_insert_inode_locked(inode);
6347 if (ret < 0)
6348 goto fail;
6349
Chris Masonb9473432009-03-13 11:00:37 -04006350 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006351 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006352 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006353 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006354
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006355 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006356 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306357
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006358 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306359 inode->i_atime = inode->i_mtime;
6360 inode->i_ctime = inode->i_mtime;
6361 BTRFS_I(inode)->i_otime = inode->i_mtime;
6362
Chris Mason5f39d392007-10-15 16:14:19 -04006363 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6364 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006365 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006366 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006367 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006368
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006369 if (name) {
6370 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6371 struct btrfs_inode_ref);
6372 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6373 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6374 ptr = (unsigned long)(ref + 1);
6375 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6376 }
Chris Mason9c583092008-01-29 15:15:18 -05006377
Chris Mason5f39d392007-10-15 16:14:19 -04006378 btrfs_mark_buffer_dirty(path->nodes[0]);
6379 btrfs_free_path(path);
6380
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006381 btrfs_inherit_iflags(inode, dir);
6382
Al Viro569254b2011-07-24 17:08:40 -04006383 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006384 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006385 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006386 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006387 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6388 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006389 }
6390
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006391 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006392
6393 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006394 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006395
Alexander Block8ea05e32012-07-25 17:35:53 +02006396 btrfs_update_root_times(trans, root);
6397
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006398 ret = btrfs_inode_inherit_props(trans, inode, dir);
6399 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006401 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006402 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006403
Chris Mason39279cc2007-06-12 06:35:45 -04006404 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006405
6406fail_unlock:
6407 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006408fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006409 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006410 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006411 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006412 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006413 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006414}
6415
6416static inline u8 btrfs_inode_type(struct inode *inode)
6417{
6418 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6419}
6420
Chris Masond352ac62008-09-29 15:18:18 -04006421/*
6422 * utility function to add 'inode' into 'parent_inode' with
6423 * a give name and a given sequence number.
6424 * if 'add_backref' is true, also insert a backref from the
6425 * inode to the parent directory.
6426 */
Chris Masone02119d2008-09-05 16:13:11 -04006427int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006428 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006429 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006430{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006431 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006432 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006433 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006434 struct btrfs_root *root = parent_inode->root;
6435 u64 ino = btrfs_ino(inode);
6436 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006437
Li Zefan33345d012011-04-20 10:31:50 +08006438 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006439 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006440 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006441 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006442 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006443 key.offset = 0;
6444 }
Chris Mason39279cc2007-06-12 06:35:45 -04006445
Li Zefan33345d012011-04-20 10:31:50 +08006446 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006447 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6448 root->root_key.objectid, parent_ino,
6449 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006450 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006451 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6452 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006453 }
6454
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006455 /* Nothing to clean up yet */
6456 if (ret)
6457 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006458
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6460 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006461 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006462 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006463 goto fail_dir_item;
6464 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006465 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006466 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006467 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006468
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006469 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006470 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006471 inode_inc_iversion(&parent_inode->vfs_inode);
6472 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6473 current_time(&parent_inode->vfs_inode);
6474 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006475 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006476 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006477 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006478
6479fail_dir_item:
6480 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6481 u64 local_index;
6482 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006483 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6484 root->root_key.objectid, parent_ino,
6485 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006486
6487 } else if (add_backref) {
6488 u64 local_index;
6489 int err;
6490
6491 err = btrfs_del_inode_ref(trans, root, name, name_len,
6492 ino, parent_ino, &local_index);
6493 }
6494 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006495}
6496
6497static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006498 struct btrfs_inode *dir, struct dentry *dentry,
6499 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006500{
Josef Bacika1b075d2010-11-19 20:36:11 +00006501 int err = btrfs_add_link(trans, dir, inode,
6502 dentry->d_name.name, dentry->d_name.len,
6503 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006504 if (err > 0)
6505 err = -EEXIST;
6506 return err;
6507}
6508
Josef Bacik618e21d2007-07-11 10:18:17 -04006509static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006510 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006511{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006512 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006513 struct btrfs_trans_handle *trans;
6514 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006515 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006516 int err;
6517 int drop_inode = 0;
6518 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006519 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006520
Josef Bacik9ed74f22009-09-11 16:12:44 -04006521 /*
6522 * 2 for inode item and ref
6523 * 2 for dir items
6524 * 1 for xattr if selinux is on
6525 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006526 trans = btrfs_start_transaction(root, 5);
6527 if (IS_ERR(trans))
6528 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006529
Li Zefan581bb052011-04-20 10:06:11 +08006530 err = btrfs_find_free_ino(root, &objectid);
6531 if (err)
6532 goto out_unlock;
6533
Josef Bacikaec74772008-07-24 12:12:38 -04006534 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006535 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6536 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006537 if (IS_ERR(inode)) {
6538 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006539 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006540 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006541
Casey Schauflerad19db72011-12-15 10:09:07 -05006542 /*
6543 * If the active LSM wants to access the inode during
6544 * d_instantiate it needs these. Smack checks to see
6545 * if the filesystem supports xattrs by looking at the
6546 * ops vector.
6547 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006548 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006549 init_special_inode(inode, inode->i_mode, rdev);
6550
6551 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006552 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006553 goto out_unlock_inode;
6554
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006555 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6556 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006557 if (err) {
6558 goto out_unlock_inode;
6559 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006560 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006561 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006562 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006563 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006564
Josef Bacik618e21d2007-07-11 10:18:17 -04006565out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006566 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006567 btrfs_balance_delayed_items(fs_info);
6568 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 if (drop_inode) {
6570 inode_dec_link_count(inode);
6571 iput(inode);
6572 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006573 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006574
6575out_unlock_inode:
6576 drop_inode = 1;
6577 unlock_new_inode(inode);
6578 goto out_unlock;
6579
Josef Bacik618e21d2007-07-11 10:18:17 -04006580}
6581
Chris Mason39279cc2007-06-12 06:35:45 -04006582static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006583 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006584{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006585 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006586 struct btrfs_trans_handle *trans;
6587 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006588 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006589 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006590 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006591 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006592 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006593
Josef Bacik9ed74f22009-09-11 16:12:44 -04006594 /*
6595 * 2 for inode item and ref
6596 * 2 for dir items
6597 * 1 for xattr if selinux is on
6598 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006599 trans = btrfs_start_transaction(root, 5);
6600 if (IS_ERR(trans))
6601 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006602
Li Zefan581bb052011-04-20 10:06:11 +08006603 err = btrfs_find_free_ino(root, &objectid);
6604 if (err)
6605 goto out_unlock;
6606
Josef Bacikaec74772008-07-24 12:12:38 -04006607 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006608 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6609 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006610 if (IS_ERR(inode)) {
6611 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006613 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006614 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006615 /*
6616 * If the active LSM wants to access the inode during
6617 * d_instantiate it needs these. Smack checks to see
6618 * if the filesystem supports xattrs by looking at the
6619 * ops vector.
6620 */
6621 inode->i_fop = &btrfs_file_operations;
6622 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006623 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006624
6625 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6626 if (err)
6627 goto out_unlock_inode;
6628
6629 err = btrfs_update_inode(trans, root, inode);
6630 if (err)
6631 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006632
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006633 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6634 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006635 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006636 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006637
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006638 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006639 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006640 d_instantiate(dentry, inode);
6641
Chris Mason39279cc2007-06-12 06:35:45 -04006642out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006643 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006644 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006645 inode_dec_link_count(inode);
6646 iput(inode);
6647 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006648 btrfs_balance_delayed_items(fs_info);
6649 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006651
6652out_unlock_inode:
6653 unlock_new_inode(inode);
6654 goto out_unlock;
6655
Chris Mason39279cc2007-06-12 06:35:45 -04006656}
6657
6658static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6659 struct dentry *dentry)
6660{
Filipe Manana271dba42016-01-05 16:24:05 +00006661 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006662 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006663 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006664 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006665 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006666 int err;
6667 int drop_inode = 0;
6668
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006669 /* do not allow sys_link's with other subvols of the same device */
6670 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006671 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006672
Mark Fashehf1863732012-08-08 11:32:27 -07006673 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006674 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006675
Nikolay Borisov877574e2017-02-20 13:50:33 +02006676 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006677 if (err)
6678 goto fail;
6679
Yan, Zhenga22285a2010-05-16 10:48:46 -04006680 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006681 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006682 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006683 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006684 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006685 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006686 if (IS_ERR(trans)) {
6687 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006688 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006689 goto fail;
6690 }
Chris Mason5f39d392007-10-15 16:14:19 -04006691
Miao Xie67de1172013-12-26 13:07:06 +08006692 /* There are several dir indexes for this inode, clear the cache. */
6693 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006694 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006695 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006696 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006697 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006698 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006699
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006700 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6701 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006702
Yan, Zhenga5719522009-09-24 09:17:31 -04006703 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006704 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006705 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006706 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006707 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006708 if (err)
6709 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006710 if (inode->i_nlink == 1) {
6711 /*
6712 * If new hard link count is 1, it's a file created
6713 * with open(2) O_TMPFILE flag.
6714 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006715 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006716 if (err)
6717 goto fail;
6718 }
Al Viro08c422c2011-12-23 07:58:13 -05006719 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006720 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006721 }
Chris Mason39279cc2007-06-12 06:35:45 -04006722
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006723 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006724fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006725 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006726 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006727 if (drop_inode) {
6728 inode_dec_link_count(inode);
6729 iput(inode);
6730 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006731 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006732 return err;
6733}
6734
Al Viro18bb1db2011-07-26 01:41:39 -04006735static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006736{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006737 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006738 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006739 struct btrfs_trans_handle *trans;
6740 struct btrfs_root *root = BTRFS_I(dir)->root;
6741 int err = 0;
6742 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006743 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006744 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006745
Josef Bacik9ed74f22009-09-11 16:12:44 -04006746 /*
6747 * 2 items for inode and ref
6748 * 2 items for dir items
6749 * 1 for xattr if selinux is on
6750 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006751 trans = btrfs_start_transaction(root, 5);
6752 if (IS_ERR(trans))
6753 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006754
Li Zefan581bb052011-04-20 10:06:11 +08006755 err = btrfs_find_free_ino(root, &objectid);
6756 if (err)
6757 goto out_fail;
6758
Josef Bacikaec74772008-07-24 12:12:38 -04006759 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006760 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6761 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006762 if (IS_ERR(inode)) {
6763 err = PTR_ERR(inode);
6764 goto out_fail;
6765 }
Chris Mason5f39d392007-10-15 16:14:19 -04006766
Chris Mason39279cc2007-06-12 06:35:45 -04006767 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006768 /* these must be set before we unlock the inode */
6769 inode->i_op = &btrfs_dir_inode_operations;
6770 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006771
Eric Paris2a7dba32011-02-01 11:05:39 -05006772 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006773 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006774 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006775
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006776 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006777 err = btrfs_update_inode(trans, root, inode);
6778 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006779 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006780
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006781 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6782 dentry->d_name.name,
6783 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006784 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006785 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006786
Chris Mason39279cc2007-06-12 06:35:45 -04006787 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006788 /*
6789 * mkdir is special. We're unlocking after we call d_instantiate
6790 * to avoid a race with nfsd calling d_instantiate.
6791 */
6792 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006793 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006794
6795out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006796 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006797 if (drop_on_err) {
6798 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006799 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006800 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006801 btrfs_balance_delayed_items(fs_info);
6802 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006803 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006804
6805out_fail_inode:
6806 unlock_new_inode(inode);
6807 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006808}
6809
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006810/* Find next extent map of a given extent map, caller needs to ensure locks */
6811static struct extent_map *next_extent_map(struct extent_map *em)
6812{
6813 struct rb_node *next;
6814
6815 next = rb_next(&em->rb_node);
6816 if (!next)
6817 return NULL;
6818 return container_of(next, struct extent_map, rb_node);
6819}
6820
6821static struct extent_map *prev_extent_map(struct extent_map *em)
6822{
6823 struct rb_node *prev;
6824
6825 prev = rb_prev(&em->rb_node);
6826 if (!prev)
6827 return NULL;
6828 return container_of(prev, struct extent_map, rb_node);
6829}
6830
Chris Masond352ac62008-09-29 15:18:18 -04006831/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006832 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006833 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006834 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006835 */
Chris Mason3b951512008-04-17 11:29:12 -04006836static int merge_extent_mapping(struct extent_map_tree *em_tree,
6837 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006838 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006839 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006840{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006841 struct extent_map *prev;
6842 struct extent_map *next;
6843 u64 start;
6844 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006845 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006846
Chris Masone6dcd2d2008-07-17 12:53:50 -04006847 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006848
6849 if (existing->start > map_start) {
6850 next = existing;
6851 prev = prev_extent_map(next);
6852 } else {
6853 prev = existing;
6854 next = next_extent_map(prev);
6855 }
6856
6857 start = prev ? extent_map_end(prev) : em->start;
6858 start = max_t(u64, start, em->start);
6859 end = next ? next->start : extent_map_end(em);
6860 end = min_t(u64, end, extent_map_end(em));
6861 start_diff = start - em->start;
6862 em->start = start;
6863 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006864 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6865 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006866 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006867 em->block_len -= start_diff;
6868 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006869 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006870}
6871
Chris Masonc8b97812008-10-29 14:49:59 -04006872static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006873 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006874 size_t pg_offset, u64 extent_offset,
6875 struct btrfs_file_extent_item *item)
6876{
6877 int ret;
6878 struct extent_buffer *leaf = path->nodes[0];
6879 char *tmp;
6880 size_t max_size;
6881 unsigned long inline_size;
6882 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006883 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006884
6885 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006886 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006887 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6888 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006889 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006890 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006891 if (!tmp)
6892 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006893 ptr = btrfs_file_extent_inline_start(item);
6894
6895 read_extent_buffer(leaf, tmp, ptr, inline_size);
6896
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006897 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006898 ret = btrfs_decompress(compress_type, tmp, page,
6899 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006900
6901 /*
6902 * decompression code contains a memset to fill in any space between the end
6903 * of the uncompressed data and the end of max_size in case the decompressed
6904 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6905 * the end of an inline extent and the beginning of the next block, so we
6906 * cover that region here.
6907 */
6908
6909 if (max_size + pg_offset < PAGE_SIZE) {
6910 char *map = kmap(page);
6911 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6912 kunmap(page);
6913 }
Chris Masonc8b97812008-10-29 14:49:59 -04006914 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006915 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006916}
6917
Chris Masond352ac62008-09-29 15:18:18 -04006918/*
6919 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006920 * the ugly parts come from merging extents from the disk with the in-ram
6921 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006922 * where the in-ram extents might be locked pending data=ordered completion.
6923 *
6924 * This also copies inline extents directly into the page.
6925 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006926struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6927 struct page *page,
6928 size_t pg_offset, u64 start, u64 len,
6929 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006930{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006932 int ret;
6933 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006934 u64 extent_start = 0;
6935 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006936 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006937 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006938 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006939 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006940 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006941 struct extent_buffer *leaf;
6942 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006943 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006944 struct extent_map_tree *em_tree = &inode->extent_tree;
6945 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006946 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006947 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006948
Chris Masona52d9a82007-08-27 16:49:44 -04006949again:
Chris Mason890871b2009-09-02 16:24:52 -04006950 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006951 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006952 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006953 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006954 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006955
Chris Masona52d9a82007-08-27 16:49:44 -04006956 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006957 if (em->start > start || em->start + em->len <= start)
6958 free_extent_map(em);
6959 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006960 free_extent_map(em);
6961 else
6962 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006963 }
David Sterba172ddd62011-04-21 00:48:27 +02006964 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006965 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006966 err = -ENOMEM;
6967 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006968 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006969 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006970 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006971 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006972 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006973 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006974
6975 if (!path) {
6976 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006977 if (!path) {
6978 err = -ENOMEM;
6979 goto out;
6980 }
6981 /*
6982 * Chances are we'll be called again, so go ahead and do
6983 * readahead
6984 */
David Sterbae4058b52015-11-27 16:31:35 +01006985 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006986 }
6987
Chris Mason179e29e2007-11-01 11:28:41 -04006988 ret = btrfs_lookup_file_extent(trans, root, path,
6989 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 if (ret < 0) {
6991 err = ret;
6992 goto out;
6993 }
6994
6995 if (ret != 0) {
6996 if (path->slots[0] == 0)
6997 goto not_found;
6998 path->slots[0]--;
6999 }
7000
Chris Mason5f39d392007-10-15 16:14:19 -04007001 leaf = path->nodes[0];
7002 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007003 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007005 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007006 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007007 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007008 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007009 /*
7010 * If we backup past the first extent we want to move forward
7011 * and see if there is an extent in front of us, otherwise we'll
7012 * say there is a hole for our whole search range which can
7013 * cause problems.
7014 */
7015 extent_end = start;
7016 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007017 }
7018
Chris Mason5f39d392007-10-15 16:14:19 -04007019 found_type = btrfs_file_extent_type(leaf, item);
7020 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007021 if (found_type == BTRFS_FILE_EXTENT_REG ||
7022 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007023 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007024 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007025
7026 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7027 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007028 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7029 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007030 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007031 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007032 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007033
7034 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7035 path->slots[0],
7036 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007037 }
Josef Bacik25a50342013-10-14 12:08:38 -04007038next:
Yan Zheng9036c102008-10-30 14:19:41 -04007039 if (start >= extent_end) {
7040 path->slots[0]++;
7041 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7042 ret = btrfs_next_leaf(root, path);
7043 if (ret < 0) {
7044 err = ret;
7045 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007046 }
Yan Zheng9036c102008-10-30 14:19:41 -04007047 if (ret > 0)
7048 goto not_found;
7049 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007050 }
Yan Zheng9036c102008-10-30 14:19:41 -04007051 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7052 if (found_key.objectid != objectid ||
7053 found_key.type != BTRFS_EXTENT_DATA_KEY)
7054 goto not_found;
7055 if (start + len <= found_key.offset)
7056 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007057 if (start > found_key.offset)
7058 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007059 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007060 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007061 em->len = found_key.offset - start;
7062 goto not_found_em;
7063 }
7064
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007065 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007066 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007067
Yan Zhengd899e052008-10-30 14:25:28 -04007068 if (found_type == BTRFS_FILE_EXTENT_REG ||
7069 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007070 goto insert;
7071 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007072 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007073 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007074 size_t size;
7075 size_t extent_offset;
7076 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007077
Filipe Manana7ffbb592014-06-09 03:48:05 +01007078 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007079 goto out;
Yan689f9342007-10-29 11:41:07 -04007080
Chris Mason514ac8a2014-01-03 21:07:00 -08007081 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007082 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007083 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7084 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007085 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007087 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007088 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007089 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007090 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007091 if (btrfs_file_extent_compression(leaf, item) !=
7092 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007093 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007094 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007095 if (ret) {
7096 err = ret;
7097 goto out;
7098 }
Chris Masonc8b97812008-10-29 14:49:59 -04007099 } else {
7100 map = kmap(page);
7101 read_extent_buffer(leaf, map + pg_offset, ptr,
7102 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007103 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007104 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007105 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007106 copy_size);
7107 }
Chris Masonc8b97812008-10-29 14:49:59 -04007108 kunmap(page);
7109 }
Chris Mason179e29e2007-11-01 11:28:41 -04007110 flush_dcache_page(page);
7111 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007112 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007113 if (!trans) {
7114 kunmap(page);
7115 free_extent_map(em);
7116 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007117
David Sterbab3b4aa72011-04-21 01:20:15 +02007118 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007119 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007120
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007121 if (IS_ERR(trans))
7122 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007123 goto again;
7124 }
Chris Masonc8b97812008-10-29 14:49:59 -04007125 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007126 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007127 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007128 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007129 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007130 }
Chris Masond1310b22008-01-24 16:13:08 -05007131 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007132 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007133 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
7135not_found:
7136 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007137 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007138 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007139not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007140 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007141 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007142insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007143 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007144 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007145 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007146 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7147 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007148 err = -EIO;
7149 goto out;
7150 }
Chris Masond1310b22008-01-24 16:13:08 -05007151
7152 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007153 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007154 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007155 /* it is possible that someone inserted the extent into the tree
7156 * while we had the lock dropped. It is also possible that
7157 * an overlapping map exists in the tree
7158 */
Chris Masona52d9a82007-08-27 16:49:44 -04007159 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007160 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007161
7162 ret = 0;
7163
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007164 existing = search_extent_mapping(em_tree, start, len);
7165 /*
7166 * existing will always be non-NULL, since there must be
7167 * extent causing the -EEXIST.
7168 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007169 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007170 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007171 em->block_start == existing->block_start) {
7172 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007173 * The existing extent map already encompasses the
7174 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007175 */
7176 free_extent_map(em);
7177 em = existing;
7178 err = 0;
7179
7180 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007181 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007182 /*
7183 * The existing extent map is the one nearest to
7184 * the [start, start + len) range which overlaps
7185 */
7186 err = merge_extent_mapping(em_tree, existing,
7187 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007188 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007189 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007190 free_extent_map(em);
7191 em = NULL;
7192 }
7193 } else {
7194 free_extent_map(em);
7195 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007196 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007197 }
Chris Masona52d9a82007-08-27 16:49:44 -04007198 }
Chris Mason890871b2009-09-02 16:24:52 -04007199 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007200out:
liubo1abe9b82011-03-24 11:18:59 +00007201
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007202 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007203
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007204 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007205 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007206 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007207 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007208 err = ret;
7209 }
Chris Masona52d9a82007-08-27 16:49:44 -04007210 if (err) {
7211 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007212 return ERR_PTR(err);
7213 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007214 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007215 return em;
7216}
7217
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007218struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7219 struct page *page,
7220 size_t pg_offset, u64 start, u64 len,
7221 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007222{
7223 struct extent_map *em;
7224 struct extent_map *hole_em = NULL;
7225 u64 range_start = start;
7226 u64 end;
7227 u64 found;
7228 u64 found_end;
7229 int err = 0;
7230
7231 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7232 if (IS_ERR(em))
7233 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007234 /*
7235 * If our em maps to:
7236 * - a hole or
7237 * - a pre-alloc extent,
7238 * there might actually be delalloc bytes behind it.
7239 */
7240 if (em->block_start != EXTENT_MAP_HOLE &&
7241 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7242 return em;
7243 else
7244 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007245
7246 /* check to see if we've wrapped (len == -1 or similar) */
7247 end = start + len;
7248 if (end < start)
7249 end = (u64)-1;
7250 else
7251 end -= 1;
7252
7253 em = NULL;
7254
7255 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007256 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007257 end, len, EXTENT_DELALLOC, 1);
7258 found_end = range_start + found;
7259 if (found_end < range_start)
7260 found_end = (u64)-1;
7261
7262 /*
7263 * we didn't find anything useful, return
7264 * the original results from get_extent()
7265 */
7266 if (range_start > end || found_end <= start) {
7267 em = hole_em;
7268 hole_em = NULL;
7269 goto out;
7270 }
7271
7272 /* adjust the range_start to make sure it doesn't
7273 * go backwards from the start they passed in
7274 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307275 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007276 found = found_end - range_start;
7277
7278 if (found > 0) {
7279 u64 hole_start = start;
7280 u64 hole_len = len;
7281
David Sterba172ddd62011-04-21 00:48:27 +02007282 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007283 if (!em) {
7284 err = -ENOMEM;
7285 goto out;
7286 }
7287 /*
7288 * when btrfs_get_extent can't find anything it
7289 * returns one huge hole
7290 *
7291 * make sure what it found really fits our range, and
7292 * adjust to make sure it is based on the start from
7293 * the caller
7294 */
7295 if (hole_em) {
7296 u64 calc_end = extent_map_end(hole_em);
7297
7298 if (calc_end <= start || (hole_em->start > end)) {
7299 free_extent_map(hole_em);
7300 hole_em = NULL;
7301 } else {
7302 hole_start = max(hole_em->start, start);
7303 hole_len = calc_end - hole_start;
7304 }
7305 }
7306 em->bdev = NULL;
7307 if (hole_em && range_start > hole_start) {
7308 /* our hole starts before our delalloc, so we
7309 * have to return just the parts of the hole
7310 * that go until the delalloc starts
7311 */
7312 em->len = min(hole_len,
7313 range_start - hole_start);
7314 em->start = hole_start;
7315 em->orig_start = hole_start;
7316 /*
7317 * don't adjust block start at all,
7318 * it is fixed at EXTENT_MAP_HOLE
7319 */
7320 em->block_start = hole_em->block_start;
7321 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007322 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7323 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007324 } else {
7325 em->start = range_start;
7326 em->len = found;
7327 em->orig_start = range_start;
7328 em->block_start = EXTENT_MAP_DELALLOC;
7329 em->block_len = found;
7330 }
7331 } else if (hole_em) {
7332 return hole_em;
7333 }
7334out:
7335
7336 free_extent_map(hole_em);
7337 if (err) {
7338 free_extent_map(em);
7339 return ERR_PTR(err);
7340 }
7341 return em;
7342}
7343
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007344static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7345 const u64 start,
7346 const u64 len,
7347 const u64 orig_start,
7348 const u64 block_start,
7349 const u64 block_len,
7350 const u64 orig_block_len,
7351 const u64 ram_bytes,
7352 const int type)
7353{
7354 struct extent_map *em = NULL;
7355 int ret;
7356
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007357 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007358 em = create_io_em(inode, start, len, orig_start,
7359 block_start, block_len, orig_block_len,
7360 ram_bytes,
7361 BTRFS_COMPRESS_NONE, /* compress_type */
7362 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007363 if (IS_ERR(em))
7364 goto out;
7365 }
7366 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7367 len, block_len, type);
7368 if (ret) {
7369 if (em) {
7370 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007371 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007372 start + len - 1, 0);
7373 }
7374 em = ERR_PTR(ret);
7375 }
7376 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007377
7378 return em;
7379}
7380
Josef Bacik4b46fce2010-05-23 11:00:55 -04007381static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7382 u64 start, u64 len)
7383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007385 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007386 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007387 struct btrfs_key ins;
7388 u64 alloc_hint;
7389 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007390
Josef Bacik4b46fce2010-05-23 11:00:55 -04007391 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007392 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007393 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007394 if (ret)
7395 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007396
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007397 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7398 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007399 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007400 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007401 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007402 btrfs_free_reserved_extent(fs_info, ins.objectid,
7403 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007404
Josef Bacik4b46fce2010-05-23 11:00:55 -04007405 return em;
7406}
7407
Chris Mason46bfbb52010-05-26 11:04:10 -04007408/*
7409 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7410 * block must be cow'd
7411 */
Josef Bacik00361582013-08-14 14:02:47 -04007412noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007413 u64 *orig_start, u64 *orig_block_len,
7414 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007415{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007416 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007417 struct btrfs_path *path;
7418 int ret;
7419 struct extent_buffer *leaf;
7420 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007421 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 struct btrfs_file_extent_item *fi;
7423 struct btrfs_key key;
7424 u64 disk_bytenr;
7425 u64 backref_offset;
7426 u64 extent_end;
7427 u64 num_bytes;
7428 int slot;
7429 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007430 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007431
Chris Mason46bfbb52010-05-26 11:04:10 -04007432 path = btrfs_alloc_path();
7433 if (!path)
7434 return -ENOMEM;
7435
David Sterbaf85b7372017-01-20 14:54:07 +01007436 ret = btrfs_lookup_file_extent(NULL, root, path,
7437 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 if (ret < 0)
7439 goto out;
7440
7441 slot = path->slots[0];
7442 if (ret == 1) {
7443 if (slot == 0) {
7444 /* can't find the item, must cow */
7445 ret = 0;
7446 goto out;
7447 }
7448 slot--;
7449 }
7450 ret = 0;
7451 leaf = path->nodes[0];
7452 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007453 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007454 key.type != BTRFS_EXTENT_DATA_KEY) {
7455 /* not our file or wrong item type, must cow */
7456 goto out;
7457 }
7458
7459 if (key.offset > offset) {
7460 /* Wrong offset, must cow */
7461 goto out;
7462 }
7463
7464 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7465 found_type = btrfs_file_extent_type(leaf, fi);
7466 if (found_type != BTRFS_FILE_EXTENT_REG &&
7467 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7468 /* not a regular extent, must cow */
7469 goto out;
7470 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007471
7472 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7473 goto out;
7474
Miao Xiee77751a2013-12-27 21:11:50 +08007475 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7476 if (extent_end <= offset)
7477 goto out;
7478
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007480 if (disk_bytenr == 0)
7481 goto out;
7482
7483 if (btrfs_file_extent_compression(leaf, fi) ||
7484 btrfs_file_extent_encryption(leaf, fi) ||
7485 btrfs_file_extent_other_encoding(leaf, fi))
7486 goto out;
7487
Chris Mason46bfbb52010-05-26 11:04:10 -04007488 backref_offset = btrfs_file_extent_offset(leaf, fi);
7489
Josef Bacik7ee9e442013-06-21 16:37:03 -04007490 if (orig_start) {
7491 *orig_start = key.offset - backref_offset;
7492 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7493 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7494 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007495
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007496 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007497 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007498
7499 num_bytes = min(offset + *len, extent_end) - offset;
7500 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7501 u64 range_end;
7502
Jeff Mahoneyda170662016-06-15 09:22:56 -04007503 range_end = round_up(offset + num_bytes,
7504 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007505 ret = test_range_bit(io_tree, offset, range_end,
7506 EXTENT_DELALLOC, 0, NULL);
7507 if (ret) {
7508 ret = -EAGAIN;
7509 goto out;
7510 }
7511 }
7512
Josef Bacik1bda19e2013-10-18 12:10:36 -04007513 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007514
7515 /*
7516 * look for other files referencing this extent, if we
7517 * find any we must cow
7518 */
Josef Bacik00361582013-08-14 14:02:47 -04007519
Liu Boe4c3b2d2017-01-30 12:25:28 -08007520 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007521 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007522 if (ret) {
7523 ret = 0;
7524 goto out;
7525 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007526
7527 /*
7528 * adjust disk_bytenr and num_bytes to cover just the bytes
7529 * in this extent we are about to write. If there
7530 * are any csums in that range we have to cow in order
7531 * to keep the csums correct
7532 */
7533 disk_bytenr += backref_offset;
7534 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007535 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7536 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007537 /*
7538 * all of the above have passed, it is safe to overwrite this extent
7539 * without cow
7540 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007541 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007542 ret = 1;
7543out:
7544 btrfs_free_path(path);
7545 return ret;
7546}
7547
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007548bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7549{
7550 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007551 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007552 void **pagep = NULL;
7553 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007554 unsigned long start_idx;
7555 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007556
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007557 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007558
7559 /*
7560 * end is the last byte in the last page. end == start is legal
7561 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007562 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007563
7564 rcu_read_lock();
7565
7566 /* Most of the code in this while loop is lifted from
7567 * find_get_page. It's been modified to begin searching from a
7568 * page and return just the first page found in that range. If the
7569 * found idx is less than or equal to the end idx then we know that
7570 * a page exists. If no pages are found or if those pages are
7571 * outside of the range then we're fine (yay!) */
7572 while (page == NULL &&
7573 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7574 page = radix_tree_deref_slot(pagep);
7575 if (unlikely(!page))
7576 break;
7577
7578 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007579 if (radix_tree_deref_retry(page)) {
7580 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007581 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007582 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007583 /*
7584 * Otherwise, shmem/tmpfs must be storing a swap entry
7585 * here as an exceptional entry: so return it without
7586 * attempting to raise page count.
7587 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007588 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007589 break; /* TODO: Is this relevant for this use case? */
7590 }
7591
Filipe Manana91405152014-06-05 13:22:24 +01007592 if (!page_cache_get_speculative(page)) {
7593 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007594 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007595 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007596
7597 /*
7598 * Has the page moved?
7599 * This is part of the lockless pagecache protocol. See
7600 * include/linux/pagemap.h for details.
7601 */
7602 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007603 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007604 page = NULL;
7605 }
7606 }
7607
7608 if (page) {
7609 if (page->index <= end_idx)
7610 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007611 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007612 }
7613
7614 rcu_read_unlock();
7615 return found;
7616}
7617
Josef Bacikeb838e72012-07-31 16:28:48 -04007618static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7619 struct extent_state **cached_state, int writing)
7620{
7621 struct btrfs_ordered_extent *ordered;
7622 int ret = 0;
7623
7624 while (1) {
7625 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007626 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 /*
7628 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007629 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 * extents in this range.
7631 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007632 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007633 lockend - lockstart + 1);
7634
7635 /*
7636 * We need to make sure there are no buffered pages in this
7637 * range either, we could have raced between the invalidate in
7638 * generic_file_direct_write and locking the extent. The
7639 * invalidate needs to happen so that reads after a write do not
7640 * get stale data.
7641 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007642 if (!ordered &&
7643 (!writing ||
7644 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 break;
7646
7647 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7648 cached_state, GFP_NOFS);
7649
7650 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007651 /*
7652 * If we are doing a DIO read and the ordered extent we
7653 * found is for a buffered write, we can not wait for it
7654 * to complete and retry, because if we do so we can
7655 * deadlock with concurrent buffered writes on page
7656 * locks. This happens only if our DIO read covers more
7657 * than one extent map, if at this point has already
7658 * created an ordered extent for a previous extent map
7659 * and locked its range in the inode's io tree, and a
7660 * concurrent write against that previous extent map's
7661 * range and this range started (we unlock the ranges
7662 * in the io tree only when the bios complete and
7663 * buffered writes always lock pages before attempting
7664 * to lock range in the io tree).
7665 */
7666 if (writing ||
7667 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7668 btrfs_start_ordered_extent(inode, ordered, 1);
7669 else
7670 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007671 btrfs_put_ordered_extent(ordered);
7672 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007673 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007674 * We could trigger writeback for this range (and wait
7675 * for it to complete) and then invalidate the pages for
7676 * this range (through invalidate_inode_pages2_range()),
7677 * but that can lead us to a deadlock with a concurrent
7678 * call to readpages() (a buffered read or a defrag call
7679 * triggered a readahead) on a page lock due to an
7680 * ordered dio extent we created before but did not have
7681 * yet a corresponding bio submitted (whence it can not
7682 * complete), which makes readpages() wait for that
7683 * ordered extent to complete while holding a lock on
7684 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007686 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 }
7688
Filipe Mananaade77022016-02-18 14:28:55 +00007689 if (ret)
7690 break;
7691
Josef Bacikeb838e72012-07-31 16:28:48 -04007692 cond_resched();
7693 }
7694
7695 return ret;
7696}
7697
Liu Bo6f9994d2017-01-31 07:50:22 -08007698/* The callers of this must take lock_extent() */
7699static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7700 u64 orig_start, u64 block_start,
7701 u64 block_len, u64 orig_block_len,
7702 u64 ram_bytes, int compress_type,
7703 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007704{
7705 struct extent_map_tree *em_tree;
7706 struct extent_map *em;
7707 struct btrfs_root *root = BTRFS_I(inode)->root;
7708 int ret;
7709
Liu Bo6f9994d2017-01-31 07:50:22 -08007710 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7711 type == BTRFS_ORDERED_COMPRESSED ||
7712 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007713 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007714
Josef Bacik69ffb542012-09-11 15:40:07 -04007715 em_tree = &BTRFS_I(inode)->extent_tree;
7716 em = alloc_extent_map();
7717 if (!em)
7718 return ERR_PTR(-ENOMEM);
7719
7720 em->start = start;
7721 em->orig_start = orig_start;
7722 em->len = len;
7723 em->block_len = block_len;
7724 em->block_start = block_start;
7725 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007726 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007727 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007728 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007729 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007730 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007731 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007732 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007733 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7734 em->compress_type = compress_type;
7735 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007736
7737 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007738 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007739 em->start + em->len - 1, 0);
7740 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007741 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007742 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007743 /*
7744 * The caller has taken lock_extent(), who could race with us
7745 * to add em?
7746 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007747 } while (ret == -EEXIST);
7748
7749 if (ret) {
7750 free_extent_map(em);
7751 return ERR_PTR(ret);
7752 }
7753
Liu Bo6f9994d2017-01-31 07:50:22 -08007754 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007755 return em;
7756}
7757
Josef Bacik4b46fce2010-05-23 11:00:55 -04007758static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7759 struct buffer_head *bh_result, int create)
7760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007761 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007762 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007763 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307764 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007765 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007768 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007769 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007770
Miao Xie172a5042013-02-21 02:48:22 -07007771 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007772 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007773 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007774 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007775
Josef Bacikc3298612012-08-03 16:49:19 -04007776 lockstart = start;
7777 lockend = start + len - 1;
7778
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007779 if (current->journal_info) {
7780 /*
7781 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007782 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007783 * confused.
7784 */
chandan50745b02015-08-28 21:10:13 +05307785 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007786 current->journal_info = NULL;
7787 }
7788
Josef Bacikeb838e72012-07-31 16:28:48 -04007789 /*
7790 * If this errors out it's because we couldn't invalidate pagecache for
7791 * this range and we need to fallback to buffered.
7792 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007793 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7794 create)) {
7795 ret = -ENOTBLK;
7796 goto err;
7797 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007798
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007799 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007800 if (IS_ERR(em)) {
7801 ret = PTR_ERR(em);
7802 goto unlock_err;
7803 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804
7805 /*
7806 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7807 * io. INLINE is special, and we could probably kludge it in here, but
7808 * it's still buffered so for safety lets just fall back to the generic
7809 * buffered path.
7810 *
7811 * For COMPRESSED we _have_ to read the entire extent in so we can
7812 * decompress it, so there will be buffering required no matter what we
7813 * do, so go ahead and fallback to buffered.
7814 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007815 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816 * to buffered IO. Don't blame me, this is the price we pay for using
7817 * the generic code.
7818 */
7819 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7820 em->block_start == EXTENT_MAP_INLINE) {
7821 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007822 ret = -ENOTBLK;
7823 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824 }
7825
7826 /* Just a good old fashioned hole, return */
7827 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7828 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7829 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007830 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831 }
7832
7833 /*
7834 * We don't allocate a new extent in the following cases
7835 *
7836 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7837 * existing extent.
7838 * 2) The extent is marked as PREALLOC. We're good to go here and can
7839 * just use the extent.
7840 *
7841 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007842 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007843 len = min(len, em->len - (start - em->start));
7844 lockstart = start + len;
7845 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007846 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847
7848 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7849 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7850 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007851 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007852 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007853
7854 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7855 type = BTRFS_ORDERED_PREALLOC;
7856 else
7857 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007858 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007859 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007860
Josef Bacik00361582013-08-14 14:02:47 -04007861 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007862 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007863 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007864 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007865
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007866 em2 = btrfs_create_dio_extent(inode, start, len,
7867 orig_start, block_start,
7868 len, orig_block_len,
7869 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007870 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007871 if (type == BTRFS_ORDERED_PREALLOC) {
7872 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007873 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007874 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007875 if (em2 && IS_ERR(em2)) {
7876 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007877 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007878 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007879 /*
7880 * For inode marked NODATACOW or extent marked PREALLOC,
7881 * use the existing or preallocated extent, so does not
7882 * need to adjust btrfs_space_info's bytes_may_use.
7883 */
7884 btrfs_free_reserved_data_space_noquota(inode,
7885 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007886 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007887 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888 }
Josef Bacik00361582013-08-14 14:02:47 -04007889
Chris Mason46bfbb52010-05-26 11:04:10 -04007890 /*
7891 * this will cow the extent, reset the len in case we changed
7892 * it above
7893 */
7894 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007895 free_extent_map(em);
7896 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007897 if (IS_ERR(em)) {
7898 ret = PTR_ERR(em);
7899 goto unlock_err;
7900 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007901 len = min(len, em->len - (start - em->start));
7902unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007903 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7904 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007905 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007906 bh_result->b_bdev = em->bdev;
7907 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007908 if (create) {
7909 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7910 set_buffer_new(bh_result);
7911
7912 /*
7913 * Need to update the i_size under the extent lock so buffered
7914 * readers will get the updated i_size when we unlock.
7915 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007916 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007917 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007918
chandan50745b02015-08-28 21:10:13 +05307919 WARN_ON(dio_data->reserve < len);
7920 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007921 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307922 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007923 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924
Josef Bacikeb838e72012-07-31 16:28:48 -04007925 /*
7926 * In the case of write we need to clear and unlock the entire range,
7927 * in the case of read we need to unlock only the end area that we
7928 * aren't using if there is any left over space.
7929 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007930 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007931 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7932 lockend, unlock_bits, 1, 0,
7933 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007934 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007935 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007936 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007937
Josef Bacik4b46fce2010-05-23 11:00:55 -04007938 free_extent_map(em);
7939
7940 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007941
7942unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007943 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7944 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007945err:
chandan50745b02015-08-28 21:10:13 +05307946 if (dio_data)
7947 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007948 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007949}
7950
Omar Sandoval58efbc92017-08-22 23:45:59 -07007951static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7952 struct bio *bio,
7953 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007954{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007955 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007956 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007957
Mike Christie37226b22016-06-05 14:31:52 -05007958 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007959
7960 bio_get(bio);
7961
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007962 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007963 if (ret)
7964 goto err;
7965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007966 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007967err:
7968 bio_put(bio);
7969 return ret;
7970}
7971
7972static int btrfs_check_dio_repairable(struct inode *inode,
7973 struct bio *failed_bio,
7974 struct io_failure_record *failrec,
7975 int failed_mirror)
7976{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007977 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007978 int num_copies;
7979
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007980 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007981 if (num_copies == 1) {
7982 /*
7983 * we only have a single copy of the data, so don't bother with
7984 * all the retry and error correction code that follows. no
7985 * matter what the error is, it is very likely to persist.
7986 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007987 btrfs_debug(fs_info,
7988 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7989 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007990 return 0;
7991 }
7992
7993 failrec->failed_mirror = failed_mirror;
7994 failrec->this_mirror++;
7995 if (failrec->this_mirror == failed_mirror)
7996 failrec->this_mirror++;
7997
7998 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007999 btrfs_debug(fs_info,
8000 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8001 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008002 return 0;
8003 }
8004
8005 return 1;
8006}
8007
Omar Sandoval58efbc92017-08-22 23:45:59 -07008008static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8009 struct page *page, unsigned int pgoff,
8010 u64 start, u64 end, int failed_mirror,
8011 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008012{
8013 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008014 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8015 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008016 struct bio *bio;
8017 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008018 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008019 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008020 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008021 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008022
Mike Christie37226b22016-06-05 14:31:52 -05008023 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008024
8025 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8026 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008027 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008028
8029 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8030 failed_mirror);
8031 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008032 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008033 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008034 }
8035
Liu Bo17347ce2017-05-15 15:33:27 -07008036 segs = bio_segments(failed_bio);
8037 if (segs > 1 ||
8038 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008039 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008040
8041 isector = start - btrfs_io_bio(failed_bio)->logical;
8042 isector >>= inode->i_sb->s_blocksize_bits;
8043 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008045 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008046
8047 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008048 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008049 read_mode, failrec->this_mirror, failrec->in_validation);
8050
Omar Sandoval58efbc92017-08-22 23:45:59 -07008051 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8052 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008053 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008054 bio_put(bio);
8055 }
8056
Omar Sandoval58efbc92017-08-22 23:45:59 -07008057 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008058}
8059
8060struct btrfs_retry_complete {
8061 struct completion done;
8062 struct inode *inode;
8063 u64 start;
8064 int uptodate;
8065};
8066
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008067static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008068{
8069 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008070 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008071 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008072 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008073 int i;
8074
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008075 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008076 goto end;
8077
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008079 io_tree = &BTRFS_I(inode)->io_tree;
8080 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8081 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082
Miao Xie8b110e32014-09-12 18:44:03 +08008083 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008084 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008085 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008086 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8087 io_tree, done->start, bvec->bv_page,
8088 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008089end:
8090 complete(&done->done);
8091 bio_put(bio);
8092}
8093
Omar Sandoval58efbc92017-08-22 23:45:59 -07008094static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8095 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008096{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008098 struct bio_vec bvec;
8099 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008100 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 unsigned int pgoff;
8103 u32 sectorsize;
8104 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008105 blk_status_t ret;
8106 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008107
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308108 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008109 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110
Miao Xiec1dc0892014-09-12 18:43:56 +08008111 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008112 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008113 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008114
Liu Bo17347ce2017-05-15 15:33:27 -07008115 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8116 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8117 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118
8119next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008120 done.uptodate = 0;
8121 done.start = start;
8122 init_completion(&done.done);
8123
Liu Bo17347ce2017-05-15 15:33:27 -07008124 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 pgoff, start, start + sectorsize - 1,
8126 io_bio->mirror_num,
8127 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008128 if (ret) {
8129 err = ret;
8130 goto next;
8131 }
Miao Xie8b110e32014-09-12 18:44:03 +08008132
David Sterba9c17f6c2017-07-19 19:26:45 +02008133 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008134
8135 if (!done.uptodate) {
8136 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008138 }
8139
Liu Bo629ebf42017-05-15 17:20:07 -07008140next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308141 start += sectorsize;
8142
Liu Bo97bf5a52017-04-07 13:11:10 -07008143 nr_sectors--;
8144 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008146 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 goto next_block_or_try_again;
8148 }
Miao Xie8b110e32014-09-12 18:44:03 +08008149 }
8150
Liu Bo629ebf42017-05-15 17:20:07 -07008151 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008152}
8153
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008154static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008155{
8156 struct btrfs_retry_complete *done = bio->bi_private;
8157 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008158 struct extent_io_tree *io_tree, *failure_tree;
8159 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008160 struct bio_vec *bvec;
8161 int uptodate;
8162 int ret;
8163 int i;
8164
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008165 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008166 goto end;
8167
8168 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308169
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308170 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008171 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172
Josef Bacik7870d082017-05-05 11:57:15 -04008173 io_tree = &BTRFS_I(inode)->io_tree;
8174 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8175
David Sterbac09abff2017-07-13 18:10:07 +02008176 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008177 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008178 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8179 bvec->bv_offset, done->start,
8180 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008181 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008182 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8183 failure_tree, io_tree, done->start,
8184 bvec->bv_page,
8185 btrfs_ino(BTRFS_I(inode)),
8186 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008187 else
8188 uptodate = 0;
8189 }
8190
8191 done->uptodate = uptodate;
8192end:
8193 complete(&done->done);
8194 bio_put(bio);
8195}
8196
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008197static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8198 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008199{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308200 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008201 struct bio_vec bvec;
8202 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008203 struct btrfs_retry_complete done;
8204 u64 start;
8205 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308206 u32 sectorsize;
8207 int nr_sectors;
8208 unsigned int pgoff;
8209 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008210 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008211 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008212 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008213
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308214 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008215 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308216
Omar Sandoval58efbc92017-08-22 23:45:59 -07008217 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008218 start = io_bio->logical;
8219 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008220 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008221
Liu Bo17347ce2017-05-15 15:33:27 -07008222 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8223 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308224
Liu Bo17347ce2017-05-15 15:33:27 -07008225 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308226next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008227 if (uptodate) {
8228 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8229 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8230 bvec.bv_page, pgoff, start, sectorsize);
8231 if (likely(!ret))
8232 goto next;
8233 }
Miao Xie8b110e32014-09-12 18:44:03 +08008234try_again:
8235 done.uptodate = 0;
8236 done.start = start;
8237 init_completion(&done.done);
8238
Omar Sandoval58efbc92017-08-22 23:45:59 -07008239 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8240 pgoff, start, start + sectorsize - 1,
8241 io_bio->mirror_num, btrfs_retry_endio,
8242 &done);
8243 if (status) {
8244 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008245 goto next;
8246 }
8247
David Sterba9c17f6c2017-07-19 19:26:45 +02008248 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008249
8250 if (!done.uptodate) {
8251 /* We might have another mirror, so try again */
8252 goto try_again;
8253 }
8254next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308255 offset += sectorsize;
8256 start += sectorsize;
8257
8258 ASSERT(nr_sectors);
8259
Liu Bo97bf5a52017-04-07 13:11:10 -07008260 nr_sectors--;
8261 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308262 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008263 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308264 goto next_block;
8265 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008266 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008267
8268 return err;
8269}
8270
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008271static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8272 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008273{
8274 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8275
8276 if (skip_csum) {
8277 if (unlikely(err))
8278 return __btrfs_correct_data_nocsum(inode, io_bio);
8279 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008280 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008281 } else {
8282 return __btrfs_subio_endio_read(inode, io_bio, err);
8283 }
8284}
8285
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008286static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008287{
8288 struct btrfs_dio_private *dip = bio->bi_private;
8289 struct inode *inode = dip->inode;
8290 struct bio *dio_bio;
8291 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008292 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008293
Liu Bo99c4e3b2017-09-15 15:06:51 -06008294 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008295 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008296
Josef Bacik4b46fce2010-05-23 11:00:55 -04008297 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008298 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008299 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008300
Josef Bacik4b46fce2010-05-23 11:00:55 -04008301 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008302
Liu Bo99c4e3b2017-09-15 15:06:51 -06008303 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008304 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008305
8306 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008308 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008309}
8310
Qu Wenruo524272602017-03-08 10:25:52 +08008311static void __endio_write_update_ordered(struct inode *inode,
8312 const u64 offset, const u64 bytes,
8313 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008316 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008317 struct btrfs_workqueue *wq;
8318 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008319 u64 ordered_offset = offset;
8320 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008321 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008322 int ret;
8323
Qu Wenruo524272602017-03-08 10:25:52 +08008324 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8325 wq = fs_info->endio_freespace_worker;
8326 func = btrfs_freespace_write_helper;
8327 } else {
8328 wq = fs_info->endio_write_workers;
8329 func = btrfs_endio_write_helper;
8330 }
8331
Chris Mason163cf092010-11-28 19:56:33 -05008332again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008333 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008334 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8335 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008336 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008337 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008338 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008339 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008340
Qu Wenruo524272602017-03-08 10:25:52 +08008341 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8342 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008343out_test:
8344 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008345 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8346 * in the range, we can exit.
8347 */
8348 if (ordered_offset == last_offset)
8349 return;
8350 /*
Chris Mason163cf092010-11-28 19:56:33 -05008351 * our bio might span multiple ordered extents. If we haven't
8352 * completed the accounting for the whole dio, go back and try again
8353 */
Filipe Manana14543772015-11-24 16:23:54 +00008354 if (ordered_offset < offset + bytes) {
8355 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008356 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008357 goto again;
8358 }
Filipe Manana14543772015-11-24 16:23:54 +00008359}
8360
8361static void btrfs_endio_direct_write(struct bio *bio)
8362{
8363 struct btrfs_dio_private *dip = bio->bi_private;
8364 struct bio *dio_bio = dip->dio_bio;
8365
Qu Wenruo524272602017-03-08 10:25:52 +08008366 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008367 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008368
Josef Bacik4b46fce2010-05-23 11:00:55 -04008369 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008370
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008371 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008372 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008373 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008374}
8375
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008376static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008377 struct bio *bio, int mirror_num,
8378 unsigned long bio_flags, u64 offset)
8379{
Josef Bacikc6100a42017-05-05 11:57:13 -04008380 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008381 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008382 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008383 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008384 return 0;
8385}
8386
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008387static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008388{
8389 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008390 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008391
Miao Xie8b110e32014-09-12 18:44:03 +08008392 if (err)
8393 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008394 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008395 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8396 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008397 (unsigned long long)bio->bi_iter.bi_sector,
8398 bio->bi_iter.bi_size, err);
8399
8400 if (dip->subio_endio)
8401 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008402
8403 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008404 dip->errors = 1;
8405
8406 /*
8407 * before atomic variable goto zero, we must make sure
8408 * dip->errors is perceived to be set.
8409 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008410 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008411 }
8412
8413 /* if there are more bios still pending for this dio, just exit */
8414 if (!atomic_dec_and_test(&dip->pending_bios))
8415 goto out;
8416
Chris Mason9be33952013-05-17 18:30:14 -04008417 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008418 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008419 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008420 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008421 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008422 }
8423out:
8424 bio_put(bio);
8425}
8426
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008427static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008428 struct btrfs_dio_private *dip,
8429 struct bio *bio,
8430 u64 file_offset)
8431{
8432 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8433 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008434 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008435
8436 /*
8437 * We load all the csum data we need when we submit
8438 * the first bio to reduce the csum tree search and
8439 * contention.
8440 */
8441 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008442 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008443 file_offset);
8444 if (ret)
8445 return ret;
8446 }
8447
8448 if (bio == dip->orig_bio)
8449 return 0;
8450
8451 file_offset -= dip->logical_offset;
8452 file_offset >>= inode->i_sb->s_blocksize_bits;
8453 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8454
8455 return 0;
8456}
8457
Omar Sandoval58efbc92017-08-22 23:45:59 -07008458static inline blk_status_t
8459__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008460 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008461{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008463 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008464 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008465 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008466
Josef Bacikb812ce22012-11-16 13:56:32 -05008467 if (async_submit)
8468 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8469
Miao Xiee65e1532010-11-22 03:04:43 +00008470 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008471
8472 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008473 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008474 if (ret)
8475 goto err;
8476 }
Miao Xiee65e1532010-11-22 03:04:43 +00008477
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008478 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008479 goto map;
8480
8481 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008482 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8483 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008484 __btrfs_submit_bio_start_direct_io,
8485 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008486 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008487 } else if (write) {
8488 /*
8489 * If we aren't doing async submit, calculate the csum of the
8490 * bio now.
8491 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008492 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008493 if (ret)
8494 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008495 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008496 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008497 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008498 if (ret)
8499 goto err;
8500 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008501map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008502 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008503err:
8504 bio_put(bio);
8505 return ret;
8506}
8507
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008508static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008509{
8510 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008511 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008512 struct bio *bio;
8513 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008514 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008515 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008516 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008517 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008518 u64 submit_len;
8519 int clone_offset = 0;
8520 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308521 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008522 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008523
Kent Overstreet4f024f32013-10-11 15:44:27 -07008524 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008525 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008526 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8527 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008528 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008529 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008530
Liu Bo725130b2017-05-16 09:51:39 -07008531 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008532 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008533 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008534 goto submit;
8535 }
8536
David Woodhouse53b381b2013-01-29 18:40:14 -05008537 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008538 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008539 async_submit = 0;
8540 else
8541 async_submit = 1;
8542
Liu Bo725130b2017-05-16 09:51:39 -07008543 /* bio split */
8544 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008545 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008546 do {
Liu Bo725130b2017-05-16 09:51:39 -07008547 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008548
Liu Bo725130b2017-05-16 09:51:39 -07008549 /*
8550 * This will never fail as it's passing GPF_NOFS and
8551 * the allocation is backed by btrfs_bioset.
8552 */
Liu Boe4770942017-05-16 10:57:14 -07008553 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008554 clone_len);
8555 bio->bi_private = dip;
8556 bio->bi_end_io = btrfs_end_dio_bio;
8557 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008558
Liu Bo725130b2017-05-16 09:51:39 -07008559 ASSERT(submit_len >= clone_len);
8560 submit_len -= clone_len;
8561 if (submit_len == 0)
8562 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008563
Liu Bo725130b2017-05-16 09:51:39 -07008564 /*
8565 * Increase the count before we submit the bio so we know
8566 * the end IO handler won't happen before we increase the
8567 * count. Otherwise, the dip might get freed before we're
8568 * done setting it up.
8569 */
8570 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008571
Linus Torvalds66ba7722017-09-09 13:27:51 -07008572 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008573 async_submit);
8574 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008575 bio_put(bio);
8576 atomic_dec(&dip->pending_bios);
8577 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008578 }
Liu Bo725130b2017-05-16 09:51:39 -07008579
8580 clone_offset += clone_len;
8581 start_sector += clone_len >> 9;
8582 file_offset += clone_len;
8583
8584 map_length = submit_len;
8585 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8586 start_sector << 9, &map_length, NULL, 0);
8587 if (ret)
8588 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008589 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008590
Josef Bacik02f57c72011-04-06 14:25:44 -04008591submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008592 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008593 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008594 return 0;
8595
8596 bio_put(bio);
8597out_err:
8598 dip->errors = 1;
8599 /*
8600 * before atomic variable goto zero, we must
8601 * make sure dip->errors is perceived to be set.
8602 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008603 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008604 if (atomic_dec_and_test(&dip->pending_bios))
8605 bio_io_error(dip->orig_bio);
8606
8607 /* bio_end_io() will handle error, so we needn't return it */
8608 return 0;
8609}
8610
Mike Christie8a4c1e42016-06-05 14:31:50 -05008611static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8612 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008613{
Filipe Manana61de7182015-07-01 12:13:10 +01008614 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008615 struct bio *bio = NULL;
8616 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008617 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008618 int ret = 0;
8619
David Sterba8b6c1d52017-06-02 17:48:13 +02008620 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621
Miao Xiec1dc0892014-09-12 18:43:56 +08008622 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008623 if (!dip) {
8624 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008625 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008626 }
8627
8628 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008629 dip->inode = inode;
8630 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008631 dip->bytes = dio_bio->bi_iter.bi_size;
8632 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008633 bio->bi_private = dip;
8634 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008635 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008636 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008637 io_bio = btrfs_io_bio(bio);
8638 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008639
Miao Xiec1dc0892014-09-12 18:43:56 +08008640 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008641 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008642 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008643 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008644 dip->subio_endio = btrfs_subio_endio_read;
8645 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008646
Filipe Mananaf28a4922015-12-08 19:23:20 +00008647 /*
8648 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8649 * even if we fail to submit a bio, because in such case we do the
8650 * corresponding error handling below and it must not be done a second
8651 * time by btrfs_direct_IO().
8652 */
8653 if (write) {
8654 struct btrfs_dio_data *dio_data = current->journal_info;
8655
8656 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8657 dip->bytes;
8658 dio_data->unsubmitted_oe_range_start =
8659 dio_data->unsubmitted_oe_range_end;
8660 }
8661
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008662 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008663 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008664 return;
Chris Mason9be33952013-05-17 18:30:14 -04008665
Liu Bo3892ac92017-04-17 15:00:28 -07008666 if (io_bio->end_io)
8667 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008668
Josef Bacik4b46fce2010-05-23 11:00:55 -04008669free_ordered:
8670 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008671 * If we arrived here it means either we failed to submit the dip
8672 * or we either failed to clone the dio_bio or failed to allocate the
8673 * dip. If we cloned the dio_bio and allocated the dip, we can just
8674 * call bio_endio against our io_bio so that we get proper resource
8675 * cleanup if we fail to submit the dip, otherwise, we must do the
8676 * same as btrfs_endio_direct_[write|read] because we can't call these
8677 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008678 */
Liu Bo3892ac92017-04-17 15:00:28 -07008679 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008680 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008681 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008682 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008683 * and all the cleanup and final put for dio_bio (through
8684 * dio_end_io()).
8685 */
8686 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008687 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008688 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008689 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008690 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008691 file_offset,
8692 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008693 false);
Filipe Manana14543772015-11-24 16:23:54 +00008694 else
Filipe Manana61de7182015-07-01 12:13:10 +01008695 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8696 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008697
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008698 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008699 /*
8700 * Releases and cleans up our dio_bio, no need to bio_put()
8701 * nor bio_endio()/bio_io_error() against dio_bio.
8702 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008703 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008704 }
Liu Bo3892ac92017-04-17 15:00:28 -07008705 if (bio)
8706 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008707 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008708}
8709
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008710static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008711 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008712{
8713 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008714 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008715 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008716 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008717
8718 if (offset & blocksize_mask)
8719 goto out;
8720
Al Viro28060d52014-03-22 05:15:17 -04008721 if (iov_iter_alignment(iter) & blocksize_mask)
8722 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008723
Al Viro28060d52014-03-22 05:15:17 -04008724 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008725 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008726 return 0;
8727 /*
8728 * Check to make sure we don't have duplicate iov_base's in this
8729 * iovec, if so return EINVAL, otherwise we'll get csum errors
8730 * when reading back.
8731 */
8732 for (seg = 0; seg < iter->nr_segs; seg++) {
8733 for (i = seg + 1; i < iter->nr_segs; i++) {
8734 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008735 goto out;
8736 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008737 }
8738 retval = 0;
8739out:
8740 return retval;
8741}
Josef Bacikeb838e72012-07-31 16:28:48 -04008742
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008743static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008744{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008745 struct file *file = iocb->ki_filp;
8746 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008747 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308748 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008749 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008750 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008751 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008752 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008753 bool wakeup = true;
8754 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008755 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008756
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008757 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008758 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008759
Jens Axboefe0f07d2015-04-15 17:05:48 -06008760 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008761
Josef Bacik0e267c42013-07-02 10:38:02 -04008762 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008763 * The generic stuff only does filemap_write_and_wait_range, which
8764 * isn't enough if we've written compressed pages to this area, so
8765 * we need to flush the dirty pages again to make absolutely sure
8766 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008767 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008768 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008769 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8770 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008771 filemap_fdatawrite_range(inode->i_mapping, offset,
8772 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008773
Omar Sandoval6f673762015-03-16 04:33:52 -07008774 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008775 /*
8776 * If the write DIO is beyond the EOF, we need update
8777 * the isize, but it is protected by i_mutex. So we can
8778 * not unlock the i_mutex at this case.
8779 */
8780 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008781 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008782 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008783 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008784 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8785 ret = -EAGAIN;
8786 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008787 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008788 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8789 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008790 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008791 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008792
8793 /*
8794 * We need to know how many extents we reserved so that we can
8795 * do the accounting properly if we go over the number we
8796 * originally calculated. Abuse current->journal_info for this.
8797 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008798 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008799 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008800 dio_data.unsubmitted_oe_range_start = (u64)offset;
8801 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308802 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308803 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008804 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8805 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008806 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008807 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8808 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008809 }
8810
Omar Sandoval17f8c842015-03-16 04:33:50 -07008811 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008812 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008813 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008814 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008815 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308816 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008817 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008818 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308819 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008820 btrfs_delalloc_release_space(inode, data_reserved,
8821 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008822 /*
8823 * On error we might have left some ordered extents
8824 * without submitting corresponding bios for them, so
8825 * cleanup them up to avoid other tasks getting them
8826 * and waiting for them to complete forever.
8827 */
8828 if (dio_data.unsubmitted_oe_range_start <
8829 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008830 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008831 dio_data.unsubmitted_oe_range_start,
8832 dio_data.unsubmitted_oe_range_end -
8833 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008834 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008835 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008836 btrfs_delalloc_release_space(inode, data_reserved,
8837 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008838 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008839 }
Miao Xie38851cc2013-02-08 07:04:11 +00008840out:
Miao Xie2e60a512013-02-08 07:01:08 +00008841 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008842 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008843 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008844 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008845
Qu Wenruo364ecf32017-02-27 15:10:38 +08008846 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008847 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008848}
8849
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008850#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8851
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008852static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8853 __u64 start, __u64 len)
8854{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008855 int ret;
8856
8857 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8858 if (ret)
8859 return ret;
8860
Chris Masonec29ed52011-02-23 16:23:20 -05008861 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008862}
8863
Chris Mason9ebefb182007-06-15 13:50:00 -04008864int btrfs_readpage(struct file *file, struct page *page)
8865{
Chris Masond1310b22008-01-24 16:13:08 -05008866 struct extent_io_tree *tree;
8867 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008868 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008869}
Chris Mason1832a6d2007-12-21 16:27:21 -05008870
Chris Mason39279cc2007-06-12 06:35:45 -04008871static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8872{
Chris Masond1310b22008-01-24 16:13:08 -05008873 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008874 struct inode *inode = page->mapping->host;
8875 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008876
8877 if (current->flags & PF_MEMALLOC) {
8878 redirty_page_for_writepage(wbc, page);
8879 unlock_page(page);
8880 return 0;
8881 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008882
8883 /*
8884 * If we are under memory pressure we will call this directly from the
8885 * VM, we need to make sure we have the inode referenced for the ordered
8886 * extent. If not just return like we didn't do anything.
8887 */
8888 if (!igrab(inode)) {
8889 redirty_page_for_writepage(wbc, page);
8890 return AOP_WRITEPAGE_ACTIVATE;
8891 }
Chris Masond1310b22008-01-24 16:13:08 -05008892 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008893 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8894 btrfs_add_delayed_iput(inode);
8895 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008896}
Chris Mason39279cc2007-06-12 06:35:45 -04008897
Eric Sandeen48a3b632013-04-25 20:41:01 +00008898static int btrfs_writepages(struct address_space *mapping,
8899 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008900{
Chris Masond1310b22008-01-24 16:13:08 -05008901 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008902
Chris Masond1310b22008-01-24 16:13:08 -05008903 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008904 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8905}
8906
Chris Mason3ab2fb52007-11-08 10:59:22 -05008907static int
8908btrfs_readpages(struct file *file, struct address_space *mapping,
8909 struct list_head *pages, unsigned nr_pages)
8910{
Chris Masond1310b22008-01-24 16:13:08 -05008911 struct extent_io_tree *tree;
8912 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008913 return extent_readpages(tree, mapping, pages, nr_pages,
8914 btrfs_get_extent);
8915}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008917{
Chris Masond1310b22008-01-24 16:13:08 -05008918 struct extent_io_tree *tree;
8919 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008920 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008921
Chris Masond1310b22008-01-24 16:13:08 -05008922 tree = &BTRFS_I(page->mapping->host)->io_tree;
8923 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008924 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008925 if (ret == 1) {
8926 ClearPagePrivate(page);
8927 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008928 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008929 }
8930 return ret;
8931}
Chris Mason39279cc2007-06-12 06:35:45 -04008932
Chris Masone6dcd2d2008-07-17 12:53:50 -04008933static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8934{
Chris Mason98509cf2008-09-11 15:51:43 -04008935 if (PageWriteback(page) || PageDirty(page))
8936 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008937 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008938}
8939
Lukas Czernerd47992f2013-05-21 23:17:23 -04008940static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8941 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008942{
Josef Bacik5fd02042012-05-02 14:00:54 -04008943 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008944 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008946 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008948 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308949 u64 start;
8950 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008951 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008952
Chris Mason8b62b722009-09-02 16:53:46 -04008953 /*
8954 * we have the page locked, so new writeback can't start,
8955 * and the dirty bit won't be cleared while we are here.
8956 *
8957 * Wait for IO on this page so that we can safely clear
8958 * the PagePrivate2 bit and do ordered accounting
8959 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008960 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008961
Josef Bacik5fd02042012-05-02 14:00:54 -04008962 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008963 if (offset) {
8964 btrfs_releasepage(page, GFP_NOFS);
8965 return;
8966 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008967
8968 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008969 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308970again:
8971 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008972 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308973 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308975 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008976 /*
8977 * IO on this page will never be started, so we need
8978 * to account for any ordered extents now
8979 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008980 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308981 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008982 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008983 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008984 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8985 EXTENT_DEFRAG, 1, 0, &cached_state,
8986 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008987 /*
8988 * whoever cleared the private bit is responsible
8989 * for the finish_ordered_io
8990 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008991 if (TestClearPagePrivate2(page)) {
8992 struct btrfs_ordered_inode_tree *tree;
8993 u64 new_len;
8994
8995 tree = &BTRFS_I(inode)->ordered_tree;
8996
8997 spin_lock_irq(&tree->lock);
8998 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308999 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04009000 if (new_len < ordered->truncated_len)
9001 ordered->truncated_len = new_len;
9002 spin_unlock_irq(&tree->lock);
9003
9004 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309005 start,
9006 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009007 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009008 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009010 if (!inode_evicting) {
9011 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309012 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009013 &cached_state);
9014 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309015
9016 start = end + 1;
9017 if (start < page_end)
9018 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009020
Qu Wenruob9d0b382015-09-29 10:35:16 +08009021 /*
9022 * Qgroup reserved space handler
9023 * Page here will be either
9024 * 1) Already written to disk
9025 * In this case, its reserved space is released from data rsv map
9026 * and will be freed by delayed_ref handler finally.
9027 * So even we call qgroup_free_data(), it won't decrease reserved
9028 * space.
9029 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009030 * This means the reserved space should be freed here. However,
9031 * if a truncate invalidates the page (by clearing PageDirty)
9032 * and the page is accounted for while allocating extent
9033 * in btrfs_check_data_free_space() we let delayed_ref to
9034 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009035 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009036 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009037 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009038 if (!inode_evicting) {
9039 clear_extent_bit(tree, page_start, page_end,
9040 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009041 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9042 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009043 &cached_state, GFP_NOFS);
9044
9045 __btrfs_releasepage(page, GFP_NOFS);
9046 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009047
Chris Mason4a096752008-07-21 10:29:44 -04009048 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009049 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009050 ClearPagePrivate(page);
9051 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009052 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009053 }
Chris Mason39279cc2007-06-12 06:35:45 -04009054}
9055
Chris Mason9ebefb182007-06-15 13:50:00 -04009056/*
9057 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9058 * called from a page fault handler when a page is first dirtied. Hence we must
9059 * be careful to check for EOF conditions here. We set the page up correctly
9060 * for a written page which means we get ENOSPC checking when writing into
9061 * holes and correct delalloc and unwritten extent mapping on filesystems that
9062 * support these features.
9063 *
9064 * We are not allowed to take the i_mutex here so we have to play games to
9065 * protect against truncate races as the page could now be beyond EOF. Because
9066 * vmtruncate() writes the inode size before removing pages, once we have the
9067 * page lock we can determine safely if the page is beyond EOF. If it is not
9068 * beyond EOF, then the page is guaranteed safe against truncation until we
9069 * unlock the page.
9070 */
Dave Jiang11bac802017-02-24 14:56:41 -08009071int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009072{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009073 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009074 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009075 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009076 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9077 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009078 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009079 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009080 char *kaddr;
9081 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009082 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009083 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009084 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309085 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009086 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009087 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309088 u64 end;
9089
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009090 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009091
Jan Karab2b5ef52012-06-12 16:20:45 +02009092 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009093 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009094 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309095 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009096
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309097 /*
9098 * Reserving delalloc space after obtaining the page lock can lead to
9099 * deadlock. For example, if a dirty page is locked by this function
9100 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9101 * dirty page write out, then the btrfs_writepage() function could
9102 * end up waiting indefinitely to get a lock on the page currently
9103 * being processed by btrfs_page_mkwrite() function.
9104 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009105 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309106 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009107 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009108 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009109 reserved = 1;
9110 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009111 if (ret) {
9112 if (ret == -ENOMEM)
9113 ret = VM_FAULT_OOM;
9114 else /* -ENOSPC, -EIO, etc */
9115 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009116 if (reserved)
9117 goto out;
9118 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009119 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009120
Nick Piggin56a76f82009-03-31 15:23:23 -07009121 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009122again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009123 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009124 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009125
Chris Mason9ebefb182007-06-15 13:50:00 -04009126 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009127 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009128 /* page got truncated out from underneath us */
9129 goto out_unlock;
9130 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009131 wait_on_page_writeback(page);
9132
David Sterbaff13db42015-12-03 14:30:40 +01009133 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009134 set_page_extent_mapped(page);
9135
Chris Masoneb84ae02008-07-17 13:53:27 -04009136 /*
9137 * we can't set the delalloc bits if there are pending ordered
9138 * extents. Drop our locks and wait for them to finish
9139 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009140 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9141 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009142 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009143 unlock_extent_cached(io_tree, page_start, page_end,
9144 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009145 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009146 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009147 btrfs_put_ordered_extent(ordered);
9148 goto again;
9149 }
9150
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009151 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009152 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009153 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009154 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309155 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009156 btrfs_delalloc_release_space(inode, data_reserved,
9157 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309158 }
9159 }
9160
Josef Bacikfbf19082009-10-01 17:10:23 -04009161 /*
Liu Bo54160342016-12-13 12:15:19 -08009162 * page_mkwrite gets called when the page is firstly dirtied after it's
9163 * faulted in, but write(2) could also dirty a page and set delalloc
9164 * bits, thus in this case for space account reason, we still need to
9165 * clear any delalloc bits within this page range since we have to
9166 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009167 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309168 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009169 EXTENT_DIRTY | EXTENT_DELALLOC |
9170 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009171 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009172
Filipe Mananae3b8a482017-11-04 00:16:59 +00009173 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009174 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009175 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009176 unlock_extent_cached(io_tree, page_start, page_end,
9177 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009178 ret = VM_FAULT_SIGBUS;
9179 goto out_unlock;
9180 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009181 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009182
9183 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009184 if (page_start + PAGE_SIZE > size)
9185 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009186 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009187 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009188
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009189 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009190 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009191 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009192 flush_dcache_page(page);
9193 kunmap(page);
9194 }
Chris Mason247e7432008-07-17 12:53:51 -04009195 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009196 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009197 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009199 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009200 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009201 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009202
Josef Bacik2ac55d42010-02-03 19:33:23 +00009203 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009204
9205out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009206 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009207 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009208 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009209 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009210 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009211 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009212 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009213out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009214 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009215 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9216 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009217out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009218 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009219 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009220 return ret;
9221}
9222
Josef Bacika41ad392011-01-31 15:30:16 -05009223static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009226 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009227 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009228 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009229 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009230 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009231 u64 mask = fs_info->sectorsize - 1;
9232 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009233
Josef Bacik0ef8b722013-10-25 16:13:35 -04009234 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9235 (u64)-1);
9236 if (ret)
9237 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009238
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009240 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009241 * 3 things going on here
9242 *
9243 * 1) We need to reserve space for our orphan item and the space to
9244 * delete our orphan item. Lord knows we don't want to have a dangling
9245 * orphan item because we didn't reserve space to remove it.
9246 *
9247 * 2) We need to reserve space to update our inode.
9248 *
9249 * 3) We need to have something to cache all the space that is going to
9250 * be free'd up by the truncate operation, but also have some slack
9251 * space reserved in case it uses space during the truncate (thank you
9252 * very much snapshotting).
9253 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009254 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009255 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009256 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009257 * doesn't end up using space reserved for updating the inode or
9258 * removing the orphan item. We also need to be able to stop the
9259 * transaction and start a new one, which means we need to be able to
9260 * update the inode several times, and we have no idea of knowing how
9261 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009262 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009263 * Then there is the orphan item, which does indeed need to be held on
9264 * to for the whole operation, and we need nobody to touch this reserved
9265 * space except the orphan code.
9266 *
9267 * So that leaves us with
9268 *
9269 * 1) root->orphan_block_rsv - for the orphan deletion.
9270 * 2) rsv - for the truncate reservation, which we will steal from the
9271 * transaction reservation.
9272 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9273 * updating the inode.
9274 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009275 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009276 if (!rsv)
9277 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009278 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009279 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009280
Josef Bacik907cbce2011-08-08 13:46:15 -04009281 /*
Josef Bacik07127182011-08-19 10:29:59 -04009282 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009283 * 1 for updating the inode.
9284 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009285 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009286 if (IS_ERR(trans)) {
9287 err = PTR_ERR(trans);
9288 goto out;
9289 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009290
Josef Bacik907cbce2011-08-08 13:46:15 -04009291 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009292 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009293 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009294 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009295
Chris Mason5a3f23d2009-03-31 13:27:11 -04009296 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009297 * So if we truncate and then write and fsync we normally would just
9298 * write the extents that changed, which is a problem if we need to
9299 * first truncate that entire inode. So set this flag so we write out
9300 * all of the extents in the inode to the sync log so we're completely
9301 * safe.
9302 */
9303 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009304 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009305
Yan, Zheng80825102009-11-12 09:35:36 +00009306 while (1) {
9307 ret = btrfs_truncate_inode_items(trans, root, inode,
9308 inode->i_size,
9309 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009310 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009311 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009312 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009313 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009314 }
Chris Mason39279cc2007-06-12 06:35:45 -04009315
Yan, Zheng80825102009-11-12 09:35:36 +00009316 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009317 if (ret) {
9318 err = ret;
9319 break;
9320 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009321
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009322 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009323 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009324
9325 trans = btrfs_start_transaction(root, 2);
9326 if (IS_ERR(trans)) {
9327 ret = err = PTR_ERR(trans);
9328 trans = NULL;
9329 break;
9330 }
9331
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009332 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009333 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009334 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009335 BUG_ON(ret); /* shouldn't happen */
9336 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009337 }
9338
Josef Bacikddfae632017-10-19 14:16:02 -04009339 /*
9340 * We can't call btrfs_truncate_block inside a trans handle as we could
9341 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9342 * we've truncated everything except the last little bit, and can do
9343 * btrfs_truncate_block and then update the disk_i_size.
9344 */
9345 if (ret == NEED_TRUNCATE_BLOCK) {
9346 btrfs_end_transaction(trans);
9347 btrfs_btree_balance_dirty(fs_info);
9348
9349 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9350 if (ret)
9351 goto out;
9352 trans = btrfs_start_transaction(root, 1);
9353 if (IS_ERR(trans)) {
9354 ret = PTR_ERR(trans);
9355 goto out;
9356 }
9357 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9358 }
9359
Yan, Zheng80825102009-11-12 09:35:36 +00009360 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009361 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009362 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009363 if (ret)
9364 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009365 }
9366
Chris Mason917c16b2011-11-08 14:49:59 -05009367 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009368 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009369 ret = btrfs_update_inode(trans, root, inode);
9370 if (ret && !err)
9371 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009372
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009373 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009374 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009375 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009376out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009377 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009378
Josef Bacik3893e332011-01-31 16:03:11 -05009379 if (ret && !err)
9380 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009381
Josef Bacik3893e332011-01-31 16:03:11 -05009382 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009383}
9384
Sven Wegener3b963622008-06-09 21:57:42 -04009385/*
Chris Masond352ac62008-09-29 15:18:18 -04009386 * create a new subvolume directory/inode (helper for the ioctl).
9387 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009388int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009389 struct btrfs_root *new_root,
9390 struct btrfs_root *parent_root,
9391 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009392{
Chris Mason39279cc2007-06-12 06:35:45 -04009393 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009394 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009395 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009396
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009397 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9398 new_dirid, new_dirid,
9399 S_IFDIR | (~current_umask() & S_IRWXUGO),
9400 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009401 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009402 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009403 inode->i_op = &btrfs_dir_inode_operations;
9404 inode->i_fop = &btrfs_dir_file_operations;
9405
Miklos Szeredibfe86842011-10-28 14:13:29 +02009406 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009407 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009408 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009409
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009410 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9411 if (err)
9412 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009413 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009414 new_root->root_key.objectid, err);
9415
Yan, Zheng76dda932009-09-21 16:00:26 -04009416 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009417
Yan, Zheng76dda932009-09-21 16:00:26 -04009418 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009419 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009420}
9421
Chris Mason39279cc2007-06-12 06:35:45 -04009422struct inode *btrfs_alloc_inode(struct super_block *sb)
9423{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009424 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009425 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009426 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009427
9428 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9429 if (!ei)
9430 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009431
9432 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009433 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009434 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009435 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009436 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009437 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009438 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009439 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009440 ei->disk_i_size = 0;
9441 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009442 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009443 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009444 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009445 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009446 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009447 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009448
Josef Bacik9e0baf62011-07-15 15:16:44 +00009449 spin_lock_init(&ei->lock);
9450 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009451 if (sb->s_magic != BTRFS_TEST_MAGIC)
9452 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9453 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009454 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009455 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009456 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009457
Miao Xie16cdcec2011-04-22 18:12:22 +08009458 ei->delayed_node = NULL;
9459
chandan r9cc97d62012-07-04 12:48:07 +05309460 ei->i_otime.tv_sec = 0;
9461 ei->i_otime.tv_nsec = 0;
9462
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009463 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009464 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009465 extent_io_tree_init(&ei->io_tree, inode);
9466 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009467 ei->io_tree.track_uptodate = 1;
9468 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009469 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009470 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009471 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009472 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009473 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009474 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009475 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009476 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009477
9478 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009479}
9480
Josef Bacikaaedb552013-10-11 14:44:09 -04009481#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9482void btrfs_test_destroy_inode(struct inode *inode)
9483{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009484 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009485 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9486}
9487#endif
9488
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009489static void btrfs_i_callback(struct rcu_head *head)
9490{
9491 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009492 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9493}
9494
Chris Mason39279cc2007-06-12 06:35:45 -04009495void btrfs_destroy_inode(struct inode *inode)
9496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009498 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009499 struct btrfs_root *root = BTRFS_I(inode)->root;
9500
Al Virob3d9b7a2012-06-09 13:51:19 -04009501 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009502 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009503 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9504 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009505 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009506 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009507 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009508 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009509 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009510
Chris Mason5a3f23d2009-03-31 13:27:11 -04009511 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009512 * This can happen where we create an inode, but somebody else also
9513 * created the same inode and we need to destroy the one we already
9514 * created.
9515 */
9516 if (!root)
9517 goto free;
9518
Josef Bacik8a35d952012-05-23 14:26:42 -04009519 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9520 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009521 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009522 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009523 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009524 }
Josef Bacik7b128762008-07-24 12:17:14 -04009525
Chris Masond3977122009-01-05 21:25:51 -05009526 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009527 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9528 if (!ordered)
9529 break;
9530 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009531 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009532 "found ordered extent %llu %llu on inode cleanup",
9533 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009534 btrfs_remove_ordered_extent(inode, ordered);
9535 btrfs_put_ordered_extent(ordered);
9536 btrfs_put_ordered_extent(ordered);
9537 }
9538 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009539 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009540 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009541 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009542free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009543 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009544}
9545
Al Viro45321ac2010-06-07 13:43:19 -04009546int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009547{
9548 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009549
Naohiro Aota6379ef92013-06-06 09:56:34 +00009550 if (root == NULL)
9551 return 1;
9552
Liu Bofa6ac872013-02-20 14:10:23 +00009553 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009554 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009555 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009556 else
Al Viro45321ac2010-06-07 13:43:19 -04009557 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009558}
9559
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009560static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009561{
9562 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9563
9564 inode_init_once(&ei->vfs_inode);
9565}
9566
9567void btrfs_destroy_cachep(void)
9568{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009569 /*
9570 * Make sure all delayed rcu free inodes are flushed before we
9571 * destroy cache.
9572 */
9573 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009574 kmem_cache_destroy(btrfs_inode_cachep);
9575 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009576 kmem_cache_destroy(btrfs_path_cachep);
9577 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009578}
9579
9580int btrfs_init_cachep(void)
9581{
David Sterba837e1972012-09-07 03:00:48 -06009582 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009583 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009584 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9585 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009586 if (!btrfs_inode_cachep)
9587 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009588
David Sterba837e1972012-09-07 03:00:48 -06009589 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009590 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009591 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009592 if (!btrfs_trans_handle_cachep)
9593 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009594
David Sterba837e1972012-09-07 03:00:48 -06009595 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009596 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009597 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009598 if (!btrfs_path_cachep)
9599 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009600
David Sterba837e1972012-09-07 03:00:48 -06009601 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009602 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009603 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009604 if (!btrfs_free_space_cachep)
9605 goto fail;
9606
Chris Mason39279cc2007-06-12 06:35:45 -04009607 return 0;
9608fail:
9609 btrfs_destroy_cachep();
9610 return -ENOMEM;
9611}
9612
David Howellsa528d352017-01-31 16:46:22 +00009613static int btrfs_getattr(const struct path *path, struct kstat *stat,
9614 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009615{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009616 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009617 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009618 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009619 u32 bi_flags = BTRFS_I(inode)->flags;
9620
9621 stat->result_mask |= STATX_BTIME;
9622 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9623 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9624 if (bi_flags & BTRFS_INODE_APPEND)
9625 stat->attributes |= STATX_ATTR_APPEND;
9626 if (bi_flags & BTRFS_INODE_COMPRESS)
9627 stat->attributes |= STATX_ATTR_COMPRESSED;
9628 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9629 stat->attributes |= STATX_ATTR_IMMUTABLE;
9630 if (bi_flags & BTRFS_INODE_NODUMP)
9631 stat->attributes |= STATX_ATTR_NODUMP;
9632
9633 stat->attributes_mask |= (STATX_ATTR_APPEND |
9634 STATX_ATTR_COMPRESSED |
9635 STATX_ATTR_IMMUTABLE |
9636 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009637
Chris Mason39279cc2007-06-12 06:35:45 -04009638 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009639 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009640
9641 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009642 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009643 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009644 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009645 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009646 return 0;
9647}
9648
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649static int btrfs_rename_exchange(struct inode *old_dir,
9650 struct dentry *old_dentry,
9651 struct inode *new_dir,
9652 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009654 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009655 struct btrfs_trans_handle *trans;
9656 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009657 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 struct inode *new_inode = new_dentry->d_inode;
9659 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009660 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009662 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9663 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 u64 old_idx = 0;
9665 u64 new_idx = 0;
9666 u64 root_objectid;
9667 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009668 bool root_log_pinned = false;
9669 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670
9671 /* we only allow rename subvolume link between subvolumes */
9672 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9673 return -EXDEV;
9674
9675 /* close the race window with snapshot create/destroy ioctl */
9676 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009677 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009679 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680
9681 /*
9682 * We want to reserve the absolute worst case amount of items. So if
9683 * both inodes are subvols and we need to unlink them then that would
9684 * require 4 item modifications, but if they are both normal inodes it
9685 * would require 5 item modifications, so we'll assume their normal
9686 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9687 * should cover the worst case number of items we'll modify.
9688 */
9689 trans = btrfs_start_transaction(root, 12);
9690 if (IS_ERR(trans)) {
9691 ret = PTR_ERR(trans);
9692 goto out_notrans;
9693 }
9694
9695 /*
9696 * We need to find a free sequence number both in the source and
9697 * in the destination directory for the exchange.
9698 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009699 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 if (ret)
9701 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009702 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 if (ret)
9704 goto out_fail;
9705
9706 BTRFS_I(old_inode)->dir_index = 0ULL;
9707 BTRFS_I(new_inode)->dir_index = 0ULL;
9708
9709 /* Reference for the source. */
9710 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9711 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009712 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009714 btrfs_pin_log_trans(root);
9715 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 ret = btrfs_insert_inode_ref(trans, dest,
9717 new_dentry->d_name.name,
9718 new_dentry->d_name.len,
9719 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009720 btrfs_ino(BTRFS_I(new_dir)),
9721 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722 if (ret)
9723 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009724 }
9725
9726 /* And now for the dest. */
9727 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9728 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009729 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009730 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009731 btrfs_pin_log_trans(dest);
9732 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 ret = btrfs_insert_inode_ref(trans, root,
9734 old_dentry->d_name.name,
9735 old_dentry->d_name.len,
9736 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009737 btrfs_ino(BTRFS_I(old_dir)),
9738 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739 if (ret)
9740 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009741 }
9742
9743 /* Update inode version and ctime/mtime. */
9744 inode_inc_iversion(old_dir);
9745 inode_inc_iversion(new_dir);
9746 inode_inc_iversion(old_inode);
9747 inode_inc_iversion(new_inode);
9748 old_dir->i_ctime = old_dir->i_mtime = ctime;
9749 new_dir->i_ctime = new_dir->i_mtime = ctime;
9750 old_inode->i_ctime = ctime;
9751 new_inode->i_ctime = ctime;
9752
9753 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009754 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9755 BTRFS_I(old_inode), 1);
9756 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9757 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758 }
9759
9760 /* src is a subvolume */
9761 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9762 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9763 ret = btrfs_unlink_subvol(trans, root, old_dir,
9764 root_objectid,
9765 old_dentry->d_name.name,
9766 old_dentry->d_name.len);
9767 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009768 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9769 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 old_dentry->d_name.name,
9771 old_dentry->d_name.len);
9772 if (!ret)
9773 ret = btrfs_update_inode(trans, root, old_inode);
9774 }
9775 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009776 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 goto out_fail;
9778 }
9779
9780 /* dest is a subvolume */
9781 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9782 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9783 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9784 root_objectid,
9785 new_dentry->d_name.name,
9786 new_dentry->d_name.len);
9787 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009788 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9789 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009790 new_dentry->d_name.name,
9791 new_dentry->d_name.len);
9792 if (!ret)
9793 ret = btrfs_update_inode(trans, dest, new_inode);
9794 }
9795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797 goto out_fail;
9798 }
9799
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009800 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 new_dentry->d_name.name,
9802 new_dentry->d_name.len, 0, old_idx);
9803 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009804 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 goto out_fail;
9806 }
9807
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009808 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809 old_dentry->d_name.name,
9810 old_dentry->d_name.len, 0, new_idx);
9811 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009812 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009813 goto out_fail;
9814 }
9815
9816 if (old_inode->i_nlink == 1)
9817 BTRFS_I(old_inode)->dir_index = old_idx;
9818 if (new_inode->i_nlink == 1)
9819 BTRFS_I(new_inode)->dir_index = new_idx;
9820
Filipe Manana86e8aa02016-05-05 02:02:27 +01009821 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009822 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9824 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009826 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009827 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009828 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009829 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009830 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9831 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009832 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009833 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009834 }
9835out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009836 /*
9837 * If we have pinned a log and an error happened, we unpin tasks
9838 * trying to sync the log and force them to fallback to a transaction
9839 * commit if the log currently contains any of the inodes involved in
9840 * this rename operation (to ensure we do not persist a log with an
9841 * inconsistent state for any of these inodes or leading to any
9842 * inconsistencies when replayed). If the transaction was aborted, the
9843 * abortion reason is propagated to userspace when attempting to commit
9844 * the transaction. If the log does not contain any of these inodes, we
9845 * allow the tasks to sync it.
9846 */
9847 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009848 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9849 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9850 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009851 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009852 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009853 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009854
9855 if (root_log_pinned) {
9856 btrfs_end_log_trans(root);
9857 root_log_pinned = false;
9858 }
9859 if (dest_log_pinned) {
9860 btrfs_end_log_trans(dest);
9861 dest_log_pinned = false;
9862 }
9863 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009864 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009865out_notrans:
9866 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009867 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009868 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009869 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009870
9871 return ret;
9872}
9873
9874static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9875 struct btrfs_root *root,
9876 struct inode *dir,
9877 struct dentry *dentry)
9878{
9879 int ret;
9880 struct inode *inode;
9881 u64 objectid;
9882 u64 index;
9883
9884 ret = btrfs_find_free_ino(root, &objectid);
9885 if (ret)
9886 return ret;
9887
9888 inode = btrfs_new_inode(trans, root, dir,
9889 dentry->d_name.name,
9890 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009891 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009892 objectid,
9893 S_IFCHR | WHITEOUT_MODE,
9894 &index);
9895
9896 if (IS_ERR(inode)) {
9897 ret = PTR_ERR(inode);
9898 return ret;
9899 }
9900
9901 inode->i_op = &btrfs_special_inode_operations;
9902 init_special_inode(inode, inode->i_mode,
9903 WHITEOUT_DEV);
9904
9905 ret = btrfs_init_inode_security(trans, inode, dir,
9906 &dentry->d_name);
9907 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009908 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009909
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009910 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9911 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009912 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009913 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009914
9915 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009916out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009917 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009918 if (ret)
9919 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009920 iput(inode);
9921
Filipe Mananac9901612016-05-05 01:41:57 +01009922 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009923}
9924
Chris Mason39279cc2007-06-12 06:35:45 -04009925static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009926 struct inode *new_dir, struct dentry *new_dentry,
9927 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009929 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009930 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009931 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009932 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9933 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009934 struct inode *new_inode = d_inode(new_dentry);
9935 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009936 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009937 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009938 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009939 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009940 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009941
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009942 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009943 return -EPERM;
9944
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009945 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009946 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009947 return -EXDEV;
9948
Li Zefan33345d012011-04-20 10:31:50 +08009949 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009950 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009951 return -ENOTEMPTY;
9952
Chris Mason39279cc2007-06-12 06:35:45 -04009953 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009954 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009955 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009956
9957
9958 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009959 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009960 new_dentry->d_name.name,
9961 new_dentry->d_name.len);
9962
9963 if (ret) {
9964 if (ret == -EEXIST) {
9965 /* we shouldn't get
9966 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309967 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009968 return ret;
9969 }
9970 } else {
9971 /* maybe -EOVERFLOW */
9972 return ret;
9973 }
9974 }
9975 ret = 0;
9976
Chris Mason5a3f23d2009-03-31 13:27:11 -04009977 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009978 * we're using rename to replace one file with another. Start IO on it
9979 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009980 */
Chris Mason8d875f92014-08-12 10:47:42 -07009981 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009982 filemap_flush(old_inode->i_mapping);
9983
Yan, Zheng76dda932009-09-21 16:00:26 -04009984 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009985 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009986 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009987 /*
9988 * We want to reserve the absolute worst case amount of items. So if
9989 * both inodes are subvols and we need to unlink them then that would
9990 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009991 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009992 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9993 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009994 * If our rename has the whiteout flag, we need more 5 units for the
9995 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9996 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009997 */
Filipe Manana5062af32016-05-05 10:26:26 +01009998 trans_num_items = 11;
9999 if (flags & RENAME_WHITEOUT)
10000 trans_num_items += 5;
10001 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010002 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010003 ret = PTR_ERR(trans);
10004 goto out_notrans;
10005 }
Chris Mason5f39d392007-10-15 16:14:19 -040010006
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010007 if (dest != root)
10008 btrfs_record_root_in_trans(trans, dest);
10009
Nikolay Borisov877574e2017-02-20 13:50:33 +020010010 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010011 if (ret)
10012 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010013
Miao Xie67de1172013-12-26 13:07:06 +080010014 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010015 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010016 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010017 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010018 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010019 btrfs_pin_log_trans(root);
10020 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010021 ret = btrfs_insert_inode_ref(trans, dest,
10022 new_dentry->d_name.name,
10023 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010024 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010025 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010026 if (ret)
10027 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010028 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010029
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010030 inode_inc_iversion(old_dir);
10031 inode_inc_iversion(new_dir);
10032 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010033 old_dir->i_ctime = old_dir->i_mtime =
10034 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010035 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010036
Chris Mason12fcfd22009-03-24 10:24:20 -040010037 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010038 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10039 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010040
Li Zefan33345d012011-04-20 10:31:50 +080010041 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010042 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10043 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10044 old_dentry->d_name.name,
10045 old_dentry->d_name.len);
10046 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010047 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10048 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010049 old_dentry->d_name.name,
10050 old_dentry->d_name.len);
10051 if (!ret)
10052 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010053 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010054 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010055 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010056 goto out_fail;
10057 }
Chris Mason39279cc2007-06-12 06:35:45 -040010058
10059 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010060 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010061 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010062 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010063 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10064 root_objectid = BTRFS_I(new_inode)->location.objectid;
10065 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10066 root_objectid,
10067 new_dentry->d_name.name,
10068 new_dentry->d_name.len);
10069 BUG_ON(new_inode->i_nlink == 0);
10070 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010071 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10072 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010073 new_dentry->d_name.name,
10074 new_dentry->d_name.len);
10075 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010076 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010077 ret = btrfs_orphan_add(trans,
10078 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010080 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010081 goto out_fail;
10082 }
Chris Mason39279cc2007-06-12 06:35:45 -040010083 }
Josef Bacikaec74772008-07-24 12:12:38 -040010084
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010085 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010086 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010087 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010088 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010089 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010090 goto out_fail;
10091 }
Chris Mason39279cc2007-06-12 06:35:45 -040010092
Miao Xie67de1172013-12-26 13:07:06 +080010093 if (old_inode->i_nlink == 1)
10094 BTRFS_I(old_inode)->dir_index = index;
10095
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010096 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010097 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010098
David Sterbaf85b7372017-01-20 14:54:07 +010010099 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10100 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010101 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010102 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010103 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010104
10105 if (flags & RENAME_WHITEOUT) {
10106 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10107 old_dentry);
10108
10109 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010110 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010111 goto out_fail;
10112 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010113 }
Chris Mason39279cc2007-06-12 06:35:45 -040010114out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010115 /*
10116 * If we have pinned the log and an error happened, we unpin tasks
10117 * trying to sync the log and force them to fallback to a transaction
10118 * commit if the log currently contains any of the inodes involved in
10119 * this rename operation (to ensure we do not persist a log with an
10120 * inconsistent state for any of these inodes or leading to any
10121 * inconsistencies when replayed). If the transaction was aborted, the
10122 * abortion reason is propagated to userspace when attempting to commit
10123 * the transaction. If the log does not contain any of these inodes, we
10124 * allow the tasks to sync it.
10125 */
10126 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010127 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10128 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10129 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010130 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010131 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010132 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010133
10134 btrfs_end_log_trans(root);
10135 log_pinned = false;
10136 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010137 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010138out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010139 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010140 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010141
Chris Mason39279cc2007-06-12 06:35:45 -040010142 return ret;
10143}
10144
Miklos Szeredi80ace852014-07-23 15:15:32 +020010145static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10146 struct inode *new_dir, struct dentry *new_dentry,
10147 unsigned int flags)
10148{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010149 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010150 return -EINVAL;
10151
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010152 if (flags & RENAME_EXCHANGE)
10153 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10154 new_dentry);
10155
10156 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010157}
10158
Miao Xie8ccf6f192012-10-25 09:28:04 +000010159static void btrfs_run_delalloc_work(struct btrfs_work *work)
10160{
10161 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010162 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010163
10164 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10165 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010166 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010167 filemap_flush(inode->i_mapping);
10168 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10169 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010170 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010171
10172 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010173 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010174 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010175 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010176 complete(&delalloc_work->completion);
10177}
10178
10179struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010180 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010181{
10182 struct btrfs_delalloc_work *work;
10183
David Sterba100d5702015-12-08 14:39:32 +010010184 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010185 if (!work)
10186 return NULL;
10187
10188 init_completion(&work->completion);
10189 INIT_LIST_HEAD(&work->list);
10190 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010191 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010192 WARN_ON_ONCE(!inode);
10193 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10194 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010195
10196 return work;
10197}
10198
10199void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10200{
10201 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010202 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010203}
10204
Chris Masond352ac62008-09-29 15:18:18 -040010205/*
10206 * some fairly slow code that needs optimization. This walks the list
10207 * of all the inodes with pending delalloc and forces them to disk.
10208 */
Miao Xie6c255e62014-03-06 13:55:01 +080010209static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10210 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010211{
Chris Masonea8c2812008-08-04 23:17:27 -040010212 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010213 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010214 struct btrfs_delalloc_work *work, *next;
10215 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010216 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010217 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010218
Miao Xie8ccf6f192012-10-25 09:28:04 +000010219 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010220 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010221
Miao Xie573bfb72014-03-06 13:55:03 +080010222 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010223 spin_lock(&root->delalloc_lock);
10224 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010225 while (!list_empty(&splice)) {
10226 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010227 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010228
Miao Xieeb73c1b2013-05-15 07:48:22 +000010229 list_move_tail(&binode->delalloc_inodes,
10230 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010231 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010232 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010233 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010234 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010235 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010237
David Sterba651d4942015-11-27 19:24:16 +010010238 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010239 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010240 if (delay_iput)
10241 btrfs_add_delayed_iput(inode);
10242 else
10243 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010244 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010245 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010246 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010247 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010248 btrfs_queue_work(root->fs_info->flush_workers,
10249 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010250 ret++;
10251 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010252 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010253 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010254 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010255 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010256 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010257
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010258out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010259 list_for_each_entry_safe(work, next, &works, list) {
10260 list_del_init(&work->list);
10261 btrfs_wait_and_free_delalloc_work(work);
10262 }
10263
Miao Xieeb73c1b2013-05-15 07:48:22 +000010264 if (!list_empty_careful(&splice)) {
10265 spin_lock(&root->delalloc_lock);
10266 list_splice_tail(&splice, &root->delalloc_inodes);
10267 spin_unlock(&root->delalloc_lock);
10268 }
Miao Xie573bfb72014-03-06 13:55:03 +080010269 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010270 return ret;
10271}
10272
10273int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010275 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 int ret;
10277
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010278 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010279 return -EROFS;
10280
Miao Xie6c255e62014-03-06 13:55:01 +080010281 ret = __start_delalloc_inodes(root, delay_iput, -1);
10282 if (ret > 0)
10283 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010284 return ret;
10285}
10286
Miao Xie6c255e62014-03-06 13:55:01 +080010287int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10288 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010289{
10290 struct btrfs_root *root;
10291 struct list_head splice;
10292 int ret;
10293
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010294 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010295 return -EROFS;
10296
10297 INIT_LIST_HEAD(&splice);
10298
Miao Xie573bfb72014-03-06 13:55:03 +080010299 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010300 spin_lock(&fs_info->delalloc_root_lock);
10301 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010302 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010303 root = list_first_entry(&splice, struct btrfs_root,
10304 delalloc_root);
10305 root = btrfs_grab_fs_root(root);
10306 BUG_ON(!root);
10307 list_move_tail(&root->delalloc_root,
10308 &fs_info->delalloc_roots);
10309 spin_unlock(&fs_info->delalloc_root_lock);
10310
Miao Xie6c255e62014-03-06 13:55:01 +080010311 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010312 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010313 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010314 goto out;
10315
Miao Xie6c255e62014-03-06 13:55:01 +080010316 if (nr != -1) {
10317 nr -= ret;
10318 WARN_ON(nr < 0);
10319 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010320 spin_lock(&fs_info->delalloc_root_lock);
10321 }
10322 spin_unlock(&fs_info->delalloc_root_lock);
10323
Miao Xie6c255e62014-03-06 13:55:01 +080010324 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010325out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010326 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010327 spin_lock(&fs_info->delalloc_root_lock);
10328 list_splice_tail(&splice, &fs_info->delalloc_roots);
10329 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010330 }
Miao Xie573bfb72014-03-06 13:55:03 +080010331 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010332 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010333}
10334
Chris Mason39279cc2007-06-12 06:35:45 -040010335static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10336 const char *symname)
10337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010338 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010339 struct btrfs_trans_handle *trans;
10340 struct btrfs_root *root = BTRFS_I(dir)->root;
10341 struct btrfs_path *path;
10342 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010343 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010344 int err;
10345 int drop_inode = 0;
10346 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010347 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010348 int name_len;
10349 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010350 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010351 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010352 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010353
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010354 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010355 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010356 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010357
Josef Bacik9ed74f22009-09-11 16:12:44 -040010358 /*
10359 * 2 items for inode item and ref
10360 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010361 * 1 item for updating parent inode item
10362 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010363 * 1 item for xattr if selinux is on
10364 */
Filipe Manana9269d122015-12-31 18:16:29 +000010365 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010366 if (IS_ERR(trans))
10367 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010368
Li Zefan581bb052011-04-20 10:06:11 +080010369 err = btrfs_find_free_ino(root, &objectid);
10370 if (err)
10371 goto out_unlock;
10372
Josef Bacikaec74772008-07-24 12:12:38 -040010373 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010374 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10375 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010376 if (IS_ERR(inode)) {
10377 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010378 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010379 }
Chris Mason39279cc2007-06-12 06:35:45 -040010380
Casey Schauflerad19db72011-12-15 10:09:07 -050010381 /*
10382 * If the active LSM wants to access the inode during
10383 * d_instantiate it needs these. Smack checks to see
10384 * if the filesystem supports xattrs by looking at the
10385 * ops vector.
10386 */
10387 inode->i_fop = &btrfs_file_operations;
10388 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010389 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010390 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10391
10392 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10393 if (err)
10394 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010395
Chris Mason39279cc2007-06-12 06:35:45 -040010396 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010397 if (!path) {
10398 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010399 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010400 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010401 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010402 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010403 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010404 datasize = btrfs_file_extent_calc_inline_size(name_len);
10405 err = btrfs_insert_empty_item(trans, root, path, &key,
10406 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010407 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010408 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010409 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010410 }
Chris Mason5f39d392007-10-15 16:14:19 -040010411 leaf = path->nodes[0];
10412 ei = btrfs_item_ptr(leaf, path->slots[0],
10413 struct btrfs_file_extent_item);
10414 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10415 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010416 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010417 btrfs_set_file_extent_encryption(leaf, ei, 0);
10418 btrfs_set_file_extent_compression(leaf, ei, 0);
10419 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10420 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10421
Chris Mason39279cc2007-06-12 06:35:45 -040010422 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010423 write_extent_buffer(leaf, symname, ptr, name_len);
10424 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010425 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010426
Chris Mason39279cc2007-06-12 06:35:45 -040010427 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010428 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010429 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010430 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010431 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010432 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010433 /*
10434 * Last step, add directory indexes for our symlink inode. This is the
10435 * last step to avoid extra cleanup of these indexes if an error happens
10436 * elsewhere above.
10437 */
10438 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010439 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10440 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010441 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010442 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010443 goto out_unlock_inode;
10444 }
10445
10446 unlock_new_inode(inode);
10447 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010448
10449out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010450 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010451 if (drop_inode) {
10452 inode_dec_link_count(inode);
10453 iput(inode);
10454 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010455 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010456 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010457
10458out_unlock_inode:
10459 drop_inode = 1;
10460 unlock_new_inode(inode);
10461 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010462}
Chris Mason16432982008-04-10 10:23:21 -040010463
Josef Bacik0af3d002010-06-21 14:48:16 -040010464static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10465 u64 start, u64 num_bytes, u64 min_size,
10466 loff_t actual_len, u64 *alloc_hint,
10467 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010469 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010470 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10471 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010472 struct btrfs_root *root = BTRFS_I(inode)->root;
10473 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010474 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010475 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010476 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010477 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010478 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010479 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010480 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010481
Josef Bacik0af3d002010-06-21 14:48:16 -040010482 if (trans)
10483 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010484 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010485 if (own_trans) {
10486 trans = btrfs_start_transaction(root, 3);
10487 if (IS_ERR(trans)) {
10488 ret = PTR_ERR(trans);
10489 break;
10490 }
Yan Zhengd899e052008-10-30 14:25:28 -040010491 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010492
Byongho Leeee221842015-12-15 01:42:10 +090010493 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010494 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010495 /*
10496 * If we are severely fragmented we could end up with really
10497 * small allocations, so if the allocator is returning small
10498 * chunks lets make its job easier by only searching for those
10499 * sized chunks.
10500 */
10501 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010502 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10503 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010504 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010505 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010506 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010507 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010508 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010509 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010510
Josef Bacik0b670dc2015-09-23 17:11:16 -040010511 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010512 ret = insert_reserved_file_extent(trans, inode,
10513 cur_offset, ins.objectid,
10514 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010515 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010516 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010517 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010518 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010519 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010520 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010521 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010522 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010523 break;
10524 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010525
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010526 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010527 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010528
Josef Bacik5dc562c2012-08-17 13:14:17 -040010529 em = alloc_extent_map();
10530 if (!em) {
10531 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10532 &BTRFS_I(inode)->runtime_flags);
10533 goto next;
10534 }
10535
10536 em->start = cur_offset;
10537 em->orig_start = cur_offset;
10538 em->len = ins.offset;
10539 em->block_start = ins.objectid;
10540 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010541 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010542 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010543 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010544 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10545 em->generation = trans->transid;
10546
10547 while (1) {
10548 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010549 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010550 write_unlock(&em_tree->lock);
10551 if (ret != -EEXIST)
10552 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010553 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010554 cur_offset + ins.offset - 1,
10555 0);
10556 }
10557 free_extent_map(em);
10558next:
Yan Zhengd899e052008-10-30 14:25:28 -040010559 num_bytes -= ins.offset;
10560 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010561 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010562
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010563 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010564 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010565 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010566 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010567 (actual_len > inode->i_size) &&
10568 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010569 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010570 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010571 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010572 i_size = cur_offset;
10573 i_size_write(inode, i_size);
10574 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010575 }
10576
Yan Zhengd899e052008-10-30 14:25:28 -040010577 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010578
10579 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010580 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010581 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010582 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010583 break;
10584 }
Yan Zhengd899e052008-10-30 14:25:28 -040010585
Josef Bacik0af3d002010-06-21 14:48:16 -040010586 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010587 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010588 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010589 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010590 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010591 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010592 return ret;
10593}
10594
Josef Bacik0af3d002010-06-21 14:48:16 -040010595int btrfs_prealloc_file_range(struct inode *inode, int mode,
10596 u64 start, u64 num_bytes, u64 min_size,
10597 loff_t actual_len, u64 *alloc_hint)
10598{
10599 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10600 min_size, actual_len, alloc_hint,
10601 NULL);
10602}
10603
10604int btrfs_prealloc_file_range_trans(struct inode *inode,
10605 struct btrfs_trans_handle *trans, int mode,
10606 u64 start, u64 num_bytes, u64 min_size,
10607 loff_t actual_len, u64 *alloc_hint)
10608{
10609 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10610 min_size, actual_len, alloc_hint, trans);
10611}
10612
Chris Masone6dcd2d2008-07-17 12:53:50 -040010613static int btrfs_set_page_dirty(struct page *page)
10614{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010615 return __set_page_dirty_nobuffers(page);
10616}
10617
Al Viro10556cb2011-06-20 19:28:19 -040010618static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010619{
Li Zefanb83cc962010-12-20 16:04:08 +080010620 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010621 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010622
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010623 if (mask & MAY_WRITE &&
10624 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10625 if (btrfs_root_readonly(root))
10626 return -EROFS;
10627 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10628 return -EACCES;
10629 }
Al Viro2830ba72011-06-20 19:16:29 -040010630 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010631}
Chris Mason39279cc2007-06-12 06:35:45 -040010632
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10634{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010635 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010636 struct btrfs_trans_handle *trans;
10637 struct btrfs_root *root = BTRFS_I(dir)->root;
10638 struct inode *inode = NULL;
10639 u64 objectid;
10640 u64 index;
10641 int ret = 0;
10642
10643 /*
10644 * 5 units required for adding orphan entry
10645 */
10646 trans = btrfs_start_transaction(root, 5);
10647 if (IS_ERR(trans))
10648 return PTR_ERR(trans);
10649
10650 ret = btrfs_find_free_ino(root, &objectid);
10651 if (ret)
10652 goto out;
10653
10654 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010655 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010656 if (IS_ERR(inode)) {
10657 ret = PTR_ERR(inode);
10658 inode = NULL;
10659 goto out;
10660 }
10661
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010662 inode->i_fop = &btrfs_file_operations;
10663 inode->i_op = &btrfs_file_inode_operations;
10664
10665 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010666 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10667
Chris Masonb0d5d102014-09-08 13:08:51 -070010668 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10669 if (ret)
10670 goto out_inode;
10671
10672 ret = btrfs_update_inode(trans, root, inode);
10673 if (ret)
10674 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010675 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010676 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010677 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010678
Filipe Manana5762b5c2014-08-01 00:10:32 +010010679 /*
10680 * We set number of links to 0 in btrfs_new_inode(), and here we set
10681 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10682 * through:
10683 *
10684 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10685 */
10686 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010687 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010688 d_tmpfile(dentry, inode);
10689 mark_inode_dirty(inode);
10690
10691out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010692 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010693 if (ret)
10694 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010695 btrfs_balance_delayed_items(fs_info);
10696 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010697 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010698
10699out_inode:
10700 unlock_new_inode(inode);
10701 goto out;
10702
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010703}
10704
David Sterba20a7db82017-02-17 16:24:29 +010010705__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010706static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010707{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010708 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010709}
10710
Josef Bacikc6100a42017-05-05 11:57:13 -040010711static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10712{
10713 struct inode *inode = private_data;
10714 return btrfs_sb(inode->i_sb);
10715}
10716
10717static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10718 u64 start, u64 end)
10719{
10720 struct inode *inode = private_data;
10721 u64 isize;
10722
10723 isize = i_size_read(inode);
10724 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10725 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10726 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10727 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10728 }
10729}
10730
10731void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10732{
10733 struct inode *inode = private_data;
10734 unsigned long index = start >> PAGE_SHIFT;
10735 unsigned long end_index = end >> PAGE_SHIFT;
10736 struct page *page;
10737
10738 while (index <= end_index) {
10739 page = find_get_page(inode->i_mapping, index);
10740 ASSERT(page); /* Pages should be in the extent_io_tree */
10741 set_page_writeback(page);
10742 put_page(page);
10743 index++;
10744 }
10745}
10746
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010747static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010748 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010749 .lookup = btrfs_lookup,
10750 .create = btrfs_create,
10751 .unlink = btrfs_unlink,
10752 .link = btrfs_link,
10753 .mkdir = btrfs_mkdir,
10754 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010755 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010756 .symlink = btrfs_symlink,
10757 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010758 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010759 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010760 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010761 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010762 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010763 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010764 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010765};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010766static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010767 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010768 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010769 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010770};
Yan, Zheng76dda932009-09-21 16:00:26 -040010771
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010772static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010773 .llseek = generic_file_llseek,
10774 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010775 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010776 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010777 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010778#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010779 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010780#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010781 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010782 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010783};
10784
David Sterba20e55062015-11-19 11:42:28 +010010785static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010786 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010787 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010788 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010789 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010790 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010791 .tree_fs_info = iotree_fs_info,
10792 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010793
10794 /* optional callbacks */
10795 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010796 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010797 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010798 .set_bit_hook = btrfs_set_bit_hook,
10799 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010800 .merge_extent_hook = btrfs_merge_extent_hook,
10801 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010802 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010803};
10804
Chris Mason35054392009-01-21 13:11:13 -050010805/*
10806 * btrfs doesn't support the bmap operation because swapfiles
10807 * use bmap to make a mapping of extents in the file. They assume
10808 * these extents won't change over the life of the file and they
10809 * use the bmap result to do IO directly to the drive.
10810 *
10811 * the btrfs bmap call would return logical addresses that aren't
10812 * suitable for IO and they also will change frequently as COW
10813 * operations happen. So, swapfile + btrfs == corruption.
10814 *
10815 * For now we're avoiding this by dropping bmap.
10816 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010817static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010818 .readpage = btrfs_readpage,
10819 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010820 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010821 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010822 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010823 .invalidatepage = btrfs_invalidatepage,
10824 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010825 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010826 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010827};
10828
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010829static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010830 .readpage = btrfs_readpage,
10831 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010832 .invalidatepage = btrfs_invalidatepage,
10833 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010834};
10835
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010836static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010837 .getattr = btrfs_getattr,
10838 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010839 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010840 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010841 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010842 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010843 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010844 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010845};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010846static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010847 .getattr = btrfs_getattr,
10848 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010849 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010850 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010851 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010852 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010853 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010854};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010855static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010856 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010857 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010858 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010859 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010860 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010861 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010862};
Yan, Zheng76dda932009-09-21 16:00:26 -040010863
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010864const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010865 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010866 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010867};