blob: 61f1ad89e97a4ae9c0754beda4f6115bd2fd5511 [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>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
138 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
139 bytes - PAGE_SIZE, false);
140}
141
Eric Sandeen48a3b632013-04-25 20:41:01 +0000142static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400143
Josef Bacik6a3891c2015-03-16 17:38:52 -0400144#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145void btrfs_test_inode_set_ops(struct inode *inode)
146{
147 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
148}
149#endif
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500152 struct inode *inode, struct inode *dir,
153 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500154{
155 int err;
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500159 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500160 return err;
161}
162
Chris Masond352ac62008-09-29 15:18:18 -0400163/*
Chris Masonc8b97812008-10-29 14:49:59 -0400164 * this does all the hard work for inserting an inline extent into
165 * the btree. The caller should have done a btrfs_drop_extents so that
166 * no overlapping inline items exist in the btree
167 */
Chris Mason40f76582014-05-21 13:35:51 -0700168static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000169 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct btrfs_root *root, struct inode *inode,
171 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000172 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page **compressed_pages)
174{
Chris Masonc8b97812008-10-29 14:49:59 -0400175 struct extent_buffer *leaf;
176 struct page *page = NULL;
177 char *kaddr;
178 unsigned long ptr;
179 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 int ret;
181 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400183
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400185 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400186
Chris Masonc8b97812008-10-29 14:49:59 -0400187 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000188
189 if (!extent_inserted) {
190 struct btrfs_key key;
191 size_t datasize;
192
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200193 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000194 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200195 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196
197 datasize = btrfs_file_extent_calc_inline_size(cur_size);
198 path->leave_spinning = 1;
199 ret = btrfs_insert_empty_item(trans, root, path, &key,
200 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200201 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000202 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400203 }
204 leaf = path->nodes[0];
205 ei = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
208 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
209 btrfs_set_file_extent_encryption(leaf, ei, 0);
210 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
211 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
212 ptr = btrfs_file_extent_inline_start(ei);
213
Li Zefan261507a02010-12-17 14:21:50 +0800214 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400215 struct page *cpage;
216 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500217 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500219 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300220 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
Cong Wang7ac687d2011-11-25 23:14:28 +0800222 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400223 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
226 i++;
227 ptr += cur_size;
228 compressed_size -= cur_size;
229 }
230 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800231 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400232 } else {
233 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 }
242 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400244
Yan, Zhengc2167752009-11-12 09:34:21 +0000245 /*
246 * we're an inline extent, so nobody can
247 * extend the file past i_size without locking
248 * a page we already have locked.
249 *
250 * We must do any isize and inode updates
251 * before we unlock the pages. Otherwise we
252 * could end up racing with unlink.
253 */
Chris Masonc8b97812008-10-29 14:49:59 -0400254 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100255 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000256
Chris Masonc8b97812008-10-29 14:49:59 -0400257fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259}
260
261
262/*
263 * conditionally insert an inline extent into the file. This
264 * does the checks required to make sure the data is small enough
265 * to fit as an inline extent.
266 */
Josef Bacik00361582013-08-14 14:02:47 -0400267static noinline int cow_file_range_inline(struct btrfs_root *root,
268 struct inode *inode, u64 start,
269 u64 end, size_t compressed_size,
270 int compress_type,
271 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400272{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400274 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400275 u64 isize = i_size_read(inode);
276 u64 actual_end = min(end + 1, isize);
277 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400278 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400279 u64 data_len = inline_len;
280 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 struct btrfs_path *path;
282 int extent_inserted = 0;
283 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400284
285 if (compressed_size)
286 data_len = compressed_size;
287
288 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400289 actual_end > fs_info->sectorsize ||
290 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400291 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400292 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400293 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400294 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400295 return 1;
296 }
297
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000298 path = btrfs_alloc_path();
299 if (!path)
300 return -ENOMEM;
301
Josef Bacik00361582013-08-14 14:02:47 -0400302 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000303 if (IS_ERR(trans)) {
304 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400305 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400307 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400308
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 if (compressed_size && compressed_pages)
310 extent_item_size = btrfs_file_extent_calc_inline_size(
311 compressed_size);
312 else
313 extent_item_size = btrfs_file_extent_calc_inline_size(
314 inline_len);
315
316 ret = __btrfs_drop_extents(trans, root, inode, path,
317 start, aligned_end, NULL,
318 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400319 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400320 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400321 goto out;
322 }
Chris Masonc8b97812008-10-29 14:49:59 -0400323
324 if (isize > actual_end)
325 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000326 ret = insert_inline_extent(trans, path, extent_inserted,
327 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400328 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000329 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400330 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400331 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400332 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400333 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400334 ret = 1;
335 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100336 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400337
Josef Bacikbdc20e62013-02-28 13:23:38 -0500338 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200339 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200340 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400341out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800342 /*
343 * Don't forget to free the reserved space, as for inlined extent
344 * it won't count as data extent, free them directly here.
345 * And at reserve time, it's always aligned to page size, so
346 * just free one page here.
347 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800348 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000349 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400350 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400351 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400352}
353
Chris Mason771ed682008-11-06 22:02:51 -0500354struct async_extent {
355 u64 start;
356 u64 ram_size;
357 u64 compressed_size;
358 struct page **pages;
359 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800360 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500361 struct list_head list;
362};
363
364struct async_cow {
365 struct inode *inode;
366 struct btrfs_root *root;
367 struct page *locked_page;
368 u64 start;
369 u64 end;
370 struct list_head extents;
371 struct btrfs_work work;
372};
373
374static noinline int add_async_extent(struct async_cow *cow,
375 u64 start, u64 ram_size,
376 u64 compressed_size,
377 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800378 unsigned long nr_pages,
379 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500380{
381 struct async_extent *async_extent;
382
383 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100384 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500385 async_extent->start = start;
386 async_extent->ram_size = ram_size;
387 async_extent->compressed_size = compressed_size;
388 async_extent->pages = pages;
389 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800390 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500391 list_add_tail(&async_extent->list, &cow->extents);
392 return 0;
393}
394
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300395static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800398
399 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400400 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800401 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200402 /* defrag ioctl */
403 if (BTRFS_I(inode)->defrag_compress)
404 return 1;
David Sterba1e20d1c2017-07-17 20:42:03 +0200405 /* bad compression ratios */
406 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
407 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400408 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800409 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200410 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300411 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800412 return 0;
413}
414
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200415static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800416 u64 start, u64 end, u64 num_bytes, u64 small_write)
417{
418 /* If this is a small write inside eof, kick off a defrag */
419 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200420 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800421 btrfs_add_inode_defrag(NULL, inode);
422}
423
Chris Masonc8b97812008-10-29 14:49:59 -0400424/*
Chris Mason771ed682008-11-06 22:02:51 -0500425 * we create compressed extents in two phases. The first
426 * phase compresses a range of pages that have already been
427 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400428 *
Chris Mason771ed682008-11-06 22:02:51 -0500429 * This is done inside an ordered work queue, and the compression
430 * is spread across many cpus. The actual IO submission is step
431 * two, and the ordered work queue takes care of making sure that
432 * happens in the same order things were put onto the queue by
433 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400434 *
Chris Mason771ed682008-11-06 22:02:51 -0500435 * If this code finds it can't get good compression, it puts an
436 * entry onto the work queue to write the uncompressed bytes. This
437 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300438 * are written in the same order that the flusher thread sent them
439 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400440 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100441static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500442 struct page *locked_page,
443 u64 start, u64 end,
444 struct async_cow *async_cow,
445 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400449 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400450 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400451 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500452 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400453 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400454 struct page **pages = NULL;
455 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400456 unsigned long total_compressed = 0;
457 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400458 int i;
459 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400461 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400462
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200463 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
464 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400465
Chris Mason42dc7ba2008-12-15 11:44:56 -0500466 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400467again:
468 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300469 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100470 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
471 nr_pages = min_t(unsigned long, nr_pages,
472 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400473
Chris Masonf03d9301f2009-02-04 09:31:06 -0500474 /*
475 * we don't want to send crud past the end of i_size through
476 * compression, that's just a waste of CPU time. So, if the
477 * end of the file is before the start of our current
478 * requested range of bytes, we bail out to the uncompressed
479 * cleanup code that can deal with all of this.
480 *
481 * It isn't really the fastest way to fix things, but this is a
482 * very uncommon corner.
483 */
484 if (actual_end <= start)
485 goto cleanup_and_bail_uncompressed;
486
Chris Masonc8b97812008-10-29 14:49:59 -0400487 total_compressed = actual_end - start;
488
Shilong Wang4bcbb332014-10-07 18:44:35 -0400489 /*
490 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400491 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400492 */
493 if (total_compressed <= blocksize &&
494 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
495 goto cleanup_and_bail_uncompressed;
496
David Sterba069eac72017-02-14 19:36:54 +0100497 total_compressed = min_t(unsigned long, total_compressed,
498 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000499 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500500 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400501 total_in = 0;
502 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400503
Chris Mason771ed682008-11-06 22:02:51 -0500504 /*
505 * we do compression for mount -o compress and when the
506 * inode has not been flagged as nocompress. This flag can
507 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400508 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300509 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400510 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100511 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800512 if (!pages) {
513 /* just bail out to the uncompressed code */
514 goto cont;
515 }
Chris Mason179e29e2007-11-01 11:28:41 -0400516
David Sterbaeec63c62017-07-17 19:41:31 +0200517 if (BTRFS_I(inode)->defrag_compress)
518 compress_type = BTRFS_I(inode)->defrag_compress;
519 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200520 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800521
Chris Mason4adaa612013-03-26 13:07:00 -0400522 /*
523 * we need to call clear_page_dirty_for_io on each
524 * page in the range. Otherwise applications with the file
525 * mmap'd can wander in and change the page contents while
526 * we are compressing them.
527 *
528 * If the compression fails for any reason, we set the pages
529 * dirty again later on.
530 */
531 extent_range_clear_dirty_for_io(inode, start, end);
532 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800533 ret = btrfs_compress_pages(compress_type,
534 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100535 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100536 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800537 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100538 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 if (!ret) {
541 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300542 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100543 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400544 char *kaddr;
545
546 /* zero the tail end of the last page, we might be
547 * sending it down to disk
548 */
549 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800550 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300552 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800553 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 }
555 will_compress = 1;
556 }
557 }
Li Zefan560f7d72011-09-08 10:22:01 +0800558cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (start == 0) {
560 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500561 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500563 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400564 */
Josef Bacik00361582013-08-14 14:02:47 -0400565 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800566 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500568 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400569 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000570 total_compressed,
571 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400574 unsigned long clear_flags = EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100575 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 unsigned long page_error_op;
577
Josef Bacik151a41b2013-07-29 13:22:24 -0400578 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100579 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400580
Chris Mason771ed682008-11-06 22:02:51 -0500581 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 * inline extent creation worked or returned error,
583 * we don't need to create any more async work items.
584 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500585 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800586 extent_clear_unlock_delalloc(inode, start, end, end,
587 NULL, clear_flags,
588 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400589 PAGE_CLEAR_DIRTY |
590 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400592 PAGE_END_WRITEBACK);
Filipe Manana1c81ba22017-03-08 16:43:49 +0000593 if (ret == 0)
594 btrfs_free_reserved_data_space_noquota(inode,
595 start,
596 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 goto free_pages_out;
598 }
599 }
600
601 if (will_compress) {
602 /*
603 * we aren't doing an inline extent round the compressed size
604 * up to a block size boundary so the allocator does sane
605 * things
606 */
Qu Wenruofda28322013-02-26 08:10:22 +0000607 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400608
609 /*
610 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300611 * win, compare the page count read with the blocks on disk,
612 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400613 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300614 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300615 if (total_compressed + blocksize <= total_in) {
Chris Masonc8b97812008-10-29 14:49:59 -0400616 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700617 *num_added += 1;
618
619 /*
620 * The async work queues will take care of doing actual
621 * allocation on disk for these compressed pages, and
622 * will submit them to the elevator.
623 */
624 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100625 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700626 compress_type);
627
628 if (start + num_bytes < end) {
629 start += num_bytes;
630 pages = NULL;
631 cond_resched();
632 goto again;
633 }
634 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
636 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400638 /*
639 * the compression code ran but failed to make things smaller,
640 * free any pages it allocated and our page pointer array
641 */
David Sterba4d3a8002017-02-14 19:04:07 +0100642 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400643 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300644 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400645 }
646 kfree(pages);
647 pages = NULL;
648 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100649 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400650
651 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400652 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200653 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500654 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500655 }
Chris Masonc8b97812008-10-29 14:49:59 -0400656 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500657cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700658 /*
659 * No compression, but we still need to write the pages in the file
660 * we've been given so far. redirty the locked page if it corresponds
661 * to our extent and set things up for the async work queue to run
662 * cow_file_range to do the normal delalloc dance.
663 */
664 if (page_offset(locked_page) >= start &&
665 page_offset(locked_page) <= end)
666 __set_page_dirty_nobuffers(locked_page);
667 /* unlocked later on in the async handlers */
668
669 if (redirty)
670 extent_range_redirty_for_io(inode, start, end);
671 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
672 BTRFS_COMPRESS_NONE);
673 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500674
Filipe Mananac44f6492014-10-09 21:15:44 +0100675 return;
Chris Mason771ed682008-11-06 22:02:51 -0500676
677free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100678 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500679 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500681 }
Chris Masond3977122009-01-05 21:25:51 -0500682 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500683}
Chris Mason771ed682008-11-06 22:02:51 -0500684
Filipe Manana40ae8372014-10-06 22:14:24 +0100685static void free_async_extent_pages(struct async_extent *async_extent)
686{
687 int i;
688
689 if (!async_extent->pages)
690 return;
691
692 for (i = 0; i < async_extent->nr_pages; i++) {
693 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300694 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100695 }
696 kfree(async_extent->pages);
697 async_extent->nr_pages = 0;
698 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500699}
700
701/*
702 * phase two of compressed writeback. This is the ordered portion
703 * of the code, which only gets called in the order the work was
704 * queued. We walk all the async extents created by compress_file_range
705 * and send them down to the disk.
706 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100707static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500708 struct async_cow *async_cow)
709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct async_extent *async_extent;
712 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500713 struct btrfs_key ins;
714 struct extent_map *em;
715 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500716 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500717 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719again:
Chris Masond3977122009-01-05 21:25:51 -0500720 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500721 async_extent = list_entry(async_cow->extents.next,
722 struct async_extent, list);
723 list_del(&async_extent->list);
724
725 io_tree = &BTRFS_I(inode)->io_tree;
726
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727retry:
Chris Mason771ed682008-11-06 22:02:51 -0500728 /* did the compression code fall back to uncompressed IO? */
729 if (!async_extent->pages) {
730 int page_started = 0;
731 unsigned long nr_written = 0;
732
733 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000734 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100735 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500736
737 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500738 ret = cow_file_range(inode, async_cow->locked_page,
739 async_extent->start,
740 async_extent->start +
741 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800742 async_extent->start +
743 async_extent->ram_size - 1,
744 &page_started, &nr_written, 0,
745 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100747 /* JDM XXX */
748
Chris Mason771ed682008-11-06 22:02:51 -0500749 /*
750 * if page_started, cow_file_range inserted an
751 * inline extent and took care of all the unlocking
752 * and IO for us. Otherwise, we need to submit
753 * all those pages down to the drive.
754 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500756 extent_write_locked_range(io_tree,
757 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500758 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500759 async_extent->ram_size - 1,
760 btrfs_get_extent,
761 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 else if (ret)
763 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500764 kfree(async_extent);
765 cond_resched();
766 continue;
767 }
768
769 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100770 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500771
Wang Xiaoguang18513092016-07-25 15:51:40 +0800772 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500773 async_extent->compressed_size,
774 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800775 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100777 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400779 if (ret == -ENOSPC) {
780 unlock_extent(io_tree, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800783
784 /*
785 * we need to redirty the pages if we decide to
786 * fallback to uncompressed IO, otherwise we
787 * will not submit these pages down to lower
788 * layers.
789 */
790 extent_range_redirty_for_io(inode,
791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400796 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500797 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500798 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000799 /*
800 * here we're doing allocation and writeback of the
801 * compressed pages
802 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800803 em = create_io_em(inode, async_extent->start,
804 async_extent->ram_size, /* len */
805 async_extent->start, /* orig_start */
806 ins.objectid, /* block_start */
807 ins.offset, /* block_len */
808 ins.offset, /* orig_block_len */
809 async_extent->ram_size, /* ram_bytes */
810 async_extent->compress_type,
811 BTRFS_ORDERED_COMPRESSED);
812 if (IS_ERR(em))
813 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200825 btrfs_drop_extent_cache(BTRFS_I(inode),
826 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 async_extent->start +
828 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400831 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500832
Chris Mason771ed682008-11-06 22:02:51 -0500833 /*
834 * clear dirty, set writeback and unlock the pages.
835 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400836 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400837 async_extent->start +
838 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800839 async_extent->start +
840 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400841 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
842 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400843 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200844 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500845 async_extent->start,
846 async_extent->ram_size,
847 ins.objectid,
848 ins.offset, async_extent->pages,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200849 async_extent->nr_pages)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
851 struct page *p = async_extent->pages[0];
852 const u64 start = async_extent->start;
853 const u64 end = start + async_extent->ram_size - 1;
854
855 p->mapping = inode->i_mapping;
856 tree->ops->writepage_end_io_hook(p, start, end,
857 NULL, 0);
858 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800859 extent_clear_unlock_delalloc(inode, start, end, end,
860 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 PAGE_END_WRITEBACK |
862 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100863 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 }
Chris Mason771ed682008-11-06 22:02:51 -0500865 alloc_hint = ins.objectid + ins.offset;
866 kfree(async_extent);
867 cond_resched();
868 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100869 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400871 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400872 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 async_extent->start +
876 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800877 async_extent->start +
878 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100880 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400881 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
882 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100883 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
884 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100885 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500888}
889
Josef Bacik4b46fce2010-05-23 11:00:55 -0400890static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
891 u64 num_bytes)
892{
893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
894 struct extent_map *em;
895 u64 alloc_hint = 0;
896
897 read_lock(&em_tree->lock);
898 em = search_extent_mapping(em_tree, start, num_bytes);
899 if (em) {
900 /*
901 * if block start isn't an actual block number then find the
902 * first block in this inode and use that as a hint. If that
903 * block is also bogus then just don't worry about it.
904 */
905 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
906 free_extent_map(em);
907 em = search_extent_mapping(em_tree, 0, 0);
908 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
909 alloc_hint = em->block_start;
910 if (em)
911 free_extent_map(em);
912 } else {
913 alloc_hint = em->block_start;
914 free_extent_map(em);
915 }
916 }
917 read_unlock(&em_tree->lock);
918
919 return alloc_hint;
920}
921
Chris Mason771ed682008-11-06 22:02:51 -0500922/*
923 * when extent_io.c finds a delayed allocation range in the file,
924 * the call backs end up in this code. The basic idea is to
925 * allocate extents on disk for the range, and create ordered data structs
926 * in ram to track those extents.
927 *
928 * locked_page is the page that writepage had locked already. We use
929 * it to make sure we don't do extra locks or unlocks.
930 *
931 * *page_started is set to one if we unlock locked_page and do everything
932 * required to start IO on it. It may be clean and already done with
933 * IO when we return.
934 */
Josef Bacik00361582013-08-14 14:02:47 -0400935static noinline int cow_file_range(struct inode *inode,
936 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800937 u64 start, u64 end, u64 delalloc_end,
938 int *page_started, unsigned long *nr_written,
939 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400942 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500943 u64 alloc_hint = 0;
944 u64 num_bytes;
945 unsigned long ram_size;
946 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000947 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400948 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500949 struct btrfs_key ins;
950 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000951 unsigned clear_bits;
952 unsigned long page_ops;
953 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500954 int ret = 0;
955
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200956 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400957 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500958 ret = -EINVAL;
959 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400960 }
Chris Mason771ed682008-11-06 22:02:51 -0500961
Qu Wenruofda28322013-02-26 08:10:22 +0000962 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500963 num_bytes = max(blocksize, num_bytes);
964 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500965
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200966 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400967
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (start == 0) {
969 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800970 ret = cow_file_range_inline(root, inode, start, end, 0,
971 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500972 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800973 extent_clear_unlock_delalloc(inode, start, end,
974 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400975 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100976 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400977 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400978 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
979 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800980 btrfs_free_reserved_data_space_noquota(inode, start,
981 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500982 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500984 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500985 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500988 }
989 }
Chris Masonc8b97812008-10-29 14:49:59 -0400990
991 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400992 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200995 btrfs_drop_extent_cache(BTRFS_I(inode), start,
996 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400997
Chris Masond3977122009-01-05 21:25:51 -0500998 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +0000999 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001000 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001001 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001002 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001003 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001005 cur_alloc_size = ins.offset;
1006 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001009 em = create_io_em(inode, start, ins.offset, /* len */
1010 start, /* orig_start */
1011 ins.objectid, /* block_start */
1012 ins.offset, /* block_len */
1013 ins.offset, /* orig_block_len */
1014 ram_size, /* ram_bytes */
1015 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001016 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001017 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001018 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001019 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020
Chris Masone6dcd2d2008-07-17 12:53:50 -04001021 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001023 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001024 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (root->root_key.objectid ==
1027 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1028 ret = btrfs_reloc_clone_csums(inode, start,
1029 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001030 /*
1031 * Only drop cache here, and process as normal.
1032 *
1033 * We must not allow extent_clear_unlock_delalloc()
1034 * at out_unlock label to free meta of this ordered
1035 * extent, as its meta should be freed by
1036 * btrfs_finish_ordered_io().
1037 *
1038 * So we must continue until @start is increased to
1039 * skip current ordered extent.
1040 */
Josef Bacik00361582013-08-14 14:02:47 -04001041 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001042 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1043 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 }
1045
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001046 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001047
Chris Masonc8b97812008-10-29 14:49:59 -04001048 /* we're not doing compressed IO, don't unlock the first
1049 * page (which the caller expects to stay locked), don't
1050 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001051 *
1052 * Do set the Private2 bit so we know this page was properly
1053 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001054 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001055 page_ops = unlock ? PAGE_UNLOCK : 0;
1056 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001057
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001059 start + ram_size - 1,
1060 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001062 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 if (disk_num_bytes < cur_alloc_size)
1064 disk_num_bytes = 0;
1065 else
1066 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001067 num_bytes -= cur_alloc_size;
1068 alloc_hint = ins.objectid + ins.offset;
1069 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001070 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071
1072 /*
1073 * btrfs_reloc_clone_csums() error, since start is increased
1074 * extent_clear_unlock_delalloc() at out_unlock label won't
1075 * free metadata of current ordered extent, we're OK to exit.
1076 */
1077 if (ret)
1078 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001080out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001081 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001082
Filipe Mananad9f85962014-08-25 10:43:00 +01001083out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001084 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001085out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001086 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001087 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001089 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1090 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1092 PAGE_END_WRITEBACK;
1093 /*
1094 * If we reserved an extent for our delalloc range (or a subrange) and
1095 * failed to create the respective ordered extent, then it means that
1096 * when we reserved the extent we decremented the extent's size from
1097 * the data space_info's bytes_may_use counter and incremented the
1098 * space_info's bytes_reserved counter by the same amount. We must make
1099 * sure extent_clear_unlock_delalloc() does not try to decrement again
1100 * the data space_info's bytes_may_use counter, therefore we do not pass
1101 * it the flag EXTENT_CLEAR_DATA_RESV.
1102 */
1103 if (extent_reserved) {
1104 extent_clear_unlock_delalloc(inode, start,
1105 start + cur_alloc_size,
1106 start + cur_alloc_size,
1107 locked_page,
1108 clear_bits,
1109 page_ops);
1110 start += cur_alloc_size;
1111 if (start >= end)
1112 goto out;
1113 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001114 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1115 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001116 clear_bits | EXTENT_CLEAR_DATA_RESV,
1117 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001119}
Chris Masonc8b97812008-10-29 14:49:59 -04001120
Chris Mason771ed682008-11-06 22:02:51 -05001121/*
1122 * work queue call back to started compression on a file and pages
1123 */
1124static noinline void async_cow_start(struct btrfs_work *work)
1125{
1126 struct async_cow *async_cow;
1127 int num_added = 0;
1128 async_cow = container_of(work, struct async_cow, work);
1129
1130 compress_file_range(async_cow->inode, async_cow->locked_page,
1131 async_cow->start, async_cow->end, async_cow,
1132 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001134 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001135 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 }
Chris Mason771ed682008-11-06 22:02:51 -05001137}
1138
1139/*
1140 * work queue call back to submit previously compressed pages
1141 */
1142static noinline void async_cow_submit(struct btrfs_work *work)
1143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001145 struct async_cow *async_cow;
1146 struct btrfs_root *root;
1147 unsigned long nr_pages;
1148
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001152 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001153 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1154 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001155
David Sterbaee863952015-02-16 19:41:40 +01001156 /*
1157 * atomic_sub_return implies a barrier for waitqueue_active
1158 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001159 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001160 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 waitqueue_active(&fs_info->async_submit_wait))
1162 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001163
Chris Masond3977122009-01-05 21:25:51 -05001164 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001165 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001166}
Chris Masonc8b97812008-10-29 14:49:59 -04001167
Chris Mason771ed682008-11-06 22:02:51 -05001168static noinline void async_cow_free(struct btrfs_work *work)
1169{
1170 struct async_cow *async_cow;
1171 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001172 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001173 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001174 kfree(async_cow);
1175}
1176
1177static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1178 u64 start, u64 end, int *page_started,
1179 unsigned long *nr_written)
1180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001182 struct async_cow *async_cow;
1183 struct btrfs_root *root = BTRFS_I(inode)->root;
1184 unsigned long nr_pages;
1185 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001186
Chris Masona3429ab2009-10-08 12:30:20 -04001187 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1188 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001189 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001190 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001192 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001193 async_cow->root = root;
1194 async_cow->locked_page = locked_page;
1195 async_cow->start = start;
1196
Wang Shilongf79707b2014-07-17 11:44:09 +08001197 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001199 cur_end = end;
1200 else
Byongho Leeee221842015-12-15 01:42:10 +09001201 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001202
1203 async_cow->end = cur_end;
1204 INIT_LIST_HEAD(&async_cow->extents);
1205
Liu Bo9e0af232014-08-15 23:36:53 +08001206 btrfs_init_work(&async_cow->work,
1207 btrfs_delalloc_helper,
1208 async_cow_start, async_cow_submit,
1209 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 nr_pages = (cur_end - start + PAGE_SIZE) >>
1212 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 while (atomic_read(&fs_info->async_submit_draining) &&
1218 atomic_read(&fs_info->async_delalloc_pages)) {
1219 wait_event(fs_info->async_submit_wait,
1220 (atomic_read(&fs_info->async_delalloc_pages) ==
1221 0));
Chris Mason771ed682008-11-06 22:02:51 -05001222 }
1223
1224 *nr_written += nr_pages;
1225 start = cur_end + 1;
1226 }
1227 *page_started = 1;
1228 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001229}
1230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001231static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001232 u64 bytenr, u64 num_bytes)
1233{
1234 int ret;
1235 struct btrfs_ordered_sum *sums;
1236 LIST_HEAD(list);
1237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001239 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 if (ret == 0 && list_empty(&list))
1241 return 0;
1242
1243 while (!list_empty(&list)) {
1244 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1245 list_del(&sums->list);
1246 kfree(sums);
1247 }
1248 return 1;
1249}
1250
Chris Masond352ac62008-09-29 15:18:18 -04001251/*
1252 * when nowcow writeback call back. This checks for snapshots or COW copies
1253 * of the extents that exist in the file, and COWs the file as required.
1254 *
1255 * If no cow copies or snapshots exist, we write directly to the existing
1256 * blocks on disk
1257 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001258static noinline int run_delalloc_nocow(struct inode *inode,
1259 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001260 u64 start, u64 end, int *page_started, int force,
1261 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_root *root = BTRFS_I(inode)->root;
1265 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001269 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 u64 cow_start;
1271 u64 cur_offset;
1272 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 u64 disk_bytenr;
1275 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001276 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001277 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001280 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 int nocow;
1282 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001283 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001284 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
1286 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001287 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001288 extent_clear_unlock_delalloc(inode, start, end, end,
1289 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001291 EXTENT_DO_ACCOUNTING |
1292 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 PAGE_CLEAR_DIRTY |
1294 PAGE_SET_WRITEBACK |
1295 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001296 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001297 }
Li Zefan82d59022011-04-20 10:33:24 +08001298
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001299 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 cow_start = (u64)-1;
1302 cur_offset = start;
1303 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001304 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001306 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001307 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key,
1311 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001312 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 found_key.type == BTRFS_EXTENT_DATA_KEY)
1314 path->slots[0]--;
1315 }
1316 check_prev = 0;
1317next_slot:
1318 leaf = path->nodes[0];
1319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1320 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001321 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001322 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 if (ret > 0)
1324 break;
1325 leaf = path->nodes[0];
1326 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 nocow = 0;
1329 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001332
Filipe Manana1d512cb2015-11-09 00:33:58 +00001333 if (found_key.objectid > ino)
1334 break;
1335 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1336 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1337 path->slots[0]++;
1338 goto next_slot;
1339 }
1340 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 found_key.offset > end)
1342 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (found_key.offset > cur_offset) {
1345 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001346 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 goto out_check;
1348 }
Chris Masonc31f8832008-01-08 15:46:31 -05001349
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 fi = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_file_extent_item);
1352 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001353
Josef Bacikcc95bef2013-04-04 14:31:27 -04001354 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001355 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1356 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001358 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 extent_end = found_key.offset +
1360 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001361 disk_num_bytes =
1362 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 if (extent_end <= start) {
1364 path->slots[0]++;
1365 goto next_slot;
1366 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001367 if (disk_bytenr == 0)
1368 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 if (btrfs_file_extent_compression(leaf, fi) ||
1370 btrfs_file_extent_encryption(leaf, fi) ||
1371 btrfs_file_extent_other_encoding(leaf, fi))
1372 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1374 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001375 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001377 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 found_key.offset -
1379 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001380 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001381 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 disk_bytenr += cur_offset - found_key.offset;
1383 num_bytes = min(end + 1, extent_end) - cur_offset;
1384 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001385 * if there are pending snapshots for this root,
1386 * we fall into common COW way.
1387 */
1388 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001389 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 if (!err)
1391 goto out_check;
1392 }
1393 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 * force cow if csum exists in the range.
1395 * this ensure that csum for a given extent are
1396 * either valid or do not exist.
1397 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001398 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001399 num_bytes)) {
1400 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001401 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001403 }
1404 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1405 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001406 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001407 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 nocow = 1;
1410 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1411 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001412 btrfs_file_extent_inline_len(leaf,
1413 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001414 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001415 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 } else {
1417 BUG_ON(1);
1418 }
1419out_check:
1420 if (extent_end <= start) {
1421 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001422 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001423 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001424 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001425 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 goto next_slot;
1427 }
1428 if (!nocow) {
1429 if (cow_start == (u64)-1)
1430 cow_start = cur_offset;
1431 cur_offset = extent_end;
1432 if (cur_offset > end)
1433 break;
1434 path->slots[0]++;
1435 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001436 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001437
David Sterbab3b4aa72011-04-21 01:20:15 +02001438 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001440 ret = cow_file_range(inode, locked_page,
1441 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001442 end, page_started, nr_written, 1,
1443 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001444 if (ret) {
1445 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001446 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001448 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001450 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454
Yan Zhengd899e052008-10-30 14:25:28 -04001455 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001456 u64 orig_start = found_key.offset - extent_offset;
1457
1458 em = create_io_em(inode, cur_offset, num_bytes,
1459 orig_start,
1460 disk_bytenr, /* block_start */
1461 num_bytes, /* block_len */
1462 disk_num_bytes, /* orig_block_len */
1463 ram_bytes, BTRFS_COMPRESS_NONE,
1464 BTRFS_ORDERED_PREALLOC);
1465 if (IS_ERR(em)) {
1466 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001467 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001468 if (nocow)
1469 btrfs_dec_nocow_writers(fs_info,
1470 disk_bytenr);
1471 ret = PTR_ERR(em);
1472 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001473 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001474 free_extent_map(em);
1475 }
1476
1477 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001478 type = BTRFS_ORDERED_PREALLOC;
1479 } else {
1480 type = BTRFS_ORDERED_NOCOW;
1481 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001482
1483 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001484 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001485 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001486 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001488
Yan, Zhengefa56462010-05-16 10:49:59 -04001489 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001490 BTRFS_DATA_RELOC_TREE_OBJECTID)
1491 /*
1492 * Error handled later, as we must prevent
1493 * extent_clear_unlock_delalloc() in error handler
1494 * from freeing metadata of created ordered extent.
1495 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001496 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1497 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001498
Josef Bacikc2790a22013-07-29 11:20:47 -04001499 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001500 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001501 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001502 EXTENT_DELALLOC |
1503 EXTENT_CLEAR_DATA_RESV,
1504 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1505
Wang Shilonge9894fd2014-03-27 11:12:25 +08001506 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001507 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001508 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001509
1510 /*
1511 * btrfs_reloc_clone_csums() error, now we're OK to call error
1512 * handler, as metadata for created ordered extent will only
1513 * be freed by btrfs_finish_ordered_io().
1514 */
1515 if (ret)
1516 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 if (cur_offset > end)
1518 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001519 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001520 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001521
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001523 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 cur_offset = end;
1525 }
1526
Yan Zheng80ff3852008-10-30 14:20:02 -04001527 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001528 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1529 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001530 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001531 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001532 }
1533
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001534error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001536 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001537 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001538 EXTENT_DELALLOC | EXTENT_DEFRAG |
1539 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1540 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 PAGE_SET_WRITEBACK |
1542 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001543 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001545}
1546
Wang Shilong47059d92014-07-03 18:22:07 +08001547static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1548{
1549
1550 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1551 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1552 return 0;
1553
1554 /*
1555 * @defrag_bytes is a hint value, no spinlock held here,
1556 * if is not zero, it means the file is defragging.
1557 * Force cow if given extent needs to be defragged.
1558 */
1559 if (BTRFS_I(inode)->defrag_bytes &&
1560 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1561 EXTENT_DEFRAG, 0, NULL))
1562 return 1;
1563
1564 return 0;
1565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * extent_io.c call back to do delayed allocation processing
1569 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001570static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001571 u64 start, u64 end, int *page_started,
1572 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001573{
Josef Bacikc6100a42017-05-05 11:57:13 -04001574 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001575 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001576 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001577
Wang Shilong47059d92014-07-03 18:22:07 +08001578 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001579 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001580 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001581 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001582 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001583 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001584 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001585 ret = cow_file_range(inode, locked_page, start, end, end,
1586 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001587 } else {
1588 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1589 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001590 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001591 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001592 }
Qu Wenruo524272602017-03-08 10:25:52 +08001593 if (ret)
1594 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001595 return ret;
1596}
1597
Josef Bacikc6100a42017-05-05 11:57:13 -04001598static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001599 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001600{
Josef Bacikc6100a42017-05-05 11:57:13 -04001601 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001602 u64 size;
1603
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001604 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001606 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607
Josef Bacikdcab6a32015-02-11 15:08:59 -05001608 size = orig->end - orig->start + 1;
1609 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001610 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001611 u64 new_size;
1612
1613 /*
Josef Bacikba117212015-03-13 15:01:24 -04001614 * See the explanation in btrfs_merge_extent_hook, the same
1615 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616 */
1617 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001618 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001619 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001620 num_extents += count_max_extents(new_size);
1621 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001622 return;
1623 }
1624
Josef Bacik9e0baf62011-07-15 15:16:44 +00001625 spin_lock(&BTRFS_I(inode)->lock);
1626 BTRFS_I(inode)->outstanding_extents++;
1627 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001628}
1629
1630/*
1631 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1632 * extents so we can keep track of new extents that are just merged onto old
1633 * extents, such as when we are doing sequential writes, so we can properly
1634 * account for the metadata space we'll need.
1635 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001636static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001637 struct extent_state *new,
1638 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001639{
Josef Bacikc6100a42017-05-05 11:57:13 -04001640 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001642 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644 /* not delalloc, ignore it */
1645 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001646 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647
Josef Bacik8461a3d2015-03-13 15:12:08 -04001648 if (new->start > other->start)
1649 new_size = new->end - other->start + 1;
1650 else
1651 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652
1653 /* we're not bigger than the max, unreserve the space and go */
1654 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1655 spin_lock(&BTRFS_I(inode)->lock);
1656 BTRFS_I(inode)->outstanding_extents--;
1657 spin_unlock(&BTRFS_I(inode)->lock);
1658 return;
1659 }
1660
1661 /*
Josef Bacikba117212015-03-13 15:01:24 -04001662 * We have to add up either side to figure out how many extents were
1663 * accounted for before we merged into one big extent. If the number of
1664 * extents we accounted for is <= the amount we need for the new range
1665 * then we can return, otherwise drop. Think of it like this
1666 *
1667 * [ 4k][MAX_SIZE]
1668 *
1669 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1670 * need 2 outstanding extents, on one side we have 1 and the other side
1671 * we have 1 so they are == and we can return. But in this case
1672 *
1673 * [MAX_SIZE+4k][MAX_SIZE+4k]
1674 *
1675 * Each range on their own accounts for 2 extents, but merged together
1676 * they are only 3 extents worth of accounting, so we need to drop in
1677 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678 */
Josef Bacikba117212015-03-13 15:01:24 -04001679 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001680 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001681 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001682 num_extents += count_max_extents(old_size);
1683 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 return;
1685
Josef Bacik9e0baf62011-07-15 15:16:44 +00001686 spin_lock(&BTRFS_I(inode)->lock);
1687 BTRFS_I(inode)->outstanding_extents--;
1688 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001689}
1690
Miao Xieeb73c1b2013-05-15 07:48:22 +00001691static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1692 struct inode *inode)
1693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1695
Miao Xieeb73c1b2013-05-15 07:48:22 +00001696 spin_lock(&root->delalloc_lock);
1697 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1698 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1699 &root->delalloc_inodes);
1700 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1701 &BTRFS_I(inode)->runtime_flags);
1702 root->nr_delalloc_inodes++;
1703 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001704 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001705 BUG_ON(!list_empty(&root->delalloc_root));
1706 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001707 &fs_info->delalloc_roots);
1708 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001709 }
1710 }
1711 spin_unlock(&root->delalloc_lock);
1712}
1713
1714static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001715 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001718
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001720 if (!list_empty(&inode->delalloc_inodes)) {
1721 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001722 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001723 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 root->nr_delalloc_inodes--;
1725 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 BUG_ON(list_empty(&root->delalloc_root));
1728 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001729 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001730 }
1731 }
1732 spin_unlock(&root->delalloc_lock);
1733}
1734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * extent_io.c set_bit_hook, used to track delayed allocation
1737 * bytes in this file, and to maintain the list of inodes that
1738 * have pending delalloc work to be done.
1739 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001740static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001741 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001742{
Josef Bacikc6100a42017-05-05 11:57:13 -04001743 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001744
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1746
Wang Shilong47059d92014-07-03 18:22:07 +08001747 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1748 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001749 /*
1750 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001751 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001752 * bit, which is only set or cleared with irqs on
1753 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001755 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001757 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001758
Josef Bacik9e0baf62011-07-15 15:16:44 +00001759 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001760 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001761 } else {
1762 spin_lock(&BTRFS_I(inode)->lock);
1763 BTRFS_I(inode)->outstanding_extents++;
1764 spin_unlock(&BTRFS_I(inode)->lock);
1765 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001766
Josef Bacik6a3891c2015-03-16 17:38:52 -04001767 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001769 return;
1770
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001771 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1772 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001774 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001775 if (*bits & EXTENT_DEFRAG)
1776 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 &BTRFS_I(inode)->runtime_flags))
1779 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001781 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001782
1783 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1784 (*bits & EXTENT_DELALLOC_NEW)) {
1785 spin_lock(&BTRFS_I(inode)->lock);
1786 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1787 state->start;
1788 spin_unlock(&BTRFS_I(inode)->lock);
1789 }
Chris Mason291d6732008-01-29 15:55:23 -05001790}
1791
Chris Masond352ac62008-09-29 15:18:18 -04001792/*
1793 * extent_io.c clear_bit_hook, see set_bit_hook for why
1794 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001795static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001796 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001797 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001798{
Josef Bacikc6100a42017-05-05 11:57:13 -04001799 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001801 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001802 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001803
Filipe Manana4a4b9642017-07-27 19:52:55 +01001804 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1805 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001806 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001807 spin_unlock(&inode->lock);
1808 }
Wang Shilong47059d92014-07-03 18:22:07 +08001809
Chris Mason75eff682008-12-15 15:54:40 -05001810 /*
1811 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001812 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001813 * bit, which is only set or cleared with irqs on
1814 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001815 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001816 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001817 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001818
Josef Bacik9e0baf62011-07-15 15:16:44 +00001819 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001820 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001821 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001822 spin_lock(&inode->lock);
1823 inode->outstanding_extents -= num_extents;
1824 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001825 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001826
Josef Bacikb6d08f02013-09-27 14:57:43 -04001827 /*
1828 * We don't reserve metadata space for space cache inodes so we
1829 * don't need to call dellalloc_release_metadata if there is an
1830 * error.
1831 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001832 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001833 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834 btrfs_delalloc_release_metadata(inode, len);
1835
Josef Bacik6a3891c2015-03-16 17:38:52 -04001836 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001837 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001838 return;
1839
Filipe Mananaa315e682017-03-06 23:04:20 +00001840 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1841 do_list && !(state->state & EXTENT_NORESERVE) &&
1842 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001843 btrfs_free_reserved_data_space_noquota(
1844 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001845 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001846
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001847 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1848 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001849 spin_lock(&inode->lock);
1850 inode->delalloc_bytes -= len;
1851 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001852 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001853 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001854 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001856 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001857
1858 if ((state->state & EXTENT_DELALLOC_NEW) &&
1859 (*bits & EXTENT_DELALLOC_NEW)) {
1860 spin_lock(&inode->lock);
1861 ASSERT(inode->new_delalloc_bytes >= len);
1862 inode->new_delalloc_bytes -= len;
1863 spin_unlock(&inode->lock);
1864 }
Chris Mason291d6732008-01-29 15:55:23 -05001865}
1866
Chris Masond352ac62008-09-29 15:18:18 -04001867/*
1868 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1869 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001870 *
1871 * return 1 if page cannot be merged to bio
1872 * return 0 if page can be merged to bio
1873 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001874 */
Mike Christie81a75f672016-06-05 14:31:54 -05001875int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001876 size_t size, struct bio *bio,
1877 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001878{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001879 struct inode *inode = page->mapping->host;
1880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001881 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001882 u64 length = 0;
1883 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001884 int ret;
1885
Chris Mason771ed682008-11-06 22:02:51 -05001886 if (bio_flags & EXTENT_BIO_COMPRESSED)
1887 return 0;
1888
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1892 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001893 if (ret < 0)
1894 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001895 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001896 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001897 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001898}
1899
Chris Masond352ac62008-09-29 15:18:18 -04001900/*
1901 * in order to insert checksums into the metadata in large chunks,
1902 * we wait until bio submission time. All the pages in the bio are
1903 * checksummed and sums are attached onto the ordered extent record.
1904 *
1905 * At IO completion time the cums attached on the ordered extent record
1906 * are inserted into the btree
1907 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001908static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001909 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001910 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001911{
Josef Bacikc6100a42017-05-05 11:57:13 -04001912 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001913 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001914
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001915 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001917 return 0;
1918}
Chris Masone0156402008-04-16 11:15:20 -04001919
Chris Mason4a69a412008-11-06 22:03:00 -05001920/*
1921 * in order to insert checksums into the metadata in large chunks,
1922 * we wait until bio submission time. All the pages in the bio are
1923 * checksummed and sums are attached onto the ordered extent record.
1924 *
1925 * At IO completion time the cums attached on the ordered extent record
1926 * are inserted into the btree
1927 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001928static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001929 int mirror_num, unsigned long bio_flags,
1930 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001931{
Josef Bacikc6100a42017-05-05 11:57:13 -04001932 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001934 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001935
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001936 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001937 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001938 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001939 bio_endio(bio);
1940 }
Stefan Behrens61891922012-11-05 18:51:52 +01001941 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001942}
1943
Chris Masond352ac62008-09-29 15:18:18 -04001944/*
Chris Masoncad321a2008-12-17 14:51:42 -05001945 * extent_io.c submission hook. This does the right thing for csum calculation
1946 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001947 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001948static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001949 int mirror_num, unsigned long bio_flags,
1950 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001951{
Josef Bacikc6100a42017-05-05 11:57:13 -04001952 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301955 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001956 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001957 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001958 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001959
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001960 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001961
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001962 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301963 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001964
Mike Christie37226b22016-06-05 14:31:52 -05001965 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001966 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001967 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001968 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001969
Chris Masond20f7042008-12-08 16:58:54 -05001970 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001971 ret = btrfs_submit_compressed_read(inode, bio,
1972 mirror_num,
1973 bio_flags);
1974 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001975 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001976 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001977 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001978 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001979 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001980 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001981 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001982 /* csum items have already been cloned */
1983 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1984 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001985 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001986 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1987 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001988 __btrfs_submit_bio_start,
1989 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001990 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001991 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001992 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001993 if (ret)
1994 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001995 }
1996
Chris Mason0b86a832008-03-24 15:01:56 -04001997mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001998 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001999
2000out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002001 if (ret) {
2002 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002003 bio_endio(bio);
2004 }
Stefan Behrens61891922012-11-05 18:51:52 +01002005 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002006}
Chris Mason6885f302008-02-20 16:11:05 -05002007
Chris Masond352ac62008-09-29 15:18:18 -04002008/*
2009 * given a list of ordered sums record them in the inode. This happens
2010 * at IO completion time based on sums calculated at bio submission time.
2011 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002012static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002013 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002014{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015 struct btrfs_ordered_sum *sum;
2016
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002017 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002018 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002019 btrfs_csum_file_blocks(trans,
2020 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002021 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002022 }
2023 return 0;
2024}
2025
Josef Bacik2ac55d42010-02-03 19:33:23 +00002026int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002027 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002028{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002029 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002030 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002031 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002032}
2033
Chris Masond352ac62008-09-29 15:18:18 -04002034/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002035struct btrfs_writepage_fixup {
2036 struct page *page;
2037 struct btrfs_work work;
2038};
2039
Christoph Hellwigb2950862008-12-02 09:54:17 -05002040static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002041{
2042 struct btrfs_writepage_fixup *fixup;
2043 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002044 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002045 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002046 struct page *page;
2047 struct inode *inode;
2048 u64 page_start;
2049 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002050 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002051
2052 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2053 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002054again:
Chris Mason247e7432008-07-17 12:53:51 -04002055 lock_page(page);
2056 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2057 ClearPageChecked(page);
2058 goto out_page;
2059 }
2060
2061 inode = page->mapping->host;
2062 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002063 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002064
David Sterbaff13db42015-12-03 14:30:40 +01002065 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002066 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002067
2068 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002069 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002070 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002071
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002072 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002074 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002075 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2076 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002077 unlock_page(page);
2078 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002079 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002080 goto again;
2081 }
Chris Mason247e7432008-07-17 12:53:51 -04002082
Qu Wenruo364ecf32017-02-27 15:10:38 +08002083 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002084 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002085 if (ret) {
2086 mapping_set_error(page->mapping, ret);
2087 end_extent_writepage(page, ret, page_start, page_end);
2088 ClearPageChecked(page);
2089 goto out;
2090 }
2091
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002092 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2093 0);
Chris Mason247e7432008-07-17 12:53:51 -04002094 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002095 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002096out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002097 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2098 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002099out_page:
2100 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002101 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002102 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002103 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002104}
2105
2106/*
2107 * There are a few paths in the higher layers of the kernel that directly
2108 * set the page dirty bit without asking the filesystem if it is a
2109 * good idea. This causes problems because we want to make sure COW
2110 * properly happens and the data=ordered rules are followed.
2111 *
Chris Masonc8b97812008-10-29 14:49:59 -04002112 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002113 * hasn't been properly setup for IO. We kick off an async process
2114 * to fix it up. The async helper will wait for ordered extents, set
2115 * the delalloc bit and make it safe to write the page.
2116 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002117static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002118{
2119 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002120 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002121 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002122
Chris Mason8b62b722009-09-02 16:53:46 -04002123 /* this page is properly in the ordered list */
2124 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002125 return 0;
2126
2127 if (PageChecked(page))
2128 return -EAGAIN;
2129
2130 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2131 if (!fixup)
2132 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002133
Chris Mason247e7432008-07-17 12:53:51 -04002134 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002135 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002136 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2137 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002138 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002139 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002140 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002141}
2142
Yan Zhengd899e052008-10-30 14:25:28 -04002143static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2144 struct inode *inode, u64 file_pos,
2145 u64 disk_bytenr, u64 disk_num_bytes,
2146 u64 num_bytes, u64 ram_bytes,
2147 u8 compression, u8 encryption,
2148 u16 other_encoding, int extent_type)
2149{
2150 struct btrfs_root *root = BTRFS_I(inode)->root;
2151 struct btrfs_file_extent_item *fi;
2152 struct btrfs_path *path;
2153 struct extent_buffer *leaf;
2154 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002155 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002156 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002157 int ret;
2158
2159 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002160 if (!path)
2161 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002162
Chris Masona1ed8352009-09-11 12:27:37 -04002163 /*
2164 * we may be replacing one extent in the tree with another.
2165 * The new extent is pinned in the extent map, and we don't want
2166 * to drop it from the cache until it is completely in the btree.
2167 *
2168 * So, tell btrfs_drop_extents to leave this extent in the cache.
2169 * the caller is expected to unpin it and allow it to be merged
2170 * with the others.
2171 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002172 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2173 file_pos + num_bytes, NULL, 0,
2174 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002175 if (ret)
2176 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002177
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002178 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002179 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002180 ins.offset = file_pos;
2181 ins.type = BTRFS_EXTENT_DATA_KEY;
2182
2183 path->leave_spinning = 1;
2184 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2185 sizeof(*fi));
2186 if (ret)
2187 goto out;
2188 }
Yan Zhengd899e052008-10-30 14:25:28 -04002189 leaf = path->nodes[0];
2190 fi = btrfs_item_ptr(leaf, path->slots[0],
2191 struct btrfs_file_extent_item);
2192 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2193 btrfs_set_file_extent_type(leaf, fi, extent_type);
2194 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2195 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2196 btrfs_set_file_extent_offset(leaf, fi, 0);
2197 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2198 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2199 btrfs_set_file_extent_compression(leaf, fi, compression);
2200 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2201 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002202
Yan Zhengd899e052008-10-30 14:25:28 -04002203 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002204 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002205
2206 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002207
2208 ins.objectid = disk_bytenr;
2209 ins.offset = disk_num_bytes;
2210 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002211
Qu Wenruo297d7502015-09-08 17:08:37 +08002212 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002213 * Release the reserved range from inode dirty range map, as it is
2214 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002215 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002216 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2217 if (ret < 0)
2218 goto out;
2219 qg_released = ret;
2220 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2221 btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002222out:
Yan Zhengd899e052008-10-30 14:25:28 -04002223 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002224
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002225 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002226}
2227
Liu Bo38c227d2013-01-29 03:18:40 +00002228/* snapshot-aware defrag */
2229struct sa_defrag_extent_backref {
2230 struct rb_node node;
2231 struct old_sa_defrag_extent *old;
2232 u64 root_id;
2233 u64 inum;
2234 u64 file_pos;
2235 u64 extent_offset;
2236 u64 num_bytes;
2237 u64 generation;
2238};
2239
2240struct old_sa_defrag_extent {
2241 struct list_head list;
2242 struct new_sa_defrag_extent *new;
2243
2244 u64 extent_offset;
2245 u64 bytenr;
2246 u64 offset;
2247 u64 len;
2248 int count;
2249};
2250
2251struct new_sa_defrag_extent {
2252 struct rb_root root;
2253 struct list_head head;
2254 struct btrfs_path *path;
2255 struct inode *inode;
2256 u64 file_pos;
2257 u64 len;
2258 u64 bytenr;
2259 u64 disk_len;
2260 u8 compress_type;
2261};
2262
2263static int backref_comp(struct sa_defrag_extent_backref *b1,
2264 struct sa_defrag_extent_backref *b2)
2265{
2266 if (b1->root_id < b2->root_id)
2267 return -1;
2268 else if (b1->root_id > b2->root_id)
2269 return 1;
2270
2271 if (b1->inum < b2->inum)
2272 return -1;
2273 else if (b1->inum > b2->inum)
2274 return 1;
2275
2276 if (b1->file_pos < b2->file_pos)
2277 return -1;
2278 else if (b1->file_pos > b2->file_pos)
2279 return 1;
2280
2281 /*
2282 * [------------------------------] ===> (a range of space)
2283 * |<--->| |<---->| =============> (fs/file tree A)
2284 * |<---------------------------->| ===> (fs/file tree B)
2285 *
2286 * A range of space can refer to two file extents in one tree while
2287 * refer to only one file extent in another tree.
2288 *
2289 * So we may process a disk offset more than one time(two extents in A)
2290 * and locate at the same extent(one extent in B), then insert two same
2291 * backrefs(both refer to the extent in B).
2292 */
2293 return 0;
2294}
2295
2296static void backref_insert(struct rb_root *root,
2297 struct sa_defrag_extent_backref *backref)
2298{
2299 struct rb_node **p = &root->rb_node;
2300 struct rb_node *parent = NULL;
2301 struct sa_defrag_extent_backref *entry;
2302 int ret;
2303
2304 while (*p) {
2305 parent = *p;
2306 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2307
2308 ret = backref_comp(backref, entry);
2309 if (ret < 0)
2310 p = &(*p)->rb_left;
2311 else
2312 p = &(*p)->rb_right;
2313 }
2314
2315 rb_link_node(&backref->node, parent, p);
2316 rb_insert_color(&backref->node, root);
2317}
2318
2319/*
2320 * Note the backref might has changed, and in this case we just return 0.
2321 */
2322static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2323 void *ctx)
2324{
2325 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002326 struct old_sa_defrag_extent *old = ctx;
2327 struct new_sa_defrag_extent *new = old->new;
2328 struct btrfs_path *path = new->path;
2329 struct btrfs_key key;
2330 struct btrfs_root *root;
2331 struct sa_defrag_extent_backref *backref;
2332 struct extent_buffer *leaf;
2333 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002334 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002335 int slot;
2336 int ret;
2337 u64 extent_offset;
2338 u64 num_bytes;
2339
2340 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002341 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002342 return 0;
2343
2344 key.objectid = root_id;
2345 key.type = BTRFS_ROOT_ITEM_KEY;
2346 key.offset = (u64)-1;
2347
Liu Bo38c227d2013-01-29 03:18:40 +00002348 root = btrfs_read_fs_root_no_name(fs_info, &key);
2349 if (IS_ERR(root)) {
2350 if (PTR_ERR(root) == -ENOENT)
2351 return 0;
2352 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002353 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002354 inum, offset, root_id);
2355 return PTR_ERR(root);
2356 }
2357
2358 key.objectid = inum;
2359 key.type = BTRFS_EXTENT_DATA_KEY;
2360 if (offset > (u64)-1 << 32)
2361 key.offset = 0;
2362 else
2363 key.offset = offset;
2364
2365 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302366 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002367 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002368 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002369
2370 while (1) {
2371 cond_resched();
2372
2373 leaf = path->nodes[0];
2374 slot = path->slots[0];
2375
2376 if (slot >= btrfs_header_nritems(leaf)) {
2377 ret = btrfs_next_leaf(root, path);
2378 if (ret < 0) {
2379 goto out;
2380 } else if (ret > 0) {
2381 ret = 0;
2382 goto out;
2383 }
2384 continue;
2385 }
2386
2387 path->slots[0]++;
2388
2389 btrfs_item_key_to_cpu(leaf, &key, slot);
2390
2391 if (key.objectid > inum)
2392 goto out;
2393
2394 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2395 continue;
2396
2397 extent = btrfs_item_ptr(leaf, slot,
2398 struct btrfs_file_extent_item);
2399
2400 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2401 continue;
2402
Liu Boe68afa42013-07-01 22:13:26 +08002403 /*
2404 * 'offset' refers to the exact key.offset,
2405 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2406 * (key.offset - extent_offset).
2407 */
2408 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002409 continue;
2410
Liu Boe68afa42013-07-01 22:13:26 +08002411 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002412 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002413
Liu Bo38c227d2013-01-29 03:18:40 +00002414 if (extent_offset >= old->extent_offset + old->offset +
2415 old->len || extent_offset + num_bytes <=
2416 old->extent_offset + old->offset)
2417 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002418 break;
2419 }
2420
2421 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2422 if (!backref) {
2423 ret = -ENOENT;
2424 goto out;
2425 }
2426
2427 backref->root_id = root_id;
2428 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002429 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002430 backref->num_bytes = num_bytes;
2431 backref->extent_offset = extent_offset;
2432 backref->generation = btrfs_file_extent_generation(leaf, extent);
2433 backref->old = old;
2434 backref_insert(&new->root, backref);
2435 old->count++;
2436out:
2437 btrfs_release_path(path);
2438 WARN_ON(ret);
2439 return ret;
2440}
2441
2442static noinline bool record_extent_backrefs(struct btrfs_path *path,
2443 struct new_sa_defrag_extent *new)
2444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002445 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002446 struct old_sa_defrag_extent *old, *tmp;
2447 int ret;
2448
2449 new->path = path;
2450
2451 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002452 ret = iterate_inodes_from_logical(old->bytenr +
2453 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002454 path, record_one_backref,
2455 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002456 if (ret < 0 && ret != -ENOENT)
2457 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002458
2459 /* no backref to be processed for this extent */
2460 if (!old->count) {
2461 list_del(&old->list);
2462 kfree(old);
2463 }
2464 }
2465
2466 if (list_empty(&new->head))
2467 return false;
2468
2469 return true;
2470}
2471
2472static int relink_is_mergable(struct extent_buffer *leaf,
2473 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002474 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002475{
Liu Bo116e0022013-08-02 16:30:40 +08002476 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002477 return 0;
2478
2479 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2480 return 0;
2481
Liu Bo116e0022013-08-02 16:30:40 +08002482 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2483 return 0;
2484
2485 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002486 btrfs_file_extent_other_encoding(leaf, fi))
2487 return 0;
2488
2489 return 1;
2490}
2491
2492/*
2493 * Note the backref might has changed, and in this case we just return 0.
2494 */
2495static noinline int relink_extent_backref(struct btrfs_path *path,
2496 struct sa_defrag_extent_backref *prev,
2497 struct sa_defrag_extent_backref *backref)
2498{
2499 struct btrfs_file_extent_item *extent;
2500 struct btrfs_file_extent_item *item;
2501 struct btrfs_ordered_extent *ordered;
2502 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002503 struct btrfs_root *root;
2504 struct btrfs_key key;
2505 struct extent_buffer *leaf;
2506 struct old_sa_defrag_extent *old = backref->old;
2507 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002508 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002509 struct inode *inode;
2510 struct extent_state *cached = NULL;
2511 int ret = 0;
2512 u64 start;
2513 u64 len;
2514 u64 lock_start;
2515 u64 lock_end;
2516 bool merge = false;
2517 int index;
2518
2519 if (prev && prev->root_id == backref->root_id &&
2520 prev->inum == backref->inum &&
2521 prev->file_pos + prev->num_bytes == backref->file_pos)
2522 merge = true;
2523
2524 /* step 1: get root */
2525 key.objectid = backref->root_id;
2526 key.type = BTRFS_ROOT_ITEM_KEY;
2527 key.offset = (u64)-1;
2528
Liu Bo38c227d2013-01-29 03:18:40 +00002529 index = srcu_read_lock(&fs_info->subvol_srcu);
2530
2531 root = btrfs_read_fs_root_no_name(fs_info, &key);
2532 if (IS_ERR(root)) {
2533 srcu_read_unlock(&fs_info->subvol_srcu, index);
2534 if (PTR_ERR(root) == -ENOENT)
2535 return 0;
2536 return PTR_ERR(root);
2537 }
Liu Bo38c227d2013-01-29 03:18:40 +00002538
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002539 if (btrfs_root_readonly(root)) {
2540 srcu_read_unlock(&fs_info->subvol_srcu, index);
2541 return 0;
2542 }
2543
Liu Bo38c227d2013-01-29 03:18:40 +00002544 /* step 2: get inode */
2545 key.objectid = backref->inum;
2546 key.type = BTRFS_INODE_ITEM_KEY;
2547 key.offset = 0;
2548
2549 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2550 if (IS_ERR(inode)) {
2551 srcu_read_unlock(&fs_info->subvol_srcu, index);
2552 return 0;
2553 }
2554
2555 srcu_read_unlock(&fs_info->subvol_srcu, index);
2556
2557 /* step 3: relink backref */
2558 lock_start = backref->file_pos;
2559 lock_end = backref->file_pos + backref->num_bytes - 1;
2560 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002561 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002562
2563 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2564 if (ordered) {
2565 btrfs_put_ordered_extent(ordered);
2566 goto out_unlock;
2567 }
2568
2569 trans = btrfs_join_transaction(root);
2570 if (IS_ERR(trans)) {
2571 ret = PTR_ERR(trans);
2572 goto out_unlock;
2573 }
2574
2575 key.objectid = backref->inum;
2576 key.type = BTRFS_EXTENT_DATA_KEY;
2577 key.offset = backref->file_pos;
2578
2579 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2580 if (ret < 0) {
2581 goto out_free_path;
2582 } else if (ret > 0) {
2583 ret = 0;
2584 goto out_free_path;
2585 }
2586
2587 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2588 struct btrfs_file_extent_item);
2589
2590 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2591 backref->generation)
2592 goto out_free_path;
2593
2594 btrfs_release_path(path);
2595
2596 start = backref->file_pos;
2597 if (backref->extent_offset < old->extent_offset + old->offset)
2598 start += old->extent_offset + old->offset -
2599 backref->extent_offset;
2600
2601 len = min(backref->extent_offset + backref->num_bytes,
2602 old->extent_offset + old->offset + old->len);
2603 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2604
2605 ret = btrfs_drop_extents(trans, root, inode, start,
2606 start + len, 1);
2607 if (ret)
2608 goto out_free_path;
2609again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002610 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002611 key.type = BTRFS_EXTENT_DATA_KEY;
2612 key.offset = start;
2613
Liu Boa09a0a72013-03-11 09:20:58 +00002614 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002615 if (merge) {
2616 struct btrfs_file_extent_item *fi;
2617 u64 extent_len;
2618 struct btrfs_key found_key;
2619
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002620 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002621 if (ret < 0)
2622 goto out_free_path;
2623
2624 path->slots[0]--;
2625 leaf = path->nodes[0];
2626 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2627
2628 fi = btrfs_item_ptr(leaf, path->slots[0],
2629 struct btrfs_file_extent_item);
2630 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2631
Liu Bo116e0022013-08-02 16:30:40 +08002632 if (extent_len + found_key.offset == start &&
2633 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002634 btrfs_set_file_extent_num_bytes(leaf, fi,
2635 extent_len + len);
2636 btrfs_mark_buffer_dirty(leaf);
2637 inode_add_bytes(inode, len);
2638
2639 ret = 1;
2640 goto out_free_path;
2641 } else {
2642 merge = false;
2643 btrfs_release_path(path);
2644 goto again;
2645 }
2646 }
2647
2648 ret = btrfs_insert_empty_item(trans, root, path, &key,
2649 sizeof(*extent));
2650 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002651 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002652 goto out_free_path;
2653 }
2654
2655 leaf = path->nodes[0];
2656 item = btrfs_item_ptr(leaf, path->slots[0],
2657 struct btrfs_file_extent_item);
2658 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2659 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2660 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2661 btrfs_set_file_extent_num_bytes(leaf, item, len);
2662 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2663 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2664 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2665 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2666 btrfs_set_file_extent_encryption(leaf, item, 0);
2667 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2668
2669 btrfs_mark_buffer_dirty(leaf);
2670 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002671 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002672
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002673 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002674 new->disk_len, 0,
2675 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002676 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002677 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002678 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002679 goto out_free_path;
2680 }
2681
2682 ret = 1;
2683out_free_path:
2684 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002685 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002686 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002687out_unlock:
2688 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2689 &cached, GFP_NOFS);
2690 iput(inode);
2691 return ret;
2692}
2693
Liu Bo6f519562013-10-29 10:45:05 +08002694static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2695{
2696 struct old_sa_defrag_extent *old, *tmp;
2697
2698 if (!new)
2699 return;
2700
2701 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002702 kfree(old);
2703 }
2704 kfree(new);
2705}
2706
Liu Bo38c227d2013-01-29 03:18:40 +00002707static void relink_file_extents(struct new_sa_defrag_extent *new)
2708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002709 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002710 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002711 struct sa_defrag_extent_backref *backref;
2712 struct sa_defrag_extent_backref *prev = NULL;
2713 struct inode *inode;
2714 struct btrfs_root *root;
2715 struct rb_node *node;
2716 int ret;
2717
2718 inode = new->inode;
2719 root = BTRFS_I(inode)->root;
2720
2721 path = btrfs_alloc_path();
2722 if (!path)
2723 return;
2724
2725 if (!record_extent_backrefs(path, new)) {
2726 btrfs_free_path(path);
2727 goto out;
2728 }
2729 btrfs_release_path(path);
2730
2731 while (1) {
2732 node = rb_first(&new->root);
2733 if (!node)
2734 break;
2735 rb_erase(node, &new->root);
2736
2737 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2738
2739 ret = relink_extent_backref(path, prev, backref);
2740 WARN_ON(ret < 0);
2741
2742 kfree(prev);
2743
2744 if (ret == 1)
2745 prev = backref;
2746 else
2747 prev = NULL;
2748 cond_resched();
2749 }
2750 kfree(prev);
2751
2752 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002753out:
Liu Bo6f519562013-10-29 10:45:05 +08002754 free_sa_defrag_extent(new);
2755
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002756 atomic_dec(&fs_info->defrag_running);
2757 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002758}
2759
2760static struct new_sa_defrag_extent *
2761record_old_file_extents(struct inode *inode,
2762 struct btrfs_ordered_extent *ordered)
2763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002765 struct btrfs_root *root = BTRFS_I(inode)->root;
2766 struct btrfs_path *path;
2767 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002768 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002769 struct new_sa_defrag_extent *new;
2770 int ret;
2771
2772 new = kmalloc(sizeof(*new), GFP_NOFS);
2773 if (!new)
2774 return NULL;
2775
2776 new->inode = inode;
2777 new->file_pos = ordered->file_offset;
2778 new->len = ordered->len;
2779 new->bytenr = ordered->start;
2780 new->disk_len = ordered->disk_len;
2781 new->compress_type = ordered->compress_type;
2782 new->root = RB_ROOT;
2783 INIT_LIST_HEAD(&new->head);
2784
2785 path = btrfs_alloc_path();
2786 if (!path)
2787 goto out_kfree;
2788
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002789 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002790 key.type = BTRFS_EXTENT_DATA_KEY;
2791 key.offset = new->file_pos;
2792
2793 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2794 if (ret < 0)
2795 goto out_free_path;
2796 if (ret > 0 && path->slots[0] > 0)
2797 path->slots[0]--;
2798
2799 /* find out all the old extents for the file range */
2800 while (1) {
2801 struct btrfs_file_extent_item *extent;
2802 struct extent_buffer *l;
2803 int slot;
2804 u64 num_bytes;
2805 u64 offset;
2806 u64 end;
2807 u64 disk_bytenr;
2808 u64 extent_offset;
2809
2810 l = path->nodes[0];
2811 slot = path->slots[0];
2812
2813 if (slot >= btrfs_header_nritems(l)) {
2814 ret = btrfs_next_leaf(root, path);
2815 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002816 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002817 else if (ret > 0)
2818 break;
2819 continue;
2820 }
2821
2822 btrfs_item_key_to_cpu(l, &key, slot);
2823
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002824 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002825 break;
2826 if (key.type != BTRFS_EXTENT_DATA_KEY)
2827 break;
2828 if (key.offset >= new->file_pos + new->len)
2829 break;
2830
2831 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2832
2833 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2834 if (key.offset + num_bytes < new->file_pos)
2835 goto next;
2836
2837 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2838 if (!disk_bytenr)
2839 goto next;
2840
2841 extent_offset = btrfs_file_extent_offset(l, extent);
2842
2843 old = kmalloc(sizeof(*old), GFP_NOFS);
2844 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002845 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002846
2847 offset = max(new->file_pos, key.offset);
2848 end = min(new->file_pos + new->len, key.offset + num_bytes);
2849
2850 old->bytenr = disk_bytenr;
2851 old->extent_offset = extent_offset;
2852 old->offset = offset - key.offset;
2853 old->len = end - offset;
2854 old->new = new;
2855 old->count = 0;
2856 list_add_tail(&old->list, &new->head);
2857next:
2858 path->slots[0]++;
2859 cond_resched();
2860 }
2861
2862 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002863 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002864
2865 return new;
2866
Liu Bo38c227d2013-01-29 03:18:40 +00002867out_free_path:
2868 btrfs_free_path(path);
2869out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002870 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002871 return NULL;
2872}
2873
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002874static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002875 u64 start, u64 len)
2876{
2877 struct btrfs_block_group_cache *cache;
2878
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002879 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002880 ASSERT(cache);
2881
2882 spin_lock(&cache->lock);
2883 cache->delalloc_bytes -= len;
2884 spin_unlock(&cache->lock);
2885
2886 btrfs_put_block_group(cache);
2887}
2888
Chris Masond352ac62008-09-29 15:18:18 -04002889/* as ordered data IO finishes, this gets called so we can finish
2890 * an ordered extent if the range of bytes in the file it covers are
2891 * fully written.
2892 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002893static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002894{
Josef Bacik5fd02042012-05-02 14:00:54 -04002895 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002897 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002898 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002899 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002900 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002901 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002902 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002903 int ret = 0;
2904 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002905 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002906 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002907 bool range_locked = false;
2908 bool clear_new_delalloc_bytes = false;
2909
2910 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2911 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2912 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2913 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002914
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002915 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002916
Josef Bacik5fd02042012-05-02 14:00:54 -04002917 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2918 ret = -EIO;
2919 goto out;
2920 }
2921
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002922 btrfs_free_io_failure_record(BTRFS_I(inode),
2923 ordered_extent->file_offset,
2924 ordered_extent->file_offset +
2925 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002926
Josef Bacik77cef2e2013-08-29 13:57:21 -04002927 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2928 truncated = true;
2929 logical_len = ordered_extent->truncated_len;
2930 /* Truncated the entire extent, don't bother adding */
2931 if (!logical_len)
2932 goto out;
2933 }
2934
Yan, Zhengc2167752009-11-12 09:34:21 +00002935 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002936 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002937
2938 /*
2939 * For mwrite(mmap + memset to write) case, we still reserve
2940 * space for NOCOW range.
2941 * As NOCOW won't cause a new delayed ref, just free the space
2942 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002943 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002944 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002945 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2946 if (nolock)
2947 trans = btrfs_join_transaction_nolock(root);
2948 else
2949 trans = btrfs_join_transaction(root);
2950 if (IS_ERR(trans)) {
2951 ret = PTR_ERR(trans);
2952 trans = NULL;
2953 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002954 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002956 ret = btrfs_update_inode_fallback(trans, root, inode);
2957 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002958 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002959 goto out;
2960 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002962 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002963 lock_extent_bits(io_tree, ordered_extent->file_offset,
2964 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002965 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966
Liu Bo38c227d2013-01-29 03:18:40 +00002967 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2968 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002969 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002970 if (ret) {
2971 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002972 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002973 /* the inode is shared */
2974 new = record_old_file_extents(inode, ordered_extent);
2975
2976 clear_extent_bit(io_tree, ordered_extent->file_offset,
2977 ordered_extent->file_offset + ordered_extent->len - 1,
2978 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2979 }
2980
Josef Bacik0cb59c92010-07-02 12:14:14 -04002981 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002982 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002983 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002984 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002985 if (IS_ERR(trans)) {
2986 ret = PTR_ERR(trans);
2987 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002988 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002989 }
Chris Masona79b7d42014-05-22 16:18:52 -07002990
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002991 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002992
Chris Masonc8b97812008-10-29 14:49:59 -04002993 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002994 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002995 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002996 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002997 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002998 ordered_extent->file_offset,
2999 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003000 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003001 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003002 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003003 ret = insert_reserved_file_extent(trans, inode,
3004 ordered_extent->file_offset,
3005 ordered_extent->start,
3006 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003007 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003008 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003009 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003010 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003011 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003012 ordered_extent->start,
3013 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003014 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003015 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3016 ordered_extent->file_offset, ordered_extent->len,
3017 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003019 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003020 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003022
David Sterbadf9f6282017-02-10 19:35:37 +01003023 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003024
Josef Bacik6c760c02012-11-09 10:53:21 -05003025 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3026 ret = btrfs_update_inode_fallback(trans, root, inode);
3027 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003028 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003029 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003030 }
3031 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003032out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003033 if (range_locked || clear_new_delalloc_bytes) {
3034 unsigned int clear_bits = 0;
3035
3036 if (range_locked)
3037 clear_bits |= EXTENT_LOCKED;
3038 if (clear_new_delalloc_bytes)
3039 clear_bits |= EXTENT_DELALLOC_NEW;
3040 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3041 ordered_extent->file_offset,
3042 ordered_extent->file_offset +
3043 ordered_extent->len - 1,
3044 clear_bits,
3045 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3046 0, &cached_state, GFP_NOFS);
3047 }
3048
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003049 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003050 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3051 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003052 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003053 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003054
Josef Bacik77cef2e2013-08-29 13:57:21 -04003055 if (ret || truncated) {
3056 u64 start, end;
3057
3058 if (truncated)
3059 start = ordered_extent->file_offset + logical_len;
3060 else
3061 start = ordered_extent->file_offset;
3062 end = ordered_extent->file_offset + ordered_extent->len - 1;
3063 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3064
3065 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003066 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003067
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003068 /*
3069 * If the ordered extent had an IOERR or something else went
3070 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003071 * back to the allocator. We only free the extent in the
3072 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003073 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003074 if ((ret || !logical_len) &&
3075 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003076 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003077 btrfs_free_reserved_extent(fs_info,
3078 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003079 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003080 }
3081
3082
Josef Bacik5fd02042012-05-02 14:00:54 -04003083 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003084 * This needs to be done to make sure anybody waiting knows we are done
3085 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003086 */
3087 btrfs_remove_ordered_extent(inode, ordered_extent);
3088
Liu Bo38c227d2013-01-29 03:18:40 +00003089 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003090 if (new) {
3091 if (ret) {
3092 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003093 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003094 } else {
3095 relink_file_extents(new);
3096 }
3097 }
Liu Bo38c227d2013-01-29 03:18:40 +00003098
Chris Masone6dcd2d2008-07-17 12:53:50 -04003099 /* once for us */
3100 btrfs_put_ordered_extent(ordered_extent);
3101 /* once for the tree */
3102 btrfs_put_ordered_extent(ordered_extent);
3103
Josef Bacik5fd02042012-05-02 14:00:54 -04003104 return ret;
3105}
3106
3107static void finish_ordered_fn(struct btrfs_work *work)
3108{
3109 struct btrfs_ordered_extent *ordered_extent;
3110 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3111 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003112}
3113
David Sterbac3988d62017-02-17 15:18:32 +01003114static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003115 struct extent_state *state, int uptodate)
3116{
Josef Bacik5fd02042012-05-02 14:00:54 -04003117 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003119 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003120 struct btrfs_workqueue *wq;
3121 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003122
liubo1abe9b82011-03-24 11:18:59 +00003123 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3124
Chris Mason8b62b722009-09-02 16:53:46 -04003125 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003126 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3127 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003128 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003129
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003130 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003131 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003132 func = btrfs_freespace_write_helper;
3133 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003134 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003135 func = btrfs_endio_write_helper;
3136 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003137
Liu Bo9e0af232014-08-15 23:36:53 +08003138 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3139 NULL);
3140 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003141}
3142
Miao Xiedc380ae2014-09-12 18:43:55 +08003143static int __readpage_endio_check(struct inode *inode,
3144 struct btrfs_io_bio *io_bio,
3145 int icsum, struct page *page,
3146 int pgoff, u64 start, size_t len)
3147{
3148 char *kaddr;
3149 u32 csum_expected;
3150 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003151
3152 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3153
3154 kaddr = kmap_atomic(page);
3155 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003156 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003157 if (csum != csum_expected)
3158 goto zeroit;
3159
3160 kunmap_atomic(kaddr);
3161 return 0;
3162zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003163 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003164 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003165 memset(kaddr + pgoff, 1, len);
3166 flush_dcache_page(page);
3167 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003168 return -EIO;
3169}
3170
Chris Masond352ac62008-09-29 15:18:18 -04003171/*
Chris Masond352ac62008-09-29 15:18:18 -04003172 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003173 * if there's a match, we allow the bio to finish. If not, the code in
3174 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003175 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003176static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3177 u64 phy_offset, struct page *page,
3178 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003179{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003180 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003181 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003183 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003184
Chris Masond20f7042008-12-08 16:58:54 -05003185 if (PageChecked(page)) {
3186 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003187 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003188 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003189
3190 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003191 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003192
Yan Zheng17d217f2008-12-12 10:03:38 -05003193 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003194 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003195 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003196 return 0;
3197 }
3198
Miao Xiefacc8a222013-07-25 19:22:34 +08003199 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003200 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3201 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003202}
Chris Masonb888db22007-08-27 16:49:44 -04003203
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003204void btrfs_add_delayed_iput(struct inode *inode)
3205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003207 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003208
3209 if (atomic_add_unless(&inode->i_count, -1, 1))
3210 return;
3211
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003212 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003213 if (binode->delayed_iput_count == 0) {
3214 ASSERT(list_empty(&binode->delayed_iput));
3215 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3216 } else {
3217 binode->delayed_iput_count++;
3218 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219 spin_unlock(&fs_info->delayed_iput_lock);
3220}
3221
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003222void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003223{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003225 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003226 while (!list_empty(&fs_info->delayed_iputs)) {
3227 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003228
David Sterba8089fe62015-11-19 14:15:51 +01003229 inode = list_first_entry(&fs_info->delayed_iputs,
3230 struct btrfs_inode, delayed_iput);
3231 if (inode->delayed_iput_count) {
3232 inode->delayed_iput_count--;
3233 list_move_tail(&inode->delayed_iput,
3234 &fs_info->delayed_iputs);
3235 } else {
3236 list_del_init(&inode->delayed_iput);
3237 }
3238 spin_unlock(&fs_info->delayed_iput_lock);
3239 iput(&inode->vfs_inode);
3240 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003241 }
David Sterba8089fe62015-11-19 14:15:51 +01003242 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003243}
3244
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003246 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 * files in the subvolume, it removes orphan item and frees block_rsv
3248 * structure.
3249 */
3250void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3251 struct btrfs_root *root)
3252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003254 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 int ret;
3256
Josef Bacik8a35d952012-05-23 14:26:42 -04003257 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3259 return;
3260
Josef Bacik90290e12011-12-02 15:44:12 -05003261 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003262 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003263 spin_unlock(&root->orphan_lock);
3264 return;
3265 }
3266
3267 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3268 spin_unlock(&root->orphan_lock);
3269 return;
3270 }
3271
3272 block_rsv = root->orphan_block_rsv;
3273 root->orphan_block_rsv = NULL;
3274 spin_unlock(&root->orphan_lock);
3275
Miao Xie27cdeb72014-04-02 19:51:05 +08003276 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003278 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003280 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003281 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003282 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003283 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3284 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 }
3286
Josef Bacik90290e12011-12-02 15:44:12 -05003287 if (block_rsv) {
3288 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003289 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 }
3291}
3292
3293/*
Josef Bacik7b128762008-07-24 12:17:14 -04003294 * This creates an orphan entry for the given inode in case something goes
3295 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 *
3297 * NOTE: caller of this function should reserve 5 units of metadata for
3298 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003299 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003300int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3301 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003302{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3304 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 struct btrfs_block_rsv *block_rsv = NULL;
3306 int reserve = 0;
3307 int insert = 0;
3308 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003309
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003311 block_rsv = btrfs_alloc_block_rsv(fs_info,
3312 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003313 if (!block_rsv)
3314 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003315 }
3316
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 spin_lock(&root->orphan_lock);
3318 if (!root->orphan_block_rsv) {
3319 root->orphan_block_rsv = block_rsv;
3320 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003321 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 block_rsv = NULL;
3323 }
Josef Bacik7b128762008-07-24 12:17:14 -04003324
Josef Bacik8a35d952012-05-23 14:26:42 -04003325 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003326 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327#if 0
3328 /*
3329 * For proper ENOSPC handling, we should do orphan
3330 * cleanup when mounting. But this introduces backward
3331 * compatibility issue.
3332 */
3333 if (!xchg(&root->orphan_item_inserted, 1))
3334 insert = 2;
3335 else
3336 insert = 1;
3337#endif
3338 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003339 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 }
Josef Bacik7b128762008-07-24 12:17:14 -04003341
Josef Bacik72ac3c02012-05-23 14:13:11 -04003342 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003343 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003346
Yan, Zhengd68fc572010-05-16 10:49:58 -04003347 /* grab metadata reservation from transaction handle */
3348 if (reserve) {
3349 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003350 ASSERT(!ret);
3351 if (ret) {
3352 atomic_dec(&root->orphan_inodes);
3353 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003354 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003355 if (insert)
3356 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003357 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003358 return ret;
3359 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003360 }
3361
3362 /* insert an orphan item to track this unlinked/truncated file */
3363 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003364 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003365 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003366 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003367 if (reserve) {
3368 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003369 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003370 btrfs_orphan_release_metadata(inode);
3371 }
3372 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003373 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003374 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003375 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003376 return ret;
3377 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003378 }
3379 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 }
3381
3382 /* insert an orphan item to track subvolume contains orphan files */
3383 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003384 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003386 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003387 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003388 return ret;
3389 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003390 }
3391 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003392}
3393
3394/*
3395 * We have done the truncate/delete so we can go ahead and remove the orphan
3396 * item for this particular inode.
3397 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003398static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003399 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003400{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003401 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 int delete_item = 0;
3403 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003404 int ret = 0;
3405
Yan, Zhengd68fc572010-05-16 10:49:58 -04003406 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003407 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003408 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003410
Josef Bacik72ac3c02012-05-23 14:13:11 -04003411 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003412 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003414 spin_unlock(&root->orphan_lock);
3415
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003416 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003417 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003418 if (trans)
3419 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003420 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003421 }
Josef Bacik7b128762008-07-24 12:17:14 -04003422
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003423 if (release_rsv)
3424 btrfs_orphan_release_metadata(inode);
3425
Josef Bacik4ef31a42013-08-13 14:10:08 -04003426 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003427}
3428
3429/*
3430 * this cleans up any orphans that may be left on the list from the last use
3431 * of this root.
3432 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003433int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003434{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003436 struct btrfs_path *path;
3437 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003438 struct btrfs_key key, found_key;
3439 struct btrfs_trans_handle *trans;
3440 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003441 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003442 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3443
Yan, Zhengd68fc572010-05-16 10:49:58 -04003444 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003445 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003446
3447 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003448 if (!path) {
3449 ret = -ENOMEM;
3450 goto out;
3451 }
David Sterbae4058b52015-11-27 16:31:35 +01003452 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003453
3454 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003455 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003456 key.offset = (u64)-1;
3457
Josef Bacik7b128762008-07-24 12:17:14 -04003458 while (1) {
3459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 if (ret < 0)
3461 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003462
3463 /*
3464 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003465 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003466 * find the key and see if we have stuff that matches
3467 */
3468 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003470 if (path->slots[0] == 0)
3471 break;
3472 path->slots[0]--;
3473 }
3474
3475 /* pull out the item */
3476 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003477 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3478
3479 /* make sure the item matches what we want */
3480 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3481 break;
David Sterba962a2982014-06-04 18:41:45 +02003482 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003483 break;
3484
3485 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003486 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003487
3488 /*
3489 * this is where we are basically btrfs_lookup, without the
3490 * crossing root thing. we store the inode number in the
3491 * offset of the orphan item.
3492 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003493
3494 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003495 btrfs_err(fs_info,
3496 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003497 ret = -EINVAL;
3498 goto out;
3499 }
3500
3501 last_objectid = found_key.offset;
3502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003503 found_key.objectid = found_key.offset;
3504 found_key.type = BTRFS_INODE_ITEM_KEY;
3505 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003506 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303507 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003508 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003510
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003511 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003512 struct btrfs_root *dead_root;
3513 struct btrfs_fs_info *fs_info = root->fs_info;
3514 int is_dead_root = 0;
3515
3516 /*
3517 * this is an orphan in the tree root. Currently these
3518 * could come from 2 sources:
3519 * a) a snapshot deletion in progress
3520 * b) a free space cache inode
3521 * We need to distinguish those two, as the snapshot
3522 * orphan must not get deleted.
3523 * find_dead_roots already ran before us, so if this
3524 * is a snapshot deletion, we should find the root
3525 * in the dead_roots list
3526 */
3527 spin_lock(&fs_info->trans_lock);
3528 list_for_each_entry(dead_root, &fs_info->dead_roots,
3529 root_list) {
3530 if (dead_root->root_key.objectid ==
3531 found_key.objectid) {
3532 is_dead_root = 1;
3533 break;
3534 }
3535 }
3536 spin_unlock(&fs_info->trans_lock);
3537 if (is_dead_root) {
3538 /* prevent this orphan from being found again */
3539 key.offset = found_key.objectid - 1;
3540 continue;
3541 }
3542 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003543 /*
3544 * Inode is already gone but the orphan item is still there,
3545 * kill the orphan item.
3546 */
Filipe Manana67710892016-06-06 11:51:25 +01003547 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003548 trans = btrfs_start_transaction(root, 1);
3549 if (IS_ERR(trans)) {
3550 ret = PTR_ERR(trans);
3551 goto out;
3552 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003553 btrfs_debug(fs_info, "auto deleting %Lu",
3554 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003555 ret = btrfs_del_orphan_item(trans, root,
3556 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003557 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003558 if (ret)
3559 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003560 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003561 }
Josef Bacik7b128762008-07-24 12:17:14 -04003562
Josef Bacik7b128762008-07-24 12:17:14 -04003563 /*
3564 * add this inode to the orphan list so btrfs_orphan_del does
3565 * the proper thing when we hit it
3566 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003567 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3568 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003569 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003570
Josef Bacik7b128762008-07-24 12:17:14 -04003571 /* if we have links, this was a truncate, lets do that */
3572 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303573 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003574 iput(inode);
3575 continue;
3576 }
Josef Bacik7b128762008-07-24 12:17:14 -04003577 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003578
3579 /* 1 for the orphan item deletion. */
3580 trans = btrfs_start_transaction(root, 1);
3581 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003582 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003583 ret = PTR_ERR(trans);
3584 goto out;
3585 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003586 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003587 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003588 if (ret) {
3589 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003590 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003591 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003592
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003593 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003594 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003595 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003596 } else {
3597 nr_unlink++;
3598 }
3599
3600 /* this will do delete_inode and everything for us */
3601 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003602 if (ret)
3603 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003604 }
Miao Xie3254c872011-11-10 20:45:05 -05003605 /* release the path since we're done with it */
3606 btrfs_release_path(path);
3607
Yan, Zhengd68fc572010-05-16 10:49:58 -04003608 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3609
3610 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003611 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003612 (u64)-1);
3613
Miao Xie27cdeb72014-04-02 19:51:05 +08003614 if (root->orphan_block_rsv ||
3615 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003616 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003617 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003618 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003619 }
Josef Bacik7b128762008-07-24 12:17:14 -04003620
3621 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003622 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003623 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003625
3626out:
3627 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003628 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003629 btrfs_free_path(path);
3630 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003631}
3632
Chris Masond352ac62008-09-29 15:18:18 -04003633/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003634 * very simple check to peek ahead in the leaf looking for xattrs. If we
3635 * don't find any xattrs, we know there can't be any acls.
3636 *
3637 * slot is the slot the inode is in, objectid is the objectid of the inode
3638 */
3639static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003640 int slot, u64 objectid,
3641 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003642{
3643 u32 nritems = btrfs_header_nritems(leaf);
3644 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003645 static u64 xattr_access = 0;
3646 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003647 int scanned = 0;
3648
Josef Bacikf23b5a52013-06-19 10:16:26 -04003649 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003650 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3651 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3652 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3653 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003654 }
3655
Chris Mason46a53cc2009-04-27 11:47:50 -04003656 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003657 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003658 while (slot < nritems) {
3659 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3660
3661 /* we found a different objectid, there must not be acls */
3662 if (found_key.objectid != objectid)
3663 return 0;
3664
3665 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003666 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003667 if (*first_xattr_slot == -1)
3668 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003669 if (found_key.offset == xattr_access ||
3670 found_key.offset == xattr_default)
3671 return 1;
3672 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003673
3674 /*
3675 * we found a key greater than an xattr key, there can't
3676 * be any acls later on
3677 */
3678 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3679 return 0;
3680
3681 slot++;
3682 scanned++;
3683
3684 /*
3685 * it goes inode, inode backrefs, xattrs, extents,
3686 * so if there are a ton of hard links to an inode there can
3687 * be a lot of backrefs. Don't waste time searching too hard,
3688 * this is just an optimization
3689 */
3690 if (scanned >= 8)
3691 break;
3692 }
3693 /* we hit the end of the leaf before we found an xattr or
3694 * something larger than an xattr. We have to assume the inode
3695 * has acls
3696 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003697 if (*first_xattr_slot == -1)
3698 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003699 return 1;
3700}
3701
3702/*
Chris Masond352ac62008-09-29 15:18:18 -04003703 * read an inode from the btree into the in-memory inode
3704 */
Filipe Manana67710892016-06-06 11:51:25 +01003705static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003708 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003709 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003710 struct btrfs_inode_item *inode_item;
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
3712 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003713 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003714 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003715 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003716 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003717 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003718 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003719
3720 ret = btrfs_fill_inode(inode, &rdev);
3721 if (!ret)
3722 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003723
3724 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003725 if (!path) {
3726 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003727 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003728 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003729
Chris Mason39279cc2007-06-12 06:35:45 -04003730 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003731
Chris Mason39279cc2007-06-12 06:35:45 -04003732 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003733 if (ret) {
3734 if (ret > 0)
3735 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003736 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003737 }
Chris Mason39279cc2007-06-12 06:35:45 -04003738
Chris Mason5f39d392007-10-15 16:14:19 -04003739 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003740
3741 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003742 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003743
Chris Mason5f39d392007-10-15 16:14:19 -04003744 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3745 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003746 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003747 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003748 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3749 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003750 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003751
David Sterbaa937b972014-12-12 17:39:12 +01003752 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3753 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
David Sterbaa937b972014-12-12 17:39:12 +01003755 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3756 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003757
David Sterbaa937b972014-12-12 17:39:12 +01003758 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3759 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
chandan r9cc97d62012-07-04 12:48:07 +05303761 BTRFS_I(inode)->i_otime.tv_sec =
3762 btrfs_timespec_sec(leaf, &inode_item->otime);
3763 BTRFS_I(inode)->i_otime.tv_nsec =
3764 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003765
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003766 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003767 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003768 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3769
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003770 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003771 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003772 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003773 rdev = btrfs_inode_rdev(leaf, inode_item);
3774
Josef Bacikaec74772008-07-24 12:12:38 -04003775 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003776 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003777
3778cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003779 /*
3780 * If we were modified in the current generation and evicted from memory
3781 * and then re-read we need to do a full sync since we don't have any
3782 * idea about which extents were modified before we were evicted from
3783 * cache.
3784 *
3785 * This is required for both inode re-read from disk and delayed inode
3786 * in delayed_nodes_tree.
3787 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003788 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003789 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3790 &BTRFS_I(inode)->runtime_flags);
3791
Filipe Mananabde6c242015-07-24 00:00:19 +01003792 /*
3793 * We don't persist the id of the transaction where an unlink operation
3794 * against the inode was last made. So here we assume the inode might
3795 * have been evicted, and therefore the exact value of last_unlink_trans
3796 * lost, and set it to last_trans to avoid metadata inconsistencies
3797 * between the inode and its parent if the inode is fsync'ed and the log
3798 * replayed. For example, in the scenario:
3799 *
3800 * touch mydir/foo
3801 * ln mydir/foo mydir/bar
3802 * sync
3803 * unlink mydir/bar
3804 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3805 * xfs_io -c fsync mydir/foo
3806 * <power failure>
3807 * mount fs, triggers fsync log replay
3808 *
3809 * We must make sure that when we fsync our inode foo we also log its
3810 * parent inode, otherwise after log replay the parent still has the
3811 * dentry with the "bar" name but our inode foo has a link count of 1
3812 * and doesn't have an inode ref with the name "bar" anymore.
3813 *
3814 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003815 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003816 * transaction commits on fsync if our inode is a directory, or if our
3817 * inode is not a directory, logging its parent unnecessarily.
3818 */
3819 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3820
Miao Xie67de1172013-12-26 13:07:06 +08003821 path->slots[0]++;
3822 if (inode->i_nlink != 1 ||
3823 path->slots[0] >= btrfs_header_nritems(leaf))
3824 goto cache_acl;
3825
3826 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003827 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003828 goto cache_acl;
3829
3830 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3831 if (location.type == BTRFS_INODE_REF_KEY) {
3832 struct btrfs_inode_ref *ref;
3833
3834 ref = (struct btrfs_inode_ref *)ptr;
3835 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3836 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3837 struct btrfs_inode_extref *extref;
3838
3839 extref = (struct btrfs_inode_extref *)ptr;
3840 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3841 extref);
3842 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003843cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003844 /*
3845 * try to precache a NULL acl entry for files that don't have
3846 * any xattrs or acls
3847 */
Li Zefan33345d012011-04-20 10:31:50 +08003848 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003849 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003850 if (first_xattr_slot != -1) {
3851 path->slots[0] = first_xattr_slot;
3852 ret = btrfs_load_inode_props(inode, path);
3853 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003854 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003855 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003856 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003857 root->root_key.objectid, ret);
3858 }
3859 btrfs_free_path(path);
3860
Al Viro72c04902009-06-24 16:58:48 -04003861 if (!maybe_acls)
3862 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003863
Chris Mason39279cc2007-06-12 06:35:45 -04003864 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003865 case S_IFREG:
3866 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003867 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003868 inode->i_fop = &btrfs_file_operations;
3869 inode->i_op = &btrfs_file_inode_operations;
3870 break;
3871 case S_IFDIR:
3872 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003873 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 break;
3875 case S_IFLNK:
3876 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003877 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003878 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3879 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003880 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003881 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003882 init_special_inode(inode, inode->i_mode, rdev);
3883 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003884 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003885
3886 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003887 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003888
3889make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003890 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003891 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003892 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003893}
3894
Chris Masond352ac62008-09-29 15:18:18 -04003895/*
3896 * given a leaf and an inode, copy the inode fields into the leaf
3897 */
Chris Masone02119d2008-09-05 16:13:11 -04003898static void fill_inode_item(struct btrfs_trans_handle *trans,
3899 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003900 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003901 struct inode *inode)
3902{
Liu Bo51fab692012-12-27 09:01:21 +00003903 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003904
Liu Bo51fab692012-12-27 09:01:21 +00003905 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
Liu Bo51fab692012-12-27 09:01:21 +00003907 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3908 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3909 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3910 &token);
3911 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3912 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003913
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003916 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003917 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003918
David Sterbaa937b972014-12-12 17:39:12 +01003919 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003920 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003921 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003922 inode->i_mtime.tv_nsec, &token);
3923
David Sterbaa937b972014-12-12 17:39:12 +01003924 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003925 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_ctime.tv_nsec, &token);
3928
chandan r9cc97d62012-07-04 12:48:07 +05303929 btrfs_set_token_timespec_sec(leaf, &item->otime,
3930 BTRFS_I(inode)->i_otime.tv_sec, &token);
3931 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3932 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3933
Liu Bo51fab692012-12-27 09:01:21 +00003934 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3935 &token);
3936 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3937 &token);
3938 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3939 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3940 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3941 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3942 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003943}
3944
Chris Masond352ac62008-09-29 15:18:18 -04003945/*
3946 * copy everything in the in-memory inode into the btree.
3947 */
Chris Mason21151332011-11-10 20:39:08 -05003948static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003949 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003950{
3951 struct btrfs_inode_item *inode_item;
3952 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003953 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003954 int ret;
3955
3956 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003957 if (!path)
3958 return -ENOMEM;
3959
Chris Masonb9473432009-03-13 11:00:37 -04003960 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003961 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3962 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003963 if (ret) {
3964 if (ret > 0)
3965 ret = -ENOENT;
3966 goto failed;
3967 }
3968
Chris Mason5f39d392007-10-15 16:14:19 -04003969 leaf = path->nodes[0];
3970 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003971 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003972
Chris Masone02119d2008-09-05 16:13:11 -04003973 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003974 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003975 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003976 ret = 0;
3977failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003978 btrfs_free_path(path);
3979 return ret;
3980}
3981
Chris Masond352ac62008-09-29 15:18:18 -04003982/*
Chris Mason21151332011-11-10 20:39:08 -05003983 * copy everything in the in-memory inode into the btree.
3984 */
3985noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3986 struct btrfs_root *root, struct inode *inode)
3987{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003988 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003989 int ret;
3990
3991 /*
3992 * If the inode is a free space inode, we can deadlock during commit
3993 * if we put it into the delayed code.
3994 *
3995 * The data relocation inode should also be directly updated
3996 * without delay
3997 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003998 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003999 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004000 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004001 btrfs_update_root_times(trans, root);
4002
Chris Mason21151332011-11-10 20:39:08 -05004003 ret = btrfs_delayed_update_inode(trans, root, inode);
4004 if (!ret)
4005 btrfs_set_inode_last_trans(trans, inode);
4006 return ret;
4007 }
4008
4009 return btrfs_update_inode_item(trans, root, inode);
4010}
4011
Josef Bacikbe6aef62012-10-22 15:43:12 -04004012noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4013 struct btrfs_root *root,
4014 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004015{
4016 int ret;
4017
4018 ret = btrfs_update_inode(trans, root, inode);
4019 if (ret == -ENOSPC)
4020 return btrfs_update_inode_item(trans, root, inode);
4021 return ret;
4022}
4023
4024/*
Chris Masond352ac62008-09-29 15:18:18 -04004025 * unlink helper that gets used here in inode.c and in the tree logging
4026 * recovery code. It remove a link in a directory with a given name, and
4027 * also drops the back refs in the inode to the directory
4028 */
Al Viro92986792011-03-04 17:14:37 +00004029static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4030 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004031 struct btrfs_inode *dir,
4032 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004033 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004034{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004035 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004037 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004038 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004039 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004040 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004041 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004042 u64 ino = btrfs_ino(inode);
4043 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004044
4045 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004046 if (!path) {
4047 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004048 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004049 }
4050
Chris Masonb9473432009-03-13 11:00:37 -04004051 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004052 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004053 name, name_len, -1);
4054 if (IS_ERR(di)) {
4055 ret = PTR_ERR(di);
4056 goto err;
4057 }
4058 if (!di) {
4059 ret = -ENOENT;
4060 goto err;
4061 }
Chris Mason5f39d392007-10-15 16:14:19 -04004062 leaf = path->nodes[0];
4063 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004065 if (ret)
4066 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004067 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004068
Miao Xie67de1172013-12-26 13:07:06 +08004069 /*
4070 * If we don't have dir index, we have to get it by looking up
4071 * the inode ref, since we get the inode ref, remove it directly,
4072 * it is unnecessary to do delayed deletion.
4073 *
4074 * But if we have dir index, needn't search inode ref to get it.
4075 * Since the inode ref is close to the inode item, it is better
4076 * that we delay to delete it, and just do this deletion when
4077 * we update the inode item.
4078 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004079 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004080 ret = btrfs_delayed_delete_inode_ref(inode);
4081 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004082 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004083 goto skip_backref;
4084 }
4085 }
4086
Li Zefan33345d012011-04-20 10:31:50 +08004087 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4088 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004089 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004090 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004091 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004092 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004093 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004094 goto err;
4095 }
Miao Xie67de1172013-12-26 13:07:06 +08004096skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004097 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004099 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004100 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 }
Chris Mason39279cc2007-06-12 06:35:45 -04004102
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004103 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4104 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004105 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004106 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 goto err;
4108 }
Chris Masone02119d2008-09-05 16:13:11 -04004109
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004110 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4111 index);
Chris Mason6418c962010-10-30 07:34:24 -04004112 if (ret == -ENOENT)
4113 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004114 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004115 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004116err:
4117 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004118 if (ret)
4119 goto out;
4120
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004121 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004122 inode_inc_iversion(&inode->vfs_inode);
4123 inode_inc_iversion(&dir->vfs_inode);
4124 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4125 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4126 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004127out:
Chris Mason39279cc2007-06-12 06:35:45 -04004128 return ret;
4129}
4130
Al Viro92986792011-03-04 17:14:37 +00004131int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4132 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004134 const char *name, int name_len)
4135{
4136 int ret;
4137 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4138 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004139 drop_nlink(&inode->vfs_inode);
4140 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004141 }
4142 return ret;
4143}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004144
4145/*
4146 * helper to start transaction for unlink and rmdir.
4147 *
Josef Bacikd52be812013-05-29 14:54:47 -04004148 * unlink and rmdir are special in btrfs, they do not always free space, so
4149 * if we cannot make our reservations the normal way try and see if there is
4150 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4151 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004152 */
Josef Bacikd52be812013-05-29 14:54:47 -04004153static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004155 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004156
Josef Bacike70bea52011-10-11 14:18:24 -04004157 /*
4158 * 1 for the possible orphan item
4159 * 1 for the dir item
4160 * 1 for the dir index
4161 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004162 * 1 for the inode
4163 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004164 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165}
4166
Chris Mason39279cc2007-06-12 06:35:45 -04004167static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4168{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004169 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004170 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004171 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004172 int ret;
4173
Josef Bacikd52be812013-05-29 14:54:47 -04004174 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004175 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004176 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004177
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4179 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004180
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004181 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4182 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4183 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004184 if (ret)
4185 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004186
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004188 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004189 if (ret)
4190 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191 }
Josef Bacik7b128762008-07-24 12:17:14 -04004192
Tsutomu Itohb5324022011-07-19 07:27:20 +00004193out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004194 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004195 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004196 return ret;
4197}
4198
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004199int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4200 struct btrfs_root *root,
4201 struct inode *dir, u64 objectid,
4202 const char *name, int name_len)
4203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004204 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205 struct btrfs_path *path;
4206 struct extent_buffer *leaf;
4207 struct btrfs_dir_item *di;
4208 struct btrfs_key key;
4209 u64 index;
4210 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004211 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212
4213 path = btrfs_alloc_path();
4214 if (!path)
4215 return -ENOMEM;
4216
Li Zefan33345d012011-04-20 10:31:50 +08004217 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (IS_ERR_OR_NULL(di)) {
4220 if (!di)
4221 ret = -ENOENT;
4222 else
4223 ret = PTR_ERR(di);
4224 goto out;
4225 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004226
4227 leaf = path->nodes[0];
4228 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4229 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4230 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 goto out;
4234 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004235 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4238 root->root_key.objectid, dir_ino,
4239 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 goto out;
4244 }
Li Zefan33345d012011-04-20 10:31:50 +08004245 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (IS_ERR_OR_NULL(di)) {
4248 if (!di)
4249 ret = -ENOENT;
4250 else
4251 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004252 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253 goto out;
4254 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255
4256 leaf = path->nodes[0];
4257 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004258 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259 index = key.offset;
4260 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004261 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004262
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004263 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004265 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 goto out;
4267 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004268
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004269 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004270 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004271 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004272 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004274 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004276 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004278}
4279
Chris Mason39279cc2007-06-12 06:35:45 -04004280static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4281{
David Howells2b0143b2015-03-17 22:25:59 +00004282 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004283 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004284 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004286 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004287
David Sterbab3ae2442012-09-13 16:04:34 -06004288 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004289 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004290 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004291 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004292
Josef Bacikd52be812013-05-29 14:54:47 -04004293 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004294 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004295 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004296
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004297 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004298 err = btrfs_unlink_subvol(trans, root, dir,
4299 BTRFS_I(inode)->location.objectid,
4300 dentry->d_name.name,
4301 dentry->d_name.len);
4302 goto out;
4303 }
4304
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004305 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004306 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004307 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004308
Filipe Manana44f714d2016-06-06 16:11:13 +01004309 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4310
Chris Mason39279cc2007-06-12 06:35:45 -04004311 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004312 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4313 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4314 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004315 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004316 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004317 /*
4318 * Propagate the last_unlink_trans value of the deleted dir to
4319 * its parent directory. This is to prevent an unrecoverable
4320 * log tree in the case we do something like this:
4321 * 1) create dir foo
4322 * 2) create snapshot under dir foo
4323 * 3) delete the snapshot
4324 * 4) rmdir foo
4325 * 5) mkdir foo
4326 * 6) fsync foo or some file inside foo
4327 */
4328 if (last_unlink_trans >= trans->transid)
4329 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4330 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004331out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004332 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004333 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004334
Chris Mason39279cc2007-06-12 06:35:45 -04004335 return err;
4336}
4337
Chris Mason28f75a02015-02-04 06:59:29 -08004338static int truncate_space_check(struct btrfs_trans_handle *trans,
4339 struct btrfs_root *root,
4340 u64 bytes_deleted)
4341{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004342 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004343 int ret;
4344
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004345 /*
4346 * This is only used to apply pressure to the enospc system, we don't
4347 * intend to use this reservation at all.
4348 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004349 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004350 bytes_deleted *= fs_info->nodesize;
4351 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004352 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004353 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004355 trans->transid,
4356 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004357 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004358 }
Chris Mason28f75a02015-02-04 06:59:29 -08004359 return ret;
4360
4361}
4362
Filipe Manana0305cd52015-10-16 12:34:25 +01004363static int truncate_inline_extent(struct inode *inode,
4364 struct btrfs_path *path,
4365 struct btrfs_key *found_key,
4366 const u64 item_end,
4367 const u64 new_size)
4368{
4369 struct extent_buffer *leaf = path->nodes[0];
4370 int slot = path->slots[0];
4371 struct btrfs_file_extent_item *fi;
4372 u32 size = (u32)(new_size - found_key->offset);
4373 struct btrfs_root *root = BTRFS_I(inode)->root;
4374
4375 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4376
4377 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4378 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004379 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004380
4381 /*
4382 * Zero out the remaining of the last page of our inline extent,
4383 * instead of directly truncating our inline extent here - that
4384 * would be much more complex (decompressing all the data, then
4385 * compressing the truncated data, which might be bigger than
4386 * the size of the inline extent, resize the extent, etc).
4387 * We release the path because to get the page we might need to
4388 * read the extent item from disk (data not in the page cache).
4389 */
4390 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304391 return btrfs_truncate_block(inode, offset, page_end - offset,
4392 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004393 }
4394
4395 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4396 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004397 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004398
4399 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4400 inode_sub_bytes(inode, item_end + 1 - new_size);
4401
4402 return 0;
4403}
4404
Chris Mason323ac952008-10-01 19:05:46 -04004405/*
Chris Mason39279cc2007-06-12 06:35:45 -04004406 * this can truncate away extent items, csum items and directory items.
4407 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004408 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004409 *
4410 * csum items that cross the new i_size are truncated to the new size
4411 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004412 *
4413 * min_type is the minimum key type to truncate down to. If set to 0, this
4414 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004415 */
Yan, Zheng80825102009-11-12 09:35:36 +00004416int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4417 struct btrfs_root *root,
4418 struct inode *inode,
4419 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004420{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004421 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004422 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004423 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004424 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004425 struct btrfs_key key;
4426 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004428 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004429 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004430 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004431 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004432 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 int found_extent;
4434 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004435 int pending_del_nr = 0;
4436 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004437 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004438 int ret;
4439 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004440 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004441 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004442 bool be_nice = 0;
4443 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004444 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004445
4446 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004447
Chris Mason28ed1342014-12-17 09:41:04 -08004448 /*
4449 * for non-free space inodes and ref cows, we want to back off from
4450 * time to time
4451 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004452 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004453 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4454 be_nice = 1;
4455
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004456 path = btrfs_alloc_path();
4457 if (!path)
4458 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004459 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004460
Josef Bacik5dc562c2012-08-17 13:14:17 -04004461 /*
4462 * We want to drop from the next block forward in case this new size is
4463 * not block aligned since we will be keeping the last block of the
4464 * extent just the way it is.
4465 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004466 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004467 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004468 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004469 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004470 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004471
Miao Xie16cdcec2011-04-22 18:12:22 +08004472 /*
4473 * This function is also used to drop the items in the log tree before
4474 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4475 * it is used to drop the loged items. So we shouldn't kill the delayed
4476 * items.
4477 */
4478 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004479 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004480
Li Zefan33345d012011-04-20 10:31:50 +08004481 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004483 key.type = (u8)-1;
4484
Chris Mason85e21ba2008-01-29 15:11:36 -05004485search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004486 /*
4487 * with a 16K leaf size and 128MB extents, you can actually queue
4488 * up a huge file in a single leaf. Most of the time that
4489 * bytes_deleted is > 0, it will be huge by the time we get here
4490 */
Byongho Leeee221842015-12-15 01:42:10 +09004491 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004492 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004493 err = -EAGAIN;
4494 goto error;
4495 }
4496 }
4497
4498
Chris Masonb9473432009-03-13 11:00:37 -04004499 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004500 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004501 if (ret < 0) {
4502 err = ret;
4503 goto out;
4504 }
Chris Masond3977122009-01-05 21:25:51 -05004505
Chris Mason85e21ba2008-01-29 15:11:36 -05004506 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004507 /* there are no items in the tree for us to truncate, we're
4508 * done
4509 */
Yan, Zheng80825102009-11-12 09:35:36 +00004510 if (path->slots[0] == 0)
4511 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004512 path->slots[0]--;
4513 }
4514
Chris Masond3977122009-01-05 21:25:51 -05004515 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004516 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004517 leaf = path->nodes[0];
4518 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004519 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004520
Li Zefan33345d012011-04-20 10:31:50 +08004521 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004522 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004523
Chris Mason85e21ba2008-01-29 15:11:36 -05004524 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004525 break;
4526
Chris Mason5f39d392007-10-15 16:14:19 -04004527 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004528 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004529 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004530 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004531 extent_type = btrfs_file_extent_type(leaf, fi);
4532 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004533 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004534 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004535
4536 trace_btrfs_truncate_show_fi_regular(
4537 BTRFS_I(inode), leaf, fi,
4538 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004539 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004540 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004541 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004542
4543 trace_btrfs_truncate_show_fi_inline(
4544 BTRFS_I(inode), leaf, fi, path->slots[0],
4545 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004546 }
Yan008630c2007-11-07 13:31:09 -05004547 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004548 }
Yan, Zheng80825102009-11-12 09:35:36 +00004549 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004550 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004551 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004552 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004553 break;
4554 if (found_key.offset >= new_size)
4555 del_item = 1;
4556 else
4557 del_item = 0;
4558 }
Chris Mason39279cc2007-06-12 06:35:45 -04004559 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004560 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004561 if (found_type != BTRFS_EXTENT_DATA_KEY)
4562 goto delete;
4563
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004564 if (del_item)
4565 last_size = found_key.offset;
4566 else
4567 last_size = new_size;
4568
Chris Mason179e29e2007-11-01 11:28:41 -04004569 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004570 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004571 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004572 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004573 u64 orig_num_bytes =
4574 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004575 extent_num_bytes = ALIGN(new_size -
4576 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004577 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004578 btrfs_set_file_extent_num_bytes(leaf, fi,
4579 extent_num_bytes);
4580 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004581 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004582 if (test_bit(BTRFS_ROOT_REF_COWS,
4583 &root->state) &&
4584 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004585 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004586 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004587 } else {
Chris Masondb945352007-10-15 16:15:53 -04004588 extent_num_bytes =
4589 btrfs_file_extent_disk_num_bytes(leaf,
4590 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004591 extent_offset = found_key.offset -
4592 btrfs_file_extent_offset(leaf, fi);
4593
Chris Mason39279cc2007-06-12 06:35:45 -04004594 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004595 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004596 if (extent_start != 0) {
4597 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004598 if (test_bit(BTRFS_ROOT_REF_COWS,
4599 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004600 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
4602 }
Chris Mason90692182008-02-08 13:49:28 -05004603 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004604 /*
4605 * we can't truncate inline items that have had
4606 * special encodings
4607 */
4608 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004609 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4610 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004611
4612 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004613 * Need to release path in order to truncate a
4614 * compressed extent. So delete any accumulated
4615 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004616 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004617 if (btrfs_file_extent_compression(leaf, fi) !=
4618 BTRFS_COMPRESS_NONE && pending_del_nr) {
4619 err = btrfs_del_items(trans, root, path,
4620 pending_del_slot,
4621 pending_del_nr);
4622 if (err) {
4623 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004624 err);
4625 goto error;
4626 }
4627 pending_del_nr = 0;
4628 }
4629
4630 err = truncate_inline_extent(inode, path,
4631 &found_key,
4632 item_end,
4633 new_size);
4634 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004635 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004636 goto error;
4637 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004638 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4639 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004640 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004641 }
Chris Mason39279cc2007-06-12 06:35:45 -04004642 }
Chris Mason179e29e2007-11-01 11:28:41 -04004643delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004644 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004645 if (!pending_del_nr) {
4646 /* no pending yet, add ourselves */
4647 pending_del_slot = path->slots[0];
4648 pending_del_nr = 1;
4649 } else if (pending_del_nr &&
4650 path->slots[0] + 1 == pending_del_slot) {
4651 /* hop on the pending chunk */
4652 pending_del_nr++;
4653 pending_del_slot = path->slots[0];
4654 } else {
Chris Masond3977122009-01-05 21:25:51 -05004655 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004656 }
Chris Mason39279cc2007-06-12 06:35:45 -04004657 } else {
4658 break;
4659 }
Chris Mason28f75a02015-02-04 06:59:29 -08004660 should_throttle = 0;
4661
Miao Xie27cdeb72014-04-02 19:51:05 +08004662 if (found_extent &&
4663 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004664 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004665 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004666 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004667 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004668 extent_num_bytes, 0,
4669 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004670 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004671 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004672 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4673 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004674 trans->delayed_ref_updates * 2,
4675 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004676 if (be_nice) {
4677 if (truncate_space_check(trans, root,
4678 extent_num_bytes)) {
4679 should_end = 1;
4680 }
4681 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004682 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004683 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004684 }
Chris Mason39279cc2007-06-12 06:35:45 -04004685 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004686
Yan, Zheng80825102009-11-12 09:35:36 +00004687 if (found_type == BTRFS_INODE_ITEM_KEY)
4688 break;
4689
4690 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004691 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004692 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004693 if (pending_del_nr) {
4694 ret = btrfs_del_items(trans, root, path,
4695 pending_del_slot,
4696 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004697 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004698 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004699 goto error;
4700 }
Yan, Zheng80825102009-11-12 09:35:36 +00004701 pending_del_nr = 0;
4702 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004703 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004704 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004705 unsigned long updates = trans->delayed_ref_updates;
4706 if (updates) {
4707 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004708 ret = btrfs_run_delayed_refs(trans,
4709 fs_info,
4710 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004711 if (ret && !err)
4712 err = ret;
4713 }
4714 }
Chris Mason28f75a02015-02-04 06:59:29 -08004715 /*
4716 * if we failed to refill our space rsv, bail out
4717 * and let the transaction restart
4718 */
4719 if (should_end) {
4720 err = -EAGAIN;
4721 goto error;
4722 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004723 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004724 } else {
4725 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004726 }
Chris Mason39279cc2007-06-12 06:35:45 -04004727 }
Yan, Zheng80825102009-11-12 09:35:36 +00004728out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004729 if (pending_del_nr) {
4730 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4731 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004732 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004733 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004734 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004735error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004736 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4737 ASSERT(last_size >= new_size);
4738 if (!err && last_size > new_size)
4739 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004740 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004741 }
Chris Mason28ed1342014-12-17 09:41:04 -08004742
Chris Mason39279cc2007-06-12 06:35:45 -04004743 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004744
Byongho Leeee221842015-12-15 01:42:10 +09004745 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004746 unsigned long updates = trans->delayed_ref_updates;
4747 if (updates) {
4748 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004749 ret = btrfs_run_delayed_refs(trans, fs_info,
4750 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004751 if (ret && !err)
4752 err = ret;
4753 }
4754 }
Yan, Zheng80825102009-11-12 09:35:36 +00004755 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004756}
4757
Chris Masona52d9a82007-08-27 16:49:44 -04004758/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004760 * @inode - inode that we're zeroing
4761 * @from - the offset to start zeroing
4762 * @len - the length to zero, 0 to zero the entire range respective to the
4763 * offset
4764 * @front - zero up to the offset instead of from the offset on
4765 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304766 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004767 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004768 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004770 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004773 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4775 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004776 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004777 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004778 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004779 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004780 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004782 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004783 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004784 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785 u64 block_start;
4786 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004787
Josef Bacik2aaa6652012-08-29 14:27:18 -04004788 if ((offset & (blocksize - 1)) == 0 &&
4789 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004790 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304791
Qu Wenruo364ecf32017-02-27 15:10:38 +08004792 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004794 if (ret)
4795 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004796
Chris Mason211c17f2008-05-15 09:13:45 -04004797again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004798 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004799 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004800 btrfs_delalloc_release_space(inode, data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 round_down(from, blocksize),
4802 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004803 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004804 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004805 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 block_start = round_down(from, blocksize);
4808 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809
Chris Masona52d9a82007-08-27 16:49:44 -04004810 if (!PageUptodate(page)) {
4811 ret = btrfs_readpage(NULL, page);
4812 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004813 if (page->mapping != mapping) {
4814 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004815 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004816 goto again;
4817 }
Chris Masona52d9a82007-08-27 16:49:44 -04004818 if (!PageUptodate(page)) {
4819 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004820 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004821 }
4822 }
Chris Mason211c17f2008-05-15 09:13:45 -04004823 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004824
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 set_page_extent_mapped(page);
4827
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004831 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004833 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004834 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004835 btrfs_put_ordered_extent(ordered);
4836 goto again;
4837 }
4838
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304839 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004840 EXTENT_DIRTY | EXTENT_DELALLOC |
4841 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004842 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004843
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004845 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004846 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304847 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004848 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004849 goto out_unlock;
4850 }
4851
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304852 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004853 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004856 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 memset(kaddr + (block_start - page_offset(page)),
4858 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004859 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304860 memset(kaddr + (block_start - page_offset(page)) + offset,
4861 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004862 flush_dcache_page(page);
4863 kunmap(page);
4864 }
Chris Mason247e7432008-07-17 12:53:51 -04004865 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004866 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004868 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004869
Chris Mason89642222008-07-24 09:41:53 -04004870out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004871 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004872 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304873 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004874 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004875 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004876out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004877 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004878 return ret;
4879}
4880
Josef Bacik16e75492013-10-22 12:18:51 -04004881static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4882 u64 offset, u64 len)
4883{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004885 struct btrfs_trans_handle *trans;
4886 int ret;
4887
4888 /*
4889 * Still need to make sure the inode looks like it's been updated so
4890 * that any holes get logged if we fsync.
4891 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4893 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004894 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4895 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4896 return 0;
4897 }
4898
4899 /*
4900 * 1 - for the one we're dropping
4901 * 1 - for the one we're adding
4902 * 1 - for updating the inode.
4903 */
4904 trans = btrfs_start_transaction(root, 3);
4905 if (IS_ERR(trans))
4906 return PTR_ERR(trans);
4907
4908 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4909 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004910 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004911 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004912 return ret;
4913 }
4914
David Sterbaf85b7372017-01-20 14:54:07 +01004915 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4916 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004917 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004918 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004919 else
4920 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004921 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004922 return ret;
4923}
4924
Josef Bacik695a0d02011-03-04 15:46:53 -05004925/*
4926 * This function puts in dummy file extents for the area we're creating a hole
4927 * for. So if we are truncating this file to a larger size we need to insert
4928 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4929 * the range between oldsize and size
4930 */
Josef Bacika41ad392011-01-31 15:30:16 -05004931int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004932{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004934 struct btrfs_root *root = BTRFS_I(inode)->root;
4935 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004936 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004937 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4940 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004941 u64 last_byte;
4942 u64 cur_offset;
4943 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004944 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004945
Josef Bacika71754f2013-06-17 17:14:39 -04004946 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304947 * If our size started in the middle of a block we need to zero out the
4948 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004949 * expose stale data.
4950 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304951 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004952 if (err)
4953 return err;
4954
Yan Zheng9036c102008-10-30 14:19:41 -04004955 if (size <= hole_start)
4956 return 0;
4957
Yan Zheng9036c102008-10-30 14:19:41 -04004958 while (1) {
4959 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004960
David Sterbaff13db42015-12-03 14:30:40 +01004961 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004962 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004963 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004964 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004965 if (!ordered)
4966 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004967 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4968 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004969 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004970 btrfs_put_ordered_extent(ordered);
4971 }
4972
Yan Zheng9036c102008-10-30 14:19:41 -04004973 cur_offset = hole_start;
4974 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004975 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004976 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004977 if (IS_ERR(em)) {
4978 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004979 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004980 break;
4981 }
Yan Zheng9036c102008-10-30 14:19:41 -04004982 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004983 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004984 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004985 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004986 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004987
Josef Bacik16e75492013-10-22 12:18:51 -04004988 err = maybe_insert_hole(root, inode, cur_offset,
4989 hole_size);
4990 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004991 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004992 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 cur_offset + hole_size - 1, 0);
4994 hole_em = alloc_extent_map();
4995 if (!hole_em) {
4996 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4997 &BTRFS_I(inode)->runtime_flags);
4998 goto next;
4999 }
5000 hole_em->start = cur_offset;
5001 hole_em->len = hole_size;
5002 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005003
Josef Bacik5dc562c2012-08-17 13:14:17 -04005004 hole_em->block_start = EXTENT_MAP_HOLE;
5005 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005006 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005007 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005008 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005009 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005010 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005011
5012 while (1) {
5013 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005014 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005015 write_unlock(&em_tree->lock);
5016 if (err != -EEXIST)
5017 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005018 btrfs_drop_extent_cache(BTRFS_I(inode),
5019 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005020 cur_offset +
5021 hole_size - 1, 0);
5022 }
5023 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005024 }
Josef Bacik16e75492013-10-22 12:18:51 -04005025next:
Yan Zheng9036c102008-10-30 14:19:41 -04005026 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005027 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005028 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005029 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005030 break;
5031 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005032 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005033 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5034 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005035 return err;
5036}
5037
Eric Sandeen3972f262013-01-12 02:57:22 +00005038static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005039{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005040 struct btrfs_root *root = BTRFS_I(inode)->root;
5041 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005042 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005043 loff_t newsize = attr->ia_size;
5044 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005045 int ret;
5046
Eric Sandeen3972f262013-01-12 02:57:22 +00005047 /*
5048 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5049 * special case where we need to update the times despite not having
5050 * these flags set. For all other operations the VFS set these flags
5051 * explicitly if it wants a timestamp update.
5052 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005053 if (newsize != oldsize) {
5054 inode_inc_iversion(inode);
5055 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5056 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005057 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005058 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005059
Josef Bacika41ad392011-01-31 15:30:16 -05005060 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005061 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005062 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005063 * This is to ensure the snapshot captures a fully consistent
5064 * state of this file - if the snapshot captures this expanding
5065 * truncation, it must capture all writes that happened before
5066 * this truncation.
5067 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005068 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005069 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005070 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005071 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005072 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 }
Yan, Zheng80825102009-11-12 09:35:36 +00005074
Miao Xief4a2f4c2011-12-14 20:12:01 -05005075 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005076 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005077 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005078 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005079 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005080
5081 i_size_write(inode, newsize);
5082 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305083 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005084 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005085 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005086 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005087 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005088
Josef Bacika41ad392011-01-31 15:30:16 -05005089 /*
5090 * We're truncating a file that used to have good data down to
5091 * zero. Make sure it gets into the ordered flush list so that
5092 * any new writes get down to disk quickly.
5093 */
5094 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005095 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5096 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005097
Josef Bacikf3fe8202013-01-07 17:03:21 -05005098 /*
5099 * 1 for the orphan item we're going to add
5100 * 1 for the orphan item deletion.
5101 */
5102 trans = btrfs_start_transaction(root, 2);
5103 if (IS_ERR(trans))
5104 return PTR_ERR(trans);
5105
5106 /*
5107 * We need to do this in case we fail at _any_ point during the
5108 * actual truncate. Once we do the truncate_setsize we could
5109 * invalidate pages which forces any outstanding ordered io to
5110 * be instantly completed which will give us extents that need
5111 * to be truncated. If we fail to get an orphan inode down we
5112 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005113 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005114 * will be consistent.
5115 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005116 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005117 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005118 if (ret)
5119 return ret;
5120
Josef Bacika41ad392011-01-31 15:30:16 -05005121 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5122 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005123
5124 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005125 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005126 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005127 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005128
Josef Bacika41ad392011-01-31 15:30:16 -05005129 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005130 if (ret && inode->i_nlink) {
5131 int err;
5132
Liu Bo19fd2df2016-12-01 13:01:02 -08005133 /* To get a stable disk_i_size */
5134 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5135 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005136 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005137 return err;
5138 }
5139
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005140 /*
5141 * failed to truncate, disk_i_size is only adjusted down
5142 * as we remove extents, so it should represent the true
5143 * size of the inode, so reset the in memory size and
5144 * delete our orphan entry.
5145 */
5146 trans = btrfs_join_transaction(root);
5147 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005148 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005149 return ret;
5150 }
5151 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005152 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005153 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005154 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005155 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005156 }
Yan, Zheng80825102009-11-12 09:35:36 +00005157 }
5158
Josef Bacika41ad392011-01-31 15:30:16 -05005159 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005160}
5161
Chris Mason39279cc2007-06-12 06:35:45 -04005162static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5163{
David Howells2b0143b2015-03-17 22:25:59 +00005164 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005165 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005166 int err;
5167
Li Zefanb83cc962010-12-20 16:04:08 +08005168 if (btrfs_root_readonly(root))
5169 return -EROFS;
5170
Jan Kara31051c82016-05-26 16:55:18 +02005171 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005172 if (err)
5173 return err;
5174
Chris Mason5a3f23d2009-03-31 13:27:11 -04005175 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005176 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005177 if (err)
5178 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005179 }
Yan Zheng9036c102008-10-30 14:19:41 -04005180
Christoph Hellwig10257742010-06-04 11:30:02 +02005181 if (attr->ia_valid) {
5182 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005183 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005184 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005185
Josef Bacik22c44fe2011-11-30 10:45:38 -05005186 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005187 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005188 }
5189
Chris Mason39279cc2007-06-12 06:35:45 -04005190 return err;
5191}
Chris Mason61295eb2008-01-14 16:24:38 -05005192
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005193/*
5194 * While truncating the inode pages during eviction, we get the VFS calling
5195 * btrfs_invalidatepage() against each page of the inode. This is slow because
5196 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5197 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5198 * extent_state structures over and over, wasting lots of time.
5199 *
5200 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5201 * those expensive operations on a per page basis and do only the ordered io
5202 * finishing, while we release here the extent_map and extent_state structures,
5203 * without the excessive merging and splitting.
5204 */
5205static void evict_inode_truncate_pages(struct inode *inode)
5206{
5207 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5208 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5209 struct rb_node *node;
5210
5211 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005212 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005213
5214 write_lock(&map_tree->lock);
5215 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5216 struct extent_map *em;
5217
5218 node = rb_first(&map_tree->map);
5219 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005220 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5221 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 remove_extent_mapping(map_tree, em);
5223 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005224 if (need_resched()) {
5225 write_unlock(&map_tree->lock);
5226 cond_resched();
5227 write_lock(&map_tree->lock);
5228 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005229 }
5230 write_unlock(&map_tree->lock);
5231
Filipe Manana6ca07092015-05-26 00:55:42 +01005232 /*
5233 * Keep looping until we have no more ranges in the io tree.
5234 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005235 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5236 * still in progress (unlocked the pages in the bio but did not yet
5237 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005238 * ranges can still be locked and eviction started because before
5239 * submitting those bios, which are executed by a separate task (work
5240 * queue kthread), inode references (inode->i_count) were not taken
5241 * (which would be dropped in the end io callback of each bio).
5242 * Therefore here we effectively end up waiting for those bios and
5243 * anyone else holding locked ranges without having bumped the inode's
5244 * reference count - if we don't do it, when they access the inode's
5245 * io_tree to unlock a range it may be too late, leading to an
5246 * use-after-free issue.
5247 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248 spin_lock(&io_tree->lock);
5249 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5250 struct extent_state *state;
5251 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005252 u64 start;
5253 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005254
5255 node = rb_first(&io_tree->state);
5256 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005257 start = state->start;
5258 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005259 spin_unlock(&io_tree->lock);
5260
David Sterbaff13db42015-12-03 14:30:40 +01005261 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005262
5263 /*
5264 * If still has DELALLOC flag, the extent didn't reach disk,
5265 * and its reserved space won't be freed by delayed_ref.
5266 * So we need to free its reserved space here.
5267 * (Refer to comment in btrfs_invalidatepage, case 2)
5268 *
5269 * Note, end is the bytenr of last byte, so we need + 1 here.
5270 */
5271 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005272 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005273
Filipe Manana6ca07092015-05-26 00:55:42 +01005274 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 EXTENT_LOCKED | EXTENT_DIRTY |
5276 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5277 EXTENT_DEFRAG, 1, 1,
5278 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279
Filipe Manana7064dd52014-08-08 02:47:05 +01005280 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005281 spin_lock(&io_tree->lock);
5282 }
5283 spin_unlock(&io_tree->lock);
5284}
5285
Al Virobd555972010-06-07 11:35:40 -04005286void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005287{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005288 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005289 struct btrfs_trans_handle *trans;
5290 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005291 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005292 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005293 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005294 int ret;
5295
liubo1abe9b82011-03-24 11:18:59 +00005296 trace_btrfs_inode_evict(inode);
5297
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005298 if (!root) {
5299 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5300 return;
5301 }
5302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005303 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005304
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005305 evict_inode_truncate_pages(inode);
5306
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005307 if (inode->i_nlink &&
5308 ((btrfs_root_refs(&root->root_item) != 0 &&
5309 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005310 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005311 goto no_delete;
5312
Chris Mason39279cc2007-06-12 06:35:45 -04005313 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005314 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005315 goto no_delete;
5316 }
Al Virobd555972010-06-07 11:35:40 -04005317 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005318 if (!special_file(inode->i_mode))
5319 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005320
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005321 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005322
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005323 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005324 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005325 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005326 goto no_delete;
5327 }
5328
Yan, Zheng76dda932009-09-21 16:00:26 -04005329 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005330 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5331 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005332 goto no_delete;
5333 }
5334
Nikolay Borisovaa790212017-01-10 20:35:40 +02005335 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005336 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005337 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005338 goto no_delete;
5339 }
5340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005341 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005342 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005343 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005344 goto no_delete;
5345 }
Josef Bacik4a338542011-08-29 11:01:31 -04005346 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005347 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005348 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005349
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005350 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005351
Josef Bacik4289a662011-08-05 13:22:24 -04005352 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005353 * This is a bit simpler than btrfs_truncate since we've already
5354 * reserved our space for our orphan item in the unlink, so we just
5355 * need to reserve some slack space in case we add bytes and update
5356 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005357 */
Yan, Zheng80825102009-11-12 09:35:36 +00005358 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005359 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5360 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005361
Josef Bacik726c35f2011-09-26 15:46:06 -04005362 /*
5363 * Try and steal from the global reserve since we will
5364 * likely not use this space anyway, we want to try as
5365 * hard as possible to get this to work.
5366 */
5367 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005368 steal_from_global++;
5369 else
5370 steal_from_global = 0;
5371 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005372
Josef Bacik3bce8762015-02-24 12:35:51 -08005373 /*
5374 * steal_from_global == 0: we reserved stuff, hooray!
5375 * steal_from_global == 1: we didn't reserve stuff, boo!
5376 * steal_from_global == 2: we've committed, still not a lot of
5377 * room but maybe we'll have room in the global reserve this
5378 * time.
5379 * steal_from_global == 3: abandon all hope!
5380 */
5381 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005382 btrfs_warn(fs_info,
5383 "Could not get space for a delete, will truncate on mount %d",
5384 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005385 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005386 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005387 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005388 }
5389
Miao Xie0e8c36a2012-12-19 06:59:51 +00005390 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005391 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005392 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005393 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005394 goto no_delete;
5395 }
5396
Josef Bacik3bce8762015-02-24 12:35:51 -08005397 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005398 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005399 * sure there is room to do it, if not we need to commit and try
5400 * again.
5401 */
5402 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005404 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005405 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005406 else
5407 ret = -ENOSPC;
5408 }
5409
5410 /*
5411 * Couldn't steal from the global reserve, we have too much
5412 * pending stuff built up, commit the transaction and try it
5413 * again.
5414 */
5415 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005416 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005417 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005418 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005419 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005420 goto no_delete;
5421 }
5422 continue;
5423 } else {
5424 steal_from_global = 0;
5425 }
5426
Josef Bacik4289a662011-08-05 13:22:24 -04005427 trans->block_rsv = rsv;
5428
Yan, Zhengd68fc572010-05-16 10:49:58 -04005429 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005430 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005431 break;
5432
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005433 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005434 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005435 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005437 }
5438
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005439 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005440
Josef Bacik4ef31a42013-08-13 14:10:08 -04005441 /*
5442 * Errors here aren't a big deal, it just means we leave orphan items
5443 * in the tree. They will be cleaned up on the next mount.
5444 */
Yan, Zheng80825102009-11-12 09:35:36 +00005445 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005446 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005447 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005448 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005449 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005450 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005451
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005452 trans->block_rsv = &fs_info->trans_block_rsv;
5453 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005454 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005455 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005456
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005457 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005458 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005459no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005460 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005461 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005462}
5463
5464/*
5465 * this returns the key found in the dir entry in the location pointer.
5466 * If no dir entries were found, location->objectid is 0.
5467 */
5468static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5469 struct btrfs_key *location)
5470{
5471 const char *name = dentry->d_name.name;
5472 int namelen = dentry->d_name.len;
5473 struct btrfs_dir_item *di;
5474 struct btrfs_path *path;
5475 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005476 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005477
5478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005479 if (!path)
5480 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005481
David Sterbaf85b7372017-01-20 14:54:07 +01005482 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5483 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005484 if (IS_ERR(di))
5485 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005486
David Sterbac7040052011-04-19 18:00:01 +02005487 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005488 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005489
Chris Mason5f39d392007-10-15 16:14:19 -04005490 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005491out:
Chris Mason39279cc2007-06-12 06:35:45 -04005492 btrfs_free_path(path);
5493 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005494out_err:
5495 location->objectid = 0;
5496 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005497}
5498
5499/*
5500 * when we hit a tree root in a directory, the btrfs part of the inode
5501 * needs to be changed to reflect the root directory of the tree root. This
5502 * is kind of like crossing a mount point.
5503 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005504static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 struct inode *dir,
5506 struct dentry *dentry,
5507 struct btrfs_key *location,
5508 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005509{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 struct btrfs_path *path;
5511 struct btrfs_root *new_root;
5512 struct btrfs_root_ref *ref;
5513 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005514 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005515 int ret;
5516 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005517
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 path = btrfs_alloc_path();
5519 if (!path) {
5520 err = -ENOMEM;
5521 goto out;
5522 }
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005525 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5526 key.type = BTRFS_ROOT_REF_KEY;
5527 key.offset = location->objectid;
5528
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005529 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 if (ret) {
5531 if (ret < 0)
5532 err = ret;
5533 goto out;
5534 }
Chris Mason39279cc2007-06-12 06:35:45 -04005535
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 leaf = path->nodes[0];
5537 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005538 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5540 goto out;
5541
5542 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5543 (unsigned long)(ref + 1),
5544 dentry->d_name.len);
5545 if (ret)
5546 goto out;
5547
David Sterbab3b4aa72011-04-21 01:20:15 +02005548 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005549
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005550 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 if (IS_ERR(new_root)) {
5552 err = PTR_ERR(new_root);
5553 goto out;
5554 }
5555
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005556 *sub_root = new_root;
5557 location->objectid = btrfs_root_dirid(&new_root->root_item);
5558 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005559 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005560 err = 0;
5561out:
5562 btrfs_free_path(path);
5563 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005564}
5565
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566static void inode_tree_add(struct inode *inode)
5567{
5568 struct btrfs_root *root = BTRFS_I(inode)->root;
5569 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005570 struct rb_node **p;
5571 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005572 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005573 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574
Al Viro1d3382cb2010-10-23 15:19:20 -04005575 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005576 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005577 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005579 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580 while (*p) {
5581 parent = *p;
5582 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5583
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005584 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005585 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005586 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005587 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 else {
5589 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005590 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005591 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005592 RB_CLEAR_NODE(parent);
5593 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005594 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 }
5596 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005597 rb_link_node(new, parent, p);
5598 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 spin_unlock(&root->inode_lock);
5600}
5601
5602static void inode_tree_del(struct inode *inode)
5603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005606 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005608 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005609 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005614 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005615
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005616 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005617 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 spin_lock(&root->inode_lock);
5619 empty = RB_EMPTY_ROOT(&root->inode_tree);
5620 spin_unlock(&root->inode_lock);
5621 if (empty)
5622 btrfs_add_dead_root(root);
5623 }
5624}
5625
Jeff Mahoney143bede2012-03-01 14:56:26 +01005626void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005627{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005628 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005629 struct rb_node *node;
5630 struct rb_node *prev;
5631 struct btrfs_inode *entry;
5632 struct inode *inode;
5633 u64 objectid = 0;
5634
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005635 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005636 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005637
5638 spin_lock(&root->inode_lock);
5639again:
5640 node = root->inode_tree.rb_node;
5641 prev = NULL;
5642 while (node) {
5643 prev = node;
5644 entry = rb_entry(node, struct btrfs_inode, rb_node);
5645
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005646 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005648 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005649 node = node->rb_right;
5650 else
5651 break;
5652 }
5653 if (!node) {
5654 while (prev) {
5655 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005656 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005657 node = prev;
5658 break;
5659 }
5660 prev = rb_next(prev);
5661 }
5662 }
5663 while (node) {
5664 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005665 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005666 inode = igrab(&entry->vfs_inode);
5667 if (inode) {
5668 spin_unlock(&root->inode_lock);
5669 if (atomic_read(&inode->i_count) > 1)
5670 d_prune_aliases(inode);
5671 /*
Al Viro45321ac2010-06-07 13:43:19 -04005672 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005673 * the inode cache when its usage count
5674 * hits zero.
5675 */
5676 iput(inode);
5677 cond_resched();
5678 spin_lock(&root->inode_lock);
5679 goto again;
5680 }
5681
5682 if (cond_resched_lock(&root->inode_lock))
5683 goto again;
5684
5685 node = rb_next(node);
5686 }
5687 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005688}
5689
Chris Masone02119d2008-09-05 16:13:11 -04005690static int btrfs_init_locked_inode(struct inode *inode, void *p)
5691{
5692 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005693 inode->i_ino = args->location->objectid;
5694 memcpy(&BTRFS_I(inode)->location, args->location,
5695 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005696 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005697 return 0;
5698}
5699
5700static int btrfs_find_actor(struct inode *inode, void *opaque)
5701{
5702 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005703 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005704 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005705}
5706
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005707static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005708 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005709 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005710{
5711 struct inode *inode;
5712 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005713 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005714
Chris Mason90d3e592014-01-09 17:28:00 -08005715 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005716 args.root = root;
5717
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005718 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005719 btrfs_init_locked_inode,
5720 (void *)&args);
5721 return inode;
5722}
5723
Balaji Rao1a54ef82008-07-21 02:01:04 +05305724/* Get an inode object given its location and corresponding root.
5725 * Returns in *is_new if the inode was read from disk
5726 */
5727struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005728 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305729{
5730 struct inode *inode;
5731
Chris Mason90d3e592014-01-09 17:28:00 -08005732 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305733 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005734 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305735
5736 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005737 int ret;
5738
5739 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005740 if (!is_bad_inode(inode)) {
5741 inode_tree_add(inode);
5742 unlock_new_inode(inode);
5743 if (new)
5744 *new = 1;
5745 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005746 unlock_new_inode(inode);
5747 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005748 ASSERT(ret < 0);
5749 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005750 }
5751 }
5752
Balaji Rao1a54ef82008-07-21 02:01:04 +05305753 return inode;
5754}
5755
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756static struct inode *new_simple_dir(struct super_block *s,
5757 struct btrfs_key *key,
5758 struct btrfs_root *root)
5759{
5760 struct inode *inode = new_inode(s);
5761
5762 if (!inode)
5763 return ERR_PTR(-ENOMEM);
5764
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005765 BTRFS_I(inode)->root = root;
5766 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005767 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005768
5769 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005770 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005771 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005772 inode->i_fop = &simple_dir_operations;
5773 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005774 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305775 inode->i_atime = inode->i_mtime;
5776 inode->i_ctime = inode->i_mtime;
5777 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005778
5779 return inode;
5780}
5781
Chris Mason3de45862008-11-17 21:02:50 -05005782struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005783{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005784 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005785 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005786 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005787 struct btrfs_root *sub_root = root;
5788 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005789 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005790 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005791
5792 if (dentry->d_name.len > BTRFS_NAME_LEN)
5793 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005794
Jeff Layton39e3c952012-11-28 11:30:53 -05005795 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005796 if (ret < 0)
5797 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005798
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005800 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801
5802 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005803 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005804 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005805 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005806
5807 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5808
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005809 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005810 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811 &location, &sub_root);
5812 if (ret < 0) {
5813 if (ret != -ENOENT)
5814 inode = ERR_PTR(ret);
5815 else
5816 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5817 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005818 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005819 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005821
Julia Lawall34d19ba2011-01-24 19:55:19 +00005822 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005823 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005824 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005825 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005826 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005827 if (ret) {
5828 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005829 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005830 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005831 }
5832
Chris Mason3de45862008-11-17 21:02:50 -05005833 return inode;
5834}
5835
Nick Pigginfe15ce42011-01-07 17:49:23 +11005836static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005837{
5838 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005839 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005840
Li Zefan848cce02012-02-21 17:04:28 +08005841 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005842 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005843
Li Zefan848cce02012-02-21 17:04:28 +08005844 if (inode) {
5845 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005846 if (btrfs_root_refs(&root->root_item) == 0)
5847 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005848
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005849 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005850 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005851 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005852 return 0;
5853}
5854
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005855static void btrfs_dentry_release(struct dentry *dentry)
5856{
Daeseok Youn944a4512014-04-14 15:37:02 +09005857 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005858}
5859
Chris Mason3de45862008-11-17 21:02:50 -05005860static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005861 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005862{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005863 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005864
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005865 inode = btrfs_lookup_dentry(dir, dentry);
5866 if (IS_ERR(inode)) {
5867 if (PTR_ERR(inode) == -ENOENT)
5868 inode = NULL;
5869 else
5870 return ERR_CAST(inode);
5871 }
5872
Al Viro41d28bc2014-10-12 22:24:21 -04005873 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005874}
5875
Miao Xie16cdcec2011-04-22 18:12:22 +08005876unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005877 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5878};
5879
Josef Bacik23b5ec72017-07-24 15:14:25 -04005880/*
5881 * All this infrastructure exists because dir_emit can fault, and we are holding
5882 * the tree lock when doing readdir. For now just allocate a buffer and copy
5883 * our information into that, and then dir_emit from the buffer. This is
5884 * similar to what NFS does, only we don't keep the buffer around in pagecache
5885 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5886 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5887 * tree lock.
5888 */
5889static int btrfs_opendir(struct inode *inode, struct file *file)
5890{
5891 struct btrfs_file_private *private;
5892
5893 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5894 if (!private)
5895 return -ENOMEM;
5896 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5897 if (!private->filldir_buf) {
5898 kfree(private);
5899 return -ENOMEM;
5900 }
5901 file->private_data = private;
5902 return 0;
5903}
5904
5905struct dir_entry {
5906 u64 ino;
5907 u64 offset;
5908 unsigned type;
5909 int name_len;
5910};
5911
5912static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5913{
5914 while (entries--) {
5915 struct dir_entry *entry = addr;
5916 char *name = (char *)(entry + 1);
5917
5918 ctx->pos = entry->offset;
5919 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5920 entry->type))
5921 return 1;
5922 addr += sizeof(struct dir_entry) + entry->name_len;
5923 ctx->pos++;
5924 }
5925 return 0;
5926}
5927
Al Viro9cdda8d2013-05-22 16:48:09 -04005928static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005929{
Al Viro9cdda8d2013-05-22 16:48:09 -04005930 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005932 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005933 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005934 struct btrfs_dir_item *di;
5935 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005936 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005938 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005939 struct list_head ins_list;
5940 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005941 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005942 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005943 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005944 char *name_ptr;
5945 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005946 int entries = 0;
5947 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005948 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005949 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005950
Al Viro9cdda8d2013-05-22 16:48:09 -04005951 if (!dir_emit_dots(file, ctx))
5952 return 0;
5953
David Woodhouse49593bf2008-08-17 17:08:36 +01005954 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005955 if (!path)
5956 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005957
Josef Bacik23b5ec72017-07-24 15:14:25 -04005958 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005959 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005960
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005961 INIT_LIST_HEAD(&ins_list);
5962 INIT_LIST_HEAD(&del_list);
5963 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005964
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005967 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005968 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005969
Chris Mason39279cc2007-06-12 06:35:45 -04005970 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5971 if (ret < 0)
5972 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005973
5974 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005975 struct dir_entry *entry;
5976
Chris Mason5f39d392007-10-15 16:14:19 -04005977 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005978 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005979 if (slot >= btrfs_header_nritems(leaf)) {
5980 ret = btrfs_next_leaf(root, path);
5981 if (ret < 0)
5982 goto err;
5983 else if (ret > 0)
5984 break;
5985 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005986 }
Chris Mason3de45862008-11-17 21:02:50 -05005987
Chris Mason5f39d392007-10-15 16:14:19 -04005988 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5989
5990 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005991 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005992 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005993 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005994 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005995 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005996 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005997 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005998 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005999 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006000 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01006001
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006003 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6004 PAGE_SIZE) {
6005 btrfs_release_path(path);
6006 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6007 if (ret)
6008 goto nopos;
6009 addr = private->filldir_buf;
6010 entries = 0;
6011 total_len = 0;
6012 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014
6015 entry = addr;
6016 entry->name_len = name_len;
6017 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006018 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6019 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006020 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006021 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006022 entry->ino = location.objectid;
6023 entry->offset = found_key.offset;
6024 entries++;
6025 addr += sizeof(struct dir_entry) + name_len;
6026 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006027next:
6028 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006029 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006030 btrfs_release_path(path);
6031
6032 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6033 if (ret)
6034 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006035
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006036 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006037 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006038 goto nopos;
6039
Zach Browndb62efb2013-07-11 16:19:42 -07006040 /*
6041 * Stop new entries from being returned after we return the last
6042 * entry.
6043 *
6044 * New directory entries are assigned a strictly increasing
6045 * offset. This means that new entries created during readdir
6046 * are *guaranteed* to be seen in the future by that readdir.
6047 * This has broken buggy programs which operate on names as
6048 * they're returned by readdir. Until we re-use freed offsets
6049 * we have this hack to stop new entries from being returned
6050 * under the assumption that they'll never reach this huge
6051 * offset.
6052 *
6053 * This is being careful not to overflow 32bit loff_t unless the
6054 * last entry requires it because doing so has broken 32bit apps
6055 * in the past.
6056 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006057 if (ctx->pos >= INT_MAX)
6058 ctx->pos = LLONG_MAX;
6059 else
6060 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006061nopos:
6062 ret = 0;
6063err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006064 if (put)
6065 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006066 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006067 return ret;
6068}
6069
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006070int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006071{
6072 struct btrfs_root *root = BTRFS_I(inode)->root;
6073 struct btrfs_trans_handle *trans;
6074 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006075 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006076
Josef Bacik72ac3c02012-05-23 14:13:11 -04006077 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006078 return 0;
6079
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006080 if (btrfs_fs_closing(root->fs_info) &&
6081 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006082 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006083
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006084 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006085 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006086 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006087 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006088 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006089 if (IS_ERR(trans))
6090 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006091 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006092 }
6093 return ret;
6094}
6095
6096/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006097 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006098 * inode changes. But, it is most likely to find the inode in cache.
6099 * FIXME, needs more benchmarking...there are no reasons other than performance
6100 * to keep or drop this code.
6101 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006102static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006103{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006104 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006105 struct btrfs_root *root = BTRFS_I(inode)->root;
6106 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006107 int ret;
6108
Josef Bacik72ac3c02012-05-23 14:13:11 -04006109 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006110 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006111
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006112 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006113 if (IS_ERR(trans))
6114 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006115
6116 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006117 if (ret && ret == -ENOSPC) {
6118 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006119 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006120 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006121 if (IS_ERR(trans))
6122 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006123
Chris Mason94b60442010-05-26 11:02:00 -04006124 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006125 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006126 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006127 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006128 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006129
6130 return ret;
6131}
6132
6133/*
6134 * This is a copy of file_update_time. We need this so we can return error on
6135 * ENOSPC for updating the inode in the case of file write and mmap writes.
6136 */
Josef Bacike41f9412012-03-26 09:46:47 -04006137static int btrfs_update_time(struct inode *inode, struct timespec *now,
6138 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006139{
Alexander Block2bc5565282012-06-15 09:49:33 +02006140 struct btrfs_root *root = BTRFS_I(inode)->root;
6141
6142 if (btrfs_root_readonly(root))
6143 return -EROFS;
6144
Josef Bacike41f9412012-03-26 09:46:47 -04006145 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006146 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006147 if (flags & S_CTIME)
6148 inode->i_ctime = *now;
6149 if (flags & S_MTIME)
6150 inode->i_mtime = *now;
6151 if (flags & S_ATIME)
6152 inode->i_atime = *now;
6153 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006154}
6155
Chris Masond352ac62008-09-29 15:18:18 -04006156/*
6157 * find the highest existing sequence number in a directory
6158 * and then set the in-memory index_cnt variable to reflect
6159 * free sequence numbers
6160 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006161static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006162{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006163 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006164 struct btrfs_key key, found_key;
6165 struct btrfs_path *path;
6166 struct extent_buffer *leaf;
6167 int ret;
6168
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006169 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006170 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006171 key.offset = (u64)-1;
6172
6173 path = btrfs_alloc_path();
6174 if (!path)
6175 return -ENOMEM;
6176
6177 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6178 if (ret < 0)
6179 goto out;
6180 /* FIXME: we should be able to handle this */
6181 if (ret == 0)
6182 goto out;
6183 ret = 0;
6184
6185 /*
6186 * MAGIC NUMBER EXPLANATION:
6187 * since we search a directory based on f_pos we have to start at 2
6188 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6189 * else has to start at 2
6190 */
6191 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006192 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006193 goto out;
6194 }
6195
6196 path->slots[0]--;
6197
6198 leaf = path->nodes[0];
6199 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6200
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006201 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006202 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006203 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006204 goto out;
6205 }
6206
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006207 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006208out:
6209 btrfs_free_path(path);
6210 return ret;
6211}
6212
Chris Masond352ac62008-09-29 15:18:18 -04006213/*
6214 * helper to find a free sequence number in a given directory. This current
6215 * code is very simple, later versions will do smarter things in the btree
6216 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006217int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006218{
6219 int ret = 0;
6220
Nikolay Borisov877574e2017-02-20 13:50:33 +02006221 if (dir->index_cnt == (u64)-1) {
6222 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006223 if (ret) {
6224 ret = btrfs_set_inode_index_count(dir);
6225 if (ret)
6226 return ret;
6227 }
Josef Bacikaec74772008-07-24 12:12:38 -04006228 }
6229
Nikolay Borisov877574e2017-02-20 13:50:33 +02006230 *index = dir->index_cnt;
6231 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006232
6233 return ret;
6234}
6235
Chris Masonb0d5d102014-09-08 13:08:51 -07006236static int btrfs_insert_inode_locked(struct inode *inode)
6237{
6238 struct btrfs_iget_args args;
6239 args.location = &BTRFS_I(inode)->location;
6240 args.root = BTRFS_I(inode)->root;
6241
6242 return insert_inode_locked4(inode,
6243 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6244 btrfs_find_actor, &args);
6245}
6246
Anand Jain19aee8d2017-07-18 17:37:05 +08006247/*
6248 * Inherit flags from the parent inode.
6249 *
6250 * Currently only the compression flags and the cow flags are inherited.
6251 */
6252static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6253{
6254 unsigned int flags;
6255
6256 if (!dir)
6257 return;
6258
6259 flags = BTRFS_I(dir)->flags;
6260
6261 if (flags & BTRFS_INODE_NOCOMPRESS) {
6262 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6263 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6264 } else if (flags & BTRFS_INODE_COMPRESS) {
6265 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6266 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6267 }
6268
6269 if (flags & BTRFS_INODE_NODATACOW) {
6270 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6271 if (S_ISREG(inode->i_mode))
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6273 }
6274
6275 btrfs_update_iflags(inode);
6276}
6277
Chris Mason39279cc2007-06-12 06:35:45 -04006278static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6279 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006280 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006281 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006282 u64 ref_objectid, u64 objectid,
6283 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006285 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006287 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006289 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006290 struct btrfs_inode_ref *ref;
6291 struct btrfs_key key[2];
6292 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006293 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006294 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006295 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006296
Chris Mason5f39d392007-10-15 16:14:19 -04006297 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006298 if (!path)
6299 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006300
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006301 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006302 if (!inode) {
6303 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006304 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006305 }
Chris Mason39279cc2007-06-12 06:35:45 -04006306
Li Zefan581bb052011-04-20 10:06:11 +08006307 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006308 * O_TMPFILE, set link count to 0, so that after this point,
6309 * we fill in an inode item with the correct link count.
6310 */
6311 if (!name)
6312 set_nlink(inode, 0);
6313
6314 /*
Li Zefan581bb052011-04-20 10:06:11 +08006315 * we have to initialize this early, so we can reclaim the inode
6316 * number if we fail afterwards in this function.
6317 */
6318 inode->i_ino = objectid;
6319
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006320 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006321 trace_btrfs_inode_request(dir);
6322
Nikolay Borisov877574e2017-02-20 13:50:33 +02006323 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006324 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006325 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006326 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006327 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006328 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006329 } else if (dir) {
6330 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006331 }
6332 /*
6333 * index_cnt is ignored for everything but a dir,
6334 * btrfs_get_inode_index_count has an explanation for the magic
6335 * number
6336 */
6337 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006338 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006339 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006340 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006341 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006342
Josef Bacik5dc562c2012-08-17 13:14:17 -04006343 /*
6344 * We could have gotten an inode number from somebody who was fsynced
6345 * and then removed in this same transaction, so let's just set full
6346 * sync since it will be a full sync anyway and this will blow away the
6347 * old info in the log.
6348 */
6349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6350
Chris Mason9c583092008-01-29 15:15:18 -05006351 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006352 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006353 key[0].offset = 0;
6354
Chris Mason9c583092008-01-29 15:15:18 -05006355 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006356
6357 if (name) {
6358 /*
6359 * Start new inodes with an inode_ref. This is slightly more
6360 * efficient for small numbers of hard links since they will
6361 * be packed into one item. Extended refs will kick in if we
6362 * add more hard links than can fit in the ref item.
6363 */
6364 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006365 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006366 key[1].offset = ref_objectid;
6367
6368 sizes[1] = name_len + sizeof(*ref);
6369 }
Chris Mason9c583092008-01-29 15:15:18 -05006370
Chris Masonb0d5d102014-09-08 13:08:51 -07006371 location = &BTRFS_I(inode)->location;
6372 location->objectid = objectid;
6373 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006374 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006375
6376 ret = btrfs_insert_inode_locked(inode);
6377 if (ret < 0)
6378 goto fail;
6379
Chris Masonb9473432009-03-13 11:00:37 -04006380 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006381 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006382 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006383 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006384
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006385 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006386 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306387
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006388 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306389 inode->i_atime = inode->i_mtime;
6390 inode->i_ctime = inode->i_mtime;
6391 BTRFS_I(inode)->i_otime = inode->i_mtime;
6392
Chris Mason5f39d392007-10-15 16:14:19 -04006393 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6394 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006395 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006396 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006397 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006398
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006399 if (name) {
6400 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6401 struct btrfs_inode_ref);
6402 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6403 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6404 ptr = (unsigned long)(ref + 1);
6405 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6406 }
Chris Mason9c583092008-01-29 15:15:18 -05006407
Chris Mason5f39d392007-10-15 16:14:19 -04006408 btrfs_mark_buffer_dirty(path->nodes[0]);
6409 btrfs_free_path(path);
6410
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006411 btrfs_inherit_iflags(inode, dir);
6412
Al Viro569254b2011-07-24 17:08:40 -04006413 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006414 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006415 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006416 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006417 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6418 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006419 }
6420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006421 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006422
6423 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006424 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006425
Alexander Block8ea05e32012-07-25 17:35:53 +02006426 btrfs_update_root_times(trans, root);
6427
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006428 ret = btrfs_inode_inherit_props(trans, inode, dir);
6429 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006430 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006431 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006432 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006433
Chris Mason39279cc2007-06-12 06:35:45 -04006434 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006435
6436fail_unlock:
6437 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006438fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006439 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006440 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006441 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006442 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006443 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006444}
6445
6446static inline u8 btrfs_inode_type(struct inode *inode)
6447{
6448 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6449}
6450
Chris Masond352ac62008-09-29 15:18:18 -04006451/*
6452 * utility function to add 'inode' into 'parent_inode' with
6453 * a give name and a given sequence number.
6454 * if 'add_backref' is true, also insert a backref from the
6455 * inode to the parent directory.
6456 */
Chris Masone02119d2008-09-05 16:13:11 -04006457int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006458 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006459 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006460{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006461 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006462 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006463 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006464 struct btrfs_root *root = parent_inode->root;
6465 u64 ino = btrfs_ino(inode);
6466 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006467
Li Zefan33345d012011-04-20 10:31:50 +08006468 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006469 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006470 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006471 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006472 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006473 key.offset = 0;
6474 }
Chris Mason39279cc2007-06-12 06:35:45 -04006475
Li Zefan33345d012011-04-20 10:31:50 +08006476 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006477 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6478 root->root_key.objectid, parent_ino,
6479 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006480 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006481 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6482 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006483 }
6484
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006485 /* Nothing to clean up yet */
6486 if (ret)
6487 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006488
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6490 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006491 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006492 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006493 goto fail_dir_item;
6494 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006495 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006496 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006497 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006499 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006501 inode_inc_iversion(&parent_inode->vfs_inode);
6502 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6503 current_time(&parent_inode->vfs_inode);
6504 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006505 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006506 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006507 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006508
6509fail_dir_item:
6510 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6511 u64 local_index;
6512 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006513 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6514 root->root_key.objectid, parent_ino,
6515 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006516
6517 } else if (add_backref) {
6518 u64 local_index;
6519 int err;
6520
6521 err = btrfs_del_inode_ref(trans, root, name, name_len,
6522 ino, parent_ino, &local_index);
6523 }
6524 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006525}
6526
6527static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006528 struct btrfs_inode *dir, struct dentry *dentry,
6529 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006530{
Josef Bacika1b075d2010-11-19 20:36:11 +00006531 int err = btrfs_add_link(trans, dir, inode,
6532 dentry->d_name.name, dentry->d_name.len,
6533 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006534 if (err > 0)
6535 err = -EEXIST;
6536 return err;
6537}
6538
Josef Bacik618e21d2007-07-11 10:18:17 -04006539static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006540 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006541{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006542 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006543 struct btrfs_trans_handle *trans;
6544 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006545 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006546 int err;
6547 int drop_inode = 0;
6548 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006549 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006550
Josef Bacik9ed74f22009-09-11 16:12:44 -04006551 /*
6552 * 2 for inode item and ref
6553 * 2 for dir items
6554 * 1 for xattr if selinux is on
6555 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006556 trans = btrfs_start_transaction(root, 5);
6557 if (IS_ERR(trans))
6558 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006559
Li Zefan581bb052011-04-20 10:06:11 +08006560 err = btrfs_find_free_ino(root, &objectid);
6561 if (err)
6562 goto out_unlock;
6563
Josef Bacikaec74772008-07-24 12:12:38 -04006564 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006565 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6566 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006567 if (IS_ERR(inode)) {
6568 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006570 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006571
Casey Schauflerad19db72011-12-15 10:09:07 -05006572 /*
6573 * If the active LSM wants to access the inode during
6574 * d_instantiate it needs these. Smack checks to see
6575 * if the filesystem supports xattrs by looking at the
6576 * ops vector.
6577 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006578 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006579 init_special_inode(inode, inode->i_mode, rdev);
6580
6581 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006582 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006583 goto out_unlock_inode;
6584
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006585 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6586 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006587 if (err) {
6588 goto out_unlock_inode;
6589 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006590 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006592 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006593 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006594
Josef Bacik618e21d2007-07-11 10:18:17 -04006595out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006596 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006597 btrfs_balance_delayed_items(fs_info);
6598 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 if (drop_inode) {
6600 inode_dec_link_count(inode);
6601 iput(inode);
6602 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006603 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006604
6605out_unlock_inode:
6606 drop_inode = 1;
6607 unlock_new_inode(inode);
6608 goto out_unlock;
6609
Josef Bacik618e21d2007-07-11 10:18:17 -04006610}
6611
Chris Mason39279cc2007-06-12 06:35:45 -04006612static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006613 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006614{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006615 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 struct btrfs_trans_handle *trans;
6617 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006618 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006619 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006620 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006621 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006622 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006623
Josef Bacik9ed74f22009-09-11 16:12:44 -04006624 /*
6625 * 2 for inode item and ref
6626 * 2 for dir items
6627 * 1 for xattr if selinux is on
6628 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006629 trans = btrfs_start_transaction(root, 5);
6630 if (IS_ERR(trans))
6631 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006632
Li Zefan581bb052011-04-20 10:06:11 +08006633 err = btrfs_find_free_ino(root, &objectid);
6634 if (err)
6635 goto out_unlock;
6636
Josef Bacikaec74772008-07-24 12:12:38 -04006637 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006638 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6639 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006640 if (IS_ERR(inode)) {
6641 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006642 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006643 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006644 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006645 /*
6646 * If the active LSM wants to access the inode during
6647 * d_instantiate it needs these. Smack checks to see
6648 * if the filesystem supports xattrs by looking at the
6649 * ops vector.
6650 */
6651 inode->i_fop = &btrfs_file_operations;
6652 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006653 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006654
6655 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6656 if (err)
6657 goto out_unlock_inode;
6658
6659 err = btrfs_update_inode(trans, root, inode);
6660 if (err)
6661 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006662
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006663 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6664 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006666 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006667
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006668 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006669 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006670 d_instantiate(dentry, inode);
6671
Chris Mason39279cc2007-06-12 06:35:45 -04006672out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006673 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006674 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006675 inode_dec_link_count(inode);
6676 iput(inode);
6677 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006678 btrfs_balance_delayed_items(fs_info);
6679 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006681
6682out_unlock_inode:
6683 unlock_new_inode(inode);
6684 goto out_unlock;
6685
Chris Mason39279cc2007-06-12 06:35:45 -04006686}
6687
6688static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6689 struct dentry *dentry)
6690{
Filipe Manana271dba452016-01-05 16:24:05 +00006691 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006692 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006693 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006695 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006696 int err;
6697 int drop_inode = 0;
6698
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006699 /* do not allow sys_link's with other subvols of the same device */
6700 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006701 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006702
Mark Fashehf1863732012-08-08 11:32:27 -07006703 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006704 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006705
Nikolay Borisov877574e2017-02-20 13:50:33 +02006706 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006707 if (err)
6708 goto fail;
6709
Yan, Zhenga22285a2010-05-16 10:48:46 -04006710 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006711 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006713 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006715 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 if (IS_ERR(trans)) {
6717 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006718 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006719 goto fail;
6720 }
Chris Mason5f39d392007-10-15 16:14:19 -04006721
Miao Xie67de1172013-12-26 13:07:06 +08006722 /* There are several dir indexes for this inode, clear the cache. */
6723 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006724 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006725 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006726 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006727 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006728 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006729
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006730 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6731 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006732
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006734 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006736 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006737 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006738 if (err)
6739 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006740 if (inode->i_nlink == 1) {
6741 /*
6742 * If new hard link count is 1, it's a file created
6743 * with open(2) O_TMPFILE flag.
6744 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006745 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006746 if (err)
6747 goto fail;
6748 }
Al Viro08c422c2011-12-23 07:58:13 -05006749 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006750 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006751 }
Chris Mason39279cc2007-06-12 06:35:45 -04006752
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006753 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006754fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006755 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006756 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006757 if (drop_inode) {
6758 inode_dec_link_count(inode);
6759 iput(inode);
6760 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006761 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006762 return err;
6763}
6764
Al Viro18bb1db2011-07-26 01:41:39 -04006765static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006766{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006767 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006768 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006769 struct btrfs_trans_handle *trans;
6770 struct btrfs_root *root = BTRFS_I(dir)->root;
6771 int err = 0;
6772 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006773 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006774 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006775
Josef Bacik9ed74f22009-09-11 16:12:44 -04006776 /*
6777 * 2 items for inode and ref
6778 * 2 items for dir items
6779 * 1 for xattr if selinux is on
6780 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006781 trans = btrfs_start_transaction(root, 5);
6782 if (IS_ERR(trans))
6783 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006784
Li Zefan581bb052011-04-20 10:06:11 +08006785 err = btrfs_find_free_ino(root, &objectid);
6786 if (err)
6787 goto out_fail;
6788
Josef Bacikaec74772008-07-24 12:12:38 -04006789 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006790 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6791 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006792 if (IS_ERR(inode)) {
6793 err = PTR_ERR(inode);
6794 goto out_fail;
6795 }
Chris Mason5f39d392007-10-15 16:14:19 -04006796
Chris Mason39279cc2007-06-12 06:35:45 -04006797 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006798 /* these must be set before we unlock the inode */
6799 inode->i_op = &btrfs_dir_inode_operations;
6800 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006801
Eric Paris2a7dba32011-02-01 11:05:39 -05006802 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006803 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006804 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006805
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006806 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006807 err = btrfs_update_inode(trans, root, inode);
6808 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006809 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006810
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006811 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6812 dentry->d_name.name,
6813 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006814 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006815 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006816
Chris Mason39279cc2007-06-12 06:35:45 -04006817 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006818 /*
6819 * mkdir is special. We're unlocking after we call d_instantiate
6820 * to avoid a race with nfsd calling d_instantiate.
6821 */
6822 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006823 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006824
6825out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006826 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006827 if (drop_on_err) {
6828 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006829 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006830 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006831 btrfs_balance_delayed_items(fs_info);
6832 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006833 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006834
6835out_fail_inode:
6836 unlock_new_inode(inode);
6837 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006838}
6839
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006840/* Find next extent map of a given extent map, caller needs to ensure locks */
6841static struct extent_map *next_extent_map(struct extent_map *em)
6842{
6843 struct rb_node *next;
6844
6845 next = rb_next(&em->rb_node);
6846 if (!next)
6847 return NULL;
6848 return container_of(next, struct extent_map, rb_node);
6849}
6850
6851static struct extent_map *prev_extent_map(struct extent_map *em)
6852{
6853 struct rb_node *prev;
6854
6855 prev = rb_prev(&em->rb_node);
6856 if (!prev)
6857 return NULL;
6858 return container_of(prev, struct extent_map, rb_node);
6859}
6860
Chris Masond352ac62008-09-29 15:18:18 -04006861/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006862 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006863 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006864 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006865 */
Chris Mason3b951512008-04-17 11:29:12 -04006866static int merge_extent_mapping(struct extent_map_tree *em_tree,
6867 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006868 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006869 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006870{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006871 struct extent_map *prev;
6872 struct extent_map *next;
6873 u64 start;
6874 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006875 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006876
Chris Masone6dcd2d2008-07-17 12:53:50 -04006877 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006878
6879 if (existing->start > map_start) {
6880 next = existing;
6881 prev = prev_extent_map(next);
6882 } else {
6883 prev = existing;
6884 next = next_extent_map(prev);
6885 }
6886
6887 start = prev ? extent_map_end(prev) : em->start;
6888 start = max_t(u64, start, em->start);
6889 end = next ? next->start : extent_map_end(em);
6890 end = min_t(u64, end, extent_map_end(em));
6891 start_diff = start - em->start;
6892 em->start = start;
6893 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006894 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6895 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006896 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006897 em->block_len -= start_diff;
6898 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006899 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006900}
6901
Chris Masonc8b97812008-10-29 14:49:59 -04006902static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006903 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006904 size_t pg_offset, u64 extent_offset,
6905 struct btrfs_file_extent_item *item)
6906{
6907 int ret;
6908 struct extent_buffer *leaf = path->nodes[0];
6909 char *tmp;
6910 size_t max_size;
6911 unsigned long inline_size;
6912 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006913 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006914
6915 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006916 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006917 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6918 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006919 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006920 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006921 if (!tmp)
6922 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006923 ptr = btrfs_file_extent_inline_start(item);
6924
6925 read_extent_buffer(leaf, tmp, ptr, inline_size);
6926
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006927 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006928 ret = btrfs_decompress(compress_type, tmp, page,
6929 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006930
6931 /*
6932 * decompression code contains a memset to fill in any space between the end
6933 * of the uncompressed data and the end of max_size in case the decompressed
6934 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6935 * the end of an inline extent and the beginning of the next block, so we
6936 * cover that region here.
6937 */
6938
6939 if (max_size + pg_offset < PAGE_SIZE) {
6940 char *map = kmap(page);
6941 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6942 kunmap(page);
6943 }
Chris Masonc8b97812008-10-29 14:49:59 -04006944 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006945 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006946}
6947
Chris Masond352ac62008-09-29 15:18:18 -04006948/*
6949 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006950 * the ugly parts come from merging extents from the disk with the in-ram
6951 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006952 * where the in-ram extents might be locked pending data=ordered completion.
6953 *
6954 * This also copies inline extents directly into the page.
6955 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006956struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6957 struct page *page,
6958 size_t pg_offset, u64 start, u64 len,
6959 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006960{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006962 int ret;
6963 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006964 u64 extent_start = 0;
6965 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006966 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006967 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006968 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006969 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006970 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006971 struct extent_buffer *leaf;
6972 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006973 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006974 struct extent_map_tree *em_tree = &inode->extent_tree;
6975 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006976 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006977 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006978
Chris Masona52d9a82007-08-27 16:49:44 -04006979again:
Chris Mason890871b2009-09-02 16:24:52 -04006980 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006981 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006982 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006983 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006984 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006985
Chris Masona52d9a82007-08-27 16:49:44 -04006986 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006987 if (em->start > start || em->start + em->len <= start)
6988 free_extent_map(em);
6989 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006990 free_extent_map(em);
6991 else
6992 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006993 }
David Sterba172ddd62011-04-21 00:48:27 +02006994 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006995 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006996 err = -ENOMEM;
6997 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006998 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006999 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05007000 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007001 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007002 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007003 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007004
7005 if (!path) {
7006 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04007007 if (!path) {
7008 err = -ENOMEM;
7009 goto out;
7010 }
7011 /*
7012 * Chances are we'll be called again, so go ahead and do
7013 * readahead
7014 */
David Sterbae4058b52015-11-27 16:31:35 +01007015 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04007016 }
7017
Chris Mason179e29e2007-11-01 11:28:41 -04007018 ret = btrfs_lookup_file_extent(trans, root, path,
7019 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04007020 if (ret < 0) {
7021 err = ret;
7022 goto out;
7023 }
7024
7025 if (ret != 0) {
7026 if (path->slots[0] == 0)
7027 goto not_found;
7028 path->slots[0]--;
7029 }
7030
Chris Mason5f39d392007-10-15 16:14:19 -04007031 leaf = path->nodes[0];
7032 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007033 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007034 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007035 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007036 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007037 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007038 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007039 /*
7040 * If we backup past the first extent we want to move forward
7041 * and see if there is an extent in front of us, otherwise we'll
7042 * say there is a hole for our whole search range which can
7043 * cause problems.
7044 */
7045 extent_end = start;
7046 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007047 }
7048
Chris Mason5f39d392007-10-15 16:14:19 -04007049 found_type = btrfs_file_extent_type(leaf, item);
7050 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007051 if (found_type == BTRFS_FILE_EXTENT_REG ||
7052 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007053 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007054 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007055
7056 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7057 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007058 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7059 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007060 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007061 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007062 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007063
7064 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7065 path->slots[0],
7066 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007067 }
Josef Bacik25a50342013-10-14 12:08:38 -04007068next:
Yan Zheng9036c102008-10-30 14:19:41 -04007069 if (start >= extent_end) {
7070 path->slots[0]++;
7071 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7072 ret = btrfs_next_leaf(root, path);
7073 if (ret < 0) {
7074 err = ret;
7075 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007076 }
Yan Zheng9036c102008-10-30 14:19:41 -04007077 if (ret > 0)
7078 goto not_found;
7079 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007080 }
Yan Zheng9036c102008-10-30 14:19:41 -04007081 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7082 if (found_key.objectid != objectid ||
7083 found_key.type != BTRFS_EXTENT_DATA_KEY)
7084 goto not_found;
7085 if (start + len <= found_key.offset)
7086 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007087 if (start > found_key.offset)
7088 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007089 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007090 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007091 em->len = found_key.offset - start;
7092 goto not_found_em;
7093 }
7094
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007095 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007096 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007097
Yan Zhengd899e052008-10-30 14:25:28 -04007098 if (found_type == BTRFS_FILE_EXTENT_REG ||
7099 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007100 goto insert;
7101 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007102 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007103 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007104 size_t size;
7105 size_t extent_offset;
7106 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007107
Filipe Manana7ffbb592014-06-09 03:48:05 +01007108 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007109 goto out;
Yan689f9342007-10-29 11:41:07 -04007110
Chris Mason514ac8a2014-01-03 21:07:00 -08007111 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007112 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007113 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7114 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007115 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007116 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007117 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007118 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007119 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007120 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007121 if (btrfs_file_extent_compression(leaf, item) !=
7122 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007123 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007124 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007125 if (ret) {
7126 err = ret;
7127 goto out;
7128 }
Chris Masonc8b97812008-10-29 14:49:59 -04007129 } else {
7130 map = kmap(page);
7131 read_extent_buffer(leaf, map + pg_offset, ptr,
7132 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007133 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007134 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007135 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007136 copy_size);
7137 }
Chris Masonc8b97812008-10-29 14:49:59 -04007138 kunmap(page);
7139 }
Chris Mason179e29e2007-11-01 11:28:41 -04007140 flush_dcache_page(page);
7141 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007142 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007143 if (!trans) {
7144 kunmap(page);
7145 free_extent_map(em);
7146 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007147
David Sterbab3b4aa72011-04-21 01:20:15 +02007148 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007149 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007150
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007151 if (IS_ERR(trans))
7152 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007153 goto again;
7154 }
Chris Masonc8b97812008-10-29 14:49:59 -04007155 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007156 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007157 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007158 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007159 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007160 }
Chris Masond1310b22008-01-24 16:13:08 -05007161 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007162 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007163 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007164 }
7165not_found:
7166 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007167 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007168 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007169not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007170 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007171 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007172insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007173 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007174 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007175 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007176 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7177 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007178 err = -EIO;
7179 goto out;
7180 }
Chris Masond1310b22008-01-24 16:13:08 -05007181
7182 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007183 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007184 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007185 /* it is possible that someone inserted the extent into the tree
7186 * while we had the lock dropped. It is also possible that
7187 * an overlapping map exists in the tree
7188 */
Chris Masona52d9a82007-08-27 16:49:44 -04007189 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007190 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007191
7192 ret = 0;
7193
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007194 existing = search_extent_mapping(em_tree, start, len);
7195 /*
7196 * existing will always be non-NULL, since there must be
7197 * extent causing the -EEXIST.
7198 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007199 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007200 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007201 em->block_start == existing->block_start) {
7202 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007203 * The existing extent map already encompasses the
7204 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007205 */
7206 free_extent_map(em);
7207 em = existing;
7208 err = 0;
7209
7210 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007211 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007212 /*
7213 * The existing extent map is the one nearest to
7214 * the [start, start + len) range which overlaps
7215 */
7216 err = merge_extent_mapping(em_tree, existing,
7217 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007218 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007219 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007220 free_extent_map(em);
7221 em = NULL;
7222 }
7223 } else {
7224 free_extent_map(em);
7225 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007226 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007227 }
Chris Masona52d9a82007-08-27 16:49:44 -04007228 }
Chris Mason890871b2009-09-02 16:24:52 -04007229 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007230out:
liubo1abe9b82011-03-24 11:18:59 +00007231
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007232 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007233
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007234 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007235 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007236 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007237 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007238 err = ret;
7239 }
Chris Masona52d9a82007-08-27 16:49:44 -04007240 if (err) {
7241 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007242 return ERR_PTR(err);
7243 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007244 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007245 return em;
7246}
7247
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007248struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7249 struct page *page,
7250 size_t pg_offset, u64 start, u64 len,
7251 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007252{
7253 struct extent_map *em;
7254 struct extent_map *hole_em = NULL;
7255 u64 range_start = start;
7256 u64 end;
7257 u64 found;
7258 u64 found_end;
7259 int err = 0;
7260
7261 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7262 if (IS_ERR(em))
7263 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007264 /*
7265 * If our em maps to:
7266 * - a hole or
7267 * - a pre-alloc extent,
7268 * there might actually be delalloc bytes behind it.
7269 */
7270 if (em->block_start != EXTENT_MAP_HOLE &&
7271 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7272 return em;
7273 else
7274 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007275
7276 /* check to see if we've wrapped (len == -1 or similar) */
7277 end = start + len;
7278 if (end < start)
7279 end = (u64)-1;
7280 else
7281 end -= 1;
7282
7283 em = NULL;
7284
7285 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007286 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007287 end, len, EXTENT_DELALLOC, 1);
7288 found_end = range_start + found;
7289 if (found_end < range_start)
7290 found_end = (u64)-1;
7291
7292 /*
7293 * we didn't find anything useful, return
7294 * the original results from get_extent()
7295 */
7296 if (range_start > end || found_end <= start) {
7297 em = hole_em;
7298 hole_em = NULL;
7299 goto out;
7300 }
7301
7302 /* adjust the range_start to make sure it doesn't
7303 * go backwards from the start they passed in
7304 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307305 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007306 found = found_end - range_start;
7307
7308 if (found > 0) {
7309 u64 hole_start = start;
7310 u64 hole_len = len;
7311
David Sterba172ddd62011-04-21 00:48:27 +02007312 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007313 if (!em) {
7314 err = -ENOMEM;
7315 goto out;
7316 }
7317 /*
7318 * when btrfs_get_extent can't find anything it
7319 * returns one huge hole
7320 *
7321 * make sure what it found really fits our range, and
7322 * adjust to make sure it is based on the start from
7323 * the caller
7324 */
7325 if (hole_em) {
7326 u64 calc_end = extent_map_end(hole_em);
7327
7328 if (calc_end <= start || (hole_em->start > end)) {
7329 free_extent_map(hole_em);
7330 hole_em = NULL;
7331 } else {
7332 hole_start = max(hole_em->start, start);
7333 hole_len = calc_end - hole_start;
7334 }
7335 }
7336 em->bdev = NULL;
7337 if (hole_em && range_start > hole_start) {
7338 /* our hole starts before our delalloc, so we
7339 * have to return just the parts of the hole
7340 * that go until the delalloc starts
7341 */
7342 em->len = min(hole_len,
7343 range_start - hole_start);
7344 em->start = hole_start;
7345 em->orig_start = hole_start;
7346 /*
7347 * don't adjust block start at all,
7348 * it is fixed at EXTENT_MAP_HOLE
7349 */
7350 em->block_start = hole_em->block_start;
7351 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007352 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7353 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007354 } else {
7355 em->start = range_start;
7356 em->len = found;
7357 em->orig_start = range_start;
7358 em->block_start = EXTENT_MAP_DELALLOC;
7359 em->block_len = found;
7360 }
7361 } else if (hole_em) {
7362 return hole_em;
7363 }
7364out:
7365
7366 free_extent_map(hole_em);
7367 if (err) {
7368 free_extent_map(em);
7369 return ERR_PTR(err);
7370 }
7371 return em;
7372}
7373
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007374static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7375 const u64 start,
7376 const u64 len,
7377 const u64 orig_start,
7378 const u64 block_start,
7379 const u64 block_len,
7380 const u64 orig_block_len,
7381 const u64 ram_bytes,
7382 const int type)
7383{
7384 struct extent_map *em = NULL;
7385 int ret;
7386
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007387 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007388 em = create_io_em(inode, start, len, orig_start,
7389 block_start, block_len, orig_block_len,
7390 ram_bytes,
7391 BTRFS_COMPRESS_NONE, /* compress_type */
7392 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007393 if (IS_ERR(em))
7394 goto out;
7395 }
7396 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7397 len, block_len, type);
7398 if (ret) {
7399 if (em) {
7400 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007401 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007402 start + len - 1, 0);
7403 }
7404 em = ERR_PTR(ret);
7405 }
7406 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007407
7408 return em;
7409}
7410
Josef Bacik4b46fce2010-05-23 11:00:55 -04007411static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7412 u64 start, u64 len)
7413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007414 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007415 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007416 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417 struct btrfs_key ins;
7418 u64 alloc_hint;
7419 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007420
Josef Bacik4b46fce2010-05-23 11:00:55 -04007421 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007422 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007423 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007424 if (ret)
7425 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007426
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007427 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7428 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007429 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007430 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007431 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007432 btrfs_free_reserved_extent(fs_info, ins.objectid,
7433 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007434
Josef Bacik4b46fce2010-05-23 11:00:55 -04007435 return em;
7436}
7437
Chris Mason46bfbb52010-05-26 11:04:10 -04007438/*
7439 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7440 * block must be cow'd
7441 */
Josef Bacik00361582013-08-14 14:02:47 -04007442noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007443 u64 *orig_start, u64 *orig_block_len,
7444 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007445{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007446 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007447 struct btrfs_path *path;
7448 int ret;
7449 struct extent_buffer *leaf;
7450 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007451 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007452 struct btrfs_file_extent_item *fi;
7453 struct btrfs_key key;
7454 u64 disk_bytenr;
7455 u64 backref_offset;
7456 u64 extent_end;
7457 u64 num_bytes;
7458 int slot;
7459 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007460 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007461
Chris Mason46bfbb52010-05-26 11:04:10 -04007462 path = btrfs_alloc_path();
7463 if (!path)
7464 return -ENOMEM;
7465
David Sterbaf85b7372017-01-20 14:54:07 +01007466 ret = btrfs_lookup_file_extent(NULL, root, path,
7467 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 if (ret < 0)
7469 goto out;
7470
7471 slot = path->slots[0];
7472 if (ret == 1) {
7473 if (slot == 0) {
7474 /* can't find the item, must cow */
7475 ret = 0;
7476 goto out;
7477 }
7478 slot--;
7479 }
7480 ret = 0;
7481 leaf = path->nodes[0];
7482 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007483 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007484 key.type != BTRFS_EXTENT_DATA_KEY) {
7485 /* not our file or wrong item type, must cow */
7486 goto out;
7487 }
7488
7489 if (key.offset > offset) {
7490 /* Wrong offset, must cow */
7491 goto out;
7492 }
7493
7494 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7495 found_type = btrfs_file_extent_type(leaf, fi);
7496 if (found_type != BTRFS_FILE_EXTENT_REG &&
7497 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7498 /* not a regular extent, must cow */
7499 goto out;
7500 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007501
7502 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7503 goto out;
7504
Miao Xiee77751a2013-12-27 21:11:50 +08007505 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7506 if (extent_end <= offset)
7507 goto out;
7508
Chris Mason46bfbb52010-05-26 11:04:10 -04007509 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007510 if (disk_bytenr == 0)
7511 goto out;
7512
7513 if (btrfs_file_extent_compression(leaf, fi) ||
7514 btrfs_file_extent_encryption(leaf, fi) ||
7515 btrfs_file_extent_other_encoding(leaf, fi))
7516 goto out;
7517
Chris Mason46bfbb52010-05-26 11:04:10 -04007518 backref_offset = btrfs_file_extent_offset(leaf, fi);
7519
Josef Bacik7ee9e442013-06-21 16:37:03 -04007520 if (orig_start) {
7521 *orig_start = key.offset - backref_offset;
7522 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7523 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7524 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007525
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007526 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007527 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007528
7529 num_bytes = min(offset + *len, extent_end) - offset;
7530 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7531 u64 range_end;
7532
Jeff Mahoneyda170662016-06-15 09:22:56 -04007533 range_end = round_up(offset + num_bytes,
7534 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007535 ret = test_range_bit(io_tree, offset, range_end,
7536 EXTENT_DELALLOC, 0, NULL);
7537 if (ret) {
7538 ret = -EAGAIN;
7539 goto out;
7540 }
7541 }
7542
Josef Bacik1bda19e2013-10-18 12:10:36 -04007543 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007544
7545 /*
7546 * look for other files referencing this extent, if we
7547 * find any we must cow
7548 */
Josef Bacik00361582013-08-14 14:02:47 -04007549
Liu Boe4c3b2d2017-01-30 12:25:28 -08007550 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007551 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007552 if (ret) {
7553 ret = 0;
7554 goto out;
7555 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007556
7557 /*
7558 * adjust disk_bytenr and num_bytes to cover just the bytes
7559 * in this extent we are about to write. If there
7560 * are any csums in that range we have to cow in order
7561 * to keep the csums correct
7562 */
7563 disk_bytenr += backref_offset;
7564 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007565 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7566 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007567 /*
7568 * all of the above have passed, it is safe to overwrite this extent
7569 * without cow
7570 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007571 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007572 ret = 1;
7573out:
7574 btrfs_free_path(path);
7575 return ret;
7576}
7577
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007578bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7579{
7580 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007581 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007582 void **pagep = NULL;
7583 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007584 unsigned long start_idx;
7585 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007586
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007587 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007588
7589 /*
7590 * end is the last byte in the last page. end == start is legal
7591 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007592 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007593
7594 rcu_read_lock();
7595
7596 /* Most of the code in this while loop is lifted from
7597 * find_get_page. It's been modified to begin searching from a
7598 * page and return just the first page found in that range. If the
7599 * found idx is less than or equal to the end idx then we know that
7600 * a page exists. If no pages are found or if those pages are
7601 * outside of the range then we're fine (yay!) */
7602 while (page == NULL &&
7603 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7604 page = radix_tree_deref_slot(pagep);
7605 if (unlikely(!page))
7606 break;
7607
7608 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007609 if (radix_tree_deref_retry(page)) {
7610 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007611 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007612 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007613 /*
7614 * Otherwise, shmem/tmpfs must be storing a swap entry
7615 * here as an exceptional entry: so return it without
7616 * attempting to raise page count.
7617 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007618 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007619 break; /* TODO: Is this relevant for this use case? */
7620 }
7621
Filipe Manana91405152014-06-05 13:22:24 +01007622 if (!page_cache_get_speculative(page)) {
7623 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007624 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007625 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007626
7627 /*
7628 * Has the page moved?
7629 * This is part of the lockless pagecache protocol. See
7630 * include/linux/pagemap.h for details.
7631 */
7632 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007633 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007634 page = NULL;
7635 }
7636 }
7637
7638 if (page) {
7639 if (page->index <= end_idx)
7640 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007641 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007642 }
7643
7644 rcu_read_unlock();
7645 return found;
7646}
7647
Josef Bacikeb838e72012-07-31 16:28:48 -04007648static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7649 struct extent_state **cached_state, int writing)
7650{
7651 struct btrfs_ordered_extent *ordered;
7652 int ret = 0;
7653
7654 while (1) {
7655 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007656 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 /*
7658 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007659 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007660 * extents in this range.
7661 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007662 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 lockend - lockstart + 1);
7664
7665 /*
7666 * We need to make sure there are no buffered pages in this
7667 * range either, we could have raced between the invalidate in
7668 * generic_file_direct_write and locking the extent. The
7669 * invalidate needs to happen so that reads after a write do not
7670 * get stale data.
7671 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007672 if (!ordered &&
7673 (!writing ||
7674 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 break;
7676
7677 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7678 cached_state, GFP_NOFS);
7679
7680 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007681 /*
7682 * If we are doing a DIO read and the ordered extent we
7683 * found is for a buffered write, we can not wait for it
7684 * to complete and retry, because if we do so we can
7685 * deadlock with concurrent buffered writes on page
7686 * locks. This happens only if our DIO read covers more
7687 * than one extent map, if at this point has already
7688 * created an ordered extent for a previous extent map
7689 * and locked its range in the inode's io tree, and a
7690 * concurrent write against that previous extent map's
7691 * range and this range started (we unlock the ranges
7692 * in the io tree only when the bios complete and
7693 * buffered writes always lock pages before attempting
7694 * to lock range in the io tree).
7695 */
7696 if (writing ||
7697 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7698 btrfs_start_ordered_extent(inode, ordered, 1);
7699 else
7700 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007701 btrfs_put_ordered_extent(ordered);
7702 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007703 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007704 * We could trigger writeback for this range (and wait
7705 * for it to complete) and then invalidate the pages for
7706 * this range (through invalidate_inode_pages2_range()),
7707 * but that can lead us to a deadlock with a concurrent
7708 * call to readpages() (a buffered read or a defrag call
7709 * triggered a readahead) on a page lock due to an
7710 * ordered dio extent we created before but did not have
7711 * yet a corresponding bio submitted (whence it can not
7712 * complete), which makes readpages() wait for that
7713 * ordered extent to complete while holding a lock on
7714 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007715 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007716 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007717 }
7718
Filipe Mananaade77022016-02-18 14:28:55 +00007719 if (ret)
7720 break;
7721
Josef Bacikeb838e72012-07-31 16:28:48 -04007722 cond_resched();
7723 }
7724
7725 return ret;
7726}
7727
Liu Bo6f9994d2017-01-31 07:50:22 -08007728/* The callers of this must take lock_extent() */
7729static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7730 u64 orig_start, u64 block_start,
7731 u64 block_len, u64 orig_block_len,
7732 u64 ram_bytes, int compress_type,
7733 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007734{
7735 struct extent_map_tree *em_tree;
7736 struct extent_map *em;
7737 struct btrfs_root *root = BTRFS_I(inode)->root;
7738 int ret;
7739
Liu Bo6f9994d2017-01-31 07:50:22 -08007740 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7741 type == BTRFS_ORDERED_COMPRESSED ||
7742 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007743 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007744
Josef Bacik69ffb542012-09-11 15:40:07 -04007745 em_tree = &BTRFS_I(inode)->extent_tree;
7746 em = alloc_extent_map();
7747 if (!em)
7748 return ERR_PTR(-ENOMEM);
7749
7750 em->start = start;
7751 em->orig_start = orig_start;
7752 em->len = len;
7753 em->block_len = block_len;
7754 em->block_start = block_start;
7755 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007756 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007757 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007758 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007759 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007760 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007761 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007762 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007763 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7764 em->compress_type = compress_type;
7765 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007766
7767 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007768 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007769 em->start + em->len - 1, 0);
7770 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007771 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007772 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007773 /*
7774 * The caller has taken lock_extent(), who could race with us
7775 * to add em?
7776 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007777 } while (ret == -EEXIST);
7778
7779 if (ret) {
7780 free_extent_map(em);
7781 return ERR_PTR(ret);
7782 }
7783
Liu Bo6f9994d2017-01-31 07:50:22 -08007784 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007785 return em;
7786}
7787
Filipe Manana9c9464c2015-11-04 09:52:04 +00007788static void adjust_dio_outstanding_extents(struct inode *inode,
7789 struct btrfs_dio_data *dio_data,
7790 const u64 len)
7791{
David Sterba823bb202017-01-04 11:09:51 +01007792 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007793
Filipe Manana9c9464c2015-11-04 09:52:04 +00007794 /*
7795 * If we have an outstanding_extents count still set then we're
7796 * within our reservation, otherwise we need to adjust our inode
7797 * counter appropriately.
7798 */
Liu Boc2931662016-12-22 17:13:54 -08007799 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007800 dio_data->outstanding_extents -= num_extents;
7801 } else {
Liu Boc2931662016-12-22 17:13:54 -08007802 /*
7803 * If dio write length has been split due to no large enough
7804 * contiguous space, we need to compensate our inode counter
7805 * appropriately.
7806 */
7807 u64 num_needed = num_extents - dio_data->outstanding_extents;
7808
Filipe Manana9c9464c2015-11-04 09:52:04 +00007809 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007810 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007811 spin_unlock(&BTRFS_I(inode)->lock);
7812 }
7813}
7814
Josef Bacik4b46fce2010-05-23 11:00:55 -04007815static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7816 struct buffer_head *bh_result, int create)
7817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007818 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007819 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007820 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307821 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007822 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007825 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007826 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007827
Miao Xie172a5042013-02-21 02:48:22 -07007828 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007829 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007830 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007831 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007832
Josef Bacikc3298612012-08-03 16:49:19 -04007833 lockstart = start;
7834 lockend = start + len - 1;
7835
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007836 if (current->journal_info) {
7837 /*
7838 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007839 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007840 * confused.
7841 */
chandan50745b02015-08-28 21:10:13 +05307842 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007843 current->journal_info = NULL;
7844 }
7845
Josef Bacikeb838e72012-07-31 16:28:48 -04007846 /*
7847 * If this errors out it's because we couldn't invalidate pagecache for
7848 * this range and we need to fallback to buffered.
7849 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007850 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7851 create)) {
7852 ret = -ENOTBLK;
7853 goto err;
7854 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007855
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007856 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007857 if (IS_ERR(em)) {
7858 ret = PTR_ERR(em);
7859 goto unlock_err;
7860 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007861
7862 /*
7863 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7864 * io. INLINE is special, and we could probably kludge it in here, but
7865 * it's still buffered so for safety lets just fall back to the generic
7866 * buffered path.
7867 *
7868 * For COMPRESSED we _have_ to read the entire extent in so we can
7869 * decompress it, so there will be buffering required no matter what we
7870 * do, so go ahead and fallback to buffered.
7871 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007872 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007873 * to buffered IO. Don't blame me, this is the price we pay for using
7874 * the generic code.
7875 */
7876 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7877 em->block_start == EXTENT_MAP_INLINE) {
7878 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007879 ret = -ENOTBLK;
7880 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007881 }
7882
7883 /* Just a good old fashioned hole, return */
7884 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7885 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7886 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007887 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888 }
7889
7890 /*
7891 * We don't allocate a new extent in the following cases
7892 *
7893 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7894 * existing extent.
7895 * 2) The extent is marked as PREALLOC. We're good to go here and can
7896 * just use the extent.
7897 *
7898 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007899 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007900 len = min(len, em->len - (start - em->start));
7901 lockstart = start + len;
7902 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007903 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904
7905 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7906 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7907 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007908 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007909 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007910
7911 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7912 type = BTRFS_ORDERED_PREALLOC;
7913 else
7914 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007915 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007916 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007917
Josef Bacik00361582013-08-14 14:02:47 -04007918 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007919 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007920 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007921 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007922
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007923 em2 = btrfs_create_dio_extent(inode, start, len,
7924 orig_start, block_start,
7925 len, orig_block_len,
7926 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007927 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007928 if (type == BTRFS_ORDERED_PREALLOC) {
7929 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007930 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007931 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007932 if (em2 && IS_ERR(em2)) {
7933 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007934 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007935 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007936 /*
7937 * For inode marked NODATACOW or extent marked PREALLOC,
7938 * use the existing or preallocated extent, so does not
7939 * need to adjust btrfs_space_info's bytes_may_use.
7940 */
7941 btrfs_free_reserved_data_space_noquota(inode,
7942 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007943 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007944 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007945 }
Josef Bacik00361582013-08-14 14:02:47 -04007946
Chris Mason46bfbb52010-05-26 11:04:10 -04007947 /*
7948 * this will cow the extent, reset the len in case we changed
7949 * it above
7950 */
7951 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007952 free_extent_map(em);
7953 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007954 if (IS_ERR(em)) {
7955 ret = PTR_ERR(em);
7956 goto unlock_err;
7957 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007958 len = min(len, em->len - (start - em->start));
7959unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007960 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7961 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007962 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007963 bh_result->b_bdev = em->bdev;
7964 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007965 if (create) {
7966 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7967 set_buffer_new(bh_result);
7968
7969 /*
7970 * Need to update the i_size under the extent lock so buffered
7971 * readers will get the updated i_size when we unlock.
7972 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007973 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007974 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007975
Filipe Manana9c9464c2015-11-04 09:52:04 +00007976 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307977 WARN_ON(dio_data->reserve < len);
7978 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007979 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307980 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007981 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007982
Josef Bacikeb838e72012-07-31 16:28:48 -04007983 /*
7984 * In the case of write we need to clear and unlock the entire range,
7985 * in the case of read we need to unlock only the end area that we
7986 * aren't using if there is any left over space.
7987 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007988 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007989 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7990 lockend, unlock_bits, 1, 0,
7991 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007992 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007993 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007994 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007995
Josef Bacik4b46fce2010-05-23 11:00:55 -04007996 free_extent_map(em);
7997
7998 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007999
8000unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04008001 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
8002 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00008003err:
chandan50745b02015-08-28 21:10:13 +05308004 if (dio_data)
8005 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00008006 /*
8007 * Compensate the delalloc release we do in btrfs_direct_IO() when we
8008 * write less data then expected, so that we don't underflow our inode's
8009 * outstanding extents counter.
8010 */
8011 if (create && dio_data)
8012 adjust_dio_outstanding_extents(inode, dio_data, len);
8013
Josef Bacikeb838e72012-07-31 16:28:48 -04008014 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008015}
8016
Miao Xie8b110e32014-09-12 18:44:03 +08008017static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05008018 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08008019{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008020 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08008021 int ret;
8022
Mike Christie37226b22016-06-05 14:31:52 -05008023 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008024
8025 bio_get(bio);
8026
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008027 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08008028 if (ret)
8029 goto err;
8030
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008031 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008032err:
8033 bio_put(bio);
8034 return ret;
8035}
8036
8037static int btrfs_check_dio_repairable(struct inode *inode,
8038 struct bio *failed_bio,
8039 struct io_failure_record *failrec,
8040 int failed_mirror)
8041{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08008043 int num_copies;
8044
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008045 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08008046 if (num_copies == 1) {
8047 /*
8048 * we only have a single copy of the data, so don't bother with
8049 * all the retry and error correction code that follows. no
8050 * matter what the error is, it is very likely to persist.
8051 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008052 btrfs_debug(fs_info,
8053 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
8054 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008055 return 0;
8056 }
8057
8058 failrec->failed_mirror = failed_mirror;
8059 failrec->this_mirror++;
8060 if (failrec->this_mirror == failed_mirror)
8061 failrec->this_mirror++;
8062
8063 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008064 btrfs_debug(fs_info,
8065 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8066 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008067 return 0;
8068 }
8069
8070 return 1;
8071}
8072
8073static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308074 struct page *page, unsigned int pgoff,
8075 u64 start, u64 end, int failed_mirror,
8076 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008077{
8078 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008079 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8080 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008081 struct bio *bio;
8082 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008083 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008084 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008085 int ret;
8086
Mike Christie37226b22016-06-05 14:31:52 -05008087 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008088
8089 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8090 if (ret)
8091 return ret;
8092
8093 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8094 failed_mirror);
8095 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008096 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008097 return -EIO;
8098 }
8099
Liu Bo17347ce2017-05-15 15:33:27 -07008100 segs = bio_segments(failed_bio);
8101 if (segs > 1 ||
8102 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008103 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008104
8105 isector = start - btrfs_io_bio(failed_bio)->logical;
8106 isector >>= inode->i_sb->s_blocksize_bits;
8107 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308108 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008109 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008110
8111 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008112 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008113 read_mode, failrec->this_mirror, failrec->in_validation);
8114
Mike Christie81a75f672016-06-05 14:31:54 -05008115 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008116 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008117 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008118 bio_put(bio);
8119 }
8120
8121 return ret;
8122}
8123
8124struct btrfs_retry_complete {
8125 struct completion done;
8126 struct inode *inode;
8127 u64 start;
8128 int uptodate;
8129};
8130
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008131static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008132{
8133 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008134 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008135 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008136 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008137 int i;
8138
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008139 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008140 goto end;
8141
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308142 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008143 io_tree = &BTRFS_I(inode)->io_tree;
8144 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8145 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308146
Miao Xie8b110e32014-09-12 18:44:03 +08008147 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008148 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008149 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008150 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8151 io_tree, done->start, bvec->bv_page,
8152 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008153end:
8154 complete(&done->done);
8155 bio_put(bio);
8156}
8157
8158static int __btrfs_correct_data_nocsum(struct inode *inode,
8159 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008160{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308161 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008162 struct bio_vec bvec;
8163 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008164 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308166 unsigned int pgoff;
8167 u32 sectorsize;
8168 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008169 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008170 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008171
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008173 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308174
Miao Xiec1dc0892014-09-12 18:43:56 +08008175 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008176 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008177 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008178
Liu Bo17347ce2017-05-15 15:33:27 -07008179 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8180 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8181 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308182
8183next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008184 done.uptodate = 0;
8185 done.start = start;
8186 init_completion(&done.done);
8187
Liu Bo17347ce2017-05-15 15:33:27 -07008188 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308189 pgoff, start, start + sectorsize - 1,
8190 io_bio->mirror_num,
8191 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008192 if (ret) {
8193 err = ret;
8194 goto next;
8195 }
Miao Xie8b110e32014-09-12 18:44:03 +08008196
David Sterba9c17f6c2017-07-19 19:26:45 +02008197 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008198
8199 if (!done.uptodate) {
8200 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308201 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008202 }
8203
Liu Bo629ebf42017-05-15 17:20:07 -07008204next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308205 start += sectorsize;
8206
Liu Bo97bf5a52017-04-07 13:11:10 -07008207 nr_sectors--;
8208 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308209 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008210 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308211 goto next_block_or_try_again;
8212 }
Miao Xie8b110e32014-09-12 18:44:03 +08008213 }
8214
Liu Bo629ebf42017-05-15 17:20:07 -07008215 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008216}
8217
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008218static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008219{
8220 struct btrfs_retry_complete *done = bio->bi_private;
8221 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008222 struct extent_io_tree *io_tree, *failure_tree;
8223 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008224 struct bio_vec *bvec;
8225 int uptodate;
8226 int ret;
8227 int i;
8228
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008229 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008230 goto end;
8231
8232 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308233
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308234 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008235 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308236
Josef Bacik7870d082017-05-05 11:57:15 -04008237 io_tree = &BTRFS_I(inode)->io_tree;
8238 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8239
David Sterbac09abff2017-07-13 18:10:07 +02008240 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008241 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008242 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8243 bvec->bv_offset, done->start,
8244 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008245 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008246 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8247 failure_tree, io_tree, done->start,
8248 bvec->bv_page,
8249 btrfs_ino(BTRFS_I(inode)),
8250 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008251 else
8252 uptodate = 0;
8253 }
8254
8255 done->uptodate = uptodate;
8256end:
8257 complete(&done->done);
8258 bio_put(bio);
8259}
8260
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008261static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8262 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008263{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308264 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008265 struct bio_vec bvec;
8266 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008267 struct btrfs_retry_complete done;
8268 u64 start;
8269 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308270 u32 sectorsize;
8271 int nr_sectors;
8272 unsigned int pgoff;
8273 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008274 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008275 int ret;
8276
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308277 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008278 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308279
Miao Xie8b110e32014-09-12 18:44:03 +08008280 err = 0;
8281 start = io_bio->logical;
8282 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008283 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008284
Liu Bo17347ce2017-05-15 15:33:27 -07008285 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8286 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308287
Liu Bo17347ce2017-05-15 15:33:27 -07008288 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308289next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008290 if (uptodate) {
8291 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8292 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8293 bvec.bv_page, pgoff, start, sectorsize);
8294 if (likely(!ret))
8295 goto next;
8296 }
Miao Xie8b110e32014-09-12 18:44:03 +08008297try_again:
8298 done.uptodate = 0;
8299 done.start = start;
8300 init_completion(&done.done);
8301
Liu Bo17347ce2017-05-15 15:33:27 -07008302 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308303 pgoff, start, start + sectorsize - 1,
8304 io_bio->mirror_num,
8305 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008306 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307 err = errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008308 goto next;
8309 }
8310
David Sterba9c17f6c2017-07-19 19:26:45 +02008311 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008312
8313 if (!done.uptodate) {
8314 /* We might have another mirror, so try again */
8315 goto try_again;
8316 }
8317next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308318 offset += sectorsize;
8319 start += sectorsize;
8320
8321 ASSERT(nr_sectors);
8322
Liu Bo97bf5a52017-04-07 13:11:10 -07008323 nr_sectors--;
8324 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308325 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008326 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308327 goto next_block;
8328 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008329 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008330
8331 return err;
8332}
8333
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008334static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8335 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008336{
8337 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8338
8339 if (skip_csum) {
8340 if (unlikely(err))
8341 return __btrfs_correct_data_nocsum(inode, io_bio);
8342 else
8343 return 0;
8344 } else {
8345 return __btrfs_subio_endio_read(inode, io_bio, err);
8346 }
8347}
8348
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008349static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008350{
8351 struct btrfs_dio_private *dip = bio->bi_private;
8352 struct inode *inode = dip->inode;
8353 struct bio *dio_bio;
8354 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008355 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008356
Liu Boe3d37fa2017-05-17 15:42:00 -06008357 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) {
Miao Xie8b110e32014-09-12 18:44:03 +08008358 err = btrfs_subio_endio_read(inode, io_bio, err);
Liu Boe3d37fa2017-05-17 15:42:00 -06008359 if (!err)
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008360 bio->bi_status = 0;
Liu Boe3d37fa2017-05-17 15:42:00 -06008361 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008362
Josef Bacik4b46fce2010-05-23 11:00:55 -04008363 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008364 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008365 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008366
Josef Bacik4b46fce2010-05-23 11:00:55 -04008367 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008368
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008369 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008370 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008371
8372 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008373 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008374 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008375}
8376
Qu Wenruo524272602017-03-08 10:25:52 +08008377static void __endio_write_update_ordered(struct inode *inode,
8378 const u64 offset, const u64 bytes,
8379 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008380{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008381 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008382 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008383 struct btrfs_workqueue *wq;
8384 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008385 u64 ordered_offset = offset;
8386 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008387 int ret;
8388
Qu Wenruo524272602017-03-08 10:25:52 +08008389 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8390 wq = fs_info->endio_freespace_worker;
8391 func = btrfs_freespace_write_helper;
8392 } else {
8393 wq = fs_info->endio_write_workers;
8394 func = btrfs_endio_write_helper;
8395 }
8396
Chris Mason163cf092010-11-28 19:56:33 -05008397again:
8398 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8399 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008400 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008401 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008402 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008403 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008404
Qu Wenruo524272602017-03-08 10:25:52 +08008405 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8406 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008407out_test:
8408 /*
8409 * our bio might span multiple ordered extents. If we haven't
8410 * completed the accounting for the whole dio, go back and try again
8411 */
Filipe Manana14543772015-11-24 16:23:54 +00008412 if (ordered_offset < offset + bytes) {
8413 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008414 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008415 goto again;
8416 }
Filipe Manana14543772015-11-24 16:23:54 +00008417}
8418
8419static void btrfs_endio_direct_write(struct bio *bio)
8420{
8421 struct btrfs_dio_private *dip = bio->bi_private;
8422 struct bio *dio_bio = dip->dio_bio;
8423
Qu Wenruo524272602017-03-08 10:25:52 +08008424 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008425 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008426
Josef Bacik4b46fce2010-05-23 11:00:55 -04008427 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008428
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008429 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008430 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008431 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008432}
8433
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008434static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008435 struct bio *bio, int mirror_num,
8436 unsigned long bio_flags, u64 offset)
8437{
Josef Bacikc6100a42017-05-05 11:57:13 -04008438 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008439 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008440 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008441 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008442 return 0;
8443}
8444
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008445static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008446{
8447 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008448 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008449
Miao Xie8b110e32014-09-12 18:44:03 +08008450 if (err)
8451 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008452 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008453 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8454 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008455 (unsigned long long)bio->bi_iter.bi_sector,
8456 bio->bi_iter.bi_size, err);
8457
8458 if (dip->subio_endio)
8459 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008460
8461 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008462 dip->errors = 1;
8463
8464 /*
8465 * before atomic variable goto zero, we must make sure
8466 * dip->errors is perceived to be set.
8467 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008468 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008469 }
8470
8471 /* if there are more bios still pending for this dio, just exit */
8472 if (!atomic_dec_and_test(&dip->pending_bios))
8473 goto out;
8474
Chris Mason9be33952013-05-17 18:30:14 -04008475 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008476 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008477 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008478 dip->dio_bio->bi_status = 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008479 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008480 }
8481out:
8482 bio_put(bio);
8483}
8484
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008485static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008486 struct btrfs_dio_private *dip,
8487 struct bio *bio,
8488 u64 file_offset)
8489{
8490 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8491 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008492 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008493
8494 /*
8495 * We load all the csum data we need when we submit
8496 * the first bio to reduce the csum tree search and
8497 * contention.
8498 */
8499 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008500 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008501 file_offset);
8502 if (ret)
8503 return ret;
8504 }
8505
8506 if (bio == dip->orig_bio)
8507 return 0;
8508
8509 file_offset -= dip->logical_offset;
8510 file_offset >>= inode->i_sb->s_blocksize_bits;
8511 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8512
8513 return 0;
8514}
8515
Miao Xiee65e1532010-11-22 03:04:43 +00008516static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008517 u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008518{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008519 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008520 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008521 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008522 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008523
Josef Bacikb812ce22012-11-16 13:56:32 -05008524 if (async_submit)
8525 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8526
Miao Xiee65e1532010-11-22 03:04:43 +00008527 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008528
8529 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008530 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008531 if (ret)
8532 goto err;
8533 }
Miao Xiee65e1532010-11-22 03:04:43 +00008534
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008535 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008536 goto map;
8537
8538 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008539 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8540 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008541 __btrfs_submit_bio_start_direct_io,
8542 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008543 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008544 } else if (write) {
8545 /*
8546 * If we aren't doing async submit, calculate the csum of the
8547 * bio now.
8548 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008549 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008550 if (ret)
8551 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008552 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008553 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008554 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008555 if (ret)
8556 goto err;
8557 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008558map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008559 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008560err:
8561 bio_put(bio);
8562 return ret;
8563}
8564
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008565static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008566{
8567 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008568 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008569 struct bio *bio;
8570 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008571 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008572 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008573 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008574 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008575 u64 submit_len;
8576 int clone_offset = 0;
8577 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308578 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008579
Kent Overstreet4f024f32013-10-11 15:44:27 -07008580 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008581 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008582 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8583 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008584 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008585 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008586
Liu Bo725130b2017-05-16 09:51:39 -07008587 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008588 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008589 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008590 goto submit;
8591 }
8592
David Woodhouse53b381b2013-01-29 18:40:14 -05008593 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008594 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008595 async_submit = 0;
8596 else
8597 async_submit = 1;
8598
Liu Bo725130b2017-05-16 09:51:39 -07008599 /* bio split */
8600 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008601 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008602 do {
Liu Bo725130b2017-05-16 09:51:39 -07008603 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008604
Liu Bo725130b2017-05-16 09:51:39 -07008605 /*
8606 * This will never fail as it's passing GPF_NOFS and
8607 * the allocation is backed by btrfs_bioset.
8608 */
Liu Boe4770942017-05-16 10:57:14 -07008609 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008610 clone_len);
8611 bio->bi_private = dip;
8612 bio->bi_end_io = btrfs_end_dio_bio;
8613 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008614
Liu Bo725130b2017-05-16 09:51:39 -07008615 ASSERT(submit_len >= clone_len);
8616 submit_len -= clone_len;
8617 if (submit_len == 0)
8618 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008619
Liu Bo725130b2017-05-16 09:51:39 -07008620 /*
8621 * Increase the count before we submit the bio so we know
8622 * the end IO handler won't happen before we increase the
8623 * count. Otherwise, the dip might get freed before we're
8624 * done setting it up.
8625 */
8626 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008627
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008628 ret = __btrfs_submit_dio_bio(bio, inode, file_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008629 async_submit);
8630 if (ret) {
8631 bio_put(bio);
8632 atomic_dec(&dip->pending_bios);
8633 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008634 }
Liu Bo725130b2017-05-16 09:51:39 -07008635
8636 clone_offset += clone_len;
8637 start_sector += clone_len >> 9;
8638 file_offset += clone_len;
8639
8640 map_length = submit_len;
8641 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8642 start_sector << 9, &map_length, NULL, 0);
8643 if (ret)
8644 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008645 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008646
Josef Bacik02f57c72011-04-06 14:25:44 -04008647submit:
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008648 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008649 if (!ret)
8650 return 0;
8651
8652 bio_put(bio);
8653out_err:
8654 dip->errors = 1;
8655 /*
8656 * before atomic variable goto zero, we must
8657 * make sure dip->errors is perceived to be set.
8658 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008659 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008660 if (atomic_dec_and_test(&dip->pending_bios))
8661 bio_io_error(dip->orig_bio);
8662
8663 /* bio_end_io() will handle error, so we needn't return it */
8664 return 0;
8665}
8666
Mike Christie8a4c1e42016-06-05 14:31:50 -05008667static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8668 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008669{
Filipe Manana61de7182015-07-01 12:13:10 +01008670 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008671 struct bio *bio = NULL;
8672 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008673 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008674 int ret = 0;
8675
David Sterba8b6c1d52017-06-02 17:48:13 +02008676 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008677
Miao Xiec1dc0892014-09-12 18:43:56 +08008678 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008679 if (!dip) {
8680 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008681 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008682 }
8683
8684 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008685 dip->inode = inode;
8686 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008687 dip->bytes = dio_bio->bi_iter.bi_size;
8688 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008689 bio->bi_private = dip;
8690 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008691 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008692 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008693 io_bio = btrfs_io_bio(bio);
8694 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008695
Miao Xiec1dc0892014-09-12 18:43:56 +08008696 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008697 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008698 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008699 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008700 dip->subio_endio = btrfs_subio_endio_read;
8701 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008702
Filipe Mananaf28a4922015-12-08 19:23:20 +00008703 /*
8704 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8705 * even if we fail to submit a bio, because in such case we do the
8706 * corresponding error handling below and it must not be done a second
8707 * time by btrfs_direct_IO().
8708 */
8709 if (write) {
8710 struct btrfs_dio_data *dio_data = current->journal_info;
8711
8712 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8713 dip->bytes;
8714 dio_data->unsubmitted_oe_range_start =
8715 dio_data->unsubmitted_oe_range_end;
8716 }
8717
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008718 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008719 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008720 return;
Chris Mason9be33952013-05-17 18:30:14 -04008721
Liu Bo3892ac92017-04-17 15:00:28 -07008722 if (io_bio->end_io)
8723 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008724
Josef Bacik4b46fce2010-05-23 11:00:55 -04008725free_ordered:
8726 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008727 * If we arrived here it means either we failed to submit the dip
8728 * or we either failed to clone the dio_bio or failed to allocate the
8729 * dip. If we cloned the dio_bio and allocated the dip, we can just
8730 * call bio_endio against our io_bio so that we get proper resource
8731 * cleanup if we fail to submit the dip, otherwise, we must do the
8732 * same as btrfs_endio_direct_[write|read] because we can't call these
8733 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008734 */
Liu Bo3892ac92017-04-17 15:00:28 -07008735 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008736 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008737 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008738 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008739 * and all the cleanup and final put for dio_bio (through
8740 * dio_end_io()).
8741 */
8742 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008743 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008744 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008745 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008746 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008747 file_offset,
8748 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008749 false);
Filipe Manana14543772015-11-24 16:23:54 +00008750 else
Filipe Manana61de7182015-07-01 12:13:10 +01008751 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8752 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008753
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008754 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008755 /*
8756 * Releases and cleans up our dio_bio, no need to bio_put()
8757 * nor bio_endio()/bio_io_error() against dio_bio.
8758 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008759 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008760 }
Liu Bo3892ac92017-04-17 15:00:28 -07008761 if (bio)
8762 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008763 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008764}
8765
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008766static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8767 struct kiocb *iocb,
8768 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008769{
8770 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008771 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008772 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008773 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008774
8775 if (offset & blocksize_mask)
8776 goto out;
8777
Al Viro28060d52014-03-22 05:15:17 -04008778 if (iov_iter_alignment(iter) & blocksize_mask)
8779 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008780
Al Viro28060d52014-03-22 05:15:17 -04008781 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008782 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008783 return 0;
8784 /*
8785 * Check to make sure we don't have duplicate iov_base's in this
8786 * iovec, if so return EINVAL, otherwise we'll get csum errors
8787 * when reading back.
8788 */
8789 for (seg = 0; seg < iter->nr_segs; seg++) {
8790 for (i = seg + 1; i < iter->nr_segs; i++) {
8791 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008792 goto out;
8793 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008794 }
8795 retval = 0;
8796out:
8797 return retval;
8798}
Josef Bacikeb838e72012-07-31 16:28:48 -04008799
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008800static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008801{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008802 struct file *file = iocb->ki_filp;
8803 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008804 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308805 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008806 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008807 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008808 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008809 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008810 bool wakeup = true;
8811 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008812 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008813
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008814 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008815 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008816
Jens Axboefe0f07d2015-04-15 17:05:48 -06008817 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008818 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008819
Josef Bacik0e267c42013-07-02 10:38:02 -04008820 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008821 * The generic stuff only does filemap_write_and_wait_range, which
8822 * isn't enough if we've written compressed pages to this area, so
8823 * we need to flush the dirty pages again to make absolutely sure
8824 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008825 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008826 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008827 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8828 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008829 filemap_fdatawrite_range(inode->i_mapping, offset,
8830 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008831
Omar Sandoval6f673762015-03-16 04:33:52 -07008832 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008833 /*
8834 * If the write DIO is beyond the EOF, we need update
8835 * the isize, but it is protected by i_mutex. So we can
8836 * not unlock the i_mutex at this case.
8837 */
8838 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008839 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008840 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008841 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008842 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8843 ret = -EAGAIN;
8844 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008845 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008846 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8847 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008848 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008849 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008850 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008851
8852 /*
8853 * We need to know how many extents we reserved so that we can
8854 * do the accounting properly if we go over the number we
8855 * originally calculated. Abuse current->journal_info for this.
8856 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008857 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008858 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008859 dio_data.unsubmitted_oe_range_start = (u64)offset;
8860 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308861 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308862 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008863 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8864 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008865 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008866 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8867 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008868 }
8869
Omar Sandoval17f8c842015-03-16 04:33:50 -07008870 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008871 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008872 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008873 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008874 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308875 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008876 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008877 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308878 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008879 btrfs_delalloc_release_space(inode, data_reserved,
8880 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008881 /*
8882 * On error we might have left some ordered extents
8883 * without submitting corresponding bios for them, so
8884 * cleanup them up to avoid other tasks getting them
8885 * and waiting for them to complete forever.
8886 */
8887 if (dio_data.unsubmitted_oe_range_start <
8888 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008889 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008890 dio_data.unsubmitted_oe_range_start,
8891 dio_data.unsubmitted_oe_range_end -
8892 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008893 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008894 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008895 btrfs_delalloc_release_space(inode, data_reserved,
8896 offset, count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008897 }
Miao Xie38851cc2013-02-08 07:04:11 +00008898out:
Miao Xie2e60a512013-02-08 07:01:08 +00008899 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008900 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008901 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008902 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008903
Qu Wenruo364ecf32017-02-27 15:10:38 +08008904 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008905 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008906}
8907
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008908#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8909
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008910static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8911 __u64 start, __u64 len)
8912{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008913 int ret;
8914
8915 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8916 if (ret)
8917 return ret;
8918
Chris Masonec29ed52011-02-23 16:23:20 -05008919 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008920}
8921
Chris Mason9ebefb182007-06-15 13:50:00 -04008922int btrfs_readpage(struct file *file, struct page *page)
8923{
Chris Masond1310b22008-01-24 16:13:08 -05008924 struct extent_io_tree *tree;
8925 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008926 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008927}
Chris Mason1832a6d2007-12-21 16:27:21 -05008928
Chris Mason39279cc2007-06-12 06:35:45 -04008929static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8930{
Chris Masond1310b22008-01-24 16:13:08 -05008931 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008932 struct inode *inode = page->mapping->host;
8933 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008934
8935 if (current->flags & PF_MEMALLOC) {
8936 redirty_page_for_writepage(wbc, page);
8937 unlock_page(page);
8938 return 0;
8939 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008940
8941 /*
8942 * If we are under memory pressure we will call this directly from the
8943 * VM, we need to make sure we have the inode referenced for the ordered
8944 * extent. If not just return like we didn't do anything.
8945 */
8946 if (!igrab(inode)) {
8947 redirty_page_for_writepage(wbc, page);
8948 return AOP_WRITEPAGE_ACTIVATE;
8949 }
Chris Masond1310b22008-01-24 16:13:08 -05008950 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008951 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8952 btrfs_add_delayed_iput(inode);
8953 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008954}
Chris Mason39279cc2007-06-12 06:35:45 -04008955
Eric Sandeen48a3b632013-04-25 20:41:01 +00008956static int btrfs_writepages(struct address_space *mapping,
8957 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008958{
Chris Masond1310b22008-01-24 16:13:08 -05008959 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008960
Chris Masond1310b22008-01-24 16:13:08 -05008961 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008962 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8963}
8964
Chris Mason3ab2fb52007-11-08 10:59:22 -05008965static int
8966btrfs_readpages(struct file *file, struct address_space *mapping,
8967 struct list_head *pages, unsigned nr_pages)
8968{
Chris Masond1310b22008-01-24 16:13:08 -05008969 struct extent_io_tree *tree;
8970 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008971 return extent_readpages(tree, mapping, pages, nr_pages,
8972 btrfs_get_extent);
8973}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008975{
Chris Masond1310b22008-01-24 16:13:08 -05008976 struct extent_io_tree *tree;
8977 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008978 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008979
Chris Masond1310b22008-01-24 16:13:08 -05008980 tree = &BTRFS_I(page->mapping->host)->io_tree;
8981 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008982 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008983 if (ret == 1) {
8984 ClearPagePrivate(page);
8985 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008986 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008987 }
8988 return ret;
8989}
Chris Mason39279cc2007-06-12 06:35:45 -04008990
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8992{
Chris Mason98509cf2008-09-11 15:51:43 -04008993 if (PageWriteback(page) || PageDirty(page))
8994 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008995 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008996}
8997
Lukas Czernerd47992f2013-05-21 23:17:23 -04008998static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8999 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04009000{
Josef Bacik5fd02042012-05-02 14:00:54 -04009001 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05009002 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009004 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009005 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009006 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309007 u64 start;
9008 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009009 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04009010
Chris Mason8b62b722009-09-02 16:53:46 -04009011 /*
9012 * we have the page locked, so new writeback can't start,
9013 * and the dirty bit won't be cleared while we are here.
9014 *
9015 * Wait for IO on this page so that we can safely clear
9016 * the PagePrivate2 bit and do ordered accounting
9017 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04009019
Josef Bacik5fd02042012-05-02 14:00:54 -04009020 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009021 if (offset) {
9022 btrfs_releasepage(page, GFP_NOFS);
9023 return;
9024 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009025
9026 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01009027 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309028again:
9029 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009030 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309031 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309033 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04009034 /*
9035 * IO on this page will never be started, so we need
9036 * to account for any ordered extents now
9037 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009038 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309039 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009040 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009041 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009042 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
9043 EXTENT_DEFRAG, 1, 0, &cached_state,
9044 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04009045 /*
9046 * whoever cleared the private bit is responsible
9047 * for the finish_ordered_io
9048 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04009049 if (TestClearPagePrivate2(page)) {
9050 struct btrfs_ordered_inode_tree *tree;
9051 u64 new_len;
9052
9053 tree = &BTRFS_I(inode)->ordered_tree;
9054
9055 spin_lock_irq(&tree->lock);
9056 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309057 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04009058 if (new_len < ordered->truncated_len)
9059 ordered->truncated_len = new_len;
9060 spin_unlock_irq(&tree->lock);
9061
9062 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309063 start,
9064 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009065 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009066 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009067 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009068 if (!inode_evicting) {
9069 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309070 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009071 &cached_state);
9072 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309073
9074 start = end + 1;
9075 if (start < page_end)
9076 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009077 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009078
Qu Wenruob9d0b382015-09-29 10:35:16 +08009079 /*
9080 * Qgroup reserved space handler
9081 * Page here will be either
9082 * 1) Already written to disk
9083 * In this case, its reserved space is released from data rsv map
9084 * and will be freed by delayed_ref handler finally.
9085 * So even we call qgroup_free_data(), it won't decrease reserved
9086 * space.
9087 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009088 * This means the reserved space should be freed here. However,
9089 * if a truncate invalidates the page (by clearing PageDirty)
9090 * and the page is accounted for while allocating extent
9091 * in btrfs_check_data_free_space() we let delayed_ref to
9092 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009093 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009094 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009095 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009096 if (!inode_evicting) {
9097 clear_extent_bit(tree, page_start, page_end,
9098 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009099 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9100 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009101 &cached_state, GFP_NOFS);
9102
9103 __btrfs_releasepage(page, GFP_NOFS);
9104 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009105
Chris Mason4a096752008-07-21 10:29:44 -04009106 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009107 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009108 ClearPagePrivate(page);
9109 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009110 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009111 }
Chris Mason39279cc2007-06-12 06:35:45 -04009112}
9113
Chris Mason9ebefb182007-06-15 13:50:00 -04009114/*
9115 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9116 * called from a page fault handler when a page is first dirtied. Hence we must
9117 * be careful to check for EOF conditions here. We set the page up correctly
9118 * for a written page which means we get ENOSPC checking when writing into
9119 * holes and correct delalloc and unwritten extent mapping on filesystems that
9120 * support these features.
9121 *
9122 * We are not allowed to take the i_mutex here so we have to play games to
9123 * protect against truncate races as the page could now be beyond EOF. Because
9124 * vmtruncate() writes the inode size before removing pages, once we have the
9125 * page lock we can determine safely if the page is beyond EOF. If it is not
9126 * beyond EOF, then the page is guaranteed safe against truncation until we
9127 * unlock the page.
9128 */
Dave Jiang11bac802017-02-24 14:56:41 -08009129int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009130{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009131 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009132 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009133 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009134 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9135 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009136 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009137 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009138 char *kaddr;
9139 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009140 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009141 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009142 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309143 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009144 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009145 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309146 u64 end;
9147
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009148 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009149
Jan Karab2b5ef52012-06-12 16:20:45 +02009150 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009151 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009152 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309153 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009154
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309155 /*
9156 * Reserving delalloc space after obtaining the page lock can lead to
9157 * deadlock. For example, if a dirty page is locked by this function
9158 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9159 * dirty page write out, then the btrfs_writepage() function could
9160 * end up waiting indefinitely to get a lock on the page currently
9161 * being processed by btrfs_page_mkwrite() function.
9162 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009163 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309164 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009165 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009166 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009167 reserved = 1;
9168 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009169 if (ret) {
9170 if (ret == -ENOMEM)
9171 ret = VM_FAULT_OOM;
9172 else /* -ENOSPC, -EIO, etc */
9173 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009174 if (reserved)
9175 goto out;
9176 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009177 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009178
Nick Piggin56a76f82009-03-31 15:23:23 -07009179 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009180again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009181 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009182 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009183
Chris Mason9ebefb182007-06-15 13:50:00 -04009184 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009185 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009186 /* page got truncated out from underneath us */
9187 goto out_unlock;
9188 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009189 wait_on_page_writeback(page);
9190
David Sterbaff13db42015-12-03 14:30:40 +01009191 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009192 set_page_extent_mapped(page);
9193
Chris Masoneb84ae02008-07-17 13:53:27 -04009194 /*
9195 * we can't set the delalloc bits if there are pending ordered
9196 * extents. Drop our locks and wait for them to finish
9197 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009198 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9199 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009200 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009201 unlock_extent_cached(io_tree, page_start, page_end,
9202 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009203 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009204 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009205 btrfs_put_ordered_extent(ordered);
9206 goto again;
9207 }
9208
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009209 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009210 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009211 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009212 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309213 end = page_start + reserved_space - 1;
9214 spin_lock(&BTRFS_I(inode)->lock);
9215 BTRFS_I(inode)->outstanding_extents++;
9216 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009217 btrfs_delalloc_release_space(inode, data_reserved,
9218 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309219 }
9220 }
9221
Josef Bacikfbf19082009-10-01 17:10:23 -04009222 /*
Liu Bo54160342016-12-13 12:15:19 -08009223 * page_mkwrite gets called when the page is firstly dirtied after it's
9224 * faulted in, but write(2) could also dirty a page and set delalloc
9225 * bits, thus in this case for space account reason, we still need to
9226 * clear any delalloc bits within this page range since we have to
9227 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009228 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309229 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009230 EXTENT_DIRTY | EXTENT_DELALLOC |
9231 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009232 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009233
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309234 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009235 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009236 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009237 unlock_extent_cached(io_tree, page_start, page_end,
9238 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009239 ret = VM_FAULT_SIGBUS;
9240 goto out_unlock;
9241 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009242 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009243
9244 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009245 if (page_start + PAGE_SIZE > size)
9246 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009247 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009248 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009249
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009250 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009251 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009252 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009253 flush_dcache_page(page);
9254 kunmap(page);
9255 }
Chris Mason247e7432008-07-17 12:53:51 -04009256 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009257 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009258 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009260 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009261 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009262 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009263
Josef Bacik2ac55d42010-02-03 19:33:23 +00009264 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009265
9266out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009267 if (!ret) {
9268 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009269 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009270 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009271 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009272 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009273out:
Qu Wenruobc42bda2017-02-27 15:10:39 +08009274 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9275 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009276out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009277 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009278 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009279 return ret;
9280}
9281
Josef Bacika41ad392011-01-31 15:30:16 -05009282static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009283{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009284 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009285 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009286 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009287 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009288 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009289 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009290 u64 mask = fs_info->sectorsize - 1;
9291 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009292
Josef Bacik0ef8b722013-10-25 16:13:35 -04009293 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9294 (u64)-1);
9295 if (ret)
9296 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009297
Josef Bacikfcb80c22011-05-03 10:40:22 -04009298 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009299 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009300 * 3 things going on here
9301 *
9302 * 1) We need to reserve space for our orphan item and the space to
9303 * delete our orphan item. Lord knows we don't want to have a dangling
9304 * orphan item because we didn't reserve space to remove it.
9305 *
9306 * 2) We need to reserve space to update our inode.
9307 *
9308 * 3) We need to have something to cache all the space that is going to
9309 * be free'd up by the truncate operation, but also have some slack
9310 * space reserved in case it uses space during the truncate (thank you
9311 * very much snapshotting).
9312 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009313 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009314 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009315 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009316 * doesn't end up using space reserved for updating the inode or
9317 * removing the orphan item. We also need to be able to stop the
9318 * transaction and start a new one, which means we need to be able to
9319 * update the inode several times, and we have no idea of knowing how
9320 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009321 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009322 * Then there is the orphan item, which does indeed need to be held on
9323 * to for the whole operation, and we need nobody to touch this reserved
9324 * space except the orphan code.
9325 *
9326 * So that leaves us with
9327 *
9328 * 1) root->orphan_block_rsv - for the orphan deletion.
9329 * 2) rsv - for the truncate reservation, which we will steal from the
9330 * transaction reservation.
9331 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9332 * updating the inode.
9333 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009334 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009335 if (!rsv)
9336 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009337 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009338 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009339
Josef Bacik907cbce2011-08-08 13:46:15 -04009340 /*
Josef Bacik07127182011-08-19 10:29:59 -04009341 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009342 * 1 for updating the inode.
9343 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009344 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009345 if (IS_ERR(trans)) {
9346 err = PTR_ERR(trans);
9347 goto out;
9348 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009349
Josef Bacik907cbce2011-08-08 13:46:15 -04009350 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009351 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009352 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009353 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009354
Chris Mason5a3f23d2009-03-31 13:27:11 -04009355 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009356 * So if we truncate and then write and fsync we normally would just
9357 * write the extents that changed, which is a problem if we need to
9358 * first truncate that entire inode. So set this flag so we write out
9359 * all of the extents in the inode to the sync log so we're completely
9360 * safe.
9361 */
9362 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009363 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009364
Yan, Zheng80825102009-11-12 09:35:36 +00009365 while (1) {
9366 ret = btrfs_truncate_inode_items(trans, root, inode,
9367 inode->i_size,
9368 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009369 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009370 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009371 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009372 }
Chris Mason39279cc2007-06-12 06:35:45 -04009373
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009374 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009375 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009376 if (ret) {
9377 err = ret;
9378 break;
9379 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009380
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009381 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009382 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009383
9384 trans = btrfs_start_transaction(root, 2);
9385 if (IS_ERR(trans)) {
9386 ret = err = PTR_ERR(trans);
9387 trans = NULL;
9388 break;
9389 }
9390
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009391 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009392 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009393 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009394 BUG_ON(ret); /* shouldn't happen */
9395 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009396 }
9397
9398 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009399 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009400 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009401 if (ret)
9402 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009403 }
9404
Chris Mason917c16b2011-11-08 14:49:59 -05009405 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009406 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009407 ret = btrfs_update_inode(trans, root, inode);
9408 if (ret && !err)
9409 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009410
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009411 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009412 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009413 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009414out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009415 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009416
Josef Bacik3893e332011-01-31 16:03:11 -05009417 if (ret && !err)
9418 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009419
Josef Bacik3893e332011-01-31 16:03:11 -05009420 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009421}
9422
Sven Wegener3b963622008-06-09 21:57:42 -04009423/*
Chris Masond352ac62008-09-29 15:18:18 -04009424 * create a new subvolume directory/inode (helper for the ioctl).
9425 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009426int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009427 struct btrfs_root *new_root,
9428 struct btrfs_root *parent_root,
9429 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009430{
Chris Mason39279cc2007-06-12 06:35:45 -04009431 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009432 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009433 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009434
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009435 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9436 new_dirid, new_dirid,
9437 S_IFDIR | (~current_umask() & S_IRWXUGO),
9438 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009439 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009440 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009441 inode->i_op = &btrfs_dir_inode_operations;
9442 inode->i_fop = &btrfs_dir_file_operations;
9443
Miklos Szeredibfe86842011-10-28 14:13:29 +02009444 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009445 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009446 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009447
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009448 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9449 if (err)
9450 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009451 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009452 new_root->root_key.objectid, err);
9453
Yan, Zheng76dda932009-09-21 16:00:26 -04009454 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009455
Yan, Zheng76dda932009-09-21 16:00:26 -04009456 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009457 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009458}
9459
Chris Mason39279cc2007-06-12 06:35:45 -04009460struct inode *btrfs_alloc_inode(struct super_block *sb)
9461{
9462 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009463 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009464
9465 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9466 if (!ei)
9467 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009468
9469 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009470 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009471 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009472 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009473 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009474 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009475 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009476 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009477 ei->disk_i_size = 0;
9478 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009479 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009480 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009481 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009482 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009483 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009484 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009485
Josef Bacik9e0baf62011-07-15 15:16:44 +00009486 spin_lock_init(&ei->lock);
9487 ei->outstanding_extents = 0;
9488 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009489
Josef Bacik72ac3c02012-05-23 14:13:11 -04009490 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009491 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009492 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009493
Miao Xie16cdcec2011-04-22 18:12:22 +08009494 ei->delayed_node = NULL;
9495
chandan r9cc97d62012-07-04 12:48:07 +05309496 ei->i_otime.tv_sec = 0;
9497 ei->i_otime.tv_nsec = 0;
9498
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009499 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009500 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009501 extent_io_tree_init(&ei->io_tree, inode);
9502 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009503 ei->io_tree.track_uptodate = 1;
9504 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009505 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009506 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009507 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009508 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009509 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009510 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009511 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009512 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009513
9514 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009515}
9516
Josef Bacikaaedb552013-10-11 14:44:09 -04009517#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9518void btrfs_test_destroy_inode(struct inode *inode)
9519{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009520 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009521 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9522}
9523#endif
9524
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009525static void btrfs_i_callback(struct rcu_head *head)
9526{
9527 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009528 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9529}
9530
Chris Mason39279cc2007-06-12 06:35:45 -04009531void btrfs_destroy_inode(struct inode *inode)
9532{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009533 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009534 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009535 struct btrfs_root *root = BTRFS_I(inode)->root;
9536
Al Virob3d9b7a2012-06-09 13:51:19 -04009537 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009538 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009539 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9540 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009541 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009542 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009543 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009544 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009545
Chris Mason5a3f23d2009-03-31 13:27:11 -04009546 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009547 * This can happen where we create an inode, but somebody else also
9548 * created the same inode and we need to destroy the one we already
9549 * created.
9550 */
9551 if (!root)
9552 goto free;
9553
Josef Bacik8a35d952012-05-23 14:26:42 -04009554 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9555 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009556 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009557 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009558 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009559 }
Josef Bacik7b128762008-07-24 12:17:14 -04009560
Chris Masond3977122009-01-05 21:25:51 -05009561 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009562 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9563 if (!ordered)
9564 break;
9565 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009566 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009567 "found ordered extent %llu %llu on inode cleanup",
9568 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009569 btrfs_remove_ordered_extent(inode, ordered);
9570 btrfs_put_ordered_extent(ordered);
9571 btrfs_put_ordered_extent(ordered);
9572 }
9573 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009574 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009575 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009576 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009577free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009578 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009579}
9580
Al Viro45321ac2010-06-07 13:43:19 -04009581int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009582{
9583 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009584
Naohiro Aota6379ef92013-06-06 09:56:34 +00009585 if (root == NULL)
9586 return 1;
9587
Liu Bofa6ac872013-02-20 14:10:23 +00009588 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009589 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009590 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009591 else
Al Viro45321ac2010-06-07 13:43:19 -04009592 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009593}
9594
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009595static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009596{
9597 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9598
9599 inode_init_once(&ei->vfs_inode);
9600}
9601
9602void btrfs_destroy_cachep(void)
9603{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009604 /*
9605 * Make sure all delayed rcu free inodes are flushed before we
9606 * destroy cache.
9607 */
9608 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009609 kmem_cache_destroy(btrfs_inode_cachep);
9610 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009611 kmem_cache_destroy(btrfs_path_cachep);
9612 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009613}
9614
9615int btrfs_init_cachep(void)
9616{
David Sterba837e1972012-09-07 03:00:48 -06009617 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009618 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009619 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9620 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009621 if (!btrfs_inode_cachep)
9622 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009623
David Sterba837e1972012-09-07 03:00:48 -06009624 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009625 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009626 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009627 if (!btrfs_trans_handle_cachep)
9628 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009629
David Sterba837e1972012-09-07 03:00:48 -06009630 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009631 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009632 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009633 if (!btrfs_path_cachep)
9634 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009635
David Sterba837e1972012-09-07 03:00:48 -06009636 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009637 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009638 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009639 if (!btrfs_free_space_cachep)
9640 goto fail;
9641
Chris Mason39279cc2007-06-12 06:35:45 -04009642 return 0;
9643fail:
9644 btrfs_destroy_cachep();
9645 return -ENOMEM;
9646}
9647
David Howellsa528d352017-01-31 16:46:22 +00009648static int btrfs_getattr(const struct path *path, struct kstat *stat,
9649 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009650{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009651 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009652 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009653 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009654 u32 bi_flags = BTRFS_I(inode)->flags;
9655
9656 stat->result_mask |= STATX_BTIME;
9657 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9658 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9659 if (bi_flags & BTRFS_INODE_APPEND)
9660 stat->attributes |= STATX_ATTR_APPEND;
9661 if (bi_flags & BTRFS_INODE_COMPRESS)
9662 stat->attributes |= STATX_ATTR_COMPRESSED;
9663 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9664 stat->attributes |= STATX_ATTR_IMMUTABLE;
9665 if (bi_flags & BTRFS_INODE_NODUMP)
9666 stat->attributes |= STATX_ATTR_NODUMP;
9667
9668 stat->attributes_mask |= (STATX_ATTR_APPEND |
9669 STATX_ATTR_COMPRESSED |
9670 STATX_ATTR_IMMUTABLE |
9671 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009672
Chris Mason39279cc2007-06-12 06:35:45 -04009673 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009674 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009675
9676 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009677 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009678 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009679 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009680 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009681 return 0;
9682}
9683
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684static int btrfs_rename_exchange(struct inode *old_dir,
9685 struct dentry *old_dentry,
9686 struct inode *new_dir,
9687 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009688{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009689 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009690 struct btrfs_trans_handle *trans;
9691 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009692 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693 struct inode *new_inode = new_dentry->d_inode;
9694 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009695 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009697 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9698 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699 u64 old_idx = 0;
9700 u64 new_idx = 0;
9701 u64 root_objectid;
9702 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009703 bool root_log_pinned = false;
9704 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705
9706 /* we only allow rename subvolume link between subvolumes */
9707 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9708 return -EXDEV;
9709
9710 /* close the race window with snapshot create/destroy ioctl */
9711 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009712 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009714 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715
9716 /*
9717 * We want to reserve the absolute worst case amount of items. So if
9718 * both inodes are subvols and we need to unlink them then that would
9719 * require 4 item modifications, but if they are both normal inodes it
9720 * would require 5 item modifications, so we'll assume their normal
9721 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9722 * should cover the worst case number of items we'll modify.
9723 */
9724 trans = btrfs_start_transaction(root, 12);
9725 if (IS_ERR(trans)) {
9726 ret = PTR_ERR(trans);
9727 goto out_notrans;
9728 }
9729
9730 /*
9731 * We need to find a free sequence number both in the source and
9732 * in the destination directory for the exchange.
9733 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009734 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009735 if (ret)
9736 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009737 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009738 if (ret)
9739 goto out_fail;
9740
9741 BTRFS_I(old_inode)->dir_index = 0ULL;
9742 BTRFS_I(new_inode)->dir_index = 0ULL;
9743
9744 /* Reference for the source. */
9745 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9746 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009747 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009749 btrfs_pin_log_trans(root);
9750 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751 ret = btrfs_insert_inode_ref(trans, dest,
9752 new_dentry->d_name.name,
9753 new_dentry->d_name.len,
9754 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009755 btrfs_ino(BTRFS_I(new_dir)),
9756 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757 if (ret)
9758 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759 }
9760
9761 /* And now for the dest. */
9762 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9763 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009764 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009765 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009766 btrfs_pin_log_trans(dest);
9767 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009768 ret = btrfs_insert_inode_ref(trans, root,
9769 old_dentry->d_name.name,
9770 old_dentry->d_name.len,
9771 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009772 btrfs_ino(BTRFS_I(old_dir)),
9773 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 if (ret)
9775 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 }
9777
9778 /* Update inode version and ctime/mtime. */
9779 inode_inc_iversion(old_dir);
9780 inode_inc_iversion(new_dir);
9781 inode_inc_iversion(old_inode);
9782 inode_inc_iversion(new_inode);
9783 old_dir->i_ctime = old_dir->i_mtime = ctime;
9784 new_dir->i_ctime = new_dir->i_mtime = ctime;
9785 old_inode->i_ctime = ctime;
9786 new_inode->i_ctime = ctime;
9787
9788 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009789 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9790 BTRFS_I(old_inode), 1);
9791 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9792 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009793 }
9794
9795 /* src is a subvolume */
9796 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9797 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9798 ret = btrfs_unlink_subvol(trans, root, old_dir,
9799 root_objectid,
9800 old_dentry->d_name.name,
9801 old_dentry->d_name.len);
9802 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009803 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9804 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 old_dentry->d_name.name,
9806 old_dentry->d_name.len);
9807 if (!ret)
9808 ret = btrfs_update_inode(trans, root, old_inode);
9809 }
9810 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009811 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812 goto out_fail;
9813 }
9814
9815 /* dest is a subvolume */
9816 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9817 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9818 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9819 root_objectid,
9820 new_dentry->d_name.name,
9821 new_dentry->d_name.len);
9822 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009823 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9824 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 new_dentry->d_name.name,
9826 new_dentry->d_name.len);
9827 if (!ret)
9828 ret = btrfs_update_inode(trans, dest, new_inode);
9829 }
9830 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009831 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009832 goto out_fail;
9833 }
9834
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009835 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009836 new_dentry->d_name.name,
9837 new_dentry->d_name.len, 0, old_idx);
9838 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009839 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009840 goto out_fail;
9841 }
9842
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009843 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009844 old_dentry->d_name.name,
9845 old_dentry->d_name.len, 0, new_idx);
9846 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009847 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848 goto out_fail;
9849 }
9850
9851 if (old_inode->i_nlink == 1)
9852 BTRFS_I(old_inode)->dir_index = old_idx;
9853 if (new_inode->i_nlink == 1)
9854 BTRFS_I(new_inode)->dir_index = new_idx;
9855
Filipe Manana86e8aa02016-05-05 02:02:27 +01009856 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009857 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009858 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9859 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009860 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009861 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009862 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009863 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009864 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009865 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9866 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009867 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009868 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009869 }
9870out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009871 /*
9872 * If we have pinned a log and an error happened, we unpin tasks
9873 * trying to sync the log and force them to fallback to a transaction
9874 * commit if the log currently contains any of the inodes involved in
9875 * this rename operation (to ensure we do not persist a log with an
9876 * inconsistent state for any of these inodes or leading to any
9877 * inconsistencies when replayed). If the transaction was aborted, the
9878 * abortion reason is propagated to userspace when attempting to commit
9879 * the transaction. If the log does not contain any of these inodes, we
9880 * allow the tasks to sync it.
9881 */
9882 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009883 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9884 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9885 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009886 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009887 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009888 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009889
9890 if (root_log_pinned) {
9891 btrfs_end_log_trans(root);
9892 root_log_pinned = false;
9893 }
9894 if (dest_log_pinned) {
9895 btrfs_end_log_trans(dest);
9896 dest_log_pinned = false;
9897 }
9898 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009899 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009900out_notrans:
9901 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009902 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009903 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009904 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009905
9906 return ret;
9907}
9908
9909static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9910 struct btrfs_root *root,
9911 struct inode *dir,
9912 struct dentry *dentry)
9913{
9914 int ret;
9915 struct inode *inode;
9916 u64 objectid;
9917 u64 index;
9918
9919 ret = btrfs_find_free_ino(root, &objectid);
9920 if (ret)
9921 return ret;
9922
9923 inode = btrfs_new_inode(trans, root, dir,
9924 dentry->d_name.name,
9925 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009926 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009927 objectid,
9928 S_IFCHR | WHITEOUT_MODE,
9929 &index);
9930
9931 if (IS_ERR(inode)) {
9932 ret = PTR_ERR(inode);
9933 return ret;
9934 }
9935
9936 inode->i_op = &btrfs_special_inode_operations;
9937 init_special_inode(inode, inode->i_mode,
9938 WHITEOUT_DEV);
9939
9940 ret = btrfs_init_inode_security(trans, inode, dir,
9941 &dentry->d_name);
9942 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009943 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009944
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009945 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9946 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009947 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009948 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009949
9950 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009951out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009952 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009953 if (ret)
9954 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009955 iput(inode);
9956
Filipe Mananac9901612016-05-05 01:41:57 +01009957 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009958}
9959
Chris Mason39279cc2007-06-12 06:35:45 -04009960static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009961 struct inode *new_dir, struct dentry *new_dentry,
9962 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009963{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009964 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009965 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009966 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009967 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9968 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009969 struct inode *new_inode = d_inode(new_dentry);
9970 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009971 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009972 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009973 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009974 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009975 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009976
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009977 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009978 return -EPERM;
9979
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009980 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009981 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009982 return -EXDEV;
9983
Li Zefan33345d012011-04-20 10:31:50 +08009984 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009985 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009986 return -ENOTEMPTY;
9987
Chris Mason39279cc2007-06-12 06:35:45 -04009988 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009989 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009990 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009991
9992
9993 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009994 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009995 new_dentry->d_name.name,
9996 new_dentry->d_name.len);
9997
9998 if (ret) {
9999 if (ret == -EEXIST) {
10000 /* we shouldn't get
10001 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +053010002 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -050010003 return ret;
10004 }
10005 } else {
10006 /* maybe -EOVERFLOW */
10007 return ret;
10008 }
10009 }
10010 ret = 0;
10011
Chris Mason5a3f23d2009-03-31 13:27:11 -040010012 /*
Chris Mason8d875f92014-08-12 10:47:42 -070010013 * we're using rename to replace one file with another. Start IO on it
10014 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -040010015 */
Chris Mason8d875f92014-08-12 10:47:42 -070010016 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -040010017 filemap_flush(old_inode->i_mapping);
10018
Yan, Zheng76dda932009-09-21 16:00:26 -040010019 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +080010020 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010021 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010022 /*
10023 * We want to reserve the absolute worst case amount of items. So if
10024 * both inodes are subvols and we need to unlink them then that would
10025 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010026 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -040010027 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
10028 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +010010029 * If our rename has the whiteout flag, we need more 5 units for the
10030 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
10031 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -040010032 */
Filipe Manana5062af32016-05-05 10:26:26 +010010033 trans_num_items = 11;
10034 if (flags & RENAME_WHITEOUT)
10035 trans_num_items += 5;
10036 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010037 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010038 ret = PTR_ERR(trans);
10039 goto out_notrans;
10040 }
Chris Mason5f39d392007-10-15 16:14:19 -040010041
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010042 if (dest != root)
10043 btrfs_record_root_in_trans(trans, dest);
10044
Nikolay Borisov877574e2017-02-20 13:50:33 +020010045 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010046 if (ret)
10047 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010048
Miao Xie67de1172013-12-26 13:07:06 +080010049 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010050 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010051 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010052 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010053 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010054 btrfs_pin_log_trans(root);
10055 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010056 ret = btrfs_insert_inode_ref(trans, dest,
10057 new_dentry->d_name.name,
10058 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010059 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010060 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010061 if (ret)
10062 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010063 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010064
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010065 inode_inc_iversion(old_dir);
10066 inode_inc_iversion(new_dir);
10067 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010068 old_dir->i_ctime = old_dir->i_mtime =
10069 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010070 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010071
Chris Mason12fcfd22009-03-24 10:24:20 -040010072 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010073 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10074 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010075
Li Zefan33345d012011-04-20 10:31:50 +080010076 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010077 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10078 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10079 old_dentry->d_name.name,
10080 old_dentry->d_name.len);
10081 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010082 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10083 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010084 old_dentry->d_name.name,
10085 old_dentry->d_name.len);
10086 if (!ret)
10087 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010088 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010089 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010090 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010091 goto out_fail;
10092 }
Chris Mason39279cc2007-06-12 06:35:45 -040010093
10094 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010095 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010096 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010097 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010098 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10099 root_objectid = BTRFS_I(new_inode)->location.objectid;
10100 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10101 root_objectid,
10102 new_dentry->d_name.name,
10103 new_dentry->d_name.len);
10104 BUG_ON(new_inode->i_nlink == 0);
10105 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010106 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10107 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010108 new_dentry->d_name.name,
10109 new_dentry->d_name.len);
10110 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010111 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010112 ret = btrfs_orphan_add(trans,
10113 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010114 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010115 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010116 goto out_fail;
10117 }
Chris Mason39279cc2007-06-12 06:35:45 -040010118 }
Josef Bacikaec74772008-07-24 12:12:38 -040010119
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010120 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010121 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010122 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010123 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010124 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010125 goto out_fail;
10126 }
Chris Mason39279cc2007-06-12 06:35:45 -040010127
Miao Xie67de1172013-12-26 13:07:06 +080010128 if (old_inode->i_nlink == 1)
10129 BTRFS_I(old_inode)->dir_index = index;
10130
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010131 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010132 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010133
David Sterbaf85b7372017-01-20 14:54:07 +010010134 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10135 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010136 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010137 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010138 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010139
10140 if (flags & RENAME_WHITEOUT) {
10141 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10142 old_dentry);
10143
10144 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010145 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010146 goto out_fail;
10147 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010148 }
Chris Mason39279cc2007-06-12 06:35:45 -040010149out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010150 /*
10151 * If we have pinned the log and an error happened, we unpin tasks
10152 * trying to sync the log and force them to fallback to a transaction
10153 * commit if the log currently contains any of the inodes involved in
10154 * this rename operation (to ensure we do not persist a log with an
10155 * inconsistent state for any of these inodes or leading to any
10156 * inconsistencies when replayed). If the transaction was aborted, the
10157 * abortion reason is propagated to userspace when attempting to commit
10158 * the transaction. If the log does not contain any of these inodes, we
10159 * allow the tasks to sync it.
10160 */
10161 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010162 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10163 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10164 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010165 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010166 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010167 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010168
10169 btrfs_end_log_trans(root);
10170 log_pinned = false;
10171 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010172 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010173out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010174 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010175 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010176
Chris Mason39279cc2007-06-12 06:35:45 -040010177 return ret;
10178}
10179
Miklos Szeredi80ace852014-07-23 15:15:32 +020010180static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10181 struct inode *new_dir, struct dentry *new_dentry,
10182 unsigned int flags)
10183{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010184 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010185 return -EINVAL;
10186
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010187 if (flags & RENAME_EXCHANGE)
10188 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10189 new_dentry);
10190
10191 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010192}
10193
Miao Xie8ccf6f192012-10-25 09:28:04 +000010194static void btrfs_run_delalloc_work(struct btrfs_work *work)
10195{
10196 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010197 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010198
10199 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10200 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010201 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010202 filemap_flush(inode->i_mapping);
10203 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10204 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010205 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010206
10207 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010208 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010209 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010210 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010211 complete(&delalloc_work->completion);
10212}
10213
10214struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010215 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010216{
10217 struct btrfs_delalloc_work *work;
10218
David Sterba100d5702015-12-08 14:39:32 +010010219 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010220 if (!work)
10221 return NULL;
10222
10223 init_completion(&work->completion);
10224 INIT_LIST_HEAD(&work->list);
10225 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010226 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010227 WARN_ON_ONCE(!inode);
10228 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10229 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010230
10231 return work;
10232}
10233
10234void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10235{
10236 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010237 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010238}
10239
Chris Masond352ac62008-09-29 15:18:18 -040010240/*
10241 * some fairly slow code that needs optimization. This walks the list
10242 * of all the inodes with pending delalloc and forces them to disk.
10243 */
Miao Xie6c255e62014-03-06 13:55:01 +080010244static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10245 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010246{
Chris Masonea8c2812008-08-04 23:17:27 -040010247 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010248 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010249 struct btrfs_delalloc_work *work, *next;
10250 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010251 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010252 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010253
Miao Xie8ccf6f192012-10-25 09:28:04 +000010254 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010255 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010256
Miao Xie573bfb72014-03-06 13:55:03 +080010257 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010258 spin_lock(&root->delalloc_lock);
10259 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010260 while (!list_empty(&splice)) {
10261 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010262 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010263
Miao Xieeb73c1b2013-05-15 07:48:22 +000010264 list_move_tail(&binode->delalloc_inodes,
10265 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010266 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010267 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010268 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010269 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010270 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010271 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010272
David Sterba651d4942015-11-27 19:24:16 +010010273 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010274 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010275 if (delay_iput)
10276 btrfs_add_delayed_iput(inode);
10277 else
10278 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010279 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010280 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010281 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010282 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010283 btrfs_queue_work(root->fs_info->flush_workers,
10284 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010285 ret++;
10286 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010287 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010288 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010289 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010290 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010291 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010292
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010293out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010294 list_for_each_entry_safe(work, next, &works, list) {
10295 list_del_init(&work->list);
10296 btrfs_wait_and_free_delalloc_work(work);
10297 }
10298
Miao Xieeb73c1b2013-05-15 07:48:22 +000010299 if (!list_empty_careful(&splice)) {
10300 spin_lock(&root->delalloc_lock);
10301 list_splice_tail(&splice, &root->delalloc_inodes);
10302 spin_unlock(&root->delalloc_lock);
10303 }
Miao Xie573bfb72014-03-06 13:55:03 +080010304 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010305 return ret;
10306}
10307
10308int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010310 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010311 int ret;
10312
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010313 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010314 return -EROFS;
10315
Miao Xie6c255e62014-03-06 13:55:01 +080010316 ret = __start_delalloc_inodes(root, delay_iput, -1);
10317 if (ret > 0)
10318 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010319 /*
10320 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010321 * we have to make sure the IO is actually started and that
10322 * ordered extents get created before we return
10323 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010324 atomic_inc(&fs_info->async_submit_draining);
10325 while (atomic_read(&fs_info->nr_async_submits) ||
10326 atomic_read(&fs_info->async_delalloc_pages)) {
10327 wait_event(fs_info->async_submit_wait,
10328 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10329 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010330 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010331 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010332 return ret;
10333}
10334
Miao Xie6c255e62014-03-06 13:55:01 +080010335int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10336 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010337{
10338 struct btrfs_root *root;
10339 struct list_head splice;
10340 int ret;
10341
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010342 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010343 return -EROFS;
10344
10345 INIT_LIST_HEAD(&splice);
10346
Miao Xie573bfb72014-03-06 13:55:03 +080010347 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010348 spin_lock(&fs_info->delalloc_root_lock);
10349 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010350 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010351 root = list_first_entry(&splice, struct btrfs_root,
10352 delalloc_root);
10353 root = btrfs_grab_fs_root(root);
10354 BUG_ON(!root);
10355 list_move_tail(&root->delalloc_root,
10356 &fs_info->delalloc_roots);
10357 spin_unlock(&fs_info->delalloc_root_lock);
10358
Miao Xie6c255e62014-03-06 13:55:01 +080010359 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010360 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010361 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010362 goto out;
10363
Miao Xie6c255e62014-03-06 13:55:01 +080010364 if (nr != -1) {
10365 nr -= ret;
10366 WARN_ON(nr < 0);
10367 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010368 spin_lock(&fs_info->delalloc_root_lock);
10369 }
10370 spin_unlock(&fs_info->delalloc_root_lock);
10371
Miao Xie6c255e62014-03-06 13:55:01 +080010372 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010373 atomic_inc(&fs_info->async_submit_draining);
10374 while (atomic_read(&fs_info->nr_async_submits) ||
10375 atomic_read(&fs_info->async_delalloc_pages)) {
10376 wait_event(fs_info->async_submit_wait,
10377 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10378 atomic_read(&fs_info->async_delalloc_pages) == 0));
10379 }
10380 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010381out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010382 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010383 spin_lock(&fs_info->delalloc_root_lock);
10384 list_splice_tail(&splice, &fs_info->delalloc_roots);
10385 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010386 }
Miao Xie573bfb72014-03-06 13:55:03 +080010387 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010388 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010389}
10390
Chris Mason39279cc2007-06-12 06:35:45 -040010391static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10392 const char *symname)
10393{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010394 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010395 struct btrfs_trans_handle *trans;
10396 struct btrfs_root *root = BTRFS_I(dir)->root;
10397 struct btrfs_path *path;
10398 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010399 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010400 int err;
10401 int drop_inode = 0;
10402 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010403 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010404 int name_len;
10405 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010406 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010407 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010408 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010409
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010410 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010411 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010412 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010413
Josef Bacik9ed74f22009-09-11 16:12:44 -040010414 /*
10415 * 2 items for inode item and ref
10416 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010417 * 1 item for updating parent inode item
10418 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010419 * 1 item for xattr if selinux is on
10420 */
Filipe Manana9269d122015-12-31 18:16:29 +000010421 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010422 if (IS_ERR(trans))
10423 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010424
Li Zefan581bb052011-04-20 10:06:11 +080010425 err = btrfs_find_free_ino(root, &objectid);
10426 if (err)
10427 goto out_unlock;
10428
Josef Bacikaec74772008-07-24 12:12:38 -040010429 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010430 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10431 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010432 if (IS_ERR(inode)) {
10433 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010434 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010435 }
Chris Mason39279cc2007-06-12 06:35:45 -040010436
Casey Schauflerad19db72011-12-15 10:09:07 -050010437 /*
10438 * If the active LSM wants to access the inode during
10439 * d_instantiate it needs these. Smack checks to see
10440 * if the filesystem supports xattrs by looking at the
10441 * ops vector.
10442 */
10443 inode->i_fop = &btrfs_file_operations;
10444 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010445 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010446 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10447
10448 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10449 if (err)
10450 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010451
Chris Mason39279cc2007-06-12 06:35:45 -040010452 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010453 if (!path) {
10454 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010455 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010456 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010457 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010458 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010459 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010460 datasize = btrfs_file_extent_calc_inline_size(name_len);
10461 err = btrfs_insert_empty_item(trans, root, path, &key,
10462 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010463 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010464 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010465 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010466 }
Chris Mason5f39d392007-10-15 16:14:19 -040010467 leaf = path->nodes[0];
10468 ei = btrfs_item_ptr(leaf, path->slots[0],
10469 struct btrfs_file_extent_item);
10470 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10471 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010472 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010473 btrfs_set_file_extent_encryption(leaf, ei, 0);
10474 btrfs_set_file_extent_compression(leaf, ei, 0);
10475 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10476 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10477
Chris Mason39279cc2007-06-12 06:35:45 -040010478 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010479 write_extent_buffer(leaf, symname, ptr, name_len);
10480 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010481 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010482
Chris Mason39279cc2007-06-12 06:35:45 -040010483 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010484 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010485 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010486 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010487 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010488 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010489 /*
10490 * Last step, add directory indexes for our symlink inode. This is the
10491 * last step to avoid extra cleanup of these indexes if an error happens
10492 * elsewhere above.
10493 */
10494 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010495 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10496 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010497 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010498 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010499 goto out_unlock_inode;
10500 }
10501
10502 unlock_new_inode(inode);
10503 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010504
10505out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010506 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010507 if (drop_inode) {
10508 inode_dec_link_count(inode);
10509 iput(inode);
10510 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010511 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010512 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010513
10514out_unlock_inode:
10515 drop_inode = 1;
10516 unlock_new_inode(inode);
10517 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010518}
Chris Mason16432982008-04-10 10:23:21 -040010519
Josef Bacik0af3d002010-06-21 14:48:16 -040010520static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10521 u64 start, u64 num_bytes, u64 min_size,
10522 loff_t actual_len, u64 *alloc_hint,
10523 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010524{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010525 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010526 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10527 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010528 struct btrfs_root *root = BTRFS_I(inode)->root;
10529 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010530 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010531 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010532 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010533 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010534 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010535 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010536 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010537
Josef Bacik0af3d002010-06-21 14:48:16 -040010538 if (trans)
10539 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010540 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010541 if (own_trans) {
10542 trans = btrfs_start_transaction(root, 3);
10543 if (IS_ERR(trans)) {
10544 ret = PTR_ERR(trans);
10545 break;
10546 }
Yan Zhengd899e052008-10-30 14:25:28 -040010547 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010548
Byongho Leeee221842015-12-15 01:42:10 +090010549 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010550 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010551 /*
10552 * If we are severely fragmented we could end up with really
10553 * small allocations, so if the allocator is returning small
10554 * chunks lets make its job easier by only searching for those
10555 * sized chunks.
10556 */
10557 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010558 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10559 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010560 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010561 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010562 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010563 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010564 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010565 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010566
Josef Bacik0b670dc2015-09-23 17:11:16 -040010567 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010568 ret = insert_reserved_file_extent(trans, inode,
10569 cur_offset, ins.objectid,
10570 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010571 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010572 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010573 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010574 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010575 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010576 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010577 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010578 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010579 break;
10580 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010581
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010582 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010583 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010584
Josef Bacik5dc562c2012-08-17 13:14:17 -040010585 em = alloc_extent_map();
10586 if (!em) {
10587 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10588 &BTRFS_I(inode)->runtime_flags);
10589 goto next;
10590 }
10591
10592 em->start = cur_offset;
10593 em->orig_start = cur_offset;
10594 em->len = ins.offset;
10595 em->block_start = ins.objectid;
10596 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010597 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010598 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010599 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010600 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10601 em->generation = trans->transid;
10602
10603 while (1) {
10604 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010605 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010606 write_unlock(&em_tree->lock);
10607 if (ret != -EEXIST)
10608 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010609 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010610 cur_offset + ins.offset - 1,
10611 0);
10612 }
10613 free_extent_map(em);
10614next:
Yan Zhengd899e052008-10-30 14:25:28 -040010615 num_bytes -= ins.offset;
10616 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010617 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010618
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010619 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010620 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010621 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010622 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010623 (actual_len > inode->i_size) &&
10624 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010625 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010626 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010627 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010628 i_size = cur_offset;
10629 i_size_write(inode, i_size);
10630 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010631 }
10632
Yan Zhengd899e052008-10-30 14:25:28 -040010633 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010634
10635 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010636 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010637 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010638 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010639 break;
10640 }
Yan Zhengd899e052008-10-30 14:25:28 -040010641
Josef Bacik0af3d002010-06-21 14:48:16 -040010642 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010643 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010644 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010645 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010646 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010647 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010648 return ret;
10649}
10650
Josef Bacik0af3d002010-06-21 14:48:16 -040010651int btrfs_prealloc_file_range(struct inode *inode, int mode,
10652 u64 start, u64 num_bytes, u64 min_size,
10653 loff_t actual_len, u64 *alloc_hint)
10654{
10655 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10656 min_size, actual_len, alloc_hint,
10657 NULL);
10658}
10659
10660int btrfs_prealloc_file_range_trans(struct inode *inode,
10661 struct btrfs_trans_handle *trans, int mode,
10662 u64 start, u64 num_bytes, u64 min_size,
10663 loff_t actual_len, u64 *alloc_hint)
10664{
10665 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10666 min_size, actual_len, alloc_hint, trans);
10667}
10668
Chris Masone6dcd2d2008-07-17 12:53:50 -040010669static int btrfs_set_page_dirty(struct page *page)
10670{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010671 return __set_page_dirty_nobuffers(page);
10672}
10673
Al Viro10556cb22011-06-20 19:28:19 -040010674static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010675{
Li Zefanb83cc962010-12-20 16:04:08 +080010676 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010677 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010678
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010679 if (mask & MAY_WRITE &&
10680 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10681 if (btrfs_root_readonly(root))
10682 return -EROFS;
10683 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10684 return -EACCES;
10685 }
Al Viro2830ba72011-06-20 19:16:29 -040010686 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010687}
Chris Mason39279cc2007-06-12 06:35:45 -040010688
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010689static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10690{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010691 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010692 struct btrfs_trans_handle *trans;
10693 struct btrfs_root *root = BTRFS_I(dir)->root;
10694 struct inode *inode = NULL;
10695 u64 objectid;
10696 u64 index;
10697 int ret = 0;
10698
10699 /*
10700 * 5 units required for adding orphan entry
10701 */
10702 trans = btrfs_start_transaction(root, 5);
10703 if (IS_ERR(trans))
10704 return PTR_ERR(trans);
10705
10706 ret = btrfs_find_free_ino(root, &objectid);
10707 if (ret)
10708 goto out;
10709
10710 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010711 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010712 if (IS_ERR(inode)) {
10713 ret = PTR_ERR(inode);
10714 inode = NULL;
10715 goto out;
10716 }
10717
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010718 inode->i_fop = &btrfs_file_operations;
10719 inode->i_op = &btrfs_file_inode_operations;
10720
10721 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010722 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10723
Chris Masonb0d5d102014-09-08 13:08:51 -070010724 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10725 if (ret)
10726 goto out_inode;
10727
10728 ret = btrfs_update_inode(trans, root, inode);
10729 if (ret)
10730 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010731 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010732 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010733 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010734
Filipe Manana5762b5c2014-08-01 00:10:32 +010010735 /*
10736 * We set number of links to 0 in btrfs_new_inode(), and here we set
10737 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10738 * through:
10739 *
10740 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10741 */
10742 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010743 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010744 d_tmpfile(dentry, inode);
10745 mark_inode_dirty(inode);
10746
10747out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010748 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010749 if (ret)
10750 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010751 btrfs_balance_delayed_items(fs_info);
10752 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010753 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010754
10755out_inode:
10756 unlock_new_inode(inode);
10757 goto out;
10758
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010759}
10760
David Sterba20a7db82017-02-17 16:24:29 +010010761__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010762static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010763{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010764 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010765}
10766
Josef Bacikc6100a42017-05-05 11:57:13 -040010767static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10768{
10769 struct inode *inode = private_data;
10770 return btrfs_sb(inode->i_sb);
10771}
10772
10773static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10774 u64 start, u64 end)
10775{
10776 struct inode *inode = private_data;
10777 u64 isize;
10778
10779 isize = i_size_read(inode);
10780 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10781 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10782 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10783 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10784 }
10785}
10786
10787void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10788{
10789 struct inode *inode = private_data;
10790 unsigned long index = start >> PAGE_SHIFT;
10791 unsigned long end_index = end >> PAGE_SHIFT;
10792 struct page *page;
10793
10794 while (index <= end_index) {
10795 page = find_get_page(inode->i_mapping, index);
10796 ASSERT(page); /* Pages should be in the extent_io_tree */
10797 set_page_writeback(page);
10798 put_page(page);
10799 index++;
10800 }
10801}
10802
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010803static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010804 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010805 .lookup = btrfs_lookup,
10806 .create = btrfs_create,
10807 .unlink = btrfs_unlink,
10808 .link = btrfs_link,
10809 .mkdir = btrfs_mkdir,
10810 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010811 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010812 .symlink = btrfs_symlink,
10813 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010814 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010815 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010816 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010817 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010818 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010819 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010820 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010821};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010822static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010823 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010824 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010825 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010826};
Yan, Zheng76dda932009-09-21 16:00:26 -040010827
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010828static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010829 .llseek = generic_file_llseek,
10830 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010831 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010832 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010833 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010834#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010835 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010836#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010837 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010838 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010839};
10840
David Sterba20e55062015-11-19 11:42:28 +010010841static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010842 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010843 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010844 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010845 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010846 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010847 .tree_fs_info = iotree_fs_info,
10848 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010849
10850 /* optional callbacks */
10851 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010852 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010853 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010854 .set_bit_hook = btrfs_set_bit_hook,
10855 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010856 .merge_extent_hook = btrfs_merge_extent_hook,
10857 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010858 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010859};
10860
Chris Mason35054392009-01-21 13:11:13 -050010861/*
10862 * btrfs doesn't support the bmap operation because swapfiles
10863 * use bmap to make a mapping of extents in the file. They assume
10864 * these extents won't change over the life of the file and they
10865 * use the bmap result to do IO directly to the drive.
10866 *
10867 * the btrfs bmap call would return logical addresses that aren't
10868 * suitable for IO and they also will change frequently as COW
10869 * operations happen. So, swapfile + btrfs == corruption.
10870 *
10871 * For now we're avoiding this by dropping bmap.
10872 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010873static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010874 .readpage = btrfs_readpage,
10875 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010876 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010877 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010878 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010879 .invalidatepage = btrfs_invalidatepage,
10880 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010881 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010882 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010883};
10884
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010885static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010886 .readpage = btrfs_readpage,
10887 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010888 .invalidatepage = btrfs_invalidatepage,
10889 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010890};
10891
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010892static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010893 .getattr = btrfs_getattr,
10894 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010895 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010896 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010897 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010898 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010899 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010900 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010901};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010902static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010903 .getattr = btrfs_getattr,
10904 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010905 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010906 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010907 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010908 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010909 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010910};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010911static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010912 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010913 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010914 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010915 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010916 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010917 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010918};
Yan, Zheng76dda932009-09-21 16:00:26 -040010919
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010920const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010921 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010922 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010923};