blob: d9cf6df40d5ec0a25b2f36eeecce6129002bccb1 [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 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300348 btrfs_qgroup_free_data(inode, 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
Wang Shilongf79707b2014-07-17 11:44:09 +0800395static inline int inode_need_compress(struct inode *inode)
396{
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;
402 /* bad compression ratios */
403 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
404 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400405 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800406 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
407 BTRFS_I(inode)->force_compress)
408 return 1;
409 return 0;
410}
411
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200412static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800413 u64 start, u64 end, u64 num_bytes, u64 small_write)
414{
415 /* If this is a small write inside eof, kick off a defrag */
416 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200417 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800418 btrfs_add_inode_defrag(NULL, inode);
419}
420
Chris Masonc8b97812008-10-29 14:49:59 -0400421/*
Chris Mason771ed682008-11-06 22:02:51 -0500422 * we create compressed extents in two phases. The first
423 * phase compresses a range of pages that have already been
424 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400425 *
Chris Mason771ed682008-11-06 22:02:51 -0500426 * This is done inside an ordered work queue, and the compression
427 * is spread across many cpus. The actual IO submission is step
428 * two, and the ordered work queue takes care of making sure that
429 * happens in the same order things were put onto the queue by
430 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400431 *
Chris Mason771ed682008-11-06 22:02:51 -0500432 * If this code finds it can't get good compression, it puts an
433 * entry onto the work queue to write the uncompressed bytes. This
434 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300435 * are written in the same order that the flusher thread sent them
436 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400437 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100438static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500439 struct page *locked_page,
440 u64 start, u64 end,
441 struct async_cow *async_cow,
442 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400444 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400445 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400446 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400448 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500449 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400450 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400451 struct page **pages = NULL;
452 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400453 unsigned long total_compressed = 0;
454 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400455 int i;
456 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400457 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400458 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400459
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200460 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
461 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400462
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400464again:
465 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300466 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100467 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
468 nr_pages = min_t(unsigned long, nr_pages,
469 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400470
Chris Masonf03d9301f2009-02-04 09:31:06 -0500471 /*
472 * we don't want to send crud past the end of i_size through
473 * compression, that's just a waste of CPU time. So, if the
474 * end of the file is before the start of our current
475 * requested range of bytes, we bail out to the uncompressed
476 * cleanup code that can deal with all of this.
477 *
478 * It isn't really the fastest way to fix things, but this is a
479 * very uncommon corner.
480 */
481 if (actual_end <= start)
482 goto cleanup_and_bail_uncompressed;
483
Chris Masonc8b97812008-10-29 14:49:59 -0400484 total_compressed = actual_end - start;
485
Shilong Wang4bcbb332014-10-07 18:44:35 -0400486 /*
487 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400488 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400489 */
490 if (total_compressed <= blocksize &&
491 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
492 goto cleanup_and_bail_uncompressed;
493
David Sterba069eac72017-02-14 19:36:54 +0100494 total_compressed = min_t(unsigned long, total_compressed,
495 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000496 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500497 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_in = 0;
499 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400500
Chris Mason771ed682008-11-06 22:02:51 -0500501 /*
502 * we do compression for mount -o compress and when the
503 * inode has not been flagged as nocompress. This flag can
504 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400505 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800506 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400507 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100508 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800509 if (!pages) {
510 /* just bail out to the uncompressed code */
511 goto cont;
512 }
Chris Mason179e29e2007-11-01 11:28:41 -0400513
Li Zefan261507a02010-12-17 14:21:50 +0800514 if (BTRFS_I(inode)->force_compress)
515 compress_type = BTRFS_I(inode)->force_compress;
516
Chris Mason4adaa612013-03-26 13:07:00 -0400517 /*
518 * we need to call clear_page_dirty_for_io on each
519 * page in the range. Otherwise applications with the file
520 * mmap'd can wander in and change the page contents while
521 * we are compressing them.
522 *
523 * If the compression fails for any reason, we set the pages
524 * dirty again later on.
525 */
526 extent_range_clear_dirty_for_io(inode, start, end);
527 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800528 ret = btrfs_compress_pages(compress_type,
529 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100530 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100531 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800532 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100533 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400534
535 if (!ret) {
536 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300537 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100538 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400539 char *kaddr;
540
541 /* zero the tail end of the last page, we might be
542 * sending it down to disk
543 */
544 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800545 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400546 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300547 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800548 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 }
550 will_compress = 1;
551 }
552 }
Li Zefan560f7d72011-09-08 10:22:01 +0800553cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400554 if (start == 0) {
555 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500556 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400557 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500558 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400559 */
Josef Bacik00361582013-08-14 14:02:47 -0400560 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800561 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400562 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500563 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400564 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000565 total_compressed,
566 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400569 unsigned long clear_flags = EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100570 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100571 unsigned long page_error_op;
572
Josef Bacik151a41b2013-07-29 13:22:24 -0400573 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100574 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400575
Chris Mason771ed682008-11-06 22:02:51 -0500576 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100577 * inline extent creation worked or returned error,
578 * we don't need to create any more async work items.
579 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500580 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800581 extent_clear_unlock_delalloc(inode, start, end, end,
582 NULL, clear_flags,
583 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400584 PAGE_CLEAR_DIRTY |
585 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100586 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400587 PAGE_END_WRITEBACK);
Filipe Manana1c81ba22017-03-08 16:43:49 +0000588 if (ret == 0)
589 btrfs_free_reserved_data_space_noquota(inode,
590 start,
591 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400592 goto free_pages_out;
593 }
594 }
595
596 if (will_compress) {
597 /*
598 * we aren't doing an inline extent round the compressed size
599 * up to a block size boundary so the allocator does sane
600 * things
601 */
Qu Wenruofda28322013-02-26 08:10:22 +0000602 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400603
604 /*
605 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300606 * win, compare the page count read with the blocks on disk,
607 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400608 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300609 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300610 if (total_compressed + blocksize <= total_in) {
Chris Masonc8b97812008-10-29 14:49:59 -0400611 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700612 *num_added += 1;
613
614 /*
615 * The async work queues will take care of doing actual
616 * allocation on disk for these compressed pages, and
617 * will submit them to the elevator.
618 */
619 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100620 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700621 compress_type);
622
623 if (start + num_bytes < end) {
624 start += num_bytes;
625 pages = NULL;
626 cond_resched();
627 goto again;
628 }
629 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400630 }
631 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700632 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400633 /*
634 * the compression code ran but failed to make things smaller,
635 * free any pages it allocated and our page pointer array
636 */
David Sterba4d3a8002017-02-14 19:04:07 +0100637 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400638 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300639 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400640 }
641 kfree(pages);
642 pages = NULL;
643 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100644 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400645
646 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400647 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500648 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500649 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500650 }
Chris Masonc8b97812008-10-29 14:49:59 -0400651 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500652cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700653 /*
654 * No compression, but we still need to write the pages in the file
655 * we've been given so far. redirty the locked page if it corresponds
656 * to our extent and set things up for the async work queue to run
657 * cow_file_range to do the normal delalloc dance.
658 */
659 if (page_offset(locked_page) >= start &&
660 page_offset(locked_page) <= end)
661 __set_page_dirty_nobuffers(locked_page);
662 /* unlocked later on in the async handlers */
663
664 if (redirty)
665 extent_range_redirty_for_io(inode, start, end);
666 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
667 BTRFS_COMPRESS_NONE);
668 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500669
Filipe Mananac44f6492014-10-09 21:15:44 +0100670 return;
Chris Mason771ed682008-11-06 22:02:51 -0500671
672free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100673 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500674 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300675 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500676 }
Chris Masond3977122009-01-05 21:25:51 -0500677 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500678}
Chris Mason771ed682008-11-06 22:02:51 -0500679
Filipe Manana40ae8372014-10-06 22:14:24 +0100680static void free_async_extent_pages(struct async_extent *async_extent)
681{
682 int i;
683
684 if (!async_extent->pages)
685 return;
686
687 for (i = 0; i < async_extent->nr_pages; i++) {
688 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300689 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100690 }
691 kfree(async_extent->pages);
692 async_extent->nr_pages = 0;
693 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500694}
695
696/*
697 * phase two of compressed writeback. This is the ordered portion
698 * of the code, which only gets called in the order the work was
699 * queued. We walk all the async extents created by compress_file_range
700 * and send them down to the disk.
701 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100702static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500703 struct async_cow *async_cow)
704{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400705 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500706 struct async_extent *async_extent;
707 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500708 struct btrfs_key ins;
709 struct extent_map *em;
710 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500713
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500714again:
Chris Masond3977122009-01-05 21:25:51 -0500715 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500716 async_extent = list_entry(async_cow->extents.next,
717 struct async_extent, list);
718 list_del(&async_extent->list);
719
720 io_tree = &BTRFS_I(inode)->io_tree;
721
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500722retry:
Chris Mason771ed682008-11-06 22:02:51 -0500723 /* did the compression code fall back to uncompressed IO? */
724 if (!async_extent->pages) {
725 int page_started = 0;
726 unsigned long nr_written = 0;
727
728 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000729 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100730 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500731
732 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 ret = cow_file_range(inode, async_cow->locked_page,
734 async_extent->start,
735 async_extent->start +
736 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800737 async_extent->start +
738 async_extent->ram_size - 1,
739 &page_started, &nr_written, 0,
740 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500741
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100742 /* JDM XXX */
743
Chris Mason771ed682008-11-06 22:02:51 -0500744 /*
745 * if page_started, cow_file_range inserted an
746 * inline extent and took care of all the unlocking
747 * and IO for us. Otherwise, we need to submit
748 * all those pages down to the drive.
749 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500750 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500751 extent_write_locked_range(io_tree,
752 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500753 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500754 async_extent->ram_size - 1,
755 btrfs_get_extent,
756 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757 else if (ret)
758 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500759 kfree(async_extent);
760 cond_resched();
761 continue;
762 }
763
764 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100765 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500766
Wang Xiaoguang18513092016-07-25 15:51:40 +0800767 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500768 async_extent->compressed_size,
769 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800770 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100772 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400774 if (ret == -ENOSPC) {
775 unlock_extent(io_tree, async_extent->start,
776 async_extent->start +
777 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800778
779 /*
780 * we need to redirty the pages if we decide to
781 * fallback to uncompressed IO, otherwise we
782 * will not submit these pages down to lower
783 * layers.
784 */
785 extent_range_redirty_for_io(inode,
786 async_extent->start,
787 async_extent->start +
788 async_extent->ram_size - 1);
789
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100790 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400791 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500792 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500793 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000794 /*
795 * here we're doing allocation and writeback of the
796 * compressed pages
797 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800798 em = create_io_em(inode, async_extent->start,
799 async_extent->ram_size, /* len */
800 async_extent->start, /* orig_start */
801 ins.objectid, /* block_start */
802 ins.offset, /* block_len */
803 ins.offset, /* orig_block_len */
804 async_extent->ram_size, /* ram_bytes */
805 async_extent->compress_type,
806 BTRFS_ORDERED_COMPRESSED);
807 if (IS_ERR(em))
808 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500809 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800810 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811
Li Zefan261507a02010-12-17 14:21:50 +0800812 ret = btrfs_add_ordered_extent_compress(inode,
813 async_extent->start,
814 ins.objectid,
815 async_extent->ram_size,
816 ins.offset,
817 BTRFS_ORDERED_COMPRESSED,
818 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100819 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200820 btrfs_drop_extent_cache(BTRFS_I(inode),
821 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100822 async_extent->start +
823 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500824 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100825 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400826 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500827
Chris Mason771ed682008-11-06 22:02:51 -0500828 /*
829 * clear dirty, set writeback and unlock the pages.
830 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400831 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400832 async_extent->start +
833 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800834 async_extent->start +
835 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400836 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
837 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400838 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500839 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500840 async_extent->start,
841 async_extent->ram_size,
842 ins.objectid,
843 ins.offset, async_extent->pages,
844 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100845 if (ret) {
846 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
847 struct page *p = async_extent->pages[0];
848 const u64 start = async_extent->start;
849 const u64 end = start + async_extent->ram_size - 1;
850
851 p->mapping = inode->i_mapping;
852 tree->ops->writepage_end_io_hook(p, start, end,
853 NULL, 0);
854 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800855 extent_clear_unlock_delalloc(inode, start, end, end,
856 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100857 PAGE_END_WRITEBACK |
858 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100859 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100860 }
Chris Mason771ed682008-11-06 22:02:51 -0500861 alloc_hint = ins.objectid + ins.offset;
862 kfree(async_extent);
863 cond_resched();
864 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100865 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500866out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400867 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400868 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100869out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400870 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500871 async_extent->start +
872 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800873 async_extent->start +
874 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400875 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100876 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400877 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
878 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100879 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
880 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100881 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100882 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500883 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500884}
885
Josef Bacik4b46fce2010-05-23 11:00:55 -0400886static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
887 u64 num_bytes)
888{
889 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
890 struct extent_map *em;
891 u64 alloc_hint = 0;
892
893 read_lock(&em_tree->lock);
894 em = search_extent_mapping(em_tree, start, num_bytes);
895 if (em) {
896 /*
897 * if block start isn't an actual block number then find the
898 * first block in this inode and use that as a hint. If that
899 * block is also bogus then just don't worry about it.
900 */
901 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
902 free_extent_map(em);
903 em = search_extent_mapping(em_tree, 0, 0);
904 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
905 alloc_hint = em->block_start;
906 if (em)
907 free_extent_map(em);
908 } else {
909 alloc_hint = em->block_start;
910 free_extent_map(em);
911 }
912 }
913 read_unlock(&em_tree->lock);
914
915 return alloc_hint;
916}
917
Chris Mason771ed682008-11-06 22:02:51 -0500918/*
919 * when extent_io.c finds a delayed allocation range in the file,
920 * the call backs end up in this code. The basic idea is to
921 * allocate extents on disk for the range, and create ordered data structs
922 * in ram to track those extents.
923 *
924 * locked_page is the page that writepage had locked already. We use
925 * it to make sure we don't do extra locks or unlocks.
926 *
927 * *page_started is set to one if we unlock locked_page and do everything
928 * required to start IO on it. It may be clean and already done with
929 * IO when we return.
930 */
Josef Bacik00361582013-08-14 14:02:47 -0400931static noinline int cow_file_range(struct inode *inode,
932 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800933 u64 start, u64 end, u64 delalloc_end,
934 int *page_started, unsigned long *nr_written,
935 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400938 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500939 u64 alloc_hint = 0;
940 u64 num_bytes;
941 unsigned long ram_size;
942 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000943 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400944 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500945 struct btrfs_key ins;
946 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000947 unsigned clear_bits;
948 unsigned long page_ops;
949 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500950 int ret = 0;
951
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200952 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400953 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500954 ret = -EINVAL;
955 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 }
Chris Mason771ed682008-11-06 22:02:51 -0500957
Qu Wenruofda28322013-02-26 08:10:22 +0000958 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500959 num_bytes = max(blocksize, num_bytes);
960 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500961
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200962 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400963
Chris Mason771ed682008-11-06 22:02:51 -0500964 if (start == 0) {
965 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800966 ret = cow_file_range_inline(root, inode, start, end, 0,
967 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800969 extent_clear_unlock_delalloc(inode, start, end,
970 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400971 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100972 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400973 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400974 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
975 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800976 btrfs_free_reserved_data_space_noquota(inode, start,
977 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500978 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300979 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500980 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500981 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100982 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100983 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500984 }
985 }
Chris Masonc8b97812008-10-29 14:49:59 -0400986
987 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400988 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400989
Josef Bacik4b46fce2010-05-23 11:00:55 -0400990 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200991 btrfs_drop_extent_cache(BTRFS_I(inode), start,
992 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400993
Chris Masond3977122009-01-05 21:25:51 -0500994 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +0000995 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800996 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400997 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800998 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400999 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001000 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001001 cur_alloc_size = ins.offset;
1002 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001003
Chris Mason771ed682008-11-06 22:02:51 -05001004 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001005 em = create_io_em(inode, start, ins.offset, /* len */
1006 start, /* orig_start */
1007 ins.objectid, /* block_start */
1008 ins.offset, /* block_len */
1009 ins.offset, /* orig_block_len */
1010 ram_size, /* ram_bytes */
1011 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001012 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001013 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001014 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001015 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016
Chris Masone6dcd2d2008-07-17 12:53:50 -04001017 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001018 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001019 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001020 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001021
Yan Zheng17d217f2008-12-12 10:03:38 -05001022 if (root->root_key.objectid ==
1023 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1024 ret = btrfs_reloc_clone_csums(inode, start,
1025 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001026 /*
1027 * Only drop cache here, and process as normal.
1028 *
1029 * We must not allow extent_clear_unlock_delalloc()
1030 * at out_unlock label to free meta of this ordered
1031 * extent, as its meta should be freed by
1032 * btrfs_finish_ordered_io().
1033 *
1034 * So we must continue until @start is increased to
1035 * skip current ordered extent.
1036 */
Josef Bacik00361582013-08-14 14:02:47 -04001037 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001038 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1039 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001040 }
1041
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001042 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001043
Chris Masonc8b97812008-10-29 14:49:59 -04001044 /* we're not doing compressed IO, don't unlock the first
1045 * page (which the caller expects to stay locked), don't
1046 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001047 *
1048 * Do set the Private2 bit so we know this page was properly
1049 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001050 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001051 page_ops = unlock ? PAGE_UNLOCK : 0;
1052 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001053
Josef Bacikc2790a22013-07-29 11:20:47 -04001054 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001055 start + ram_size - 1,
1056 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001057 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001058 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 if (disk_num_bytes < cur_alloc_size)
1060 disk_num_bytes = 0;
1061 else
1062 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001063 num_bytes -= cur_alloc_size;
1064 alloc_hint = ins.objectid + ins.offset;
1065 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001066 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001067
1068 /*
1069 * btrfs_reloc_clone_csums() error, since start is increased
1070 * extent_clear_unlock_delalloc() at out_unlock label won't
1071 * free metadata of current ordered extent, we're OK to exit.
1072 */
1073 if (ret)
1074 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001075 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001077 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001078
Filipe Mananad9f85962014-08-25 10:43:00 +01001079out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001080 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001081out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001082 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001083 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001085 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1086 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001087 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1088 PAGE_END_WRITEBACK;
1089 /*
1090 * If we reserved an extent for our delalloc range (or a subrange) and
1091 * failed to create the respective ordered extent, then it means that
1092 * when we reserved the extent we decremented the extent's size from
1093 * the data space_info's bytes_may_use counter and incremented the
1094 * space_info's bytes_reserved counter by the same amount. We must make
1095 * sure extent_clear_unlock_delalloc() does not try to decrement again
1096 * the data space_info's bytes_may_use counter, therefore we do not pass
1097 * it the flag EXTENT_CLEAR_DATA_RESV.
1098 */
1099 if (extent_reserved) {
1100 extent_clear_unlock_delalloc(inode, start,
1101 start + cur_alloc_size,
1102 start + cur_alloc_size,
1103 locked_page,
1104 clear_bits,
1105 page_ops);
1106 start += cur_alloc_size;
1107 if (start >= end)
1108 goto out;
1109 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001110 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1111 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 clear_bits | EXTENT_CLEAR_DATA_RESV,
1113 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001114 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001115}
Chris Masonc8b97812008-10-29 14:49:59 -04001116
Chris Mason771ed682008-11-06 22:02:51 -05001117/*
1118 * work queue call back to started compression on a file and pages
1119 */
1120static noinline void async_cow_start(struct btrfs_work *work)
1121{
1122 struct async_cow *async_cow;
1123 int num_added = 0;
1124 async_cow = container_of(work, struct async_cow, work);
1125
1126 compress_file_range(async_cow->inode, async_cow->locked_page,
1127 async_cow->start, async_cow->end, async_cow,
1128 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001129 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001130 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001131 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001132 }
Chris Mason771ed682008-11-06 22:02:51 -05001133}
1134
1135/*
1136 * work queue call back to submit previously compressed pages
1137 */
1138static noinline void async_cow_submit(struct btrfs_work *work)
1139{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001140 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001141 struct async_cow *async_cow;
1142 struct btrfs_root *root;
1143 unsigned long nr_pages;
1144
1145 async_cow = container_of(work, struct async_cow, work);
1146
1147 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001148 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1150 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001151
David Sterbaee863952015-02-16 19:41:40 +01001152 /*
1153 * atomic_sub_return implies a barrier for waitqueue_active
1154 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001155 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001156 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001157 waitqueue_active(&fs_info->async_submit_wait))
1158 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001159
Chris Masond3977122009-01-05 21:25:51 -05001160 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001161 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001162}
Chris Masonc8b97812008-10-29 14:49:59 -04001163
Chris Mason771ed682008-11-06 22:02:51 -05001164static noinline void async_cow_free(struct btrfs_work *work)
1165{
1166 struct async_cow *async_cow;
1167 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001168 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001169 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001170 kfree(async_cow);
1171}
1172
1173static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1174 u64 start, u64 end, int *page_started,
1175 unsigned long *nr_written)
1176{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001177 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001178 struct async_cow *async_cow;
1179 struct btrfs_root *root = BTRFS_I(inode)->root;
1180 unsigned long nr_pages;
1181 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001182
Chris Masona3429ab2009-10-08 12:30:20 -04001183 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1184 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001185 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001186 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001189 async_cow->root = root;
1190 async_cow->locked_page = locked_page;
1191 async_cow->start = start;
1192
Wang Shilongf79707b2014-07-17 11:44:09 +08001193 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001194 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001195 cur_end = end;
1196 else
Byongho Leeee221842015-12-15 01:42:10 +09001197 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001198
1199 async_cow->end = cur_end;
1200 INIT_LIST_HEAD(&async_cow->extents);
1201
Liu Bo9e0af232014-08-15 23:36:53 +08001202 btrfs_init_work(&async_cow->work,
1203 btrfs_delalloc_helper,
1204 async_cow_start, async_cow_submit,
1205 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001206
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001207 nr_pages = (cur_end - start + PAGE_SIZE) >>
1208 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001209 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001211 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001212
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 while (atomic_read(&fs_info->async_submit_draining) &&
1214 atomic_read(&fs_info->async_delalloc_pages)) {
1215 wait_event(fs_info->async_submit_wait,
1216 (atomic_read(&fs_info->async_delalloc_pages) ==
1217 0));
Chris Mason771ed682008-11-06 22:02:51 -05001218 }
1219
1220 *nr_written += nr_pages;
1221 start = cur_end + 1;
1222 }
1223 *page_started = 1;
1224 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001225}
1226
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001227static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001228 u64 bytenr, u64 num_bytes)
1229{
1230 int ret;
1231 struct btrfs_ordered_sum *sums;
1232 LIST_HEAD(list);
1233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001234 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001235 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001236 if (ret == 0 && list_empty(&list))
1237 return 0;
1238
1239 while (!list_empty(&list)) {
1240 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1241 list_del(&sums->list);
1242 kfree(sums);
1243 }
1244 return 1;
1245}
1246
Chris Masond352ac62008-09-29 15:18:18 -04001247/*
1248 * when nowcow writeback call back. This checks for snapshots or COW copies
1249 * of the extents that exist in the file, and COWs the file as required.
1250 *
1251 * If no cow copies or snapshots exist, we write directly to the existing
1252 * blocks on disk
1253 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001254static noinline int run_delalloc_nocow(struct inode *inode,
1255 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001256 u64 start, u64 end, int *page_started, int force,
1257 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001258{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001259 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001260 struct btrfs_root *root = BTRFS_I(inode)->root;
1261 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001262 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001265 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 u64 cow_start;
1267 u64 cur_offset;
1268 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001269 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 u64 disk_bytenr;
1271 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001272 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001273 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001276 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 int nocow;
1278 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001279 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001280 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
1282 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001283 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001284 extent_clear_unlock_delalloc(inode, start, end, end,
1285 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001286 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001287 EXTENT_DO_ACCOUNTING |
1288 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001289 PAGE_CLEAR_DIRTY |
1290 PAGE_SET_WRITEBACK |
1291 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001292 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001293 }
Li Zefan82d59022011-04-20 10:33:24 +08001294
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001295 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001296
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 cow_start = (u64)-1;
1298 cur_offset = start;
1299 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001300 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001302 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001303 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1305 leaf = path->nodes[0];
1306 btrfs_item_key_to_cpu(leaf, &found_key,
1307 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001308 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 found_key.type == BTRFS_EXTENT_DATA_KEY)
1310 path->slots[0]--;
1311 }
1312 check_prev = 0;
1313next_slot:
1314 leaf = path->nodes[0];
1315 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1316 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001317 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001318 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (ret > 0)
1320 break;
1321 leaf = path->nodes[0];
1322 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001323
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 nocow = 0;
1325 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001328
Filipe Manana1d512cb2015-11-09 00:33:58 +00001329 if (found_key.objectid > ino)
1330 break;
1331 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1332 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1333 path->slots[0]++;
1334 goto next_slot;
1335 }
1336 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 found_key.offset > end)
1338 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001339
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 if (found_key.offset > cur_offset) {
1341 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001342 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 goto out_check;
1344 }
Chris Masonc31f8832008-01-08 15:46:31 -05001345
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 fi = btrfs_item_ptr(leaf, path->slots[0],
1347 struct btrfs_file_extent_item);
1348 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001349
Josef Bacikcc95bef2013-04-04 14:31:27 -04001350 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001351 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1352 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 extent_end = found_key.offset +
1356 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001357 disk_num_bytes =
1358 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (extent_end <= start) {
1360 path->slots[0]++;
1361 goto next_slot;
1362 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001363 if (disk_bytenr == 0)
1364 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 if (btrfs_file_extent_compression(leaf, fi) ||
1366 btrfs_file_extent_encryption(leaf, fi) ||
1367 btrfs_file_extent_other_encoding(leaf, fi))
1368 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001369 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1370 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001371 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001373 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 found_key.offset -
1375 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001376 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001377 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001378 disk_bytenr += cur_offset - found_key.offset;
1379 num_bytes = min(end + 1, extent_end) - cur_offset;
1380 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001381 * if there are pending snapshots for this root,
1382 * we fall into common COW way.
1383 */
1384 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001385 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001386 if (!err)
1387 goto out_check;
1388 }
1389 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001390 * force cow if csum exists in the range.
1391 * this ensure that csum for a given extent are
1392 * either valid or do not exist.
1393 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001394 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001395 num_bytes)) {
1396 if (!nolock)
1397 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001399 }
1400 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1401 if (!nolock)
1402 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001403 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001404 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 nocow = 1;
1406 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1407 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001408 btrfs_file_extent_inline_len(leaf,
1409 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001410 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001411 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001412 } else {
1413 BUG_ON(1);
1414 }
1415out_check:
1416 if (extent_end <= start) {
1417 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001418 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001419 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001420 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001421 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001422 goto next_slot;
1423 }
1424 if (!nocow) {
1425 if (cow_start == (u64)-1)
1426 cow_start = cur_offset;
1427 cur_offset = extent_end;
1428 if (cur_offset > end)
1429 break;
1430 path->slots[0]++;
1431 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001432 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001433
David Sterbab3b4aa72011-04-21 01:20:15 +02001434 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001436 ret = cow_file_range(inode, locked_page,
1437 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001438 end, page_started, nr_written, 1,
1439 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001440 if (ret) {
1441 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001442 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001443 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001444 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001445 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001447 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001449 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001450
Yan Zhengd899e052008-10-30 14:25:28 -04001451 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001452 u64 orig_start = found_key.offset - extent_offset;
1453
1454 em = create_io_em(inode, cur_offset, num_bytes,
1455 orig_start,
1456 disk_bytenr, /* block_start */
1457 num_bytes, /* block_len */
1458 disk_num_bytes, /* orig_block_len */
1459 ram_bytes, BTRFS_COMPRESS_NONE,
1460 BTRFS_ORDERED_PREALLOC);
1461 if (IS_ERR(em)) {
1462 if (!nolock && nocow)
1463 btrfs_end_write_no_snapshoting(root);
1464 if (nocow)
1465 btrfs_dec_nocow_writers(fs_info,
1466 disk_bytenr);
1467 ret = PTR_ERR(em);
1468 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001469 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001470 free_extent_map(em);
1471 }
1472
1473 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001474 type = BTRFS_ORDERED_PREALLOC;
1475 } else {
1476 type = BTRFS_ORDERED_NOCOW;
1477 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001478
1479 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001480 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001481 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001482 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001483 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001484
Yan, Zhengefa56462010-05-16 10:49:59 -04001485 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001486 BTRFS_DATA_RELOC_TREE_OBJECTID)
1487 /*
1488 * Error handled later, as we must prevent
1489 * extent_clear_unlock_delalloc() in error handler
1490 * from freeing metadata of created ordered extent.
1491 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001492 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1493 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001494
Josef Bacikc2790a22013-07-29 11:20:47 -04001495 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001496 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001497 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001498 EXTENT_DELALLOC |
1499 EXTENT_CLEAR_DATA_RESV,
1500 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1501
Wang Shilonge9894fd2014-03-27 11:12:25 +08001502 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001503 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001504 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001505
1506 /*
1507 * btrfs_reloc_clone_csums() error, now we're OK to call error
1508 * handler, as metadata for created ordered extent will only
1509 * be freed by btrfs_finish_ordered_io().
1510 */
1511 if (ret)
1512 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001513 if (cur_offset > end)
1514 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001515 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001516 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001517
Josef Bacik17ca04a2012-05-31 15:58:55 -04001518 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001520 cur_offset = end;
1521 }
1522
Yan Zheng80ff3852008-10-30 14:20:02 -04001523 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001524 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1525 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001526 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001527 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 }
1529
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001530error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001531 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001532 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001533 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001534 EXTENT_DELALLOC | EXTENT_DEFRAG |
1535 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1536 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001537 PAGE_SET_WRITEBACK |
1538 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001539 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001540 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001541}
1542
Wang Shilong47059d92014-07-03 18:22:07 +08001543static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1544{
1545
1546 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1547 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1548 return 0;
1549
1550 /*
1551 * @defrag_bytes is a hint value, no spinlock held here,
1552 * if is not zero, it means the file is defragging.
1553 * Force cow if given extent needs to be defragged.
1554 */
1555 if (BTRFS_I(inode)->defrag_bytes &&
1556 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1557 EXTENT_DEFRAG, 0, NULL))
1558 return 1;
1559
1560 return 0;
1561}
1562
Chris Masond352ac62008-09-29 15:18:18 -04001563/*
1564 * extent_io.c call back to do delayed allocation processing
1565 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001566static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001567 u64 start, u64 end, int *page_started,
1568 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001569{
Josef Bacikc6100a42017-05-05 11:57:13 -04001570 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001571 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001572 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001573
Wang Shilong47059d92014-07-03 18:22:07 +08001574 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001575 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001576 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001577 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001578 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001579 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001580 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001581 ret = cow_file_range(inode, locked_page, start, end, end,
1582 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001583 } else {
1584 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1585 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001586 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001587 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001588 }
Qu Wenruo524272602017-03-08 10:25:52 +08001589 if (ret)
1590 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001591 return ret;
1592}
1593
Josef Bacikc6100a42017-05-05 11:57:13 -04001594static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001595 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001596{
Josef Bacikc6100a42017-05-05 11:57:13 -04001597 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001598 u64 size;
1599
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001601 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001602 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 size = orig->end - orig->start + 1;
1605 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001606 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001607 u64 new_size;
1608
1609 /*
Josef Bacikba117212015-03-13 15:01:24 -04001610 * See the explanation in btrfs_merge_extent_hook, the same
1611 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612 */
1613 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001614 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001615 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001616 num_extents += count_max_extents(new_size);
1617 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 return;
1619 }
1620
Josef Bacik9e0baf62011-07-15 15:16:44 +00001621 spin_lock(&BTRFS_I(inode)->lock);
1622 BTRFS_I(inode)->outstanding_extents++;
1623 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624}
1625
1626/*
1627 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1628 * extents so we can keep track of new extents that are just merged onto old
1629 * extents, such as when we are doing sequential writes, so we can properly
1630 * account for the metadata space we'll need.
1631 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001632static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001633 struct extent_state *new,
1634 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635{
Josef Bacikc6100a42017-05-05 11:57:13 -04001636 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001637 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001638 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639
Josef Bacik9ed74f22009-09-11 16:12:44 -04001640 /* not delalloc, ignore it */
1641 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001642 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643
Josef Bacik8461a3d2015-03-13 15:12:08 -04001644 if (new->start > other->start)
1645 new_size = new->end - other->start + 1;
1646 else
1647 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001648
1649 /* we're not bigger than the max, unreserve the space and go */
1650 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1651 spin_lock(&BTRFS_I(inode)->lock);
1652 BTRFS_I(inode)->outstanding_extents--;
1653 spin_unlock(&BTRFS_I(inode)->lock);
1654 return;
1655 }
1656
1657 /*
Josef Bacikba117212015-03-13 15:01:24 -04001658 * We have to add up either side to figure out how many extents were
1659 * accounted for before we merged into one big extent. If the number of
1660 * extents we accounted for is <= the amount we need for the new range
1661 * then we can return, otherwise drop. Think of it like this
1662 *
1663 * [ 4k][MAX_SIZE]
1664 *
1665 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1666 * need 2 outstanding extents, on one side we have 1 and the other side
1667 * we have 1 so they are == and we can return. But in this case
1668 *
1669 * [MAX_SIZE+4k][MAX_SIZE+4k]
1670 *
1671 * Each range on their own accounts for 2 extents, but merged together
1672 * they are only 3 extents worth of accounting, so we need to drop in
1673 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001674 */
Josef Bacikba117212015-03-13 15:01:24 -04001675 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001676 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001677 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001678 num_extents += count_max_extents(old_size);
1679 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001680 return;
1681
Josef Bacik9e0baf62011-07-15 15:16:44 +00001682 spin_lock(&BTRFS_I(inode)->lock);
1683 BTRFS_I(inode)->outstanding_extents--;
1684 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001685}
1686
Miao Xieeb73c1b2013-05-15 07:48:22 +00001687static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1688 struct inode *inode)
1689{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001690 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1691
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692 spin_lock(&root->delalloc_lock);
1693 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1694 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1695 &root->delalloc_inodes);
1696 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1697 &BTRFS_I(inode)->runtime_flags);
1698 root->nr_delalloc_inodes++;
1699 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001700 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001701 BUG_ON(!list_empty(&root->delalloc_root));
1702 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001703 &fs_info->delalloc_roots);
1704 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001705 }
1706 }
1707 spin_unlock(&root->delalloc_lock);
1708}
1709
1710static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001711 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001712{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001714
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001716 if (!list_empty(&inode->delalloc_inodes)) {
1717 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001718 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001719 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001720 root->nr_delalloc_inodes--;
1721 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 BUG_ON(list_empty(&root->delalloc_root));
1724 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001725 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001726 }
1727 }
1728 spin_unlock(&root->delalloc_lock);
1729}
1730
Chris Masond352ac62008-09-29 15:18:18 -04001731/*
1732 * extent_io.c set_bit_hook, used to track delayed allocation
1733 * bytes in this file, and to maintain the list of inodes that
1734 * have pending delalloc work to be done.
1735 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001736static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001737 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001738{
Josef Bacikc6100a42017-05-05 11:57:13 -04001739 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001740
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1742
Wang Shilong47059d92014-07-03 18:22:07 +08001743 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1744 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001745 /*
1746 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001747 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001748 * bit, which is only set or cleared with irqs on
1749 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001751 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001752 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001753 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001754
Josef Bacik9e0baf62011-07-15 15:16:44 +00001755 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001757 } else {
1758 spin_lock(&BTRFS_I(inode)->lock);
1759 BTRFS_I(inode)->outstanding_extents++;
1760 spin_unlock(&BTRFS_I(inode)->lock);
1761 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001762
Josef Bacik6a3891c2015-03-16 17:38:52 -04001763 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001765 return;
1766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001767 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1768 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001769 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001771 if (*bits & EXTENT_DEFRAG)
1772 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001774 &BTRFS_I(inode)->runtime_flags))
1775 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001776 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001777 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001778
1779 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1780 (*bits & EXTENT_DELALLOC_NEW)) {
1781 spin_lock(&BTRFS_I(inode)->lock);
1782 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1783 state->start;
1784 spin_unlock(&BTRFS_I(inode)->lock);
1785 }
Chris Mason291d6732008-01-29 15:55:23 -05001786}
1787
Chris Masond352ac62008-09-29 15:18:18 -04001788/*
1789 * extent_io.c clear_bit_hook, see set_bit_hook for why
1790 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001791static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001792 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001793 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001794{
Josef Bacikc6100a42017-05-05 11:57:13 -04001795 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001796 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001797 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001798 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001799
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001800 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001801 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001802 inode->defrag_bytes -= len;
1803 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001804
Chris Mason75eff682008-12-15 15:54:40 -05001805 /*
1806 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001807 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001808 * bit, which is only set or cleared with irqs on
1809 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001810 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001811 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001812 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001813
Josef Bacik9e0baf62011-07-15 15:16:44 +00001814 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001815 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001816 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001817 spin_lock(&inode->lock);
1818 inode->outstanding_extents -= num_extents;
1819 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001820 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001821
Josef Bacikb6d08f02013-09-27 14:57:43 -04001822 /*
1823 * We don't reserve metadata space for space cache inodes so we
1824 * don't need to call dellalloc_release_metadata if there is an
1825 * error.
1826 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001827 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001828 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001829 btrfs_delalloc_release_metadata(inode, len);
1830
Josef Bacik6a3891c2015-03-16 17:38:52 -04001831 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001832 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001833 return;
1834
Filipe Mananaa315e682017-03-06 23:04:20 +00001835 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1836 do_list && !(state->state & EXTENT_NORESERVE) &&
1837 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001838 btrfs_free_reserved_data_space_noquota(
1839 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001840 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001841
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1843 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001844 spin_lock(&inode->lock);
1845 inode->delalloc_bytes -= len;
1846 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001847 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001848 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001849 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001850 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001851 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001852
1853 if ((state->state & EXTENT_DELALLOC_NEW) &&
1854 (*bits & EXTENT_DELALLOC_NEW)) {
1855 spin_lock(&inode->lock);
1856 ASSERT(inode->new_delalloc_bytes >= len);
1857 inode->new_delalloc_bytes -= len;
1858 spin_unlock(&inode->lock);
1859 }
Chris Mason291d6732008-01-29 15:55:23 -05001860}
1861
Chris Masond352ac62008-09-29 15:18:18 -04001862/*
1863 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1864 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001865 *
1866 * return 1 if page cannot be merged to bio
1867 * return 0 if page can be merged to bio
1868 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001869 */
Mike Christie81a75f672016-06-05 14:31:54 -05001870int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001871 size_t size, struct bio *bio,
1872 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001873{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001874 struct inode *inode = page->mapping->host;
1875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001876 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001877 u64 length = 0;
1878 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001879 int ret;
1880
Chris Mason771ed682008-11-06 22:02:51 -05001881 if (bio_flags & EXTENT_BIO_COMPRESSED)
1882 return 0;
1883
Kent Overstreet4f024f32013-10-11 15:44:27 -07001884 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001885 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001886 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1887 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001888 if (ret < 0)
1889 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001890 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001891 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001892 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001893}
1894
Chris Masond352ac62008-09-29 15:18:18 -04001895/*
1896 * in order to insert checksums into the metadata in large chunks,
1897 * we wait until bio submission time. All the pages in the bio are
1898 * checksummed and sums are attached onto the ordered extent record.
1899 *
1900 * At IO completion time the cums attached on the ordered extent record
1901 * are inserted into the btree
1902 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001903static int __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001904 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001905 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001906{
Josef Bacikc6100a42017-05-05 11:57:13 -04001907 struct inode *inode = private_data;
Chris Mason065631f2008-02-20 12:07:25 -05001908 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001910 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001912 return 0;
1913}
Chris Masone0156402008-04-16 11:15:20 -04001914
Chris Mason4a69a412008-11-06 22:03:00 -05001915/*
1916 * in order to insert checksums into the metadata in large chunks,
1917 * we wait until bio submission time. All the pages in the bio are
1918 * checksummed and sums are attached onto the ordered extent record.
1919 *
1920 * At IO completion time the cums attached on the ordered extent record
1921 * are inserted into the btree
1922 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001923static int __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001924 int mirror_num, unsigned long bio_flags,
1925 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001926{
Josef Bacikc6100a42017-05-05 11:57:13 -04001927 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001928 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001929 int ret;
1930
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001931 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001932 if (ret) {
1933 bio->bi_error = ret;
1934 bio_endio(bio);
1935 }
Stefan Behrens61891922012-11-05 18:51:52 +01001936 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001937}
1938
Chris Masond352ac62008-09-29 15:18:18 -04001939/*
Chris Masoncad321a2008-12-17 14:51:42 -05001940 * extent_io.c submission hook. This does the right thing for csum calculation
1941 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001942 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001943static int btrfs_submit_bio_hook(void *private_data, struct bio *bio,
1944 int mirror_num, unsigned long bio_flags,
1945 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001946{
Josef Bacikc6100a42017-05-05 11:57:13 -04001947 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001948 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001949 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301950 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001951 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001952 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001953 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001954
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001955 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001956
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001957 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301958 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001959
Mike Christie37226b22016-06-05 14:31:52 -05001960 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001962 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001963 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001964
Chris Masond20f7042008-12-08 16:58:54 -05001965 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001966 ret = btrfs_submit_compressed_read(inode, bio,
1967 mirror_num,
1968 bio_flags);
1969 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001970 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001971 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001972 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001973 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001974 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001975 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001976 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001977 /* csum items have already been cloned */
1978 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1979 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001980 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001981 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1982 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001983 __btrfs_submit_bio_start,
1984 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001985 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001986 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001987 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001988 if (ret)
1989 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001990 }
1991
Chris Mason0b86a832008-03-24 15:01:56 -04001992mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001993 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001994
1995out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001996 if (ret < 0) {
1997 bio->bi_error = ret;
1998 bio_endio(bio);
1999 }
Stefan Behrens61891922012-11-05 18:51:52 +01002000 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002001}
Chris Mason6885f302008-02-20 16:11:05 -05002002
Chris Masond352ac62008-09-29 15:18:18 -04002003/*
2004 * given a list of ordered sums record them in the inode. This happens
2005 * at IO completion time based on sums calculated at bio submission time.
2006 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002007static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002008 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002009{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002010 struct btrfs_ordered_sum *sum;
2011
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002012 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002013 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002014 btrfs_csum_file_blocks(trans,
2015 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002016 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002017 }
2018 return 0;
2019}
2020
Josef Bacik2ac55d42010-02-03 19:33:23 +00002021int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002022 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002023{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002024 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002025 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002026 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002027}
2028
Chris Masond352ac62008-09-29 15:18:18 -04002029/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002030struct btrfs_writepage_fixup {
2031 struct page *page;
2032 struct btrfs_work work;
2033};
2034
Christoph Hellwigb2950862008-12-02 09:54:17 -05002035static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002036{
2037 struct btrfs_writepage_fixup *fixup;
2038 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002039 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002040 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002041 struct page *page;
2042 struct inode *inode;
2043 u64 page_start;
2044 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002045 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002046
2047 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2048 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002049again:
Chris Mason247e7432008-07-17 12:53:51 -04002050 lock_page(page);
2051 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2052 ClearPageChecked(page);
2053 goto out_page;
2054 }
2055
2056 inode = page->mapping->host;
2057 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002058 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002059
David Sterbaff13db42015-12-03 14:30:40 +01002060 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002061 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002062
2063 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002064 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002065 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002066
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002067 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002068 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002069 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002070 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2071 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002072 unlock_page(page);
2073 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002074 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002075 goto again;
2076 }
Chris Mason247e7432008-07-17 12:53:51 -04002077
Qu Wenruo364ecf32017-02-27 15:10:38 +08002078 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002079 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002080 if (ret) {
2081 mapping_set_error(page->mapping, ret);
2082 end_extent_writepage(page, ret, page_start, page_end);
2083 ClearPageChecked(page);
2084 goto out;
2085 }
2086
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002087 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2088 0);
Chris Mason247e7432008-07-17 12:53:51 -04002089 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002090 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002091out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002092 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2093 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002094out_page:
2095 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002097 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002098 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002099}
2100
2101/*
2102 * There are a few paths in the higher layers of the kernel that directly
2103 * set the page dirty bit without asking the filesystem if it is a
2104 * good idea. This causes problems because we want to make sure COW
2105 * properly happens and the data=ordered rules are followed.
2106 *
Chris Masonc8b97812008-10-29 14:49:59 -04002107 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002108 * hasn't been properly setup for IO. We kick off an async process
2109 * to fix it up. The async helper will wait for ordered extents, set
2110 * the delalloc bit and make it safe to write the page.
2111 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002112static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002113{
2114 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002115 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002116 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002117
Chris Mason8b62b722009-09-02 16:53:46 -04002118 /* this page is properly in the ordered list */
2119 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002120 return 0;
2121
2122 if (PageChecked(page))
2123 return -EAGAIN;
2124
2125 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2126 if (!fixup)
2127 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002128
Chris Mason247e7432008-07-17 12:53:51 -04002129 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002130 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002131 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2132 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002133 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002134 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002135 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002136}
2137
Yan Zhengd899e052008-10-30 14:25:28 -04002138static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2139 struct inode *inode, u64 file_pos,
2140 u64 disk_bytenr, u64 disk_num_bytes,
2141 u64 num_bytes, u64 ram_bytes,
2142 u8 compression, u8 encryption,
2143 u16 other_encoding, int extent_type)
2144{
2145 struct btrfs_root *root = BTRFS_I(inode)->root;
2146 struct btrfs_file_extent_item *fi;
2147 struct btrfs_path *path;
2148 struct extent_buffer *leaf;
2149 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002150 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002151 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002152 int ret;
2153
2154 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002155 if (!path)
2156 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002157
Chris Masona1ed8352009-09-11 12:27:37 -04002158 /*
2159 * we may be replacing one extent in the tree with another.
2160 * The new extent is pinned in the extent map, and we don't want
2161 * to drop it from the cache until it is completely in the btree.
2162 *
2163 * So, tell btrfs_drop_extents to leave this extent in the cache.
2164 * the caller is expected to unpin it and allow it to be merged
2165 * with the others.
2166 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002167 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2168 file_pos + num_bytes, NULL, 0,
2169 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002170 if (ret)
2171 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002172
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002173 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002174 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002175 ins.offset = file_pos;
2176 ins.type = BTRFS_EXTENT_DATA_KEY;
2177
2178 path->leave_spinning = 1;
2179 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2180 sizeof(*fi));
2181 if (ret)
2182 goto out;
2183 }
Yan Zhengd899e052008-10-30 14:25:28 -04002184 leaf = path->nodes[0];
2185 fi = btrfs_item_ptr(leaf, path->slots[0],
2186 struct btrfs_file_extent_item);
2187 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2188 btrfs_set_file_extent_type(leaf, fi, extent_type);
2189 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2190 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2191 btrfs_set_file_extent_offset(leaf, fi, 0);
2192 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2193 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2194 btrfs_set_file_extent_compression(leaf, fi, compression);
2195 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2196 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002197
Yan Zhengd899e052008-10-30 14:25:28 -04002198 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002199 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002200
2201 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002202
2203 ins.objectid = disk_bytenr;
2204 ins.offset = disk_num_bytes;
2205 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002206
Qu Wenruo297d7502015-09-08 17:08:37 +08002207 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002208 * Release the reserved range from inode dirty range map, as it is
2209 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002210 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002211 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2212 if (ret < 0)
2213 goto out;
2214 qg_released = ret;
2215 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2216 btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002217out:
Yan Zhengd899e052008-10-30 14:25:28 -04002218 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002219
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002220 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002221}
2222
Liu Bo38c227d2013-01-29 03:18:40 +00002223/* snapshot-aware defrag */
2224struct sa_defrag_extent_backref {
2225 struct rb_node node;
2226 struct old_sa_defrag_extent *old;
2227 u64 root_id;
2228 u64 inum;
2229 u64 file_pos;
2230 u64 extent_offset;
2231 u64 num_bytes;
2232 u64 generation;
2233};
2234
2235struct old_sa_defrag_extent {
2236 struct list_head list;
2237 struct new_sa_defrag_extent *new;
2238
2239 u64 extent_offset;
2240 u64 bytenr;
2241 u64 offset;
2242 u64 len;
2243 int count;
2244};
2245
2246struct new_sa_defrag_extent {
2247 struct rb_root root;
2248 struct list_head head;
2249 struct btrfs_path *path;
2250 struct inode *inode;
2251 u64 file_pos;
2252 u64 len;
2253 u64 bytenr;
2254 u64 disk_len;
2255 u8 compress_type;
2256};
2257
2258static int backref_comp(struct sa_defrag_extent_backref *b1,
2259 struct sa_defrag_extent_backref *b2)
2260{
2261 if (b1->root_id < b2->root_id)
2262 return -1;
2263 else if (b1->root_id > b2->root_id)
2264 return 1;
2265
2266 if (b1->inum < b2->inum)
2267 return -1;
2268 else if (b1->inum > b2->inum)
2269 return 1;
2270
2271 if (b1->file_pos < b2->file_pos)
2272 return -1;
2273 else if (b1->file_pos > b2->file_pos)
2274 return 1;
2275
2276 /*
2277 * [------------------------------] ===> (a range of space)
2278 * |<--->| |<---->| =============> (fs/file tree A)
2279 * |<---------------------------->| ===> (fs/file tree B)
2280 *
2281 * A range of space can refer to two file extents in one tree while
2282 * refer to only one file extent in another tree.
2283 *
2284 * So we may process a disk offset more than one time(two extents in A)
2285 * and locate at the same extent(one extent in B), then insert two same
2286 * backrefs(both refer to the extent in B).
2287 */
2288 return 0;
2289}
2290
2291static void backref_insert(struct rb_root *root,
2292 struct sa_defrag_extent_backref *backref)
2293{
2294 struct rb_node **p = &root->rb_node;
2295 struct rb_node *parent = NULL;
2296 struct sa_defrag_extent_backref *entry;
2297 int ret;
2298
2299 while (*p) {
2300 parent = *p;
2301 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2302
2303 ret = backref_comp(backref, entry);
2304 if (ret < 0)
2305 p = &(*p)->rb_left;
2306 else
2307 p = &(*p)->rb_right;
2308 }
2309
2310 rb_link_node(&backref->node, parent, p);
2311 rb_insert_color(&backref->node, root);
2312}
2313
2314/*
2315 * Note the backref might has changed, and in this case we just return 0.
2316 */
2317static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2318 void *ctx)
2319{
2320 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002321 struct old_sa_defrag_extent *old = ctx;
2322 struct new_sa_defrag_extent *new = old->new;
2323 struct btrfs_path *path = new->path;
2324 struct btrfs_key key;
2325 struct btrfs_root *root;
2326 struct sa_defrag_extent_backref *backref;
2327 struct extent_buffer *leaf;
2328 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002329 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002330 int slot;
2331 int ret;
2332 u64 extent_offset;
2333 u64 num_bytes;
2334
2335 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002336 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002337 return 0;
2338
2339 key.objectid = root_id;
2340 key.type = BTRFS_ROOT_ITEM_KEY;
2341 key.offset = (u64)-1;
2342
Liu Bo38c227d2013-01-29 03:18:40 +00002343 root = btrfs_read_fs_root_no_name(fs_info, &key);
2344 if (IS_ERR(root)) {
2345 if (PTR_ERR(root) == -ENOENT)
2346 return 0;
2347 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002348 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002349 inum, offset, root_id);
2350 return PTR_ERR(root);
2351 }
2352
2353 key.objectid = inum;
2354 key.type = BTRFS_EXTENT_DATA_KEY;
2355 if (offset > (u64)-1 << 32)
2356 key.offset = 0;
2357 else
2358 key.offset = offset;
2359
2360 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302361 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002362 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002363 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002364
2365 while (1) {
2366 cond_resched();
2367
2368 leaf = path->nodes[0];
2369 slot = path->slots[0];
2370
2371 if (slot >= btrfs_header_nritems(leaf)) {
2372 ret = btrfs_next_leaf(root, path);
2373 if (ret < 0) {
2374 goto out;
2375 } else if (ret > 0) {
2376 ret = 0;
2377 goto out;
2378 }
2379 continue;
2380 }
2381
2382 path->slots[0]++;
2383
2384 btrfs_item_key_to_cpu(leaf, &key, slot);
2385
2386 if (key.objectid > inum)
2387 goto out;
2388
2389 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2390 continue;
2391
2392 extent = btrfs_item_ptr(leaf, slot,
2393 struct btrfs_file_extent_item);
2394
2395 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2396 continue;
2397
Liu Boe68afa42013-07-01 22:13:26 +08002398 /*
2399 * 'offset' refers to the exact key.offset,
2400 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2401 * (key.offset - extent_offset).
2402 */
2403 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002404 continue;
2405
Liu Boe68afa42013-07-01 22:13:26 +08002406 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002407 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002408
Liu Bo38c227d2013-01-29 03:18:40 +00002409 if (extent_offset >= old->extent_offset + old->offset +
2410 old->len || extent_offset + num_bytes <=
2411 old->extent_offset + old->offset)
2412 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002413 break;
2414 }
2415
2416 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2417 if (!backref) {
2418 ret = -ENOENT;
2419 goto out;
2420 }
2421
2422 backref->root_id = root_id;
2423 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002424 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002425 backref->num_bytes = num_bytes;
2426 backref->extent_offset = extent_offset;
2427 backref->generation = btrfs_file_extent_generation(leaf, extent);
2428 backref->old = old;
2429 backref_insert(&new->root, backref);
2430 old->count++;
2431out:
2432 btrfs_release_path(path);
2433 WARN_ON(ret);
2434 return ret;
2435}
2436
2437static noinline bool record_extent_backrefs(struct btrfs_path *path,
2438 struct new_sa_defrag_extent *new)
2439{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002440 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002441 struct old_sa_defrag_extent *old, *tmp;
2442 int ret;
2443
2444 new->path = path;
2445
2446 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002447 ret = iterate_inodes_from_logical(old->bytenr +
2448 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002449 path, record_one_backref,
2450 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002451 if (ret < 0 && ret != -ENOENT)
2452 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002453
2454 /* no backref to be processed for this extent */
2455 if (!old->count) {
2456 list_del(&old->list);
2457 kfree(old);
2458 }
2459 }
2460
2461 if (list_empty(&new->head))
2462 return false;
2463
2464 return true;
2465}
2466
2467static int relink_is_mergable(struct extent_buffer *leaf,
2468 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002469 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002470{
Liu Bo116e0022013-08-02 16:30:40 +08002471 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002472 return 0;
2473
2474 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2475 return 0;
2476
Liu Bo116e0022013-08-02 16:30:40 +08002477 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2478 return 0;
2479
2480 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002481 btrfs_file_extent_other_encoding(leaf, fi))
2482 return 0;
2483
2484 return 1;
2485}
2486
2487/*
2488 * Note the backref might has changed, and in this case we just return 0.
2489 */
2490static noinline int relink_extent_backref(struct btrfs_path *path,
2491 struct sa_defrag_extent_backref *prev,
2492 struct sa_defrag_extent_backref *backref)
2493{
2494 struct btrfs_file_extent_item *extent;
2495 struct btrfs_file_extent_item *item;
2496 struct btrfs_ordered_extent *ordered;
2497 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002498 struct btrfs_root *root;
2499 struct btrfs_key key;
2500 struct extent_buffer *leaf;
2501 struct old_sa_defrag_extent *old = backref->old;
2502 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002504 struct inode *inode;
2505 struct extent_state *cached = NULL;
2506 int ret = 0;
2507 u64 start;
2508 u64 len;
2509 u64 lock_start;
2510 u64 lock_end;
2511 bool merge = false;
2512 int index;
2513
2514 if (prev && prev->root_id == backref->root_id &&
2515 prev->inum == backref->inum &&
2516 prev->file_pos + prev->num_bytes == backref->file_pos)
2517 merge = true;
2518
2519 /* step 1: get root */
2520 key.objectid = backref->root_id;
2521 key.type = BTRFS_ROOT_ITEM_KEY;
2522 key.offset = (u64)-1;
2523
Liu Bo38c227d2013-01-29 03:18:40 +00002524 index = srcu_read_lock(&fs_info->subvol_srcu);
2525
2526 root = btrfs_read_fs_root_no_name(fs_info, &key);
2527 if (IS_ERR(root)) {
2528 srcu_read_unlock(&fs_info->subvol_srcu, index);
2529 if (PTR_ERR(root) == -ENOENT)
2530 return 0;
2531 return PTR_ERR(root);
2532 }
Liu Bo38c227d2013-01-29 03:18:40 +00002533
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002534 if (btrfs_root_readonly(root)) {
2535 srcu_read_unlock(&fs_info->subvol_srcu, index);
2536 return 0;
2537 }
2538
Liu Bo38c227d2013-01-29 03:18:40 +00002539 /* step 2: get inode */
2540 key.objectid = backref->inum;
2541 key.type = BTRFS_INODE_ITEM_KEY;
2542 key.offset = 0;
2543
2544 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2545 if (IS_ERR(inode)) {
2546 srcu_read_unlock(&fs_info->subvol_srcu, index);
2547 return 0;
2548 }
2549
2550 srcu_read_unlock(&fs_info->subvol_srcu, index);
2551
2552 /* step 3: relink backref */
2553 lock_start = backref->file_pos;
2554 lock_end = backref->file_pos + backref->num_bytes - 1;
2555 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002556 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002557
2558 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2559 if (ordered) {
2560 btrfs_put_ordered_extent(ordered);
2561 goto out_unlock;
2562 }
2563
2564 trans = btrfs_join_transaction(root);
2565 if (IS_ERR(trans)) {
2566 ret = PTR_ERR(trans);
2567 goto out_unlock;
2568 }
2569
2570 key.objectid = backref->inum;
2571 key.type = BTRFS_EXTENT_DATA_KEY;
2572 key.offset = backref->file_pos;
2573
2574 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2575 if (ret < 0) {
2576 goto out_free_path;
2577 } else if (ret > 0) {
2578 ret = 0;
2579 goto out_free_path;
2580 }
2581
2582 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2583 struct btrfs_file_extent_item);
2584
2585 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2586 backref->generation)
2587 goto out_free_path;
2588
2589 btrfs_release_path(path);
2590
2591 start = backref->file_pos;
2592 if (backref->extent_offset < old->extent_offset + old->offset)
2593 start += old->extent_offset + old->offset -
2594 backref->extent_offset;
2595
2596 len = min(backref->extent_offset + backref->num_bytes,
2597 old->extent_offset + old->offset + old->len);
2598 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2599
2600 ret = btrfs_drop_extents(trans, root, inode, start,
2601 start + len, 1);
2602 if (ret)
2603 goto out_free_path;
2604again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002605 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002606 key.type = BTRFS_EXTENT_DATA_KEY;
2607 key.offset = start;
2608
Liu Boa09a0a72013-03-11 09:20:58 +00002609 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002610 if (merge) {
2611 struct btrfs_file_extent_item *fi;
2612 u64 extent_len;
2613 struct btrfs_key found_key;
2614
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002615 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002616 if (ret < 0)
2617 goto out_free_path;
2618
2619 path->slots[0]--;
2620 leaf = path->nodes[0];
2621 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2622
2623 fi = btrfs_item_ptr(leaf, path->slots[0],
2624 struct btrfs_file_extent_item);
2625 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2626
Liu Bo116e0022013-08-02 16:30:40 +08002627 if (extent_len + found_key.offset == start &&
2628 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002629 btrfs_set_file_extent_num_bytes(leaf, fi,
2630 extent_len + len);
2631 btrfs_mark_buffer_dirty(leaf);
2632 inode_add_bytes(inode, len);
2633
2634 ret = 1;
2635 goto out_free_path;
2636 } else {
2637 merge = false;
2638 btrfs_release_path(path);
2639 goto again;
2640 }
2641 }
2642
2643 ret = btrfs_insert_empty_item(trans, root, path, &key,
2644 sizeof(*extent));
2645 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002646 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002647 goto out_free_path;
2648 }
2649
2650 leaf = path->nodes[0];
2651 item = btrfs_item_ptr(leaf, path->slots[0],
2652 struct btrfs_file_extent_item);
2653 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2654 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2655 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2656 btrfs_set_file_extent_num_bytes(leaf, item, len);
2657 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2658 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2659 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2660 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2661 btrfs_set_file_extent_encryption(leaf, item, 0);
2662 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2663
2664 btrfs_mark_buffer_dirty(leaf);
2665 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002666 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002667
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002668 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002669 new->disk_len, 0,
2670 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002671 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002672 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002673 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002674 goto out_free_path;
2675 }
2676
2677 ret = 1;
2678out_free_path:
2679 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002680 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002681 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002682out_unlock:
2683 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2684 &cached, GFP_NOFS);
2685 iput(inode);
2686 return ret;
2687}
2688
Liu Bo6f519562013-10-29 10:45:05 +08002689static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2690{
2691 struct old_sa_defrag_extent *old, *tmp;
2692
2693 if (!new)
2694 return;
2695
2696 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002697 kfree(old);
2698 }
2699 kfree(new);
2700}
2701
Liu Bo38c227d2013-01-29 03:18:40 +00002702static void relink_file_extents(struct new_sa_defrag_extent *new)
2703{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002704 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002705 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002706 struct sa_defrag_extent_backref *backref;
2707 struct sa_defrag_extent_backref *prev = NULL;
2708 struct inode *inode;
2709 struct btrfs_root *root;
2710 struct rb_node *node;
2711 int ret;
2712
2713 inode = new->inode;
2714 root = BTRFS_I(inode)->root;
2715
2716 path = btrfs_alloc_path();
2717 if (!path)
2718 return;
2719
2720 if (!record_extent_backrefs(path, new)) {
2721 btrfs_free_path(path);
2722 goto out;
2723 }
2724 btrfs_release_path(path);
2725
2726 while (1) {
2727 node = rb_first(&new->root);
2728 if (!node)
2729 break;
2730 rb_erase(node, &new->root);
2731
2732 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2733
2734 ret = relink_extent_backref(path, prev, backref);
2735 WARN_ON(ret < 0);
2736
2737 kfree(prev);
2738
2739 if (ret == 1)
2740 prev = backref;
2741 else
2742 prev = NULL;
2743 cond_resched();
2744 }
2745 kfree(prev);
2746
2747 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002748out:
Liu Bo6f519562013-10-29 10:45:05 +08002749 free_sa_defrag_extent(new);
2750
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002751 atomic_dec(&fs_info->defrag_running);
2752 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002753}
2754
2755static struct new_sa_defrag_extent *
2756record_old_file_extents(struct inode *inode,
2757 struct btrfs_ordered_extent *ordered)
2758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002760 struct btrfs_root *root = BTRFS_I(inode)->root;
2761 struct btrfs_path *path;
2762 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002763 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002764 struct new_sa_defrag_extent *new;
2765 int ret;
2766
2767 new = kmalloc(sizeof(*new), GFP_NOFS);
2768 if (!new)
2769 return NULL;
2770
2771 new->inode = inode;
2772 new->file_pos = ordered->file_offset;
2773 new->len = ordered->len;
2774 new->bytenr = ordered->start;
2775 new->disk_len = ordered->disk_len;
2776 new->compress_type = ordered->compress_type;
2777 new->root = RB_ROOT;
2778 INIT_LIST_HEAD(&new->head);
2779
2780 path = btrfs_alloc_path();
2781 if (!path)
2782 goto out_kfree;
2783
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002784 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002785 key.type = BTRFS_EXTENT_DATA_KEY;
2786 key.offset = new->file_pos;
2787
2788 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2789 if (ret < 0)
2790 goto out_free_path;
2791 if (ret > 0 && path->slots[0] > 0)
2792 path->slots[0]--;
2793
2794 /* find out all the old extents for the file range */
2795 while (1) {
2796 struct btrfs_file_extent_item *extent;
2797 struct extent_buffer *l;
2798 int slot;
2799 u64 num_bytes;
2800 u64 offset;
2801 u64 end;
2802 u64 disk_bytenr;
2803 u64 extent_offset;
2804
2805 l = path->nodes[0];
2806 slot = path->slots[0];
2807
2808 if (slot >= btrfs_header_nritems(l)) {
2809 ret = btrfs_next_leaf(root, path);
2810 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002811 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002812 else if (ret > 0)
2813 break;
2814 continue;
2815 }
2816
2817 btrfs_item_key_to_cpu(l, &key, slot);
2818
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002819 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002820 break;
2821 if (key.type != BTRFS_EXTENT_DATA_KEY)
2822 break;
2823 if (key.offset >= new->file_pos + new->len)
2824 break;
2825
2826 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2827
2828 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2829 if (key.offset + num_bytes < new->file_pos)
2830 goto next;
2831
2832 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2833 if (!disk_bytenr)
2834 goto next;
2835
2836 extent_offset = btrfs_file_extent_offset(l, extent);
2837
2838 old = kmalloc(sizeof(*old), GFP_NOFS);
2839 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002840 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002841
2842 offset = max(new->file_pos, key.offset);
2843 end = min(new->file_pos + new->len, key.offset + num_bytes);
2844
2845 old->bytenr = disk_bytenr;
2846 old->extent_offset = extent_offset;
2847 old->offset = offset - key.offset;
2848 old->len = end - offset;
2849 old->new = new;
2850 old->count = 0;
2851 list_add_tail(&old->list, &new->head);
2852next:
2853 path->slots[0]++;
2854 cond_resched();
2855 }
2856
2857 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002858 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002859
2860 return new;
2861
Liu Bo38c227d2013-01-29 03:18:40 +00002862out_free_path:
2863 btrfs_free_path(path);
2864out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002865 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002866 return NULL;
2867}
2868
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002869static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002870 u64 start, u64 len)
2871{
2872 struct btrfs_block_group_cache *cache;
2873
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002875 ASSERT(cache);
2876
2877 spin_lock(&cache->lock);
2878 cache->delalloc_bytes -= len;
2879 spin_unlock(&cache->lock);
2880
2881 btrfs_put_block_group(cache);
2882}
2883
Chris Masond352ac62008-09-29 15:18:18 -04002884/* as ordered data IO finishes, this gets called so we can finish
2885 * an ordered extent if the range of bytes in the file it covers are
2886 * fully written.
2887 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002888static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002889{
Josef Bacik5fd02042012-05-02 14:00:54 -04002890 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002892 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002893 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002894 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002895 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002896 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002897 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002898 int ret = 0;
2899 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002900 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002901 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002902 bool range_locked = false;
2903 bool clear_new_delalloc_bytes = false;
2904
2905 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2906 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2907 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2908 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002909
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002910 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002911
Josef Bacik5fd02042012-05-02 14:00:54 -04002912 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2913 ret = -EIO;
2914 goto out;
2915 }
2916
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002917 btrfs_free_io_failure_record(BTRFS_I(inode),
2918 ordered_extent->file_offset,
2919 ordered_extent->file_offset +
2920 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002921
Josef Bacik77cef2e2013-08-29 13:57:21 -04002922 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2923 truncated = true;
2924 logical_len = ordered_extent->truncated_len;
2925 /* Truncated the entire extent, don't bother adding */
2926 if (!logical_len)
2927 goto out;
2928 }
2929
Yan, Zhengc2167752009-11-12 09:34:21 +00002930 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002931 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002932
2933 /*
2934 * For mwrite(mmap + memset to write) case, we still reserve
2935 * space for NOCOW range.
2936 * As NOCOW won't cause a new delayed ref, just free the space
2937 */
2938 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2939 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002940 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2941 if (nolock)
2942 trans = btrfs_join_transaction_nolock(root);
2943 else
2944 trans = btrfs_join_transaction(root);
2945 if (IS_ERR(trans)) {
2946 ret = PTR_ERR(trans);
2947 trans = NULL;
2948 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002949 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002950 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002951 ret = btrfs_update_inode_fallback(trans, root, inode);
2952 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002953 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002954 goto out;
2955 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002956
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002957 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002958 lock_extent_bits(io_tree, ordered_extent->file_offset,
2959 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002960 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961
Liu Bo38c227d2013-01-29 03:18:40 +00002962 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2963 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002964 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002965 if (ret) {
2966 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002967 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002968 /* the inode is shared */
2969 new = record_old_file_extents(inode, ordered_extent);
2970
2971 clear_extent_bit(io_tree, ordered_extent->file_offset,
2972 ordered_extent->file_offset + ordered_extent->len - 1,
2973 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2974 }
2975
Josef Bacik0cb59c92010-07-02 12:14:14 -04002976 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002977 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002978 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002979 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002980 if (IS_ERR(trans)) {
2981 ret = PTR_ERR(trans);
2982 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002983 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 }
Chris Masona79b7d42014-05-22 16:18:52 -07002985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002986 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002987
Chris Masonc8b97812008-10-29 14:49:59 -04002988 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002989 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002990 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002991 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002992 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002993 ordered_extent->file_offset,
2994 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002995 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002996 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002998 ret = insert_reserved_file_extent(trans, inode,
2999 ordered_extent->file_offset,
3000 ordered_extent->start,
3001 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003002 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003003 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003004 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003005 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003006 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003007 ordered_extent->start,
3008 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003009 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003010 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3011 ordered_extent->file_offset, ordered_extent->len,
3012 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003013 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003014 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003015 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003016 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003017
David Sterbadf9f6282017-02-10 19:35:37 +01003018 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003019
Josef Bacik6c760c02012-11-09 10:53:21 -05003020 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3021 ret = btrfs_update_inode_fallback(trans, root, inode);
3022 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003023 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003024 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003025 }
3026 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003027out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003028 if (range_locked || clear_new_delalloc_bytes) {
3029 unsigned int clear_bits = 0;
3030
3031 if (range_locked)
3032 clear_bits |= EXTENT_LOCKED;
3033 if (clear_new_delalloc_bytes)
3034 clear_bits |= EXTENT_DELALLOC_NEW;
3035 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3036 ordered_extent->file_offset,
3037 ordered_extent->file_offset +
3038 ordered_extent->len - 1,
3039 clear_bits,
3040 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3041 0, &cached_state, GFP_NOFS);
3042 }
3043
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003044 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003045 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3046 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003047 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003048 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003049
Josef Bacik77cef2e2013-08-29 13:57:21 -04003050 if (ret || truncated) {
3051 u64 start, end;
3052
3053 if (truncated)
3054 start = ordered_extent->file_offset + logical_len;
3055 else
3056 start = ordered_extent->file_offset;
3057 end = ordered_extent->file_offset + ordered_extent->len - 1;
3058 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3059
3060 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003061 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003062
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003063 /*
3064 * If the ordered extent had an IOERR or something else went
3065 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003066 * back to the allocator. We only free the extent in the
3067 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003068 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003069 if ((ret || !logical_len) &&
3070 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003071 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003072 btrfs_free_reserved_extent(fs_info,
3073 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003074 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003075 }
3076
3077
Josef Bacik5fd02042012-05-02 14:00:54 -04003078 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003079 * This needs to be done to make sure anybody waiting knows we are done
3080 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003081 */
3082 btrfs_remove_ordered_extent(inode, ordered_extent);
3083
Liu Bo38c227d2013-01-29 03:18:40 +00003084 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003085 if (new) {
3086 if (ret) {
3087 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003088 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003089 } else {
3090 relink_file_extents(new);
3091 }
3092 }
Liu Bo38c227d2013-01-29 03:18:40 +00003093
Chris Masone6dcd2d2008-07-17 12:53:50 -04003094 /* once for us */
3095 btrfs_put_ordered_extent(ordered_extent);
3096 /* once for the tree */
3097 btrfs_put_ordered_extent(ordered_extent);
3098
Josef Bacik5fd02042012-05-02 14:00:54 -04003099 return ret;
3100}
3101
3102static void finish_ordered_fn(struct btrfs_work *work)
3103{
3104 struct btrfs_ordered_extent *ordered_extent;
3105 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3106 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003107}
3108
David Sterbac3988d62017-02-17 15:18:32 +01003109static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003110 struct extent_state *state, int uptodate)
3111{
Josef Bacik5fd02042012-05-02 14:00:54 -04003112 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003113 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003114 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003115 struct btrfs_workqueue *wq;
3116 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003117
liubo1abe9b82011-03-24 11:18:59 +00003118 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3119
Chris Mason8b62b722009-09-02 16:53:46 -04003120 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003121 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3122 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003123 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003124
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003125 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003126 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003127 func = btrfs_freespace_write_helper;
3128 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003129 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003130 func = btrfs_endio_write_helper;
3131 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003132
Liu Bo9e0af232014-08-15 23:36:53 +08003133 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3134 NULL);
3135 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003136}
3137
Miao Xiedc380ae2014-09-12 18:43:55 +08003138static int __readpage_endio_check(struct inode *inode,
3139 struct btrfs_io_bio *io_bio,
3140 int icsum, struct page *page,
3141 int pgoff, u64 start, size_t len)
3142{
3143 char *kaddr;
3144 u32 csum_expected;
3145 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003146
3147 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3148
3149 kaddr = kmap_atomic(page);
3150 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003151 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003152 if (csum != csum_expected)
3153 goto zeroit;
3154
3155 kunmap_atomic(kaddr);
3156 return 0;
3157zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003158 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003159 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003160 memset(kaddr + pgoff, 1, len);
3161 flush_dcache_page(page);
3162 kunmap_atomic(kaddr);
3163 if (csum_expected == 0)
3164 return 0;
3165 return -EIO;
3166}
3167
Chris Masond352ac62008-09-29 15:18:18 -04003168/*
Chris Masond352ac62008-09-29 15:18:18 -04003169 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003170 * if there's a match, we allow the bio to finish. If not, the code in
3171 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003172 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003173static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3174 u64 phy_offset, struct page *page,
3175 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003176{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003177 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003178 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003179 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003180 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003181
Chris Masond20f7042008-12-08 16:58:54 -05003182 if (PageChecked(page)) {
3183 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003184 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003185 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003186
3187 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003188 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003189
Yan Zheng17d217f2008-12-12 10:03:38 -05003190 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003191 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003192 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003193 return 0;
3194 }
3195
Miao Xiefacc8a222013-07-25 19:22:34 +08003196 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003197 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3198 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003199}
Chris Masonb888db2b2007-08-27 16:49:44 -04003200
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003201void btrfs_add_delayed_iput(struct inode *inode)
3202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003204 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003205
3206 if (atomic_add_unless(&inode->i_count, -1, 1))
3207 return;
3208
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003209 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003210 if (binode->delayed_iput_count == 0) {
3211 ASSERT(list_empty(&binode->delayed_iput));
3212 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3213 } else {
3214 binode->delayed_iput_count++;
3215 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003216 spin_unlock(&fs_info->delayed_iput_lock);
3217}
3218
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003219void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003220{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003221
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003222 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003223 while (!list_empty(&fs_info->delayed_iputs)) {
3224 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003225
David Sterba8089fe62015-11-19 14:15:51 +01003226 inode = list_first_entry(&fs_info->delayed_iputs,
3227 struct btrfs_inode, delayed_iput);
3228 if (inode->delayed_iput_count) {
3229 inode->delayed_iput_count--;
3230 list_move_tail(&inode->delayed_iput,
3231 &fs_info->delayed_iputs);
3232 } else {
3233 list_del_init(&inode->delayed_iput);
3234 }
3235 spin_unlock(&fs_info->delayed_iput_lock);
3236 iput(&inode->vfs_inode);
3237 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003238 }
David Sterba8089fe62015-11-19 14:15:51 +01003239 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003240}
3241
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003243 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244 * files in the subvolume, it removes orphan item and frees block_rsv
3245 * structure.
3246 */
3247void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3248 struct btrfs_root *root)
3249{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003250 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003251 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252 int ret;
3253
Josef Bacik8a35d952012-05-23 14:26:42 -04003254 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3256 return;
3257
Josef Bacik90290e12011-12-02 15:44:12 -05003258 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003259 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003260 spin_unlock(&root->orphan_lock);
3261 return;
3262 }
3263
3264 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3265 spin_unlock(&root->orphan_lock);
3266 return;
3267 }
3268
3269 block_rsv = root->orphan_block_rsv;
3270 root->orphan_block_rsv = NULL;
3271 spin_unlock(&root->orphan_lock);
3272
Miao Xie27cdeb72014-04-02 19:51:05 +08003273 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003275 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003277 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003278 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003279 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003280 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3281 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 }
3283
Josef Bacik90290e12011-12-02 15:44:12 -05003284 if (block_rsv) {
3285 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003286 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 }
3288}
3289
3290/*
Josef Bacik7b128762008-07-24 12:17:14 -04003291 * This creates an orphan entry for the given inode in case something goes
3292 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 *
3294 * NOTE: caller of this function should reserve 5 units of metadata for
3295 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003296 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003297int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3298 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003299{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003300 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3301 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 struct btrfs_block_rsv *block_rsv = NULL;
3303 int reserve = 0;
3304 int insert = 0;
3305 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003306
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003308 block_rsv = btrfs_alloc_block_rsv(fs_info,
3309 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003310 if (!block_rsv)
3311 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003312 }
3313
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 spin_lock(&root->orphan_lock);
3315 if (!root->orphan_block_rsv) {
3316 root->orphan_block_rsv = block_rsv;
3317 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003318 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 block_rsv = NULL;
3320 }
Josef Bacik7b128762008-07-24 12:17:14 -04003321
Josef Bacik8a35d952012-05-23 14:26:42 -04003322 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003323 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324#if 0
3325 /*
3326 * For proper ENOSPC handling, we should do orphan
3327 * cleanup when mounting. But this introduces backward
3328 * compatibility issue.
3329 */
3330 if (!xchg(&root->orphan_item_inserted, 1))
3331 insert = 2;
3332 else
3333 insert = 1;
3334#endif
3335 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003336 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 }
Josef Bacik7b128762008-07-24 12:17:14 -04003338
Josef Bacik72ac3c02012-05-23 14:13:11 -04003339 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003340 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003343
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 /* grab metadata reservation from transaction handle */
3345 if (reserve) {
3346 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003347 ASSERT(!ret);
3348 if (ret) {
3349 atomic_dec(&root->orphan_inodes);
3350 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003351 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003352 if (insert)
3353 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003354 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003355 return ret;
3356 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 }
3358
3359 /* insert an orphan item to track this unlinked/truncated file */
3360 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003361 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003362 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003363 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003364 if (reserve) {
3365 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003366 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003367 btrfs_orphan_release_metadata(inode);
3368 }
3369 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003370 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003371 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003372 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003373 return ret;
3374 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003375 }
3376 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003377 }
3378
3379 /* insert an orphan item to track subvolume contains orphan files */
3380 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003382 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003383 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003384 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003385 return ret;
3386 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003387 }
3388 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003389}
3390
3391/*
3392 * We have done the truncate/delete so we can go ahead and remove the orphan
3393 * item for this particular inode.
3394 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003395static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003396 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003397{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003398 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003399 int delete_item = 0;
3400 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003401 int ret = 0;
3402
Yan, Zhengd68fc572010-05-16 10:49:58 -04003403 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003404 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003405 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003406 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003407
Josef Bacik72ac3c02012-05-23 14:13:11 -04003408 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003409 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003410 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003411 spin_unlock(&root->orphan_lock);
3412
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003413 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003414 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003415 if (trans)
3416 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003417 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003418 }
Josef Bacik7b128762008-07-24 12:17:14 -04003419
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003420 if (release_rsv)
3421 btrfs_orphan_release_metadata(inode);
3422
Josef Bacik4ef31a42013-08-13 14:10:08 -04003423 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003424}
3425
3426/*
3427 * this cleans up any orphans that may be left on the list from the last use
3428 * of this root.
3429 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003430int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003431{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003432 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003433 struct btrfs_path *path;
3434 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003435 struct btrfs_key key, found_key;
3436 struct btrfs_trans_handle *trans;
3437 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003438 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003439 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3440
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003442 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003443
3444 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003445 if (!path) {
3446 ret = -ENOMEM;
3447 goto out;
3448 }
David Sterbae4058b52015-11-27 16:31:35 +01003449 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003450
3451 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003452 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003453 key.offset = (u64)-1;
3454
Josef Bacik7b128762008-07-24 12:17:14 -04003455 while (1) {
3456 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 if (ret < 0)
3458 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003459
3460 /*
3461 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003462 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003463 * find the key and see if we have stuff that matches
3464 */
3465 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003466 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003467 if (path->slots[0] == 0)
3468 break;
3469 path->slots[0]--;
3470 }
3471
3472 /* pull out the item */
3473 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003474 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3475
3476 /* make sure the item matches what we want */
3477 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3478 break;
David Sterba962a2982014-06-04 18:41:45 +02003479 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003480 break;
3481
3482 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003483 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003484
3485 /*
3486 * this is where we are basically btrfs_lookup, without the
3487 * crossing root thing. we store the inode number in the
3488 * offset of the orphan item.
3489 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003490
3491 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003492 btrfs_err(fs_info,
3493 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003494 ret = -EINVAL;
3495 goto out;
3496 }
3497
3498 last_objectid = found_key.offset;
3499
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003500 found_key.objectid = found_key.offset;
3501 found_key.type = BTRFS_INODE_ITEM_KEY;
3502 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003503 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303504 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003505 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003506 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003507
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003508 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003509 struct btrfs_root *dead_root;
3510 struct btrfs_fs_info *fs_info = root->fs_info;
3511 int is_dead_root = 0;
3512
3513 /*
3514 * this is an orphan in the tree root. Currently these
3515 * could come from 2 sources:
3516 * a) a snapshot deletion in progress
3517 * b) a free space cache inode
3518 * We need to distinguish those two, as the snapshot
3519 * orphan must not get deleted.
3520 * find_dead_roots already ran before us, so if this
3521 * is a snapshot deletion, we should find the root
3522 * in the dead_roots list
3523 */
3524 spin_lock(&fs_info->trans_lock);
3525 list_for_each_entry(dead_root, &fs_info->dead_roots,
3526 root_list) {
3527 if (dead_root->root_key.objectid ==
3528 found_key.objectid) {
3529 is_dead_root = 1;
3530 break;
3531 }
3532 }
3533 spin_unlock(&fs_info->trans_lock);
3534 if (is_dead_root) {
3535 /* prevent this orphan from being found again */
3536 key.offset = found_key.objectid - 1;
3537 continue;
3538 }
3539 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003540 /*
3541 * Inode is already gone but the orphan item is still there,
3542 * kill the orphan item.
3543 */
Filipe Manana67710892016-06-06 11:51:25 +01003544 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003545 trans = btrfs_start_transaction(root, 1);
3546 if (IS_ERR(trans)) {
3547 ret = PTR_ERR(trans);
3548 goto out;
3549 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003550 btrfs_debug(fs_info, "auto deleting %Lu",
3551 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003552 ret = btrfs_del_orphan_item(trans, root,
3553 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003554 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003555 if (ret)
3556 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003557 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003558 }
Josef Bacik7b128762008-07-24 12:17:14 -04003559
Josef Bacik7b128762008-07-24 12:17:14 -04003560 /*
3561 * add this inode to the orphan list so btrfs_orphan_del does
3562 * the proper thing when we hit it
3563 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003564 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3565 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003566 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003567
Josef Bacik7b128762008-07-24 12:17:14 -04003568 /* if we have links, this was a truncate, lets do that */
3569 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303570 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003571 iput(inode);
3572 continue;
3573 }
Josef Bacik7b128762008-07-24 12:17:14 -04003574 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003575
3576 /* 1 for the orphan item deletion. */
3577 trans = btrfs_start_transaction(root, 1);
3578 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003579 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003580 ret = PTR_ERR(trans);
3581 goto out;
3582 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003583 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003584 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003585 if (ret) {
3586 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003587 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003588 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003589
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003590 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003591 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003592 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003593 } else {
3594 nr_unlink++;
3595 }
3596
3597 /* this will do delete_inode and everything for us */
3598 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003599 if (ret)
3600 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003601 }
Miao Xie3254c872011-11-10 20:45:05 -05003602 /* release the path since we're done with it */
3603 btrfs_release_path(path);
3604
Yan, Zhengd68fc572010-05-16 10:49:58 -04003605 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3606
3607 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003608 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003609 (u64)-1);
3610
Miao Xie27cdeb72014-04-02 19:51:05 +08003611 if (root->orphan_block_rsv ||
3612 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003613 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003614 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003615 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003616 }
Josef Bacik7b128762008-07-24 12:17:14 -04003617
3618 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003619 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003620 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003621 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003622
3623out:
3624 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003625 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003626 btrfs_free_path(path);
3627 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003628}
3629
Chris Masond352ac62008-09-29 15:18:18 -04003630/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003631 * very simple check to peek ahead in the leaf looking for xattrs. If we
3632 * don't find any xattrs, we know there can't be any acls.
3633 *
3634 * slot is the slot the inode is in, objectid is the objectid of the inode
3635 */
3636static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003637 int slot, u64 objectid,
3638 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003639{
3640 u32 nritems = btrfs_header_nritems(leaf);
3641 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003642 static u64 xattr_access = 0;
3643 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003644 int scanned = 0;
3645
Josef Bacikf23b5a52013-06-19 10:16:26 -04003646 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003647 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3648 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3649 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3650 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003651 }
3652
Chris Mason46a53cc2009-04-27 11:47:50 -04003653 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003654 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003655 while (slot < nritems) {
3656 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3657
3658 /* we found a different objectid, there must not be acls */
3659 if (found_key.objectid != objectid)
3660 return 0;
3661
3662 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003663 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003664 if (*first_xattr_slot == -1)
3665 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003666 if (found_key.offset == xattr_access ||
3667 found_key.offset == xattr_default)
3668 return 1;
3669 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003670
3671 /*
3672 * we found a key greater than an xattr key, there can't
3673 * be any acls later on
3674 */
3675 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3676 return 0;
3677
3678 slot++;
3679 scanned++;
3680
3681 /*
3682 * it goes inode, inode backrefs, xattrs, extents,
3683 * so if there are a ton of hard links to an inode there can
3684 * be a lot of backrefs. Don't waste time searching too hard,
3685 * this is just an optimization
3686 */
3687 if (scanned >= 8)
3688 break;
3689 }
3690 /* we hit the end of the leaf before we found an xattr or
3691 * something larger than an xattr. We have to assume the inode
3692 * has acls
3693 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003694 if (*first_xattr_slot == -1)
3695 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003696 return 1;
3697}
3698
3699/*
Chris Masond352ac62008-09-29 15:18:18 -04003700 * read an inode from the btree into the in-memory inode
3701 */
Filipe Manana67710892016-06-06 11:51:25 +01003702static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003703{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003704 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003705 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003706 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003707 struct btrfs_inode_item *inode_item;
3708 struct btrfs_root *root = BTRFS_I(inode)->root;
3709 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003710 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003711 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003712 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003713 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003714 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003715 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003716
3717 ret = btrfs_fill_inode(inode, &rdev);
3718 if (!ret)
3719 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003720
3721 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003722 if (!path) {
3723 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003724 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003725 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003726
Chris Mason39279cc2007-06-12 06:35:45 -04003727 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003728
Chris Mason39279cc2007-06-12 06:35:45 -04003729 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003730 if (ret) {
3731 if (ret > 0)
3732 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003733 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003734 }
Chris Mason39279cc2007-06-12 06:35:45 -04003735
Chris Mason5f39d392007-10-15 16:14:19 -04003736 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003737
3738 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003739 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003740
Chris Mason5f39d392007-10-15 16:14:19 -04003741 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3742 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003743 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003744 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003745 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3746 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003747 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003748
David Sterbaa937b972014-12-12 17:39:12 +01003749 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3750 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003751
David Sterbaa937b972014-12-12 17:39:12 +01003752 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3753 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
David Sterbaa937b972014-12-12 17:39:12 +01003755 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3756 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003757
chandan r9cc97d62012-07-04 12:48:07 +05303758 BTRFS_I(inode)->i_otime.tv_sec =
3759 btrfs_timespec_sec(leaf, &inode_item->otime);
3760 BTRFS_I(inode)->i_otime.tv_nsec =
3761 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003762
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003763 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003764 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003765 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3766
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003767 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003768 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003769 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003770 rdev = btrfs_inode_rdev(leaf, inode_item);
3771
Josef Bacikaec74772008-07-24 12:12:38 -04003772 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003773 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003774
3775cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003776 /*
3777 * If we were modified in the current generation and evicted from memory
3778 * and then re-read we need to do a full sync since we don't have any
3779 * idea about which extents were modified before we were evicted from
3780 * cache.
3781 *
3782 * This is required for both inode re-read from disk and delayed inode
3783 * in delayed_nodes_tree.
3784 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003785 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003786 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3787 &BTRFS_I(inode)->runtime_flags);
3788
Filipe Mananabde6c242015-07-24 00:00:19 +01003789 /*
3790 * We don't persist the id of the transaction where an unlink operation
3791 * against the inode was last made. So here we assume the inode might
3792 * have been evicted, and therefore the exact value of last_unlink_trans
3793 * lost, and set it to last_trans to avoid metadata inconsistencies
3794 * between the inode and its parent if the inode is fsync'ed and the log
3795 * replayed. For example, in the scenario:
3796 *
3797 * touch mydir/foo
3798 * ln mydir/foo mydir/bar
3799 * sync
3800 * unlink mydir/bar
3801 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3802 * xfs_io -c fsync mydir/foo
3803 * <power failure>
3804 * mount fs, triggers fsync log replay
3805 *
3806 * We must make sure that when we fsync our inode foo we also log its
3807 * parent inode, otherwise after log replay the parent still has the
3808 * dentry with the "bar" name but our inode foo has a link count of 1
3809 * and doesn't have an inode ref with the name "bar" anymore.
3810 *
3811 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003812 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003813 * transaction commits on fsync if our inode is a directory, or if our
3814 * inode is not a directory, logging its parent unnecessarily.
3815 */
3816 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3817
Miao Xie67de1172013-12-26 13:07:06 +08003818 path->slots[0]++;
3819 if (inode->i_nlink != 1 ||
3820 path->slots[0] >= btrfs_header_nritems(leaf))
3821 goto cache_acl;
3822
3823 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003824 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003825 goto cache_acl;
3826
3827 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3828 if (location.type == BTRFS_INODE_REF_KEY) {
3829 struct btrfs_inode_ref *ref;
3830
3831 ref = (struct btrfs_inode_ref *)ptr;
3832 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3833 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3834 struct btrfs_inode_extref *extref;
3835
3836 extref = (struct btrfs_inode_extref *)ptr;
3837 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3838 extref);
3839 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003840cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003841 /*
3842 * try to precache a NULL acl entry for files that don't have
3843 * any xattrs or acls
3844 */
Li Zefan33345d012011-04-20 10:31:50 +08003845 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003846 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003847 if (first_xattr_slot != -1) {
3848 path->slots[0] = first_xattr_slot;
3849 ret = btrfs_load_inode_props(inode, path);
3850 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003851 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003852 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003853 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003854 root->root_key.objectid, ret);
3855 }
3856 btrfs_free_path(path);
3857
Al Viro72c04902009-06-24 16:58:48 -04003858 if (!maybe_acls)
3859 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003860
Chris Mason39279cc2007-06-12 06:35:45 -04003861 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003862 case S_IFREG:
3863 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003864 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003865 inode->i_fop = &btrfs_file_operations;
3866 inode->i_op = &btrfs_file_inode_operations;
3867 break;
3868 case S_IFDIR:
3869 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003870 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 break;
3872 case S_IFLNK:
3873 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003874 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003875 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3876 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003877 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003878 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003879 init_special_inode(inode, inode->i_mode, rdev);
3880 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003881 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003882
3883 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003884 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003885
3886make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003887 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003888 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003889 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003890}
3891
Chris Masond352ac62008-09-29 15:18:18 -04003892/*
3893 * given a leaf and an inode, copy the inode fields into the leaf
3894 */
Chris Masone02119d2008-09-05 16:13:11 -04003895static void fill_inode_item(struct btrfs_trans_handle *trans,
3896 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003897 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003898 struct inode *inode)
3899{
Liu Bo51fab692012-12-27 09:01:21 +00003900 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003901
Liu Bo51fab692012-12-27 09:01:21 +00003902 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003903
Liu Bo51fab692012-12-27 09:01:21 +00003904 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3905 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3906 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3907 &token);
3908 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3909 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003910
David Sterbaa937b972014-12-12 17:39:12 +01003911 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003912 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003913 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003914 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003915
David Sterbaa937b972014-12-12 17:39:12 +01003916 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003917 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003918 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003919 inode->i_mtime.tv_nsec, &token);
3920
David Sterbaa937b972014-12-12 17:39:12 +01003921 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003922 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003923 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003924 inode->i_ctime.tv_nsec, &token);
3925
chandan r9cc97d62012-07-04 12:48:07 +05303926 btrfs_set_token_timespec_sec(leaf, &item->otime,
3927 BTRFS_I(inode)->i_otime.tv_sec, &token);
3928 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3929 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3930
Liu Bo51fab692012-12-27 09:01:21 +00003931 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3932 &token);
3933 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3934 &token);
3935 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3936 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3937 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3938 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3939 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003940}
3941
Chris Masond352ac62008-09-29 15:18:18 -04003942/*
3943 * copy everything in the in-memory inode into the btree.
3944 */
Chris Mason21151332011-11-10 20:39:08 -05003945static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003946 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003947{
3948 struct btrfs_inode_item *inode_item;
3949 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003950 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003951 int ret;
3952
3953 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003954 if (!path)
3955 return -ENOMEM;
3956
Chris Masonb9473432009-03-13 11:00:37 -04003957 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003958 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3959 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003960 if (ret) {
3961 if (ret > 0)
3962 ret = -ENOENT;
3963 goto failed;
3964 }
3965
Chris Mason5f39d392007-10-15 16:14:19 -04003966 leaf = path->nodes[0];
3967 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003968 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003969
Chris Masone02119d2008-09-05 16:13:11 -04003970 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003971 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003972 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003973 ret = 0;
3974failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003975 btrfs_free_path(path);
3976 return ret;
3977}
3978
Chris Masond352ac62008-09-29 15:18:18 -04003979/*
Chris Mason21151332011-11-10 20:39:08 -05003980 * copy everything in the in-memory inode into the btree.
3981 */
3982noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3983 struct btrfs_root *root, struct inode *inode)
3984{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003985 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003986 int ret;
3987
3988 /*
3989 * If the inode is a free space inode, we can deadlock during commit
3990 * if we put it into the delayed code.
3991 *
3992 * The data relocation inode should also be directly updated
3993 * without delay
3994 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003995 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003996 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003997 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003998 btrfs_update_root_times(trans, root);
3999
Chris Mason21151332011-11-10 20:39:08 -05004000 ret = btrfs_delayed_update_inode(trans, root, inode);
4001 if (!ret)
4002 btrfs_set_inode_last_trans(trans, inode);
4003 return ret;
4004 }
4005
4006 return btrfs_update_inode_item(trans, root, inode);
4007}
4008
Josef Bacikbe6aef62012-10-22 15:43:12 -04004009noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4010 struct btrfs_root *root,
4011 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004012{
4013 int ret;
4014
4015 ret = btrfs_update_inode(trans, root, inode);
4016 if (ret == -ENOSPC)
4017 return btrfs_update_inode_item(trans, root, inode);
4018 return ret;
4019}
4020
4021/*
Chris Masond352ac62008-09-29 15:18:18 -04004022 * unlink helper that gets used here in inode.c and in the tree logging
4023 * recovery code. It remove a link in a directory with a given name, and
4024 * also drops the back refs in the inode to the directory
4025 */
Al Viro92986792011-03-04 17:14:37 +00004026static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4027 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004028 struct btrfs_inode *dir,
4029 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004030 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004031{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004032 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004034 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004035 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004037 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004038 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004039 u64 ino = btrfs_ino(inode);
4040 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004041
4042 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004043 if (!path) {
4044 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004045 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004046 }
4047
Chris Masonb9473432009-03-13 11:00:37 -04004048 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004049 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004050 name, name_len, -1);
4051 if (IS_ERR(di)) {
4052 ret = PTR_ERR(di);
4053 goto err;
4054 }
4055 if (!di) {
4056 ret = -ENOENT;
4057 goto err;
4058 }
Chris Mason5f39d392007-10-15 16:14:19 -04004059 leaf = path->nodes[0];
4060 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004061 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004062 if (ret)
4063 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004064 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004065
Miao Xie67de1172013-12-26 13:07:06 +08004066 /*
4067 * If we don't have dir index, we have to get it by looking up
4068 * the inode ref, since we get the inode ref, remove it directly,
4069 * it is unnecessary to do delayed deletion.
4070 *
4071 * But if we have dir index, needn't search inode ref to get it.
4072 * Since the inode ref is close to the inode item, it is better
4073 * that we delay to delete it, and just do this deletion when
4074 * we update the inode item.
4075 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004076 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004077 ret = btrfs_delayed_delete_inode_ref(inode);
4078 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004079 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004080 goto skip_backref;
4081 }
4082 }
4083
Li Zefan33345d012011-04-20 10:31:50 +08004084 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4085 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004086 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004087 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004088 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004089 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004090 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004091 goto err;
4092 }
Miao Xie67de1172013-12-26 13:07:06 +08004093skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004094 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004097 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 }
Chris Mason39279cc2007-06-12 06:35:45 -04004099
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004100 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4101 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004102 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004103 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 goto err;
4105 }
Chris Masone02119d2008-09-05 16:13:11 -04004106
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004107 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4108 index);
Chris Mason6418c962010-10-30 07:34:24 -04004109 if (ret == -ENOENT)
4110 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004111 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004112 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004113err:
4114 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004115 if (ret)
4116 goto out;
4117
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004118 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004119 inode_inc_iversion(&inode->vfs_inode);
4120 inode_inc_iversion(&dir->vfs_inode);
4121 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4122 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4123 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004124out:
Chris Mason39279cc2007-06-12 06:35:45 -04004125 return ret;
4126}
4127
Al Viro92986792011-03-04 17:14:37 +00004128int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4129 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004130 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004131 const char *name, int name_len)
4132{
4133 int ret;
4134 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4135 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004136 drop_nlink(&inode->vfs_inode);
4137 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004138 }
4139 return ret;
4140}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004141
4142/*
4143 * helper to start transaction for unlink and rmdir.
4144 *
Josef Bacikd52be812013-05-29 14:54:47 -04004145 * unlink and rmdir are special in btrfs, they do not always free space, so
4146 * if we cannot make our reservations the normal way try and see if there is
4147 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4148 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149 */
Josef Bacikd52be812013-05-29 14:54:47 -04004150static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004151{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004152 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004153
Josef Bacike70bea52011-10-11 14:18:24 -04004154 /*
4155 * 1 for the possible orphan item
4156 * 1 for the dir item
4157 * 1 for the dir index
4158 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004159 * 1 for the inode
4160 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004161 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004162}
4163
Chris Mason39279cc2007-06-12 06:35:45 -04004164static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4165{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004166 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004168 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004169 int ret;
4170
Josef Bacikd52be812013-05-29 14:54:47 -04004171 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004172 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004173 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004174
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004175 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4176 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004177
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4179 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4180 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004181 if (ret)
4182 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004183
Yan, Zhenga22285a2010-05-16 10:48:46 -04004184 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004185 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004186 if (ret)
4187 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004188 }
Josef Bacik7b128762008-07-24 12:17:14 -04004189
Tsutomu Itohb5324022011-07-19 07:27:20 +00004190out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004191 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004192 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004193 return ret;
4194}
4195
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004196int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4197 struct btrfs_root *root,
4198 struct inode *dir, u64 objectid,
4199 const char *name, int name_len)
4200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004201 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 struct btrfs_path *path;
4203 struct extent_buffer *leaf;
4204 struct btrfs_dir_item *di;
4205 struct btrfs_key key;
4206 u64 index;
4207 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004208 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209
4210 path = btrfs_alloc_path();
4211 if (!path)
4212 return -ENOMEM;
4213
Li Zefan33345d012011-04-20 10:31:50 +08004214 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004215 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004216 if (IS_ERR_OR_NULL(di)) {
4217 if (!di)
4218 ret = -ENOENT;
4219 else
4220 ret = PTR_ERR(di);
4221 goto out;
4222 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004223
4224 leaf = path->nodes[0];
4225 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4226 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4227 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004228 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004229 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004230 goto out;
4231 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004232 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004234 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4235 root->root_key.objectid, dir_ino,
4236 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004238 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004239 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240 goto out;
4241 }
Li Zefan33345d012011-04-20 10:31:50 +08004242 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004244 if (IS_ERR_OR_NULL(di)) {
4245 if (!di)
4246 ret = -ENOENT;
4247 else
4248 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004249 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 goto out;
4251 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252
4253 leaf = path->nodes[0];
4254 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004255 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 index = key.offset;
4257 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004258 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004260 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004262 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 goto out;
4264 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004265
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004266 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004267 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004268 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004269 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004271 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004273 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275}
4276
Chris Mason39279cc2007-06-12 06:35:45 -04004277static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4278{
David Howells2b0143b2015-03-17 22:25:59 +00004279 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004280 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004281 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004282 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004283 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004284
David Sterbab3ae2442012-09-13 16:04:34 -06004285 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004286 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004287 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004288 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004289
Josef Bacikd52be812013-05-29 14:54:47 -04004290 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004291 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004292 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004293
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004294 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004295 err = btrfs_unlink_subvol(trans, root, dir,
4296 BTRFS_I(inode)->location.objectid,
4297 dentry->d_name.name,
4298 dentry->d_name.len);
4299 goto out;
4300 }
4301
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004302 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004303 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004304 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004305
Filipe Manana44f714d2016-06-06 16:11:13 +01004306 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4307
Chris Mason39279cc2007-06-12 06:35:45 -04004308 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004309 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4310 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4311 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004312 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004313 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004314 /*
4315 * Propagate the last_unlink_trans value of the deleted dir to
4316 * its parent directory. This is to prevent an unrecoverable
4317 * log tree in the case we do something like this:
4318 * 1) create dir foo
4319 * 2) create snapshot under dir foo
4320 * 3) delete the snapshot
4321 * 4) rmdir foo
4322 * 5) mkdir foo
4323 * 6) fsync foo or some file inside foo
4324 */
4325 if (last_unlink_trans >= trans->transid)
4326 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4327 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004328out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004329 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004330 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004331
Chris Mason39279cc2007-06-12 06:35:45 -04004332 return err;
4333}
4334
Chris Mason28f75a02015-02-04 06:59:29 -08004335static int truncate_space_check(struct btrfs_trans_handle *trans,
4336 struct btrfs_root *root,
4337 u64 bytes_deleted)
4338{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004339 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004340 int ret;
4341
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004342 /*
4343 * This is only used to apply pressure to the enospc system, we don't
4344 * intend to use this reservation at all.
4345 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004346 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004347 bytes_deleted *= fs_info->nodesize;
4348 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004349 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004350 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004351 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004352 trans->transid,
4353 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004354 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004355 }
Chris Mason28f75a02015-02-04 06:59:29 -08004356 return ret;
4357
4358}
4359
Filipe Manana0305cd52015-10-16 12:34:25 +01004360static int truncate_inline_extent(struct inode *inode,
4361 struct btrfs_path *path,
4362 struct btrfs_key *found_key,
4363 const u64 item_end,
4364 const u64 new_size)
4365{
4366 struct extent_buffer *leaf = path->nodes[0];
4367 int slot = path->slots[0];
4368 struct btrfs_file_extent_item *fi;
4369 u32 size = (u32)(new_size - found_key->offset);
4370 struct btrfs_root *root = BTRFS_I(inode)->root;
4371
4372 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4373
4374 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4375 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004376 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004377
4378 /*
4379 * Zero out the remaining of the last page of our inline extent,
4380 * instead of directly truncating our inline extent here - that
4381 * would be much more complex (decompressing all the data, then
4382 * compressing the truncated data, which might be bigger than
4383 * the size of the inline extent, resize the extent, etc).
4384 * We release the path because to get the page we might need to
4385 * read the extent item from disk (data not in the page cache).
4386 */
4387 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304388 return btrfs_truncate_block(inode, offset, page_end - offset,
4389 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004390 }
4391
4392 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4393 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004394 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004395
4396 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4397 inode_sub_bytes(inode, item_end + 1 - new_size);
4398
4399 return 0;
4400}
4401
Chris Mason323ac952008-10-01 19:05:46 -04004402/*
Chris Mason39279cc2007-06-12 06:35:45 -04004403 * this can truncate away extent items, csum items and directory items.
4404 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004405 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004406 *
4407 * csum items that cross the new i_size are truncated to the new size
4408 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004409 *
4410 * min_type is the minimum key type to truncate down to. If set to 0, this
4411 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004412 */
Yan, Zheng80825102009-11-12 09:35:36 +00004413int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4414 struct btrfs_root *root,
4415 struct inode *inode,
4416 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004418 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004419 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004420 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004422 struct btrfs_key key;
4423 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004424 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004425 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004426 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004428 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004429 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004430 int found_extent;
4431 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004432 int pending_del_nr = 0;
4433 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004434 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004435 int ret;
4436 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004437 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004438 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004439 bool be_nice = 0;
4440 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004441 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004442
4443 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004444
Chris Mason28ed1342014-12-17 09:41:04 -08004445 /*
4446 * for non-free space inodes and ref cows, we want to back off from
4447 * time to time
4448 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004449 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004450 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4451 be_nice = 1;
4452
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004453 path = btrfs_alloc_path();
4454 if (!path)
4455 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004456 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004457
Josef Bacik5dc562c2012-08-17 13:14:17 -04004458 /*
4459 * We want to drop from the next block forward in case this new size is
4460 * not block aligned since we will be keeping the last block of the
4461 * extent just the way it is.
4462 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004463 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004464 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004465 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004466 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004467 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004468
Miao Xie16cdcec2011-04-22 18:12:22 +08004469 /*
4470 * This function is also used to drop the items in the log tree before
4471 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4472 * it is used to drop the loged items. So we shouldn't kill the delayed
4473 * items.
4474 */
4475 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004476 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004477
Li Zefan33345d012011-04-20 10:31:50 +08004478 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004480 key.type = (u8)-1;
4481
Chris Mason85e21ba2008-01-29 15:11:36 -05004482search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004483 /*
4484 * with a 16K leaf size and 128MB extents, you can actually queue
4485 * up a huge file in a single leaf. Most of the time that
4486 * bytes_deleted is > 0, it will be huge by the time we get here
4487 */
Byongho Leeee221842015-12-15 01:42:10 +09004488 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004489 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004490 err = -EAGAIN;
4491 goto error;
4492 }
4493 }
4494
4495
Chris Masonb9473432009-03-13 11:00:37 -04004496 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004497 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004498 if (ret < 0) {
4499 err = ret;
4500 goto out;
4501 }
Chris Masond3977122009-01-05 21:25:51 -05004502
Chris Mason85e21ba2008-01-29 15:11:36 -05004503 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004504 /* there are no items in the tree for us to truncate, we're
4505 * done
4506 */
Yan, Zheng80825102009-11-12 09:35:36 +00004507 if (path->slots[0] == 0)
4508 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004509 path->slots[0]--;
4510 }
4511
Chris Masond3977122009-01-05 21:25:51 -05004512 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004513 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004514 leaf = path->nodes[0];
4515 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004516 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004517
Li Zefan33345d012011-04-20 10:31:50 +08004518 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004519 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004520
Chris Mason85e21ba2008-01-29 15:11:36 -05004521 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004522 break;
4523
Chris Mason5f39d392007-10-15 16:14:19 -04004524 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004525 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004526 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004527 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004528 extent_type = btrfs_file_extent_type(leaf, fi);
4529 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004530 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004531 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004532
4533 trace_btrfs_truncate_show_fi_regular(
4534 BTRFS_I(inode), leaf, fi,
4535 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004536 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004537 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004538 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004539
4540 trace_btrfs_truncate_show_fi_inline(
4541 BTRFS_I(inode), leaf, fi, path->slots[0],
4542 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004543 }
Yan008630c2007-11-07 13:31:09 -05004544 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004545 }
Yan, Zheng80825102009-11-12 09:35:36 +00004546 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004547 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004548 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004549 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004550 break;
4551 if (found_key.offset >= new_size)
4552 del_item = 1;
4553 else
4554 del_item = 0;
4555 }
Chris Mason39279cc2007-06-12 06:35:45 -04004556 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004557 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004558 if (found_type != BTRFS_EXTENT_DATA_KEY)
4559 goto delete;
4560
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004561 if (del_item)
4562 last_size = found_key.offset;
4563 else
4564 last_size = new_size;
4565
Chris Mason179e29e2007-11-01 11:28:41 -04004566 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004567 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004568 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004569 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004570 u64 orig_num_bytes =
4571 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004572 extent_num_bytes = ALIGN(new_size -
4573 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004574 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004575 btrfs_set_file_extent_num_bytes(leaf, fi,
4576 extent_num_bytes);
4577 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004578 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004579 if (test_bit(BTRFS_ROOT_REF_COWS,
4580 &root->state) &&
4581 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004582 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004583 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004584 } else {
Chris Masondb945352007-10-15 16:15:53 -04004585 extent_num_bytes =
4586 btrfs_file_extent_disk_num_bytes(leaf,
4587 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004588 extent_offset = found_key.offset -
4589 btrfs_file_extent_offset(leaf, fi);
4590
Chris Mason39279cc2007-06-12 06:35:45 -04004591 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004592 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004593 if (extent_start != 0) {
4594 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004595 if (test_bit(BTRFS_ROOT_REF_COWS,
4596 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004597 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004598 }
4599 }
Chris Mason90692182008-02-08 13:49:28 -05004600 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004601 /*
4602 * we can't truncate inline items that have had
4603 * special encodings
4604 */
4605 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004606 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4607 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004608
4609 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004610 * Need to release path in order to truncate a
4611 * compressed extent. So delete any accumulated
4612 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004613 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004614 if (btrfs_file_extent_compression(leaf, fi) !=
4615 BTRFS_COMPRESS_NONE && pending_del_nr) {
4616 err = btrfs_del_items(trans, root, path,
4617 pending_del_slot,
4618 pending_del_nr);
4619 if (err) {
4620 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004621 err);
4622 goto error;
4623 }
4624 pending_del_nr = 0;
4625 }
4626
4627 err = truncate_inline_extent(inode, path,
4628 &found_key,
4629 item_end,
4630 new_size);
4631 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004632 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004633 goto error;
4634 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004635 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4636 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004637 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004638 }
Chris Mason39279cc2007-06-12 06:35:45 -04004639 }
Chris Mason179e29e2007-11-01 11:28:41 -04004640delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004641 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004642 if (!pending_del_nr) {
4643 /* no pending yet, add ourselves */
4644 pending_del_slot = path->slots[0];
4645 pending_del_nr = 1;
4646 } else if (pending_del_nr &&
4647 path->slots[0] + 1 == pending_del_slot) {
4648 /* hop on the pending chunk */
4649 pending_del_nr++;
4650 pending_del_slot = path->slots[0];
4651 } else {
Chris Masond3977122009-01-05 21:25:51 -05004652 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004653 }
Chris Mason39279cc2007-06-12 06:35:45 -04004654 } else {
4655 break;
4656 }
Chris Mason28f75a02015-02-04 06:59:29 -08004657 should_throttle = 0;
4658
Miao Xie27cdeb72014-04-02 19:51:05 +08004659 if (found_extent &&
4660 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004661 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004662 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004663 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004664 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665 extent_num_bytes, 0,
4666 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004667 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004668 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004669 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4670 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004671 trans->delayed_ref_updates * 2,
4672 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004673 if (be_nice) {
4674 if (truncate_space_check(trans, root,
4675 extent_num_bytes)) {
4676 should_end = 1;
4677 }
4678 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004679 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004680 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004681 }
Chris Mason39279cc2007-06-12 06:35:45 -04004682 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004683
Yan, Zheng80825102009-11-12 09:35:36 +00004684 if (found_type == BTRFS_INODE_ITEM_KEY)
4685 break;
4686
4687 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004688 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004689 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004690 if (pending_del_nr) {
4691 ret = btrfs_del_items(trans, root, path,
4692 pending_del_slot,
4693 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004694 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004695 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004696 goto error;
4697 }
Yan, Zheng80825102009-11-12 09:35:36 +00004698 pending_del_nr = 0;
4699 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004700 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004701 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004702 unsigned long updates = trans->delayed_ref_updates;
4703 if (updates) {
4704 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004705 ret = btrfs_run_delayed_refs(trans,
4706 fs_info,
4707 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004708 if (ret && !err)
4709 err = ret;
4710 }
4711 }
Chris Mason28f75a02015-02-04 06:59:29 -08004712 /*
4713 * if we failed to refill our space rsv, bail out
4714 * and let the transaction restart
4715 */
4716 if (should_end) {
4717 err = -EAGAIN;
4718 goto error;
4719 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004720 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004721 } else {
4722 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004723 }
Chris Mason39279cc2007-06-12 06:35:45 -04004724 }
Yan, Zheng80825102009-11-12 09:35:36 +00004725out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004726 if (pending_del_nr) {
4727 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4728 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004729 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004730 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004731 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004732error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004733 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4734 ASSERT(last_size >= new_size);
4735 if (!err && last_size > new_size)
4736 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004737 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004738 }
Chris Mason28ed1342014-12-17 09:41:04 -08004739
Chris Mason39279cc2007-06-12 06:35:45 -04004740 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004741
Byongho Leeee221842015-12-15 01:42:10 +09004742 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004743 unsigned long updates = trans->delayed_ref_updates;
4744 if (updates) {
4745 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004746 ret = btrfs_run_delayed_refs(trans, fs_info,
4747 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004748 if (ret && !err)
4749 err = ret;
4750 }
4751 }
Yan, Zheng80825102009-11-12 09:35:36 +00004752 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004753}
4754
Chris Masona52d9a82007-08-27 16:49:44 -04004755/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304756 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004757 * @inode - inode that we're zeroing
4758 * @from - the offset to start zeroing
4759 * @len - the length to zero, 0 to zero the entire range respective to the
4760 * offset
4761 * @front - zero up to the offset instead of from the offset on
4762 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004764 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004765 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304766int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004767 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004768{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004769 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004770 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4772 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004773 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004774 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004776 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004777 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004779 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004780 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004781 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 u64 block_start;
4783 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004784
Josef Bacik2aaa6652012-08-29 14:27:18 -04004785 if ((offset & (blocksize - 1)) == 0 &&
4786 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004787 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788
Qu Wenruo364ecf32017-02-27 15:10:38 +08004789 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004791 if (ret)
4792 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004793
Chris Mason211c17f2008-05-15 09:13:45 -04004794again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004795 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004796 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004797 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 round_down(from, blocksize),
4799 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004800 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004801 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004802 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004803
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304804 block_start = round_down(from, blocksize);
4805 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806
Chris Masona52d9a82007-08-27 16:49:44 -04004807 if (!PageUptodate(page)) {
4808 ret = btrfs_readpage(NULL, page);
4809 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004810 if (page->mapping != mapping) {
4811 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004812 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004813 goto again;
4814 }
Chris Masona52d9a82007-08-27 16:49:44 -04004815 if (!PageUptodate(page)) {
4816 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004817 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004818 }
4819 }
Chris Mason211c17f2008-05-15 09:13:45 -04004820 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004823 set_page_extent_mapped(page);
4824
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004828 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004830 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004831 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 btrfs_put_ordered_extent(ordered);
4833 goto again;
4834 }
4835
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304836 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004837 EXTENT_DIRTY | EXTENT_DELALLOC |
4838 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004839 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004840
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004842 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004843 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004845 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004846 goto out_unlock;
4847 }
4848
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304849 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004850 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004852 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004853 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 memset(kaddr + (block_start - page_offset(page)),
4855 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004856 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 memset(kaddr + (block_start - page_offset(page)) + offset,
4858 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004859 flush_dcache_page(page);
4860 kunmap(page);
4861 }
Chris Mason247e7432008-07-17 12:53:51 -04004862 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004863 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304864 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004865 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004866
Chris Mason89642222008-07-24 09:41:53 -04004867out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004868 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304869 btrfs_delalloc_release_space(inode, block_start,
4870 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004871 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004872 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004873out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004874 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004875 return ret;
4876}
4877
Josef Bacik16e75492013-10-22 12:18:51 -04004878static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4879 u64 offset, u64 len)
4880{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004882 struct btrfs_trans_handle *trans;
4883 int ret;
4884
4885 /*
4886 * Still need to make sure the inode looks like it's been updated so
4887 * that any holes get logged if we fsync.
4888 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004889 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4890 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004891 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4892 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4893 return 0;
4894 }
4895
4896 /*
4897 * 1 - for the one we're dropping
4898 * 1 - for the one we're adding
4899 * 1 - for updating the inode.
4900 */
4901 trans = btrfs_start_transaction(root, 3);
4902 if (IS_ERR(trans))
4903 return PTR_ERR(trans);
4904
4905 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4906 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004907 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004908 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004909 return ret;
4910 }
4911
David Sterbaf85b7372017-01-20 14:54:07 +01004912 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4913 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004914 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004915 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004916 else
4917 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004918 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004919 return ret;
4920}
4921
Josef Bacik695a0d02011-03-04 15:46:53 -05004922/*
4923 * This function puts in dummy file extents for the area we're creating a hole
4924 * for. So if we are truncating this file to a larger size we need to insert
4925 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4926 * the range between oldsize and size
4927 */
Josef Bacika41ad392011-01-31 15:30:16 -05004928int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004929{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004931 struct btrfs_root *root = BTRFS_I(inode)->root;
4932 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004933 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004934 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004935 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4937 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004938 u64 last_byte;
4939 u64 cur_offset;
4940 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004941 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004942
Josef Bacika71754f2013-06-17 17:14:39 -04004943 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304944 * If our size started in the middle of a block we need to zero out the
4945 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004946 * expose stale data.
4947 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304948 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004949 if (err)
4950 return err;
4951
Yan Zheng9036c102008-10-30 14:19:41 -04004952 if (size <= hole_start)
4953 return 0;
4954
Yan Zheng9036c102008-10-30 14:19:41 -04004955 while (1) {
4956 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004957
David Sterbaff13db42015-12-03 14:30:40 +01004958 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004959 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004960 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004961 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004962 if (!ordered)
4963 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004964 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4965 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004966 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004967 btrfs_put_ordered_extent(ordered);
4968 }
4969
Yan Zheng9036c102008-10-30 14:19:41 -04004970 cur_offset = hole_start;
4971 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004972 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004973 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004974 if (IS_ERR(em)) {
4975 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004976 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004977 break;
4978 }
Yan Zheng9036c102008-10-30 14:19:41 -04004979 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004980 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004981 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004982 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004983 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004984
Josef Bacik16e75492013-10-22 12:18:51 -04004985 err = maybe_insert_hole(root, inode, cur_offset,
4986 hole_size);
4987 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004988 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004989 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004990 cur_offset + hole_size - 1, 0);
4991 hole_em = alloc_extent_map();
4992 if (!hole_em) {
4993 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4994 &BTRFS_I(inode)->runtime_flags);
4995 goto next;
4996 }
4997 hole_em->start = cur_offset;
4998 hole_em->len = hole_size;
4999 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005000
Josef Bacik5dc562c2012-08-17 13:14:17 -04005001 hole_em->block_start = EXTENT_MAP_HOLE;
5002 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005003 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005004 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005005 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005006 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005007 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005008
5009 while (1) {
5010 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005011 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005012 write_unlock(&em_tree->lock);
5013 if (err != -EEXIST)
5014 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005015 btrfs_drop_extent_cache(BTRFS_I(inode),
5016 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005017 cur_offset +
5018 hole_size - 1, 0);
5019 }
5020 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005021 }
Josef Bacik16e75492013-10-22 12:18:51 -04005022next:
Yan Zheng9036c102008-10-30 14:19:41 -04005023 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005024 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005025 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005026 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005027 break;
5028 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005029 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005030 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5031 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005032 return err;
5033}
5034
Eric Sandeen3972f262013-01-12 02:57:22 +00005035static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005036{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005037 struct btrfs_root *root = BTRFS_I(inode)->root;
5038 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005039 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005040 loff_t newsize = attr->ia_size;
5041 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005042 int ret;
5043
Eric Sandeen3972f262013-01-12 02:57:22 +00005044 /*
5045 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5046 * special case where we need to update the times despite not having
5047 * these flags set. For all other operations the VFS set these flags
5048 * explicitly if it wants a timestamp update.
5049 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005050 if (newsize != oldsize) {
5051 inode_inc_iversion(inode);
5052 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5053 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005054 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005055 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005056
Josef Bacika41ad392011-01-31 15:30:16 -05005057 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005058 /*
5059 * Don't do an expanding truncate while snapshoting is ongoing.
5060 * This is to ensure the snapshot captures a fully consistent
5061 * state of this file - if the snapshot captures this expanding
5062 * truncation, it must capture all writes that happened before
5063 * this truncation.
5064 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005065 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005066 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005067 if (ret) {
5068 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005069 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005070 }
Yan, Zheng80825102009-11-12 09:35:36 +00005071
Miao Xief4a2f4c2011-12-14 20:12:01 -05005072 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 if (IS_ERR(trans)) {
5074 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005075 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005076 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005077
5078 i_size_write(inode, newsize);
5079 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305080 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005081 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005082 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005083 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005084 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005085
Josef Bacika41ad392011-01-31 15:30:16 -05005086 /*
5087 * We're truncating a file that used to have good data down to
5088 * zero. Make sure it gets into the ordered flush list so that
5089 * any new writes get down to disk quickly.
5090 */
5091 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005092 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5093 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005094
Josef Bacikf3fe8202013-01-07 17:03:21 -05005095 /*
5096 * 1 for the orphan item we're going to add
5097 * 1 for the orphan item deletion.
5098 */
5099 trans = btrfs_start_transaction(root, 2);
5100 if (IS_ERR(trans))
5101 return PTR_ERR(trans);
5102
5103 /*
5104 * We need to do this in case we fail at _any_ point during the
5105 * actual truncate. Once we do the truncate_setsize we could
5106 * invalidate pages which forces any outstanding ordered io to
5107 * be instantly completed which will give us extents that need
5108 * to be truncated. If we fail to get an orphan inode down we
5109 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005110 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005111 * will be consistent.
5112 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005113 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005114 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005115 if (ret)
5116 return ret;
5117
Josef Bacika41ad392011-01-31 15:30:16 -05005118 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5119 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005120
5121 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005122 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005123 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005124 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005125
Josef Bacika41ad392011-01-31 15:30:16 -05005126 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005127 if (ret && inode->i_nlink) {
5128 int err;
5129
Liu Bo19fd2df2016-12-01 13:01:02 -08005130 /* To get a stable disk_i_size */
5131 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5132 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005133 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005134 return err;
5135 }
5136
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005137 /*
5138 * failed to truncate, disk_i_size is only adjusted down
5139 * as we remove extents, so it should represent the true
5140 * size of the inode, so reset the in memory size and
5141 * delete our orphan entry.
5142 */
5143 trans = btrfs_join_transaction(root);
5144 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005145 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005146 return ret;
5147 }
5148 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005149 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005150 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005151 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005152 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005153 }
Yan, Zheng80825102009-11-12 09:35:36 +00005154 }
5155
Josef Bacika41ad392011-01-31 15:30:16 -05005156 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005157}
5158
Chris Mason39279cc2007-06-12 06:35:45 -04005159static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5160{
David Howells2b0143b2015-03-17 22:25:59 +00005161 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005162 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005163 int err;
5164
Li Zefanb83cc962010-12-20 16:04:08 +08005165 if (btrfs_root_readonly(root))
5166 return -EROFS;
5167
Jan Kara31051c82016-05-26 16:55:18 +02005168 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005169 if (err)
5170 return err;
5171
Chris Mason5a3f23d2009-03-31 13:27:11 -04005172 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005173 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005174 if (err)
5175 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005176 }
Yan Zheng9036c102008-10-30 14:19:41 -04005177
Christoph Hellwig10257742010-06-04 11:30:02 +02005178 if (attr->ia_valid) {
5179 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005180 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005181 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005182
Josef Bacik22c44fe2011-11-30 10:45:38 -05005183 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005184 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005185 }
5186
Chris Mason39279cc2007-06-12 06:35:45 -04005187 return err;
5188}
Chris Mason61295eb2008-01-14 16:24:38 -05005189
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005190/*
5191 * While truncating the inode pages during eviction, we get the VFS calling
5192 * btrfs_invalidatepage() against each page of the inode. This is slow because
5193 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5194 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5195 * extent_state structures over and over, wasting lots of time.
5196 *
5197 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5198 * those expensive operations on a per page basis and do only the ordered io
5199 * finishing, while we release here the extent_map and extent_state structures,
5200 * without the excessive merging and splitting.
5201 */
5202static void evict_inode_truncate_pages(struct inode *inode)
5203{
5204 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5205 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5206 struct rb_node *node;
5207
5208 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005209 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005210
5211 write_lock(&map_tree->lock);
5212 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5213 struct extent_map *em;
5214
5215 node = rb_first(&map_tree->map);
5216 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005217 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5218 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005219 remove_extent_mapping(map_tree, em);
5220 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005221 if (need_resched()) {
5222 write_unlock(&map_tree->lock);
5223 cond_resched();
5224 write_lock(&map_tree->lock);
5225 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005226 }
5227 write_unlock(&map_tree->lock);
5228
Filipe Manana6ca07092015-05-26 00:55:42 +01005229 /*
5230 * Keep looping until we have no more ranges in the io tree.
5231 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005232 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5233 * still in progress (unlocked the pages in the bio but did not yet
5234 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005235 * ranges can still be locked and eviction started because before
5236 * submitting those bios, which are executed by a separate task (work
5237 * queue kthread), inode references (inode->i_count) were not taken
5238 * (which would be dropped in the end io callback of each bio).
5239 * Therefore here we effectively end up waiting for those bios and
5240 * anyone else holding locked ranges without having bumped the inode's
5241 * reference count - if we don't do it, when they access the inode's
5242 * io_tree to unlock a range it may be too late, leading to an
5243 * use-after-free issue.
5244 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005245 spin_lock(&io_tree->lock);
5246 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5247 struct extent_state *state;
5248 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005249 u64 start;
5250 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005251
5252 node = rb_first(&io_tree->state);
5253 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005254 start = state->start;
5255 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005256 spin_unlock(&io_tree->lock);
5257
David Sterbaff13db42015-12-03 14:30:40 +01005258 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005259
5260 /*
5261 * If still has DELALLOC flag, the extent didn't reach disk,
5262 * and its reserved space won't be freed by delayed_ref.
5263 * So we need to free its reserved space here.
5264 * (Refer to comment in btrfs_invalidatepage, case 2)
5265 *
5266 * Note, end is the bytenr of last byte, so we need + 1 here.
5267 */
5268 if (state->state & EXTENT_DELALLOC)
5269 btrfs_qgroup_free_data(inode, start, end - start + 1);
5270
Filipe Manana6ca07092015-05-26 00:55:42 +01005271 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005272 EXTENT_LOCKED | EXTENT_DIRTY |
5273 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5274 EXTENT_DEFRAG, 1, 1,
5275 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005276
Filipe Manana7064dd52014-08-08 02:47:05 +01005277 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278 spin_lock(&io_tree->lock);
5279 }
5280 spin_unlock(&io_tree->lock);
5281}
5282
Al Virobd555972010-06-07 11:35:40 -04005283void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005285 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005286 struct btrfs_trans_handle *trans;
5287 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005288 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005289 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005290 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005291 int ret;
5292
liubo1abe9b82011-03-24 11:18:59 +00005293 trace_btrfs_inode_evict(inode);
5294
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005295 if (!root) {
5296 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5297 return;
5298 }
5299
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005300 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005301
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005302 evict_inode_truncate_pages(inode);
5303
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005304 if (inode->i_nlink &&
5305 ((btrfs_root_refs(&root->root_item) != 0 &&
5306 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005307 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005308 goto no_delete;
5309
Chris Mason39279cc2007-06-12 06:35:45 -04005310 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005311 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005312 goto no_delete;
5313 }
Al Virobd555972010-06-07 11:35:40 -04005314 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005315 if (!special_file(inode->i_mode))
5316 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005317
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005318 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005319
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005320 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005321 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005322 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005323 goto no_delete;
5324 }
5325
Yan, Zheng76dda932009-09-21 16:00:26 -04005326 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005327 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5328 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005329 goto no_delete;
5330 }
5331
Nikolay Borisovaa790212017-01-10 20:35:40 +02005332 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005333 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005334 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005335 goto no_delete;
5336 }
5337
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005338 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005339 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005340 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005341 goto no_delete;
5342 }
Josef Bacik4a338542011-08-29 11:01:31 -04005343 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005344 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005345 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005346
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005347 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005348
Josef Bacik4289a662011-08-05 13:22:24 -04005349 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005350 * This is a bit simpler than btrfs_truncate since we've already
5351 * reserved our space for our orphan item in the unlink, so we just
5352 * need to reserve some slack space in case we add bytes and update
5353 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005354 */
Yan, Zheng80825102009-11-12 09:35:36 +00005355 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005356 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5357 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005358
Josef Bacik726c35f2011-09-26 15:46:06 -04005359 /*
5360 * Try and steal from the global reserve since we will
5361 * likely not use this space anyway, we want to try as
5362 * hard as possible to get this to work.
5363 */
5364 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005365 steal_from_global++;
5366 else
5367 steal_from_global = 0;
5368 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005369
Josef Bacik3bce8762015-02-24 12:35:51 -08005370 /*
5371 * steal_from_global == 0: we reserved stuff, hooray!
5372 * steal_from_global == 1: we didn't reserve stuff, boo!
5373 * steal_from_global == 2: we've committed, still not a lot of
5374 * room but maybe we'll have room in the global reserve this
5375 * time.
5376 * steal_from_global == 3: abandon all hope!
5377 */
5378 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005379 btrfs_warn(fs_info,
5380 "Could not get space for a delete, will truncate on mount %d",
5381 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005382 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005383 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005384 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005385 }
5386
Miao Xie0e8c36a2012-12-19 06:59:51 +00005387 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005388 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005389 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005390 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005391 goto no_delete;
5392 }
5393
Josef Bacik3bce8762015-02-24 12:35:51 -08005394 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005395 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005396 * sure there is room to do it, if not we need to commit and try
5397 * again.
5398 */
5399 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005400 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005401 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005402 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005403 else
5404 ret = -ENOSPC;
5405 }
5406
5407 /*
5408 * Couldn't steal from the global reserve, we have too much
5409 * pending stuff built up, commit the transaction and try it
5410 * again.
5411 */
5412 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005413 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005414 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005415 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005416 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005417 goto no_delete;
5418 }
5419 continue;
5420 } else {
5421 steal_from_global = 0;
5422 }
5423
Josef Bacik4289a662011-08-05 13:22:24 -04005424 trans->block_rsv = rsv;
5425
Yan, Zhengd68fc572010-05-16 10:49:58 -04005426 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005427 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005428 break;
5429
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005430 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005431 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005432 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005433 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005434 }
5435
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005437
Josef Bacik4ef31a42013-08-13 14:10:08 -04005438 /*
5439 * Errors here aren't a big deal, it just means we leave orphan items
5440 * in the tree. They will be cleaned up on the next mount.
5441 */
Yan, Zheng80825102009-11-12 09:35:36 +00005442 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005443 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005444 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005445 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005446 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005447 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005449 trans->block_rsv = &fs_info->trans_block_rsv;
5450 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005451 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005452 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005453
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005454 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005455 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005456no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005457 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005458 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005459}
5460
5461/*
5462 * this returns the key found in the dir entry in the location pointer.
5463 * If no dir entries were found, location->objectid is 0.
5464 */
5465static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5466 struct btrfs_key *location)
5467{
5468 const char *name = dentry->d_name.name;
5469 int namelen = dentry->d_name.len;
5470 struct btrfs_dir_item *di;
5471 struct btrfs_path *path;
5472 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005473 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005474
5475 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005476 if (!path)
5477 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005478
David Sterbaf85b7372017-01-20 14:54:07 +01005479 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5480 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005481 if (IS_ERR(di))
5482 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005483
David Sterbac7040052011-04-19 18:00:01 +02005484 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005485 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005486
Chris Mason5f39d392007-10-15 16:14:19 -04005487 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005488out:
Chris Mason39279cc2007-06-12 06:35:45 -04005489 btrfs_free_path(path);
5490 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005491out_err:
5492 location->objectid = 0;
5493 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005494}
5495
5496/*
5497 * when we hit a tree root in a directory, the btrfs part of the inode
5498 * needs to be changed to reflect the root directory of the tree root. This
5499 * is kind of like crossing a mount point.
5500 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005501static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 struct inode *dir,
5503 struct dentry *dentry,
5504 struct btrfs_key *location,
5505 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005506{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 struct btrfs_path *path;
5508 struct btrfs_root *new_root;
5509 struct btrfs_root_ref *ref;
5510 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005511 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512 int ret;
5513 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005514
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005515 path = btrfs_alloc_path();
5516 if (!path) {
5517 err = -ENOMEM;
5518 goto out;
5519 }
Chris Mason39279cc2007-06-12 06:35:45 -04005520
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005522 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5523 key.type = BTRFS_ROOT_REF_KEY;
5524 key.offset = location->objectid;
5525
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005526 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 if (ret) {
5528 if (ret < 0)
5529 err = ret;
5530 goto out;
5531 }
Chris Mason39279cc2007-06-12 06:35:45 -04005532
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005533 leaf = path->nodes[0];
5534 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005535 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5537 goto out;
5538
5539 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5540 (unsigned long)(ref + 1),
5541 dentry->d_name.len);
5542 if (ret)
5543 goto out;
5544
David Sterbab3b4aa72011-04-21 01:20:15 +02005545 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005547 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 if (IS_ERR(new_root)) {
5549 err = PTR_ERR(new_root);
5550 goto out;
5551 }
5552
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005553 *sub_root = new_root;
5554 location->objectid = btrfs_root_dirid(&new_root->root_item);
5555 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005556 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557 err = 0;
5558out:
5559 btrfs_free_path(path);
5560 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005561}
5562
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563static void inode_tree_add(struct inode *inode)
5564{
5565 struct btrfs_root *root = BTRFS_I(inode)->root;
5566 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005567 struct rb_node **p;
5568 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005569 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005570 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005571
Al Viro1d3382cb2010-10-23 15:19:20 -04005572 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005573 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005574 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005576 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577 while (*p) {
5578 parent = *p;
5579 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5580
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005581 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005583 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005584 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 else {
5586 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005587 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005588 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005589 RB_CLEAR_NODE(parent);
5590 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005591 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592 }
5593 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005594 rb_link_node(new, parent, p);
5595 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 spin_unlock(&root->inode_lock);
5597}
5598
5599static void inode_tree_del(struct inode *inode)
5600{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005601 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005603 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005605 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005611 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005612
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005613 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005614 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 spin_lock(&root->inode_lock);
5616 empty = RB_EMPTY_ROOT(&root->inode_tree);
5617 spin_unlock(&root->inode_lock);
5618 if (empty)
5619 btrfs_add_dead_root(root);
5620 }
5621}
5622
Jeff Mahoney143bede2012-03-01 14:56:26 +01005623void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005624{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005625 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005626 struct rb_node *node;
5627 struct rb_node *prev;
5628 struct btrfs_inode *entry;
5629 struct inode *inode;
5630 u64 objectid = 0;
5631
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005632 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005633 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005634
5635 spin_lock(&root->inode_lock);
5636again:
5637 node = root->inode_tree.rb_node;
5638 prev = NULL;
5639 while (node) {
5640 prev = node;
5641 entry = rb_entry(node, struct btrfs_inode, rb_node);
5642
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005643 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005644 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005645 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005646 node = node->rb_right;
5647 else
5648 break;
5649 }
5650 if (!node) {
5651 while (prev) {
5652 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005653 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005654 node = prev;
5655 break;
5656 }
5657 prev = rb_next(prev);
5658 }
5659 }
5660 while (node) {
5661 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005662 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005663 inode = igrab(&entry->vfs_inode);
5664 if (inode) {
5665 spin_unlock(&root->inode_lock);
5666 if (atomic_read(&inode->i_count) > 1)
5667 d_prune_aliases(inode);
5668 /*
Al Viro45321ac2010-06-07 13:43:19 -04005669 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005670 * the inode cache when its usage count
5671 * hits zero.
5672 */
5673 iput(inode);
5674 cond_resched();
5675 spin_lock(&root->inode_lock);
5676 goto again;
5677 }
5678
5679 if (cond_resched_lock(&root->inode_lock))
5680 goto again;
5681
5682 node = rb_next(node);
5683 }
5684 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005685}
5686
Chris Masone02119d2008-09-05 16:13:11 -04005687static int btrfs_init_locked_inode(struct inode *inode, void *p)
5688{
5689 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005690 inode->i_ino = args->location->objectid;
5691 memcpy(&BTRFS_I(inode)->location, args->location,
5692 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005693 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005694 return 0;
5695}
5696
5697static int btrfs_find_actor(struct inode *inode, void *opaque)
5698{
5699 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005700 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005701 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005702}
5703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005704static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005705 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005706 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005707{
5708 struct inode *inode;
5709 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005710 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005711
Chris Mason90d3e592014-01-09 17:28:00 -08005712 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005713 args.root = root;
5714
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005715 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005716 btrfs_init_locked_inode,
5717 (void *)&args);
5718 return inode;
5719}
5720
Balaji Rao1a54ef82008-07-21 02:01:04 +05305721/* Get an inode object given its location and corresponding root.
5722 * Returns in *is_new if the inode was read from disk
5723 */
5724struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005725 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305726{
5727 struct inode *inode;
5728
Chris Mason90d3e592014-01-09 17:28:00 -08005729 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305730 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005731 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305732
5733 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005734 int ret;
5735
5736 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005737 if (!is_bad_inode(inode)) {
5738 inode_tree_add(inode);
5739 unlock_new_inode(inode);
5740 if (new)
5741 *new = 1;
5742 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005743 unlock_new_inode(inode);
5744 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005745 ASSERT(ret < 0);
5746 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005747 }
5748 }
5749
Balaji Rao1a54ef82008-07-21 02:01:04 +05305750 return inode;
5751}
5752
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005753static struct inode *new_simple_dir(struct super_block *s,
5754 struct btrfs_key *key,
5755 struct btrfs_root *root)
5756{
5757 struct inode *inode = new_inode(s);
5758
5759 if (!inode)
5760 return ERR_PTR(-ENOMEM);
5761
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 BTRFS_I(inode)->root = root;
5763 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005764 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005765
5766 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005767 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005768 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769 inode->i_fop = &simple_dir_operations;
5770 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005771 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305772 inode->i_atime = inode->i_mtime;
5773 inode->i_ctime = inode->i_mtime;
5774 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005775
5776 return inode;
5777}
5778
Chris Mason3de45862008-11-17 21:02:50 -05005779struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005780{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005781 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005782 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005783 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005784 struct btrfs_root *sub_root = root;
5785 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005786 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005787 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005788
5789 if (dentry->d_name.len > BTRFS_NAME_LEN)
5790 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005791
Jeff Layton39e3c952012-11-28 11:30:53 -05005792 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005793 if (ret < 0)
5794 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005795
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005797 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798
5799 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005800 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005802 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005803
5804 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5805
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005806 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005807 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005808 &location, &sub_root);
5809 if (ret < 0) {
5810 if (ret != -ENOENT)
5811 inode = ERR_PTR(ret);
5812 else
5813 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5814 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005815 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005816 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005817 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005818
Julia Lawall34d19ba2011-01-24 19:55:19 +00005819 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005821 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005822 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005823 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005824 if (ret) {
5825 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005826 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005827 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005828 }
5829
Chris Mason3de45862008-11-17 21:02:50 -05005830 return inode;
5831}
5832
Nick Pigginfe15ce42011-01-07 17:49:23 +11005833static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005834{
5835 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005836 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005837
Li Zefan848cce02012-02-21 17:04:28 +08005838 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005839 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005840
Li Zefan848cce02012-02-21 17:04:28 +08005841 if (inode) {
5842 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005843 if (btrfs_root_refs(&root->root_item) == 0)
5844 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005845
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005846 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005847 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005848 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005849 return 0;
5850}
5851
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005852static void btrfs_dentry_release(struct dentry *dentry)
5853{
Daeseok Youn944a4512014-04-14 15:37:02 +09005854 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005855}
5856
Chris Mason3de45862008-11-17 21:02:50 -05005857static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005858 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005859{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005860 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005861
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005862 inode = btrfs_lookup_dentry(dir, dentry);
5863 if (IS_ERR(inode)) {
5864 if (PTR_ERR(inode) == -ENOENT)
5865 inode = NULL;
5866 else
5867 return ERR_CAST(inode);
5868 }
5869
Al Viro41d28bc2014-10-12 22:24:21 -04005870 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005871}
5872
Miao Xie16cdcec2011-04-22 18:12:22 +08005873unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005874 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5875};
5876
Al Viro9cdda8d2013-05-22 16:48:09 -04005877static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005878{
Al Viro9cdda8d2013-05-22 16:48:09 -04005879 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005881 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005882 struct btrfs_dir_item *di;
5883 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005884 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005886 struct list_head ins_list;
5887 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005888 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005889 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005890 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 unsigned char d_type;
5892 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005893 char tmp_name[32];
5894 char *name_ptr;
5895 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005896 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005897 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005898
Al Viro9cdda8d2013-05-22 16:48:09 -04005899 if (!dir_emit_dots(file, ctx))
5900 return 0;
5901
David Woodhouse49593bf2008-08-17 17:08:36 +01005902 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005903 if (!path)
5904 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005905
David Sterbae4058b52015-11-27 16:31:35 +01005906 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005907
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005908 INIT_LIST_HEAD(&ins_list);
5909 INIT_LIST_HEAD(&del_list);
5910 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005911
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005912 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005913 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005914 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005915
Chris Mason39279cc2007-06-12 06:35:45 -04005916 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5917 if (ret < 0)
5918 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005919
5920 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005921 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005922 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005923 if (slot >= btrfs_header_nritems(leaf)) {
5924 ret = btrfs_next_leaf(root, path);
5925 if (ret < 0)
5926 goto err;
5927 else if (ret > 0)
5928 break;
5929 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 }
Chris Mason3de45862008-11-17 21:02:50 -05005931
Chris Mason5f39d392007-10-15 16:14:19 -04005932 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5933
5934 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005935 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005936 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005937 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005938 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005939 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005940 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005941 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005942
Al Viro9cdda8d2013-05-22 16:48:09 -04005943 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005944
Chris Mason39279cc2007-06-12 06:35:45 -04005945 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005946 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005947 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005948
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005949 name_len = btrfs_dir_name_len(leaf, di);
5950 if (name_len <= sizeof(tmp_name)) {
5951 name_ptr = tmp_name;
5952 } else {
5953 name_ptr = kmalloc(name_len, GFP_KERNEL);
5954 if (!name_ptr) {
5955 ret = -ENOMEM;
5956 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005957 }
Chris Mason39279cc2007-06-12 06:35:45 -04005958 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005959 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5960 name_len);
5961
5962 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5963 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5964
5965 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5966 d_type);
5967
5968 if (name_ptr != tmp_name)
5969 kfree(name_ptr);
5970
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005971 if (over)
5972 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005973 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005974next:
5975 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005976 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005977
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005978 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005979 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005980 goto nopos;
5981
Zach Browndb62efb2013-07-11 16:19:42 -07005982 /*
5983 * Stop new entries from being returned after we return the last
5984 * entry.
5985 *
5986 * New directory entries are assigned a strictly increasing
5987 * offset. This means that new entries created during readdir
5988 * are *guaranteed* to be seen in the future by that readdir.
5989 * This has broken buggy programs which operate on names as
5990 * they're returned by readdir. Until we re-use freed offsets
5991 * we have this hack to stop new entries from being returned
5992 * under the assumption that they'll never reach this huge
5993 * offset.
5994 *
5995 * This is being careful not to overflow 32bit loff_t unless the
5996 * last entry requires it because doing so has broken 32bit apps
5997 * in the past.
5998 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005999 if (ctx->pos >= INT_MAX)
6000 ctx->pos = LLONG_MAX;
6001 else
6002 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006003nopos:
6004 ret = 0;
6005err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006006 if (put)
6007 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006008 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006009 return ret;
6010}
6011
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006012int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006013{
6014 struct btrfs_root *root = BTRFS_I(inode)->root;
6015 struct btrfs_trans_handle *trans;
6016 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006017 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006018
Josef Bacik72ac3c02012-05-23 14:13:11 -04006019 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006020 return 0;
6021
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006022 if (btrfs_fs_closing(root->fs_info) &&
6023 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006024 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006025
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006026 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006027 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006028 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006029 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006030 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006031 if (IS_ERR(trans))
6032 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006033 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006034 }
6035 return ret;
6036}
6037
6038/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006039 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006040 * inode changes. But, it is most likely to find the inode in cache.
6041 * FIXME, needs more benchmarking...there are no reasons other than performance
6042 * to keep or drop this code.
6043 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006044static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006045{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006046 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006047 struct btrfs_root *root = BTRFS_I(inode)->root;
6048 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006049 int ret;
6050
Josef Bacik72ac3c02012-05-23 14:13:11 -04006051 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006052 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006053
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006054 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006055 if (IS_ERR(trans))
6056 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006057
6058 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006059 if (ret && ret == -ENOSPC) {
6060 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006061 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006062 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006063 if (IS_ERR(trans))
6064 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006065
Chris Mason94b60442010-05-26 11:02:00 -04006066 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006067 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006068 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006069 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006070 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006071
6072 return ret;
6073}
6074
6075/*
6076 * This is a copy of file_update_time. We need this so we can return error on
6077 * ENOSPC for updating the inode in the case of file write and mmap writes.
6078 */
Josef Bacike41f9412012-03-26 09:46:47 -04006079static int btrfs_update_time(struct inode *inode, struct timespec *now,
6080 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006081{
Alexander Block2bc5565282012-06-15 09:49:33 +02006082 struct btrfs_root *root = BTRFS_I(inode)->root;
6083
6084 if (btrfs_root_readonly(root))
6085 return -EROFS;
6086
Josef Bacike41f9412012-03-26 09:46:47 -04006087 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006088 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006089 if (flags & S_CTIME)
6090 inode->i_ctime = *now;
6091 if (flags & S_MTIME)
6092 inode->i_mtime = *now;
6093 if (flags & S_ATIME)
6094 inode->i_atime = *now;
6095 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006096}
6097
Chris Masond352ac62008-09-29 15:18:18 -04006098/*
6099 * find the highest existing sequence number in a directory
6100 * and then set the in-memory index_cnt variable to reflect
6101 * free sequence numbers
6102 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006103static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006104{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006105 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006106 struct btrfs_key key, found_key;
6107 struct btrfs_path *path;
6108 struct extent_buffer *leaf;
6109 int ret;
6110
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006111 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006112 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006113 key.offset = (u64)-1;
6114
6115 path = btrfs_alloc_path();
6116 if (!path)
6117 return -ENOMEM;
6118
6119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6120 if (ret < 0)
6121 goto out;
6122 /* FIXME: we should be able to handle this */
6123 if (ret == 0)
6124 goto out;
6125 ret = 0;
6126
6127 /*
6128 * MAGIC NUMBER EXPLANATION:
6129 * since we search a directory based on f_pos we have to start at 2
6130 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6131 * else has to start at 2
6132 */
6133 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006134 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006135 goto out;
6136 }
6137
6138 path->slots[0]--;
6139
6140 leaf = path->nodes[0];
6141 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6142
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006143 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006144 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006146 goto out;
6147 }
6148
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006149 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006150out:
6151 btrfs_free_path(path);
6152 return ret;
6153}
6154
Chris Masond352ac62008-09-29 15:18:18 -04006155/*
6156 * helper to find a free sequence number in a given directory. This current
6157 * code is very simple, later versions will do smarter things in the btree
6158 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006159int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006160{
6161 int ret = 0;
6162
Nikolay Borisov877574e2017-02-20 13:50:33 +02006163 if (dir->index_cnt == (u64)-1) {
6164 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006165 if (ret) {
6166 ret = btrfs_set_inode_index_count(dir);
6167 if (ret)
6168 return ret;
6169 }
Josef Bacikaec74772008-07-24 12:12:38 -04006170 }
6171
Nikolay Borisov877574e2017-02-20 13:50:33 +02006172 *index = dir->index_cnt;
6173 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006174
6175 return ret;
6176}
6177
Chris Masonb0d5d102014-09-08 13:08:51 -07006178static int btrfs_insert_inode_locked(struct inode *inode)
6179{
6180 struct btrfs_iget_args args;
6181 args.location = &BTRFS_I(inode)->location;
6182 args.root = BTRFS_I(inode)->root;
6183
6184 return insert_inode_locked4(inode,
6185 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6186 btrfs_find_actor, &args);
6187}
6188
Chris Mason39279cc2007-06-12 06:35:45 -04006189static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6190 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006191 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006192 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006193 u64 ref_objectid, u64 objectid,
6194 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006195{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006196 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006197 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006198 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006199 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006200 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006201 struct btrfs_inode_ref *ref;
6202 struct btrfs_key key[2];
6203 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006204 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006205 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006206 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006207
Chris Mason5f39d392007-10-15 16:14:19 -04006208 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006209 if (!path)
6210 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006211
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006212 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006213 if (!inode) {
6214 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006215 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006216 }
Chris Mason39279cc2007-06-12 06:35:45 -04006217
Li Zefan581bb052011-04-20 10:06:11 +08006218 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006219 * O_TMPFILE, set link count to 0, so that after this point,
6220 * we fill in an inode item with the correct link count.
6221 */
6222 if (!name)
6223 set_nlink(inode, 0);
6224
6225 /*
Li Zefan581bb052011-04-20 10:06:11 +08006226 * we have to initialize this early, so we can reclaim the inode
6227 * number if we fail afterwards in this function.
6228 */
6229 inode->i_ino = objectid;
6230
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006231 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006232 trace_btrfs_inode_request(dir);
6233
Nikolay Borisov877574e2017-02-20 13:50:33 +02006234 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006235 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006236 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006237 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006238 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006239 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006240 } else if (dir) {
6241 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006242 }
6243 /*
6244 * index_cnt is ignored for everything but a dir,
6245 * btrfs_get_inode_index_count has an explanation for the magic
6246 * number
6247 */
6248 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006249 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006250 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006251 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006252 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006253
Josef Bacik5dc562c2012-08-17 13:14:17 -04006254 /*
6255 * We could have gotten an inode number from somebody who was fsynced
6256 * and then removed in this same transaction, so let's just set full
6257 * sync since it will be a full sync anyway and this will blow away the
6258 * old info in the log.
6259 */
6260 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6261
Chris Mason9c583092008-01-29 15:15:18 -05006262 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006263 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006264 key[0].offset = 0;
6265
Chris Mason9c583092008-01-29 15:15:18 -05006266 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006267
6268 if (name) {
6269 /*
6270 * Start new inodes with an inode_ref. This is slightly more
6271 * efficient for small numbers of hard links since they will
6272 * be packed into one item. Extended refs will kick in if we
6273 * add more hard links than can fit in the ref item.
6274 */
6275 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006276 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006277 key[1].offset = ref_objectid;
6278
6279 sizes[1] = name_len + sizeof(*ref);
6280 }
Chris Mason9c583092008-01-29 15:15:18 -05006281
Chris Masonb0d5d102014-09-08 13:08:51 -07006282 location = &BTRFS_I(inode)->location;
6283 location->objectid = objectid;
6284 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006285 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006286
6287 ret = btrfs_insert_inode_locked(inode);
6288 if (ret < 0)
6289 goto fail;
6290
Chris Masonb9473432009-03-13 11:00:37 -04006291 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006292 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006293 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006294 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006295
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006296 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006297 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306298
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006299 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306300 inode->i_atime = inode->i_mtime;
6301 inode->i_ctime = inode->i_mtime;
6302 BTRFS_I(inode)->i_otime = inode->i_mtime;
6303
Chris Mason5f39d392007-10-15 16:14:19 -04006304 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6305 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006306 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006307 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006308 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006309
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006310 if (name) {
6311 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6312 struct btrfs_inode_ref);
6313 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6314 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6315 ptr = (unsigned long)(ref + 1);
6316 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6317 }
Chris Mason9c583092008-01-29 15:15:18 -05006318
Chris Mason5f39d392007-10-15 16:14:19 -04006319 btrfs_mark_buffer_dirty(path->nodes[0]);
6320 btrfs_free_path(path);
6321
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006322 btrfs_inherit_iflags(inode, dir);
6323
Al Viro569254b2011-07-24 17:08:40 -04006324 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006325 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006326 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006327 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006328 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6329 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006330 }
6331
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006332 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006333
6334 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006335 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006336
Alexander Block8ea05e32012-07-25 17:35:53 +02006337 btrfs_update_root_times(trans, root);
6338
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006339 ret = btrfs_inode_inherit_props(trans, inode, dir);
6340 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006341 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006342 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006343 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006344
Chris Mason39279cc2007-06-12 06:35:45 -04006345 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006346
6347fail_unlock:
6348 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006349fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006350 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006351 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006352 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006353 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006354 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006355}
6356
6357static inline u8 btrfs_inode_type(struct inode *inode)
6358{
6359 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6360}
6361
Chris Masond352ac62008-09-29 15:18:18 -04006362/*
6363 * utility function to add 'inode' into 'parent_inode' with
6364 * a give name and a given sequence number.
6365 * if 'add_backref' is true, also insert a backref from the
6366 * inode to the parent directory.
6367 */
Chris Masone02119d2008-09-05 16:13:11 -04006368int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006369 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006370 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006371{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006373 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006374 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006375 struct btrfs_root *root = parent_inode->root;
6376 u64 ino = btrfs_ino(inode);
6377 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006378
Li Zefan33345d012011-04-20 10:31:50 +08006379 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006380 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006381 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006382 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006383 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006384 key.offset = 0;
6385 }
Chris Mason39279cc2007-06-12 06:35:45 -04006386
Li Zefan33345d012011-04-20 10:31:50 +08006387 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006388 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6389 root->root_key.objectid, parent_ino,
6390 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006391 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006392 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6393 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006394 }
6395
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006396 /* Nothing to clean up yet */
6397 if (ret)
6398 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006399
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006400 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6401 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006402 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006403 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006404 goto fail_dir_item;
6405 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006406 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006407 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006408 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006409
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006410 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006411 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006412 inode_inc_iversion(&parent_inode->vfs_inode);
6413 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6414 current_time(&parent_inode->vfs_inode);
6415 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006416 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006417 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006418 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006419
6420fail_dir_item:
6421 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6422 u64 local_index;
6423 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006424 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6425 root->root_key.objectid, parent_ino,
6426 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006427
6428 } else if (add_backref) {
6429 u64 local_index;
6430 int err;
6431
6432 err = btrfs_del_inode_ref(trans, root, name, name_len,
6433 ino, parent_ino, &local_index);
6434 }
6435 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006436}
6437
6438static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006439 struct btrfs_inode *dir, struct dentry *dentry,
6440 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006441{
Josef Bacika1b075d2010-11-19 20:36:11 +00006442 int err = btrfs_add_link(trans, dir, inode,
6443 dentry->d_name.name, dentry->d_name.len,
6444 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006445 if (err > 0)
6446 err = -EEXIST;
6447 return err;
6448}
6449
Josef Bacik618e21d2007-07-11 10:18:17 -04006450static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006451 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006452{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006453 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006454 struct btrfs_trans_handle *trans;
6455 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006456 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006457 int err;
6458 int drop_inode = 0;
6459 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006460 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006461
Josef Bacik9ed74f22009-09-11 16:12:44 -04006462 /*
6463 * 2 for inode item and ref
6464 * 2 for dir items
6465 * 1 for xattr if selinux is on
6466 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006467 trans = btrfs_start_transaction(root, 5);
6468 if (IS_ERR(trans))
6469 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006470
Li Zefan581bb052011-04-20 10:06:11 +08006471 err = btrfs_find_free_ino(root, &objectid);
6472 if (err)
6473 goto out_unlock;
6474
Josef Bacikaec74772008-07-24 12:12:38 -04006475 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006476 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6477 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006478 if (IS_ERR(inode)) {
6479 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006480 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006481 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006482
Casey Schauflerad19db72011-12-15 10:09:07 -05006483 /*
6484 * If the active LSM wants to access the inode during
6485 * d_instantiate it needs these. Smack checks to see
6486 * if the filesystem supports xattrs by looking at the
6487 * ops vector.
6488 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006489 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006490 init_special_inode(inode, inode->i_mode, rdev);
6491
6492 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006493 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006494 goto out_unlock_inode;
6495
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006496 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6497 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006498 if (err) {
6499 goto out_unlock_inode;
6500 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006501 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006502 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006503 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006504 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006505
Josef Bacik618e21d2007-07-11 10:18:17 -04006506out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006507 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006508 btrfs_balance_delayed_items(fs_info);
6509 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006510 if (drop_inode) {
6511 inode_dec_link_count(inode);
6512 iput(inode);
6513 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006514 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006515
6516out_unlock_inode:
6517 drop_inode = 1;
6518 unlock_new_inode(inode);
6519 goto out_unlock;
6520
Josef Bacik618e21d2007-07-11 10:18:17 -04006521}
6522
Chris Mason39279cc2007-06-12 06:35:45 -04006523static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006524 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006525{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006526 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006527 struct btrfs_trans_handle *trans;
6528 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006529 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006530 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006531 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006532 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006533 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006534
Josef Bacik9ed74f22009-09-11 16:12:44 -04006535 /*
6536 * 2 for inode item and ref
6537 * 2 for dir items
6538 * 1 for xattr if selinux is on
6539 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006540 trans = btrfs_start_transaction(root, 5);
6541 if (IS_ERR(trans))
6542 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006543
Li Zefan581bb052011-04-20 10:06:11 +08006544 err = btrfs_find_free_ino(root, &objectid);
6545 if (err)
6546 goto out_unlock;
6547
Josef Bacikaec74772008-07-24 12:12:38 -04006548 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006549 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6550 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006551 if (IS_ERR(inode)) {
6552 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006553 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006554 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006555 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006556 /*
6557 * If the active LSM wants to access the inode during
6558 * d_instantiate it needs these. Smack checks to see
6559 * if the filesystem supports xattrs by looking at the
6560 * ops vector.
6561 */
6562 inode->i_fop = &btrfs_file_operations;
6563 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006564 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006565
6566 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6567 if (err)
6568 goto out_unlock_inode;
6569
6570 err = btrfs_update_inode(trans, root, inode);
6571 if (err)
6572 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006573
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006574 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6575 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006576 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006577 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006578
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006579 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006580 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006581 d_instantiate(dentry, inode);
6582
Chris Mason39279cc2007-06-12 06:35:45 -04006583out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006584 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006585 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006586 inode_dec_link_count(inode);
6587 iput(inode);
6588 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006589 btrfs_balance_delayed_items(fs_info);
6590 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006592
6593out_unlock_inode:
6594 unlock_new_inode(inode);
6595 goto out_unlock;
6596
Chris Mason39279cc2007-06-12 06:35:45 -04006597}
6598
6599static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6600 struct dentry *dentry)
6601{
Filipe Manana271dba452016-01-05 16:24:05 +00006602 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006603 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006604 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006605 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006606 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006607 int err;
6608 int drop_inode = 0;
6609
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006610 /* do not allow sys_link's with other subvols of the same device */
6611 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006612 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006613
Mark Fashehf1863732012-08-08 11:32:27 -07006614 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006615 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006616
Nikolay Borisov877574e2017-02-20 13:50:33 +02006617 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006618 if (err)
6619 goto fail;
6620
Yan, Zhenga22285a2010-05-16 10:48:46 -04006621 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006622 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006623 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006624 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006625 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006626 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006627 if (IS_ERR(trans)) {
6628 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006629 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006630 goto fail;
6631 }
Chris Mason5f39d392007-10-15 16:14:19 -04006632
Miao Xie67de1172013-12-26 13:07:06 +08006633 /* There are several dir indexes for this inode, clear the cache. */
6634 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006635 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006636 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006637 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006638 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006639 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006640
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006641 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6642 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006643
Yan, Zhenga5719522009-09-24 09:17:31 -04006644 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006645 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006646 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006647 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006648 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006649 if (err)
6650 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006651 if (inode->i_nlink == 1) {
6652 /*
6653 * If new hard link count is 1, it's a file created
6654 * with open(2) O_TMPFILE flag.
6655 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006656 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006657 if (err)
6658 goto fail;
6659 }
Al Viro08c422c2011-12-23 07:58:13 -05006660 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006661 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006662 }
Chris Mason39279cc2007-06-12 06:35:45 -04006663
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006664 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006665fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006666 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006667 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 if (drop_inode) {
6669 inode_dec_link_count(inode);
6670 iput(inode);
6671 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006672 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006673 return err;
6674}
6675
Al Viro18bb1db2011-07-26 01:41:39 -04006676static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006677{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006678 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006679 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006680 struct btrfs_trans_handle *trans;
6681 struct btrfs_root *root = BTRFS_I(dir)->root;
6682 int err = 0;
6683 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006684 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006685 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006686
Josef Bacik9ed74f22009-09-11 16:12:44 -04006687 /*
6688 * 2 items for inode and ref
6689 * 2 items for dir items
6690 * 1 for xattr if selinux is on
6691 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006692 trans = btrfs_start_transaction(root, 5);
6693 if (IS_ERR(trans))
6694 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006695
Li Zefan581bb052011-04-20 10:06:11 +08006696 err = btrfs_find_free_ino(root, &objectid);
6697 if (err)
6698 goto out_fail;
6699
Josef Bacikaec74772008-07-24 12:12:38 -04006700 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006701 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6702 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006703 if (IS_ERR(inode)) {
6704 err = PTR_ERR(inode);
6705 goto out_fail;
6706 }
Chris Mason5f39d392007-10-15 16:14:19 -04006707
Chris Mason39279cc2007-06-12 06:35:45 -04006708 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006709 /* these must be set before we unlock the inode */
6710 inode->i_op = &btrfs_dir_inode_operations;
6711 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006712
Eric Paris2a7dba32011-02-01 11:05:39 -05006713 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006714 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006715 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006716
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006717 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006718 err = btrfs_update_inode(trans, root, inode);
6719 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006720 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006721
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006722 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6723 dentry->d_name.name,
6724 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006725 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006726 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006727
Chris Mason39279cc2007-06-12 06:35:45 -04006728 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006729 /*
6730 * mkdir is special. We're unlocking after we call d_instantiate
6731 * to avoid a race with nfsd calling d_instantiate.
6732 */
6733 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006734 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006735
6736out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006737 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006738 if (drop_on_err) {
6739 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006740 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006741 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006742 btrfs_balance_delayed_items(fs_info);
6743 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006744 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006745
6746out_fail_inode:
6747 unlock_new_inode(inode);
6748 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006749}
6750
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006751/* Find next extent map of a given extent map, caller needs to ensure locks */
6752static struct extent_map *next_extent_map(struct extent_map *em)
6753{
6754 struct rb_node *next;
6755
6756 next = rb_next(&em->rb_node);
6757 if (!next)
6758 return NULL;
6759 return container_of(next, struct extent_map, rb_node);
6760}
6761
6762static struct extent_map *prev_extent_map(struct extent_map *em)
6763{
6764 struct rb_node *prev;
6765
6766 prev = rb_prev(&em->rb_node);
6767 if (!prev)
6768 return NULL;
6769 return container_of(prev, struct extent_map, rb_node);
6770}
6771
Chris Masond352ac62008-09-29 15:18:18 -04006772/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006773 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006774 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006775 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006776 */
Chris Mason3b951512008-04-17 11:29:12 -04006777static int merge_extent_mapping(struct extent_map_tree *em_tree,
6778 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006779 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006780 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006781{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006782 struct extent_map *prev;
6783 struct extent_map *next;
6784 u64 start;
6785 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006786 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006787
Chris Masone6dcd2d2008-07-17 12:53:50 -04006788 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006789
6790 if (existing->start > map_start) {
6791 next = existing;
6792 prev = prev_extent_map(next);
6793 } else {
6794 prev = existing;
6795 next = next_extent_map(prev);
6796 }
6797
6798 start = prev ? extent_map_end(prev) : em->start;
6799 start = max_t(u64, start, em->start);
6800 end = next ? next->start : extent_map_end(em);
6801 end = min_t(u64, end, extent_map_end(em));
6802 start_diff = start - em->start;
6803 em->start = start;
6804 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006805 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6806 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006807 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006808 em->block_len -= start_diff;
6809 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006810 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006811}
6812
Chris Masonc8b97812008-10-29 14:49:59 -04006813static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006814 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006815 size_t pg_offset, u64 extent_offset,
6816 struct btrfs_file_extent_item *item)
6817{
6818 int ret;
6819 struct extent_buffer *leaf = path->nodes[0];
6820 char *tmp;
6821 size_t max_size;
6822 unsigned long inline_size;
6823 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006824 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006825
6826 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006827 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006828 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6829 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006830 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006831 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006832 if (!tmp)
6833 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006834 ptr = btrfs_file_extent_inline_start(item);
6835
6836 read_extent_buffer(leaf, tmp, ptr, inline_size);
6837
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006838 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006839 ret = btrfs_decompress(compress_type, tmp, page,
6840 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006841
6842 /*
6843 * decompression code contains a memset to fill in any space between the end
6844 * of the uncompressed data and the end of max_size in case the decompressed
6845 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6846 * the end of an inline extent and the beginning of the next block, so we
6847 * cover that region here.
6848 */
6849
6850 if (max_size + pg_offset < PAGE_SIZE) {
6851 char *map = kmap(page);
6852 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6853 kunmap(page);
6854 }
Chris Masonc8b97812008-10-29 14:49:59 -04006855 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006856 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006857}
6858
Chris Masond352ac62008-09-29 15:18:18 -04006859/*
6860 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006861 * the ugly parts come from merging extents from the disk with the in-ram
6862 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006863 * where the in-ram extents might be locked pending data=ordered completion.
6864 *
6865 * This also copies inline extents directly into the page.
6866 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006867struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6868 struct page *page,
6869 size_t pg_offset, u64 start, u64 len,
6870 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006871{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006873 int ret;
6874 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006875 u64 extent_start = 0;
6876 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006877 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006878 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006879 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006880 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006881 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006882 struct extent_buffer *leaf;
6883 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006885 struct extent_map_tree *em_tree = &inode->extent_tree;
6886 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006887 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006888 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006889
Chris Masona52d9a82007-08-27 16:49:44 -04006890again:
Chris Mason890871b2009-09-02 16:24:52 -04006891 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006892 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006893 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006894 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006895 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006896
Chris Masona52d9a82007-08-27 16:49:44 -04006897 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006898 if (em->start > start || em->start + em->len <= start)
6899 free_extent_map(em);
6900 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006901 free_extent_map(em);
6902 else
6903 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006904 }
David Sterba172ddd62011-04-21 00:48:27 +02006905 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006906 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006907 err = -ENOMEM;
6908 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006910 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006911 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006912 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006913 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006914 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006915
6916 if (!path) {
6917 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006918 if (!path) {
6919 err = -ENOMEM;
6920 goto out;
6921 }
6922 /*
6923 * Chances are we'll be called again, so go ahead and do
6924 * readahead
6925 */
David Sterbae4058b52015-11-27 16:31:35 +01006926 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006927 }
6928
Chris Mason179e29e2007-11-01 11:28:41 -04006929 ret = btrfs_lookup_file_extent(trans, root, path,
6930 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006931 if (ret < 0) {
6932 err = ret;
6933 goto out;
6934 }
6935
6936 if (ret != 0) {
6937 if (path->slots[0] == 0)
6938 goto not_found;
6939 path->slots[0]--;
6940 }
6941
Chris Mason5f39d392007-10-15 16:14:19 -04006942 leaf = path->nodes[0];
6943 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006944 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006945 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006947 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006948 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006949 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006950 /*
6951 * If we backup past the first extent we want to move forward
6952 * and see if there is an extent in front of us, otherwise we'll
6953 * say there is a hole for our whole search range which can
6954 * cause problems.
6955 */
6956 extent_end = start;
6957 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006958 }
6959
Chris Mason5f39d392007-10-15 16:14:19 -04006960 found_type = btrfs_file_extent_type(leaf, item);
6961 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006962 if (found_type == BTRFS_FILE_EXTENT_REG ||
6963 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006964 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006965 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006966
6967 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6968 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006969 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6970 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006971 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006972 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006973 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006974
6975 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6976 path->slots[0],
6977 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006978 }
Josef Bacik25a50342013-10-14 12:08:38 -04006979next:
Yan Zheng9036c102008-10-30 14:19:41 -04006980 if (start >= extent_end) {
6981 path->slots[0]++;
6982 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6983 ret = btrfs_next_leaf(root, path);
6984 if (ret < 0) {
6985 err = ret;
6986 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006987 }
Yan Zheng9036c102008-10-30 14:19:41 -04006988 if (ret > 0)
6989 goto not_found;
6990 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
Yan Zheng9036c102008-10-30 14:19:41 -04006992 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6993 if (found_key.objectid != objectid ||
6994 found_key.type != BTRFS_EXTENT_DATA_KEY)
6995 goto not_found;
6996 if (start + len <= found_key.offset)
6997 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006998 if (start > found_key.offset)
6999 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007000 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007001 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007002 em->len = found_key.offset - start;
7003 goto not_found_em;
7004 }
7005
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007006 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007007 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007008
Yan Zhengd899e052008-10-30 14:25:28 -04007009 if (found_type == BTRFS_FILE_EXTENT_REG ||
7010 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007011 goto insert;
7012 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007013 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007014 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007015 size_t size;
7016 size_t extent_offset;
7017 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007018
Filipe Manana7ffbb592014-06-09 03:48:05 +01007019 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007020 goto out;
Yan689f9342007-10-29 11:41:07 -04007021
Chris Mason514ac8a2014-01-03 21:07:00 -08007022 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007023 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007024 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7025 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007026 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007027 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007028 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007029 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007030 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007031 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007032 if (btrfs_file_extent_compression(leaf, item) !=
7033 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007034 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007035 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007036 if (ret) {
7037 err = ret;
7038 goto out;
7039 }
Chris Masonc8b97812008-10-29 14:49:59 -04007040 } else {
7041 map = kmap(page);
7042 read_extent_buffer(leaf, map + pg_offset, ptr,
7043 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007044 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007045 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007046 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007047 copy_size);
7048 }
Chris Masonc8b97812008-10-29 14:49:59 -04007049 kunmap(page);
7050 }
Chris Mason179e29e2007-11-01 11:28:41 -04007051 flush_dcache_page(page);
7052 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007053 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007054 if (!trans) {
7055 kunmap(page);
7056 free_extent_map(em);
7057 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007058
David Sterbab3b4aa72011-04-21 01:20:15 +02007059 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007060 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007061
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007062 if (IS_ERR(trans))
7063 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007064 goto again;
7065 }
Chris Masonc8b97812008-10-29 14:49:59 -04007066 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007067 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007068 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007069 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007070 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007071 }
Chris Masond1310b22008-01-24 16:13:08 -05007072 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007073 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007074 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007075 }
7076not_found:
7077 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007078 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007079 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007080not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007081 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007082 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007083insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007084 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007085 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007087 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7088 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007089 err = -EIO;
7090 goto out;
7091 }
Chris Masond1310b22008-01-24 16:13:08 -05007092
7093 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007094 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007095 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007096 /* it is possible that someone inserted the extent into the tree
7097 * while we had the lock dropped. It is also possible that
7098 * an overlapping map exists in the tree
7099 */
Chris Masona52d9a82007-08-27 16:49:44 -04007100 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007101 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007102
7103 ret = 0;
7104
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007105 existing = search_extent_mapping(em_tree, start, len);
7106 /*
7107 * existing will always be non-NULL, since there must be
7108 * extent causing the -EEXIST.
7109 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007110 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007111 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007112 em->block_start == existing->block_start) {
7113 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007114 * The existing extent map already encompasses the
7115 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007116 */
7117 free_extent_map(em);
7118 em = existing;
7119 err = 0;
7120
7121 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007122 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007123 /*
7124 * The existing extent map is the one nearest to
7125 * the [start, start + len) range which overlaps
7126 */
7127 err = merge_extent_mapping(em_tree, existing,
7128 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007129 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007130 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007131 free_extent_map(em);
7132 em = NULL;
7133 }
7134 } else {
7135 free_extent_map(em);
7136 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007137 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007138 }
Chris Masona52d9a82007-08-27 16:49:44 -04007139 }
Chris Mason890871b2009-09-02 16:24:52 -04007140 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007141out:
liubo1abe9b82011-03-24 11:18:59 +00007142
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007143 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007144
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007145 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007146 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007147 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007148 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007149 err = ret;
7150 }
Chris Masona52d9a82007-08-27 16:49:44 -04007151 if (err) {
7152 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007153 return ERR_PTR(err);
7154 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007155 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007156 return em;
7157}
7158
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007159struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7160 struct page *page,
7161 size_t pg_offset, u64 start, u64 len,
7162 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007163{
7164 struct extent_map *em;
7165 struct extent_map *hole_em = NULL;
7166 u64 range_start = start;
7167 u64 end;
7168 u64 found;
7169 u64 found_end;
7170 int err = 0;
7171
7172 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7173 if (IS_ERR(em))
7174 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007175 /*
7176 * If our em maps to:
7177 * - a hole or
7178 * - a pre-alloc extent,
7179 * there might actually be delalloc bytes behind it.
7180 */
7181 if (em->block_start != EXTENT_MAP_HOLE &&
7182 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7183 return em;
7184 else
7185 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007186
7187 /* check to see if we've wrapped (len == -1 or similar) */
7188 end = start + len;
7189 if (end < start)
7190 end = (u64)-1;
7191 else
7192 end -= 1;
7193
7194 em = NULL;
7195
7196 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007197 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007198 end, len, EXTENT_DELALLOC, 1);
7199 found_end = range_start + found;
7200 if (found_end < range_start)
7201 found_end = (u64)-1;
7202
7203 /*
7204 * we didn't find anything useful, return
7205 * the original results from get_extent()
7206 */
7207 if (range_start > end || found_end <= start) {
7208 em = hole_em;
7209 hole_em = NULL;
7210 goto out;
7211 }
7212
7213 /* adjust the range_start to make sure it doesn't
7214 * go backwards from the start they passed in
7215 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307216 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007217 found = found_end - range_start;
7218
7219 if (found > 0) {
7220 u64 hole_start = start;
7221 u64 hole_len = len;
7222
David Sterba172ddd62011-04-21 00:48:27 +02007223 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007224 if (!em) {
7225 err = -ENOMEM;
7226 goto out;
7227 }
7228 /*
7229 * when btrfs_get_extent can't find anything it
7230 * returns one huge hole
7231 *
7232 * make sure what it found really fits our range, and
7233 * adjust to make sure it is based on the start from
7234 * the caller
7235 */
7236 if (hole_em) {
7237 u64 calc_end = extent_map_end(hole_em);
7238
7239 if (calc_end <= start || (hole_em->start > end)) {
7240 free_extent_map(hole_em);
7241 hole_em = NULL;
7242 } else {
7243 hole_start = max(hole_em->start, start);
7244 hole_len = calc_end - hole_start;
7245 }
7246 }
7247 em->bdev = NULL;
7248 if (hole_em && range_start > hole_start) {
7249 /* our hole starts before our delalloc, so we
7250 * have to return just the parts of the hole
7251 * that go until the delalloc starts
7252 */
7253 em->len = min(hole_len,
7254 range_start - hole_start);
7255 em->start = hole_start;
7256 em->orig_start = hole_start;
7257 /*
7258 * don't adjust block start at all,
7259 * it is fixed at EXTENT_MAP_HOLE
7260 */
7261 em->block_start = hole_em->block_start;
7262 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007263 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7264 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007265 } else {
7266 em->start = range_start;
7267 em->len = found;
7268 em->orig_start = range_start;
7269 em->block_start = EXTENT_MAP_DELALLOC;
7270 em->block_len = found;
7271 }
7272 } else if (hole_em) {
7273 return hole_em;
7274 }
7275out:
7276
7277 free_extent_map(hole_em);
7278 if (err) {
7279 free_extent_map(em);
7280 return ERR_PTR(err);
7281 }
7282 return em;
7283}
7284
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007285static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7286 const u64 start,
7287 const u64 len,
7288 const u64 orig_start,
7289 const u64 block_start,
7290 const u64 block_len,
7291 const u64 orig_block_len,
7292 const u64 ram_bytes,
7293 const int type)
7294{
7295 struct extent_map *em = NULL;
7296 int ret;
7297
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007298 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007299 em = create_io_em(inode, start, len, orig_start,
7300 block_start, block_len, orig_block_len,
7301 ram_bytes,
7302 BTRFS_COMPRESS_NONE, /* compress_type */
7303 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007304 if (IS_ERR(em))
7305 goto out;
7306 }
7307 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7308 len, block_len, type);
7309 if (ret) {
7310 if (em) {
7311 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007312 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007313 start + len - 1, 0);
7314 }
7315 em = ERR_PTR(ret);
7316 }
7317 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007318
7319 return em;
7320}
7321
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7323 u64 start, u64 len)
7324{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007327 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328 struct btrfs_key ins;
7329 u64 alloc_hint;
7330 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007331
Josef Bacik4b46fce2010-05-23 11:00:55 -04007332 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007333 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007334 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007335 if (ret)
7336 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007337
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007338 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7339 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007340 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007341 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007342 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007343 btrfs_free_reserved_extent(fs_info, ins.objectid,
7344 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007345
Josef Bacik4b46fce2010-05-23 11:00:55 -04007346 return em;
7347}
7348
Chris Mason46bfbb52010-05-26 11:04:10 -04007349/*
7350 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7351 * block must be cow'd
7352 */
Josef Bacik00361582013-08-14 14:02:47 -04007353noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007354 u64 *orig_start, u64 *orig_block_len,
7355 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007356{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007358 struct btrfs_path *path;
7359 int ret;
7360 struct extent_buffer *leaf;
7361 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007362 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007363 struct btrfs_file_extent_item *fi;
7364 struct btrfs_key key;
7365 u64 disk_bytenr;
7366 u64 backref_offset;
7367 u64 extent_end;
7368 u64 num_bytes;
7369 int slot;
7370 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007371 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007372
Chris Mason46bfbb52010-05-26 11:04:10 -04007373 path = btrfs_alloc_path();
7374 if (!path)
7375 return -ENOMEM;
7376
David Sterbaf85b7372017-01-20 14:54:07 +01007377 ret = btrfs_lookup_file_extent(NULL, root, path,
7378 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007379 if (ret < 0)
7380 goto out;
7381
7382 slot = path->slots[0];
7383 if (ret == 1) {
7384 if (slot == 0) {
7385 /* can't find the item, must cow */
7386 ret = 0;
7387 goto out;
7388 }
7389 slot--;
7390 }
7391 ret = 0;
7392 leaf = path->nodes[0];
7393 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007394 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007395 key.type != BTRFS_EXTENT_DATA_KEY) {
7396 /* not our file or wrong item type, must cow */
7397 goto out;
7398 }
7399
7400 if (key.offset > offset) {
7401 /* Wrong offset, must cow */
7402 goto out;
7403 }
7404
7405 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7406 found_type = btrfs_file_extent_type(leaf, fi);
7407 if (found_type != BTRFS_FILE_EXTENT_REG &&
7408 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7409 /* not a regular extent, must cow */
7410 goto out;
7411 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007412
7413 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7414 goto out;
7415
Miao Xiee77751a2013-12-27 21:11:50 +08007416 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7417 if (extent_end <= offset)
7418 goto out;
7419
Chris Mason46bfbb52010-05-26 11:04:10 -04007420 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007421 if (disk_bytenr == 0)
7422 goto out;
7423
7424 if (btrfs_file_extent_compression(leaf, fi) ||
7425 btrfs_file_extent_encryption(leaf, fi) ||
7426 btrfs_file_extent_other_encoding(leaf, fi))
7427 goto out;
7428
Chris Mason46bfbb52010-05-26 11:04:10 -04007429 backref_offset = btrfs_file_extent_offset(leaf, fi);
7430
Josef Bacik7ee9e442013-06-21 16:37:03 -04007431 if (orig_start) {
7432 *orig_start = key.offset - backref_offset;
7433 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7434 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7435 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007436
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007437 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007439
7440 num_bytes = min(offset + *len, extent_end) - offset;
7441 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7442 u64 range_end;
7443
Jeff Mahoneyda170662016-06-15 09:22:56 -04007444 range_end = round_up(offset + num_bytes,
7445 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007446 ret = test_range_bit(io_tree, offset, range_end,
7447 EXTENT_DELALLOC, 0, NULL);
7448 if (ret) {
7449 ret = -EAGAIN;
7450 goto out;
7451 }
7452 }
7453
Josef Bacik1bda19e2013-10-18 12:10:36 -04007454 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007455
7456 /*
7457 * look for other files referencing this extent, if we
7458 * find any we must cow
7459 */
Josef Bacik00361582013-08-14 14:02:47 -04007460
Liu Boe4c3b2d2017-01-30 12:25:28 -08007461 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007462 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007463 if (ret) {
7464 ret = 0;
7465 goto out;
7466 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007467
7468 /*
7469 * adjust disk_bytenr and num_bytes to cover just the bytes
7470 * in this extent we are about to write. If there
7471 * are any csums in that range we have to cow in order
7472 * to keep the csums correct
7473 */
7474 disk_bytenr += backref_offset;
7475 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007476 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7477 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007478 /*
7479 * all of the above have passed, it is safe to overwrite this extent
7480 * without cow
7481 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007482 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007483 ret = 1;
7484out:
7485 btrfs_free_path(path);
7486 return ret;
7487}
7488
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007489bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7490{
7491 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007492 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007493 void **pagep = NULL;
7494 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007495 unsigned long start_idx;
7496 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007497
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007498 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007499
7500 /*
7501 * end is the last byte in the last page. end == start is legal
7502 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007503 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007504
7505 rcu_read_lock();
7506
7507 /* Most of the code in this while loop is lifted from
7508 * find_get_page. It's been modified to begin searching from a
7509 * page and return just the first page found in that range. If the
7510 * found idx is less than or equal to the end idx then we know that
7511 * a page exists. If no pages are found or if those pages are
7512 * outside of the range then we're fine (yay!) */
7513 while (page == NULL &&
7514 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7515 page = radix_tree_deref_slot(pagep);
7516 if (unlikely(!page))
7517 break;
7518
7519 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007520 if (radix_tree_deref_retry(page)) {
7521 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007522 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007523 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007524 /*
7525 * Otherwise, shmem/tmpfs must be storing a swap entry
7526 * here as an exceptional entry: so return it without
7527 * attempting to raise page count.
7528 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007529 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007530 break; /* TODO: Is this relevant for this use case? */
7531 }
7532
Filipe Manana91405152014-06-05 13:22:24 +01007533 if (!page_cache_get_speculative(page)) {
7534 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007535 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007536 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007537
7538 /*
7539 * Has the page moved?
7540 * This is part of the lockless pagecache protocol. See
7541 * include/linux/pagemap.h for details.
7542 */
7543 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007544 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007545 page = NULL;
7546 }
7547 }
7548
7549 if (page) {
7550 if (page->index <= end_idx)
7551 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007552 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007553 }
7554
7555 rcu_read_unlock();
7556 return found;
7557}
7558
Josef Bacikeb838e72012-07-31 16:28:48 -04007559static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7560 struct extent_state **cached_state, int writing)
7561{
7562 struct btrfs_ordered_extent *ordered;
7563 int ret = 0;
7564
7565 while (1) {
7566 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007567 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 /*
7569 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007570 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007571 * extents in this range.
7572 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007573 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007574 lockend - lockstart + 1);
7575
7576 /*
7577 * We need to make sure there are no buffered pages in this
7578 * range either, we could have raced between the invalidate in
7579 * generic_file_direct_write and locking the extent. The
7580 * invalidate needs to happen so that reads after a write do not
7581 * get stale data.
7582 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007583 if (!ordered &&
7584 (!writing ||
7585 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007586 break;
7587
7588 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7589 cached_state, GFP_NOFS);
7590
7591 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007592 /*
7593 * If we are doing a DIO read and the ordered extent we
7594 * found is for a buffered write, we can not wait for it
7595 * to complete and retry, because if we do so we can
7596 * deadlock with concurrent buffered writes on page
7597 * locks. This happens only if our DIO read covers more
7598 * than one extent map, if at this point has already
7599 * created an ordered extent for a previous extent map
7600 * and locked its range in the inode's io tree, and a
7601 * concurrent write against that previous extent map's
7602 * range and this range started (we unlock the ranges
7603 * in the io tree only when the bios complete and
7604 * buffered writes always lock pages before attempting
7605 * to lock range in the io tree).
7606 */
7607 if (writing ||
7608 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7609 btrfs_start_ordered_extent(inode, ordered, 1);
7610 else
7611 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007612 btrfs_put_ordered_extent(ordered);
7613 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007614 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007615 * We could trigger writeback for this range (and wait
7616 * for it to complete) and then invalidate the pages for
7617 * this range (through invalidate_inode_pages2_range()),
7618 * but that can lead us to a deadlock with a concurrent
7619 * call to readpages() (a buffered read or a defrag call
7620 * triggered a readahead) on a page lock due to an
7621 * ordered dio extent we created before but did not have
7622 * yet a corresponding bio submitted (whence it can not
7623 * complete), which makes readpages() wait for that
7624 * ordered extent to complete while holding a lock on
7625 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007626 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007627 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007628 }
7629
Filipe Mananaade77022016-02-18 14:28:55 +00007630 if (ret)
7631 break;
7632
Josef Bacikeb838e72012-07-31 16:28:48 -04007633 cond_resched();
7634 }
7635
7636 return ret;
7637}
7638
Liu Bo6f9994d2017-01-31 07:50:22 -08007639/* The callers of this must take lock_extent() */
7640static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7641 u64 orig_start, u64 block_start,
7642 u64 block_len, u64 orig_block_len,
7643 u64 ram_bytes, int compress_type,
7644 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007645{
7646 struct extent_map_tree *em_tree;
7647 struct extent_map *em;
7648 struct btrfs_root *root = BTRFS_I(inode)->root;
7649 int ret;
7650
Liu Bo6f9994d2017-01-31 07:50:22 -08007651 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7652 type == BTRFS_ORDERED_COMPRESSED ||
7653 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007654 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007655
Josef Bacik69ffb542012-09-11 15:40:07 -04007656 em_tree = &BTRFS_I(inode)->extent_tree;
7657 em = alloc_extent_map();
7658 if (!em)
7659 return ERR_PTR(-ENOMEM);
7660
7661 em->start = start;
7662 em->orig_start = orig_start;
7663 em->len = len;
7664 em->block_len = block_len;
7665 em->block_start = block_start;
7666 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007667 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007668 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007669 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007670 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007671 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007672 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007673 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007674 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7675 em->compress_type = compress_type;
7676 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007677
7678 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007679 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007680 em->start + em->len - 1, 0);
7681 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007682 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007683 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007684 /*
7685 * The caller has taken lock_extent(), who could race with us
7686 * to add em?
7687 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007688 } while (ret == -EEXIST);
7689
7690 if (ret) {
7691 free_extent_map(em);
7692 return ERR_PTR(ret);
7693 }
7694
Liu Bo6f9994d2017-01-31 07:50:22 -08007695 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007696 return em;
7697}
7698
Filipe Manana9c9464c2015-11-04 09:52:04 +00007699static void adjust_dio_outstanding_extents(struct inode *inode,
7700 struct btrfs_dio_data *dio_data,
7701 const u64 len)
7702{
David Sterba823bb202017-01-04 11:09:51 +01007703 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007704
Filipe Manana9c9464c2015-11-04 09:52:04 +00007705 /*
7706 * If we have an outstanding_extents count still set then we're
7707 * within our reservation, otherwise we need to adjust our inode
7708 * counter appropriately.
7709 */
Liu Boc2931662016-12-22 17:13:54 -08007710 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007711 dio_data->outstanding_extents -= num_extents;
7712 } else {
Liu Boc2931662016-12-22 17:13:54 -08007713 /*
7714 * If dio write length has been split due to no large enough
7715 * contiguous space, we need to compensate our inode counter
7716 * appropriately.
7717 */
7718 u64 num_needed = num_extents - dio_data->outstanding_extents;
7719
Filipe Manana9c9464c2015-11-04 09:52:04 +00007720 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007721 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007722 spin_unlock(&BTRFS_I(inode)->lock);
7723 }
7724}
7725
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7727 struct buffer_head *bh_result, int create)
7728{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007729 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007731 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307732 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007733 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007734 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007735 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007736 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007737 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007738
Miao Xie172a5042013-02-21 02:48:22 -07007739 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007740 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007741 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007742 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007743
Josef Bacikc3298612012-08-03 16:49:19 -04007744 lockstart = start;
7745 lockend = start + len - 1;
7746
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007747 if (current->journal_info) {
7748 /*
7749 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007750 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007751 * confused.
7752 */
chandan50745b02015-08-28 21:10:13 +05307753 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007754 current->journal_info = NULL;
7755 }
7756
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 /*
7758 * If this errors out it's because we couldn't invalidate pagecache for
7759 * this range and we need to fallback to buffered.
7760 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007761 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7762 create)) {
7763 ret = -ENOTBLK;
7764 goto err;
7765 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007766
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007767 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007768 if (IS_ERR(em)) {
7769 ret = PTR_ERR(em);
7770 goto unlock_err;
7771 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007772
7773 /*
7774 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7775 * io. INLINE is special, and we could probably kludge it in here, but
7776 * it's still buffered so for safety lets just fall back to the generic
7777 * buffered path.
7778 *
7779 * For COMPRESSED we _have_ to read the entire extent in so we can
7780 * decompress it, so there will be buffering required no matter what we
7781 * do, so go ahead and fallback to buffered.
7782 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007783 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007784 * to buffered IO. Don't blame me, this is the price we pay for using
7785 * the generic code.
7786 */
7787 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7788 em->block_start == EXTENT_MAP_INLINE) {
7789 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007790 ret = -ENOTBLK;
7791 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007792 }
7793
7794 /* Just a good old fashioned hole, return */
7795 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7796 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7797 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007798 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007799 }
7800
7801 /*
7802 * We don't allocate a new extent in the following cases
7803 *
7804 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7805 * existing extent.
7806 * 2) The extent is marked as PREALLOC. We're good to go here and can
7807 * just use the extent.
7808 *
7809 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007810 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007811 len = min(len, em->len - (start - em->start));
7812 lockstart = start + len;
7813 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007814 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007815
7816 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7817 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7818 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007819 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007820 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007821
7822 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7823 type = BTRFS_ORDERED_PREALLOC;
7824 else
7825 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007826 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007828
Josef Bacik00361582013-08-14 14:02:47 -04007829 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007830 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007831 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007832 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007833
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007834 em2 = btrfs_create_dio_extent(inode, start, len,
7835 orig_start, block_start,
7836 len, orig_block_len,
7837 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007838 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007839 if (type == BTRFS_ORDERED_PREALLOC) {
7840 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007841 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007842 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007843 if (em2 && IS_ERR(em2)) {
7844 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007845 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007846 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007847 /*
7848 * For inode marked NODATACOW or extent marked PREALLOC,
7849 * use the existing or preallocated extent, so does not
7850 * need to adjust btrfs_space_info's bytes_may_use.
7851 */
7852 btrfs_free_reserved_data_space_noquota(inode,
7853 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007854 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007856 }
Josef Bacik00361582013-08-14 14:02:47 -04007857
Chris Mason46bfbb52010-05-26 11:04:10 -04007858 /*
7859 * this will cow the extent, reset the len in case we changed
7860 * it above
7861 */
7862 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007863 free_extent_map(em);
7864 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007865 if (IS_ERR(em)) {
7866 ret = PTR_ERR(em);
7867 goto unlock_err;
7868 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007869 len = min(len, em->len - (start - em->start));
7870unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007871 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7872 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007873 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007874 bh_result->b_bdev = em->bdev;
7875 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007876 if (create) {
7877 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7878 set_buffer_new(bh_result);
7879
7880 /*
7881 * Need to update the i_size under the extent lock so buffered
7882 * readers will get the updated i_size when we unlock.
7883 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007884 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007885 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007886
Filipe Manana9c9464c2015-11-04 09:52:04 +00007887 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307888 WARN_ON(dio_data->reserve < len);
7889 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007890 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307891 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007892 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893
Josef Bacikeb838e72012-07-31 16:28:48 -04007894 /*
7895 * In the case of write we need to clear and unlock the entire range,
7896 * in the case of read we need to unlock only the end area that we
7897 * aren't using if there is any left over space.
7898 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007899 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007900 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7901 lockend, unlock_bits, 1, 0,
7902 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007903 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007904 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007905 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007906
Josef Bacik4b46fce2010-05-23 11:00:55 -04007907 free_extent_map(em);
7908
7909 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007910
7911unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007912 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7913 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007914err:
chandan50745b02015-08-28 21:10:13 +05307915 if (dio_data)
7916 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007917 /*
7918 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7919 * write less data then expected, so that we don't underflow our inode's
7920 * outstanding extents counter.
7921 */
7922 if (create && dio_data)
7923 adjust_dio_outstanding_extents(inode, dio_data, len);
7924
Josef Bacikeb838e72012-07-31 16:28:48 -04007925 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926}
7927
Miao Xie8b110e32014-09-12 18:44:03 +08007928static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007929 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007930{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007932 int ret;
7933
Mike Christie37226b22016-06-05 14:31:52 -05007934 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007935
7936 bio_get(bio);
7937
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007938 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007939 if (ret)
7940 goto err;
7941
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007942 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007943err:
7944 bio_put(bio);
7945 return ret;
7946}
7947
7948static int btrfs_check_dio_repairable(struct inode *inode,
7949 struct bio *failed_bio,
7950 struct io_failure_record *failrec,
7951 int failed_mirror)
7952{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007954 int num_copies;
7955
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007956 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007957 if (num_copies == 1) {
7958 /*
7959 * we only have a single copy of the data, so don't bother with
7960 * all the retry and error correction code that follows. no
7961 * matter what the error is, it is very likely to persist.
7962 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007963 btrfs_debug(fs_info,
7964 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7965 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007966 return 0;
7967 }
7968
7969 failrec->failed_mirror = failed_mirror;
7970 failrec->this_mirror++;
7971 if (failrec->this_mirror == failed_mirror)
7972 failrec->this_mirror++;
7973
7974 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007975 btrfs_debug(fs_info,
7976 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7977 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007978 return 0;
7979 }
7980
7981 return 1;
7982}
7983
7984static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307985 struct page *page, unsigned int pgoff,
7986 u64 start, u64 end, int failed_mirror,
7987 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007988{
7989 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007990 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7991 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007992 struct bio *bio;
7993 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007994 int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007995 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 int ret;
7997
Mike Christie37226b22016-06-05 14:31:52 -05007998 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007999
8000 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8001 if (ret)
8002 return ret;
8003
8004 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8005 failed_mirror);
8006 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008007 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008008 return -EIO;
8009 }
8010
Liu Bo17347ce2017-05-15 15:33:27 -07008011 segs = bio_segments(failed_bio);
8012 if (segs > 1 ||
8013 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008014 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008015
8016 isector = start - btrfs_io_bio(failed_bio)->logical;
8017 isector >>= inode->i_sb->s_blocksize_bits;
8018 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008020 if (!bio) {
Josef Bacik7870d082017-05-05 11:57:15 -04008021 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008022 return -EIO;
8023 }
Mike Christie37226b22016-06-05 14:31:52 -05008024 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008025
8026 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8027 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8028 read_mode, failrec->this_mirror, failrec->in_validation);
8029
Mike Christie81a75f672016-06-05 14:31:54 -05008030 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008031 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008032 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008033 bio_put(bio);
8034 }
8035
8036 return ret;
8037}
8038
8039struct btrfs_retry_complete {
8040 struct completion done;
8041 struct inode *inode;
8042 u64 start;
8043 int uptodate;
8044};
8045
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008046static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008047{
8048 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008049 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008050 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008051 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008052 int i;
8053
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008054 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008055 goto end;
8056
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008058 io_tree = &BTRFS_I(inode)->io_tree;
8059 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8060 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061
Miao Xie8b110e32014-09-12 18:44:03 +08008062 done->uptodate = 1;
8063 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008064 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8065 io_tree, done->start, bvec->bv_page,
8066 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008067end:
8068 complete(&done->done);
8069 bio_put(bio);
8070}
8071
8072static int __btrfs_correct_data_nocsum(struct inode *inode,
8073 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008074{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008076 struct bio_vec bvec;
8077 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008078 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008079 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308080 unsigned int pgoff;
8081 u32 sectorsize;
8082 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008083 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008084 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008085
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308086 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008087 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088
Miao Xiec1dc0892014-09-12 18:43:56 +08008089 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008090 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008091 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008092
Liu Bo17347ce2017-05-15 15:33:27 -07008093 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8094 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8095 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308096
8097next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008098 done.uptodate = 0;
8099 done.start = start;
8100 init_completion(&done.done);
8101
Liu Bo17347ce2017-05-15 15:33:27 -07008102 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103 pgoff, start, start + sectorsize - 1,
8104 io_bio->mirror_num,
8105 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008106 if (ret) {
8107 err = ret;
8108 goto next;
8109 }
Miao Xie8b110e32014-09-12 18:44:03 +08008110
8111 wait_for_completion(&done.done);
8112
8113 if (!done.uptodate) {
8114 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308115 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008116 }
8117
Liu Bo629ebf42017-05-15 17:20:07 -07008118next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308119 start += sectorsize;
8120
Liu Bo97bf5a52017-04-07 13:11:10 -07008121 nr_sectors--;
8122 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308123 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008124 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 goto next_block_or_try_again;
8126 }
Miao Xie8b110e32014-09-12 18:44:03 +08008127 }
8128
Liu Bo629ebf42017-05-15 17:20:07 -07008129 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008130}
8131
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008132static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008133{
8134 struct btrfs_retry_complete *done = bio->bi_private;
8135 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008136 struct extent_io_tree *io_tree, *failure_tree;
8137 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008138 struct bio_vec *bvec;
8139 int uptodate;
8140 int ret;
8141 int i;
8142
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008143 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008144 goto end;
8145
8146 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308148 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008149 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150
Josef Bacik7870d082017-05-05 11:57:15 -04008151 io_tree = &BTRFS_I(inode)->io_tree;
8152 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8153
Miao Xie8b110e32014-09-12 18:44:03 +08008154 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008155 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8156 bvec->bv_offset, done->start,
8157 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008158 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008159 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8160 failure_tree, io_tree, done->start,
8161 bvec->bv_page,
8162 btrfs_ino(BTRFS_I(inode)),
8163 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008164 else
8165 uptodate = 0;
8166 }
8167
8168 done->uptodate = uptodate;
8169end:
8170 complete(&done->done);
8171 bio_put(bio);
8172}
8173
8174static int __btrfs_subio_endio_read(struct inode *inode,
8175 struct btrfs_io_bio *io_bio, int err)
8176{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308177 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008178 struct bio_vec bvec;
8179 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008180 struct btrfs_retry_complete done;
8181 u64 start;
8182 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308183 u32 sectorsize;
8184 int nr_sectors;
8185 unsigned int pgoff;
8186 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008187 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008188 int ret;
8189
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308190 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008191 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308192
Miao Xie8b110e32014-09-12 18:44:03 +08008193 err = 0;
8194 start = io_bio->logical;
8195 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008196 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008197
Liu Bo17347ce2017-05-15 15:33:27 -07008198 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8199 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308200
Liu Bo17347ce2017-05-15 15:33:27 -07008201 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308202next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008203 if (uptodate) {
8204 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8205 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8206 bvec.bv_page, pgoff, start, sectorsize);
8207 if (likely(!ret))
8208 goto next;
8209 }
Miao Xie8b110e32014-09-12 18:44:03 +08008210try_again:
8211 done.uptodate = 0;
8212 done.start = start;
8213 init_completion(&done.done);
8214
Liu Bo17347ce2017-05-15 15:33:27 -07008215 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308216 pgoff, start, start + sectorsize - 1,
8217 io_bio->mirror_num,
8218 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008219 if (ret) {
8220 err = ret;
8221 goto next;
8222 }
8223
8224 wait_for_completion(&done.done);
8225
8226 if (!done.uptodate) {
8227 /* We might have another mirror, so try again */
8228 goto try_again;
8229 }
8230next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308231 offset += sectorsize;
8232 start += sectorsize;
8233
8234 ASSERT(nr_sectors);
8235
Liu Bo97bf5a52017-04-07 13:11:10 -07008236 nr_sectors--;
8237 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308238 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008239 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308240 goto next_block;
8241 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008242 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008243
8244 return err;
8245}
8246
Miao Xie8b110e32014-09-12 18:44:03 +08008247static int btrfs_subio_endio_read(struct inode *inode,
8248 struct btrfs_io_bio *io_bio, int err)
8249{
8250 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8251
8252 if (skip_csum) {
8253 if (unlikely(err))
8254 return __btrfs_correct_data_nocsum(inode, io_bio);
8255 else
8256 return 0;
8257 } else {
8258 return __btrfs_subio_endio_read(inode, io_bio, err);
8259 }
8260}
8261
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008262static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008263{
8264 struct btrfs_dio_private *dip = bio->bi_private;
8265 struct inode *inode = dip->inode;
8266 struct bio *dio_bio;
8267 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008268 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008269
Liu Boe3d37fa2017-05-17 15:42:00 -06008270 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) {
Miao Xie8b110e32014-09-12 18:44:03 +08008271 err = btrfs_subio_endio_read(inode, io_bio, err);
Liu Boe3d37fa2017-05-17 15:42:00 -06008272 if (!err)
8273 bio->bi_error = 0;
8274 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008275
Josef Bacik4b46fce2010-05-23 11:00:55 -04008276 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008277 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008278 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008279
Josef Bacik4b46fce2010-05-23 11:00:55 -04008280 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008281
Filipe Manana1636d1d2016-02-15 16:20:26 +00008282 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008283 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008284
8285 if (io_bio->end_io)
8286 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008287 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008288}
8289
Qu Wenruo524272602017-03-08 10:25:52 +08008290static void __endio_write_update_ordered(struct inode *inode,
8291 const u64 offset, const u64 bytes,
8292 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008295 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008296 struct btrfs_workqueue *wq;
8297 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008298 u64 ordered_offset = offset;
8299 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008300 int ret;
8301
Qu Wenruo524272602017-03-08 10:25:52 +08008302 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8303 wq = fs_info->endio_freespace_worker;
8304 func = btrfs_freespace_write_helper;
8305 } else {
8306 wq = fs_info->endio_write_workers;
8307 func = btrfs_endio_write_helper;
8308 }
8309
Chris Mason163cf092010-11-28 19:56:33 -05008310again:
8311 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8312 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008313 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008314 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008315 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008316 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008317
Qu Wenruo524272602017-03-08 10:25:52 +08008318 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8319 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008320out_test:
8321 /*
8322 * our bio might span multiple ordered extents. If we haven't
8323 * completed the accounting for the whole dio, go back and try again
8324 */
Filipe Manana14543772015-11-24 16:23:54 +00008325 if (ordered_offset < offset + bytes) {
8326 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008327 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008328 goto again;
8329 }
Filipe Manana14543772015-11-24 16:23:54 +00008330}
8331
8332static void btrfs_endio_direct_write(struct bio *bio)
8333{
8334 struct btrfs_dio_private *dip = bio->bi_private;
8335 struct bio *dio_bio = dip->dio_bio;
8336
Qu Wenruo524272602017-03-08 10:25:52 +08008337 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8338 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008339
Josef Bacik4b46fce2010-05-23 11:00:55 -04008340 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008341
Filipe Manana1636d1d2016-02-15 16:20:26 +00008342 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008343 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008344 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008345}
8346
Josef Bacikc6100a42017-05-05 11:57:13 -04008347static int __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008348 struct bio *bio, int mirror_num,
8349 unsigned long bio_flags, u64 offset)
8350{
Josef Bacikc6100a42017-05-05 11:57:13 -04008351 struct inode *inode = private_data;
Chris Masoneaf25d92010-05-25 09:48:28 -04008352 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008353 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008354 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008355 return 0;
8356}
8357
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008358static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008359{
8360 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008361 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008362
Miao Xie8b110e32014-09-12 18:44:03 +08008363 if (err)
8364 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008365 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008366 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8367 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008368 (unsigned long long)bio->bi_iter.bi_sector,
8369 bio->bi_iter.bi_size, err);
8370
8371 if (dip->subio_endio)
8372 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008373
8374 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008375 dip->errors = 1;
8376
8377 /*
8378 * before atomic variable goto zero, we must make sure
8379 * dip->errors is perceived to be set.
8380 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008381 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008382 }
8383
8384 /* if there are more bios still pending for this dio, just exit */
8385 if (!atomic_dec_and_test(&dip->pending_bios))
8386 goto out;
8387
Chris Mason9be33952013-05-17 18:30:14 -04008388 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008389 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008390 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008391 dip->dio_bio->bi_error = 0;
8392 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008393 }
8394out:
8395 bio_put(bio);
8396}
8397
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008398static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008399 struct btrfs_dio_private *dip,
8400 struct bio *bio,
8401 u64 file_offset)
8402{
8403 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8404 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8405 int ret;
8406
8407 /*
8408 * We load all the csum data we need when we submit
8409 * the first bio to reduce the csum tree search and
8410 * contention.
8411 */
8412 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008413 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008414 file_offset);
8415 if (ret)
8416 return ret;
8417 }
8418
8419 if (bio == dip->orig_bio)
8420 return 0;
8421
8422 file_offset -= dip->logical_offset;
8423 file_offset >>= inode->i_sb->s_blocksize_bits;
8424 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8425
8426 return 0;
8427}
8428
Miao Xiee65e1532010-11-22 03:04:43 +00008429static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008430 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008431 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008433 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008434 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008435 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008436 int ret;
8437
Josef Bacikb812ce22012-11-16 13:56:32 -05008438 if (async_submit)
8439 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8440
Miao Xiee65e1532010-11-22 03:04:43 +00008441 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008442
8443 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008444 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008445 if (ret)
8446 goto err;
8447 }
Miao Xiee65e1532010-11-22 03:04:43 +00008448
Josef Bacik1ae39932011-04-06 14:41:34 -04008449 if (skip_sum)
8450 goto map;
8451
8452 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008453 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8454 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008455 __btrfs_submit_bio_start_direct_io,
8456 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008457 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008458 } else if (write) {
8459 /*
8460 * If we aren't doing async submit, calculate the csum of the
8461 * bio now.
8462 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008463 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008464 if (ret)
8465 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008466 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008467 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008468 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008469 if (ret)
8470 goto err;
8471 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008472map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008473 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008474err:
8475 bio_put(bio);
8476 return ret;
8477}
8478
Mike Christie81a75f672016-06-05 14:31:54 -05008479static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008480 int skip_sum)
8481{
8482 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008484 struct bio *bio;
8485 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008486 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008487 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008488 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008489 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008490 u64 submit_len;
8491 int clone_offset = 0;
8492 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308493 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008494
Kent Overstreet4f024f32013-10-11 15:44:27 -07008495 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008496 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008497 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8498 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008499 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008500 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008501
Liu Bo725130b2017-05-16 09:51:39 -07008502 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008503 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008504 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008505 goto submit;
8506 }
8507
David Woodhouse53b381b2013-01-29 18:40:14 -05008508 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008509 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008510 async_submit = 0;
8511 else
8512 async_submit = 1;
8513
Liu Bo725130b2017-05-16 09:51:39 -07008514 /* bio split */
8515 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008516 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008517 do {
Liu Bo725130b2017-05-16 09:51:39 -07008518 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008519
Liu Bo725130b2017-05-16 09:51:39 -07008520 /*
8521 * This will never fail as it's passing GPF_NOFS and
8522 * the allocation is backed by btrfs_bioset.
8523 */
Liu Boe4770942017-05-16 10:57:14 -07008524 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008525 clone_len);
8526 bio->bi_private = dip;
8527 bio->bi_end_io = btrfs_end_dio_bio;
8528 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008529
Liu Bo725130b2017-05-16 09:51:39 -07008530 ASSERT(submit_len >= clone_len);
8531 submit_len -= clone_len;
8532 if (submit_len == 0)
8533 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008534
Liu Bo725130b2017-05-16 09:51:39 -07008535 /*
8536 * Increase the count before we submit the bio so we know
8537 * the end IO handler won't happen before we increase the
8538 * count. Otherwise, the dip might get freed before we're
8539 * done setting it up.
8540 */
8541 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008542
Liu Bo725130b2017-05-16 09:51:39 -07008543 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8544 async_submit);
8545 if (ret) {
8546 bio_put(bio);
8547 atomic_dec(&dip->pending_bios);
8548 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008549 }
Liu Bo725130b2017-05-16 09:51:39 -07008550
8551 clone_offset += clone_len;
8552 start_sector += clone_len >> 9;
8553 file_offset += clone_len;
8554
8555 map_length = submit_len;
8556 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8557 start_sector << 9, &map_length, NULL, 0);
8558 if (ret)
8559 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008560 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008561
Josef Bacik02f57c72011-04-06 14:25:44 -04008562submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008563 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008564 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008565 if (!ret)
8566 return 0;
8567
8568 bio_put(bio);
8569out_err:
8570 dip->errors = 1;
8571 /*
8572 * before atomic variable goto zero, we must
8573 * make sure dip->errors is perceived to be set.
8574 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008575 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008576 if (atomic_dec_and_test(&dip->pending_bios))
8577 bio_io_error(dip->orig_bio);
8578
8579 /* bio_end_io() will handle error, so we needn't return it */
8580 return 0;
8581}
8582
Mike Christie8a4c1e42016-06-05 14:31:50 -05008583static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8584 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008585{
Filipe Manana61de7182015-07-01 12:13:10 +01008586 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008587 struct bio *bio = NULL;
8588 struct btrfs_io_bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008589 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008590 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591 int ret = 0;
8592
8593 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8594
David Sterba8b6c1d52017-06-02 17:48:13 +02008595 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008596
Miao Xiec1dc0892014-09-12 18:43:56 +08008597 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008598 if (!dip) {
8599 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008600 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008601 }
8602
8603 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008604 dip->inode = inode;
8605 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008606 dip->bytes = dio_bio->bi_iter.bi_size;
8607 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008608 bio->bi_private = dip;
8609 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008610 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008611 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008612 io_bio = btrfs_io_bio(bio);
8613 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008614
Miao Xiec1dc0892014-09-12 18:43:56 +08008615 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008616 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008617 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008618 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008619 dip->subio_endio = btrfs_subio_endio_read;
8620 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621
Filipe Mananaf28a4922015-12-08 19:23:20 +00008622 /*
8623 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8624 * even if we fail to submit a bio, because in such case we do the
8625 * corresponding error handling below and it must not be done a second
8626 * time by btrfs_direct_IO().
8627 */
8628 if (write) {
8629 struct btrfs_dio_data *dio_data = current->journal_info;
8630
8631 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8632 dip->bytes;
8633 dio_data->unsubmitted_oe_range_start =
8634 dio_data->unsubmitted_oe_range_end;
8635 }
8636
Mike Christie81a75f672016-06-05 14:31:54 -05008637 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008638 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008639 return;
Chris Mason9be33952013-05-17 18:30:14 -04008640
Liu Bo3892ac92017-04-17 15:00:28 -07008641 if (io_bio->end_io)
8642 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008643
Josef Bacik4b46fce2010-05-23 11:00:55 -04008644free_ordered:
8645 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008646 * If we arrived here it means either we failed to submit the dip
8647 * or we either failed to clone the dio_bio or failed to allocate the
8648 * dip. If we cloned the dio_bio and allocated the dip, we can just
8649 * call bio_endio against our io_bio so that we get proper resource
8650 * cleanup if we fail to submit the dip, otherwise, we must do the
8651 * same as btrfs_endio_direct_[write|read] because we can't call these
8652 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008653 */
Liu Bo3892ac92017-04-17 15:00:28 -07008654 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008655 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008656 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008657 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008658 * and all the cleanup and final put for dio_bio (through
8659 * dio_end_io()).
8660 */
8661 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008662 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008663 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008664 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008665 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008666 file_offset,
8667 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008668 false);
Filipe Manana14543772015-11-24 16:23:54 +00008669 else
Filipe Manana61de7182015-07-01 12:13:10 +01008670 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8671 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008672
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008673 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008674 /*
8675 * Releases and cleans up our dio_bio, no need to bio_put()
8676 * nor bio_endio()/bio_io_error() against dio_bio.
8677 */
8678 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008679 }
Liu Bo3892ac92017-04-17 15:00:28 -07008680 if (bio)
8681 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008682 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008683}
8684
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008685static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8686 struct kiocb *iocb,
8687 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008688{
8689 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008690 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008691 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008692 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008693
8694 if (offset & blocksize_mask)
8695 goto out;
8696
Al Viro28060d52014-03-22 05:15:17 -04008697 if (iov_iter_alignment(iter) & blocksize_mask)
8698 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008699
Al Viro28060d52014-03-22 05:15:17 -04008700 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008701 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008702 return 0;
8703 /*
8704 * Check to make sure we don't have duplicate iov_base's in this
8705 * iovec, if so return EINVAL, otherwise we'll get csum errors
8706 * when reading back.
8707 */
8708 for (seg = 0; seg < iter->nr_segs; seg++) {
8709 for (i = seg + 1; i < iter->nr_segs; i++) {
8710 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008711 goto out;
8712 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008713 }
8714 retval = 0;
8715out:
8716 return retval;
8717}
Josef Bacikeb838e72012-07-31 16:28:48 -04008718
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008719static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008720{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008721 struct file *file = iocb->ki_filp;
8722 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008723 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308724 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008725 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008726 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008727 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008728 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008729 bool wakeup = true;
8730 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008731 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008732
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008733 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008734 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008735
Jens Axboefe0f07d2015-04-15 17:05:48 -06008736 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008737 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008738
Josef Bacik0e267c42013-07-02 10:38:02 -04008739 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008740 * The generic stuff only does filemap_write_and_wait_range, which
8741 * isn't enough if we've written compressed pages to this area, so
8742 * we need to flush the dirty pages again to make absolutely sure
8743 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008744 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008745 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008746 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8747 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008748 filemap_fdatawrite_range(inode->i_mapping, offset,
8749 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008750
Omar Sandoval6f673762015-03-16 04:33:52 -07008751 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008752 /*
8753 * If the write DIO is beyond the EOF, we need update
8754 * the isize, but it is protected by i_mutex. So we can
8755 * not unlock the i_mutex at this case.
8756 */
8757 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008758 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008759 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008760 relock = true;
8761 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008762 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8763 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008764 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008765 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008766 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008767
8768 /*
8769 * We need to know how many extents we reserved so that we can
8770 * do the accounting properly if we go over the number we
8771 * originally calculated. Abuse current->journal_info for this.
8772 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008773 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008774 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008775 dio_data.unsubmitted_oe_range_start = (u64)offset;
8776 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308777 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308778 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008779 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8780 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008781 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008782 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8783 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008784 }
8785
Omar Sandoval17f8c842015-03-16 04:33:50 -07008786 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008787 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008788 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008789 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008790 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308791 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008792 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008793 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308794 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008795 btrfs_delalloc_release_space(inode, offset,
8796 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008797 /*
8798 * On error we might have left some ordered extents
8799 * without submitting corresponding bios for them, so
8800 * cleanup them up to avoid other tasks getting them
8801 * and waiting for them to complete forever.
8802 */
8803 if (dio_data.unsubmitted_oe_range_start <
8804 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008805 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008806 dio_data.unsubmitted_oe_range_start,
8807 dio_data.unsubmitted_oe_range_end -
8808 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008809 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008810 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008811 btrfs_delalloc_release_space(inode, offset,
8812 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008813 }
Miao Xie38851cc2013-02-08 07:04:11 +00008814out:
Miao Xie2e60a512013-02-08 07:01:08 +00008815 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008816 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008817 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008818 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008819
Qu Wenruo364ecf32017-02-27 15:10:38 +08008820 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008821 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008822}
8823
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008824#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8825
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008826static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8827 __u64 start, __u64 len)
8828{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008829 int ret;
8830
8831 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8832 if (ret)
8833 return ret;
8834
Chris Masonec29ed52011-02-23 16:23:20 -05008835 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008836}
8837
Chris Mason9ebefb182007-06-15 13:50:00 -04008838int btrfs_readpage(struct file *file, struct page *page)
8839{
Chris Masond1310b22008-01-24 16:13:08 -05008840 struct extent_io_tree *tree;
8841 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008842 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008843}
Chris Mason1832a6d2007-12-21 16:27:21 -05008844
Chris Mason39279cc2007-06-12 06:35:45 -04008845static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8846{
Chris Masond1310b22008-01-24 16:13:08 -05008847 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008848 struct inode *inode = page->mapping->host;
8849 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008850
8851 if (current->flags & PF_MEMALLOC) {
8852 redirty_page_for_writepage(wbc, page);
8853 unlock_page(page);
8854 return 0;
8855 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008856
8857 /*
8858 * If we are under memory pressure we will call this directly from the
8859 * VM, we need to make sure we have the inode referenced for the ordered
8860 * extent. If not just return like we didn't do anything.
8861 */
8862 if (!igrab(inode)) {
8863 redirty_page_for_writepage(wbc, page);
8864 return AOP_WRITEPAGE_ACTIVATE;
8865 }
Chris Masond1310b22008-01-24 16:13:08 -05008866 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008867 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8868 btrfs_add_delayed_iput(inode);
8869 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008870}
Chris Mason39279cc2007-06-12 06:35:45 -04008871
Eric Sandeen48a3b632013-04-25 20:41:01 +00008872static int btrfs_writepages(struct address_space *mapping,
8873 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008874{
Chris Masond1310b22008-01-24 16:13:08 -05008875 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008876
Chris Masond1310b22008-01-24 16:13:08 -05008877 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008878 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8879}
8880
Chris Mason3ab2fb52007-11-08 10:59:22 -05008881static int
8882btrfs_readpages(struct file *file, struct address_space *mapping,
8883 struct list_head *pages, unsigned nr_pages)
8884{
Chris Masond1310b22008-01-24 16:13:08 -05008885 struct extent_io_tree *tree;
8886 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008887 return extent_readpages(tree, mapping, pages, nr_pages,
8888 btrfs_get_extent);
8889}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008891{
Chris Masond1310b22008-01-24 16:13:08 -05008892 struct extent_io_tree *tree;
8893 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008894 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008895
Chris Masond1310b22008-01-24 16:13:08 -05008896 tree = &BTRFS_I(page->mapping->host)->io_tree;
8897 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008898 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008899 if (ret == 1) {
8900 ClearPagePrivate(page);
8901 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008902 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008903 }
8904 return ret;
8905}
Chris Mason39279cc2007-06-12 06:35:45 -04008906
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8908{
Chris Mason98509cf2008-09-11 15:51:43 -04008909 if (PageWriteback(page) || PageDirty(page))
8910 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008911 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912}
8913
Lukas Czernerd47992f2013-05-21 23:17:23 -04008914static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8915 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008916{
Josef Bacik5fd02042012-05-02 14:00:54 -04008917 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008918 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008920 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008921 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008922 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308923 u64 start;
8924 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008925 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008926
Chris Mason8b62b722009-09-02 16:53:46 -04008927 /*
8928 * we have the page locked, so new writeback can't start,
8929 * and the dirty bit won't be cleared while we are here.
8930 *
8931 * Wait for IO on this page so that we can safely clear
8932 * the PagePrivate2 bit and do ordered accounting
8933 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008935
Josef Bacik5fd02042012-05-02 14:00:54 -04008936 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008937 if (offset) {
8938 btrfs_releasepage(page, GFP_NOFS);
8939 return;
8940 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008941
8942 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008943 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308944again:
8945 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008946 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308947 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308949 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008950 /*
8951 * IO on this page will never be started, so we need
8952 * to account for any ordered extents now
8953 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008954 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308955 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008956 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008957 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008958 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8959 EXTENT_DEFRAG, 1, 0, &cached_state,
8960 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008961 /*
8962 * whoever cleared the private bit is responsible
8963 * for the finish_ordered_io
8964 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008965 if (TestClearPagePrivate2(page)) {
8966 struct btrfs_ordered_inode_tree *tree;
8967 u64 new_len;
8968
8969 tree = &BTRFS_I(inode)->ordered_tree;
8970
8971 spin_lock_irq(&tree->lock);
8972 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308973 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008974 if (new_len < ordered->truncated_len)
8975 ordered->truncated_len = new_len;
8976 spin_unlock_irq(&tree->lock);
8977
8978 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308979 start,
8980 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008981 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008982 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008983 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008984 if (!inode_evicting) {
8985 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308986 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008987 &cached_state);
8988 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308989
8990 start = end + 1;
8991 if (start < page_end)
8992 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008993 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008994
Qu Wenruob9d0b382015-09-29 10:35:16 +08008995 /*
8996 * Qgroup reserved space handler
8997 * Page here will be either
8998 * 1) Already written to disk
8999 * In this case, its reserved space is released from data rsv map
9000 * and will be freed by delayed_ref handler finally.
9001 * So even we call qgroup_free_data(), it won't decrease reserved
9002 * space.
9003 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009004 * This means the reserved space should be freed here. However,
9005 * if a truncate invalidates the page (by clearing PageDirty)
9006 * and the page is accounted for while allocating extent
9007 * in btrfs_check_data_free_space() we let delayed_ref to
9008 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009009 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009010 if (PageDirty(page))
9011 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009012 if (!inode_evicting) {
9013 clear_extent_bit(tree, page_start, page_end,
9014 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009015 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9016 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009017 &cached_state, GFP_NOFS);
9018
9019 __btrfs_releasepage(page, GFP_NOFS);
9020 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009021
Chris Mason4a096752008-07-21 10:29:44 -04009022 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009023 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009024 ClearPagePrivate(page);
9025 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009026 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009027 }
Chris Mason39279cc2007-06-12 06:35:45 -04009028}
9029
Chris Mason9ebefb182007-06-15 13:50:00 -04009030/*
9031 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9032 * called from a page fault handler when a page is first dirtied. Hence we must
9033 * be careful to check for EOF conditions here. We set the page up correctly
9034 * for a written page which means we get ENOSPC checking when writing into
9035 * holes and correct delalloc and unwritten extent mapping on filesystems that
9036 * support these features.
9037 *
9038 * We are not allowed to take the i_mutex here so we have to play games to
9039 * protect against truncate races as the page could now be beyond EOF. Because
9040 * vmtruncate() writes the inode size before removing pages, once we have the
9041 * page lock we can determine safely if the page is beyond EOF. If it is not
9042 * beyond EOF, then the page is guaranteed safe against truncation until we
9043 * unlock the page.
9044 */
Dave Jiang11bac802017-02-24 14:56:41 -08009045int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009046{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009047 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009048 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009050 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9051 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009052 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009053 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009054 char *kaddr;
9055 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009056 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009057 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009058 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309059 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009060 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009061 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309062 u64 end;
9063
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009064 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009065
Jan Karab2b5ef52012-06-12 16:20:45 +02009066 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009067 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009068 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309069 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009070
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309071 /*
9072 * Reserving delalloc space after obtaining the page lock can lead to
9073 * deadlock. For example, if a dirty page is locked by this function
9074 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9075 * dirty page write out, then the btrfs_writepage() function could
9076 * end up waiting indefinitely to get a lock on the page currently
9077 * being processed by btrfs_page_mkwrite() function.
9078 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009079 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309080 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009081 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009082 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009083 reserved = 1;
9084 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009085 if (ret) {
9086 if (ret == -ENOMEM)
9087 ret = VM_FAULT_OOM;
9088 else /* -ENOSPC, -EIO, etc */
9089 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009090 if (reserved)
9091 goto out;
9092 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009093 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009094
Nick Piggin56a76f82009-03-31 15:23:23 -07009095 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009096again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009097 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009098 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009099
Chris Mason9ebefb182007-06-15 13:50:00 -04009100 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009101 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009102 /* page got truncated out from underneath us */
9103 goto out_unlock;
9104 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009105 wait_on_page_writeback(page);
9106
David Sterbaff13db42015-12-03 14:30:40 +01009107 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009108 set_page_extent_mapped(page);
9109
Chris Masoneb84ae02008-07-17 13:53:27 -04009110 /*
9111 * we can't set the delalloc bits if there are pending ordered
9112 * extents. Drop our locks and wait for them to finish
9113 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009114 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9115 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009116 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009117 unlock_extent_cached(io_tree, page_start, page_end,
9118 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009119 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009120 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009121 btrfs_put_ordered_extent(ordered);
9122 goto again;
9123 }
9124
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009125 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009126 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009127 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009128 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309129 end = page_start + reserved_space - 1;
9130 spin_lock(&BTRFS_I(inode)->lock);
9131 BTRFS_I(inode)->outstanding_extents++;
9132 spin_unlock(&BTRFS_I(inode)->lock);
9133 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009134 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309135 }
9136 }
9137
Josef Bacikfbf19082009-10-01 17:10:23 -04009138 /*
Liu Bo54160342016-12-13 12:15:19 -08009139 * page_mkwrite gets called when the page is firstly dirtied after it's
9140 * faulted in, but write(2) could also dirty a page and set delalloc
9141 * bits, thus in this case for space account reason, we still need to
9142 * clear any delalloc bits within this page range since we have to
9143 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009144 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309145 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009146 EXTENT_DIRTY | EXTENT_DELALLOC |
9147 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009148 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009149
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309150 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009151 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009152 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009153 unlock_extent_cached(io_tree, page_start, page_end,
9154 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009155 ret = VM_FAULT_SIGBUS;
9156 goto out_unlock;
9157 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009158 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009159
9160 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009161 if (page_start + PAGE_SIZE > size)
9162 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009163 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009164 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009165
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009166 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009167 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009168 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009169 flush_dcache_page(page);
9170 kunmap(page);
9171 }
Chris Mason247e7432008-07-17 12:53:51 -04009172 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009173 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009174 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009175
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009176 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009177 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009178 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009179
Josef Bacik2ac55d42010-02-03 19:33:23 +00009180 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009181
9182out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009183 if (!ret) {
9184 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009185 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009186 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009187 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009188 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009189out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309190 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009191out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009192 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009193 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009194 return ret;
9195}
9196
Josef Bacika41ad392011-01-31 15:30:16 -05009197static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009200 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009201 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009202 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009203 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009204 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009205 u64 mask = fs_info->sectorsize - 1;
9206 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009207
Josef Bacik0ef8b722013-10-25 16:13:35 -04009208 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9209 (u64)-1);
9210 if (ret)
9211 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009212
Josef Bacikfcb80c22011-05-03 10:40:22 -04009213 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009214 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009215 * 3 things going on here
9216 *
9217 * 1) We need to reserve space for our orphan item and the space to
9218 * delete our orphan item. Lord knows we don't want to have a dangling
9219 * orphan item because we didn't reserve space to remove it.
9220 *
9221 * 2) We need to reserve space to update our inode.
9222 *
9223 * 3) We need to have something to cache all the space that is going to
9224 * be free'd up by the truncate operation, but also have some slack
9225 * space reserved in case it uses space during the truncate (thank you
9226 * very much snapshotting).
9227 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009228 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009229 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009230 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009231 * doesn't end up using space reserved for updating the inode or
9232 * removing the orphan item. We also need to be able to stop the
9233 * transaction and start a new one, which means we need to be able to
9234 * update the inode several times, and we have no idea of knowing how
9235 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009236 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009237 * Then there is the orphan item, which does indeed need to be held on
9238 * to for the whole operation, and we need nobody to touch this reserved
9239 * space except the orphan code.
9240 *
9241 * So that leaves us with
9242 *
9243 * 1) root->orphan_block_rsv - for the orphan deletion.
9244 * 2) rsv - for the truncate reservation, which we will steal from the
9245 * transaction reservation.
9246 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9247 * updating the inode.
9248 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009249 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250 if (!rsv)
9251 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009252 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009253 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009254
Josef Bacik907cbce2011-08-08 13:46:15 -04009255 /*
Josef Bacik07127182011-08-19 10:29:59 -04009256 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009257 * 1 for updating the inode.
9258 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009259 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009260 if (IS_ERR(trans)) {
9261 err = PTR_ERR(trans);
9262 goto out;
9263 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009264
Josef Bacik907cbce2011-08-08 13:46:15 -04009265 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009266 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009267 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009268 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009269
Chris Mason5a3f23d2009-03-31 13:27:11 -04009270 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009271 * So if we truncate and then write and fsync we normally would just
9272 * write the extents that changed, which is a problem if we need to
9273 * first truncate that entire inode. So set this flag so we write out
9274 * all of the extents in the inode to the sync log so we're completely
9275 * safe.
9276 */
9277 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009278 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009279
Yan, Zheng80825102009-11-12 09:35:36 +00009280 while (1) {
9281 ret = btrfs_truncate_inode_items(trans, root, inode,
9282 inode->i_size,
9283 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009284 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009285 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009286 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009287 }
Chris Mason39279cc2007-06-12 06:35:45 -04009288
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009289 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009290 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009291 if (ret) {
9292 err = ret;
9293 break;
9294 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009295
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009296 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009297 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009298
9299 trans = btrfs_start_transaction(root, 2);
9300 if (IS_ERR(trans)) {
9301 ret = err = PTR_ERR(trans);
9302 trans = NULL;
9303 break;
9304 }
9305
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009306 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009307 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009308 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009309 BUG_ON(ret); /* shouldn't happen */
9310 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009311 }
9312
9313 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009314 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009315 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009316 if (ret)
9317 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009318 }
9319
Chris Mason917c16b2011-11-08 14:49:59 -05009320 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009321 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009322 ret = btrfs_update_inode(trans, root, inode);
9323 if (ret && !err)
9324 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009325
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009326 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009327 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009328 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009329out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009330 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009331
Josef Bacik3893e332011-01-31 16:03:11 -05009332 if (ret && !err)
9333 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009334
Josef Bacik3893e332011-01-31 16:03:11 -05009335 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009336}
9337
Sven Wegener3b963622008-06-09 21:57:42 -04009338/*
Chris Masond352ac62008-09-29 15:18:18 -04009339 * create a new subvolume directory/inode (helper for the ioctl).
9340 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009341int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009342 struct btrfs_root *new_root,
9343 struct btrfs_root *parent_root,
9344 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009345{
Chris Mason39279cc2007-06-12 06:35:45 -04009346 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009347 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009348 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009349
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009350 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9351 new_dirid, new_dirid,
9352 S_IFDIR | (~current_umask() & S_IRWXUGO),
9353 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009354 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009355 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009356 inode->i_op = &btrfs_dir_inode_operations;
9357 inode->i_fop = &btrfs_dir_file_operations;
9358
Miklos Szeredibfe86842011-10-28 14:13:29 +02009359 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009360 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009361 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009362
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009363 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9364 if (err)
9365 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009366 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009367 new_root->root_key.objectid, err);
9368
Yan, Zheng76dda932009-09-21 16:00:26 -04009369 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009370
Yan, Zheng76dda932009-09-21 16:00:26 -04009371 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009372 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009373}
9374
Chris Mason39279cc2007-06-12 06:35:45 -04009375struct inode *btrfs_alloc_inode(struct super_block *sb)
9376{
9377 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009378 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009379
9380 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9381 if (!ei)
9382 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009383
9384 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009385 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009386 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009387 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009388 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009389 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009390 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009391 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009392 ei->disk_i_size = 0;
9393 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009394 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009395 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009396 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009397 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009398 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009399 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009400
Josef Bacik9e0baf62011-07-15 15:16:44 +00009401 spin_lock_init(&ei->lock);
9402 ei->outstanding_extents = 0;
9403 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009404
Josef Bacik72ac3c02012-05-23 14:13:11 -04009405 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009406 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009407
Miao Xie16cdcec2011-04-22 18:12:22 +08009408 ei->delayed_node = NULL;
9409
chandan r9cc97d62012-07-04 12:48:07 +05309410 ei->i_otime.tv_sec = 0;
9411 ei->i_otime.tv_nsec = 0;
9412
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009414 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009415 extent_io_tree_init(&ei->io_tree, inode);
9416 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009417 ei->io_tree.track_uptodate = 1;
9418 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009419 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009420 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009421 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009422 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009423 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009424 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009425 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009426 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009427
9428 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009429}
9430
Josef Bacikaaedb552013-10-11 14:44:09 -04009431#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9432void btrfs_test_destroy_inode(struct inode *inode)
9433{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009434 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009435 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9436}
9437#endif
9438
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009439static void btrfs_i_callback(struct rcu_head *head)
9440{
9441 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009442 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9443}
9444
Chris Mason39279cc2007-06-12 06:35:45 -04009445void btrfs_destroy_inode(struct inode *inode)
9446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009448 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009449 struct btrfs_root *root = BTRFS_I(inode)->root;
9450
Al Virob3d9b7a2012-06-09 13:51:19 -04009451 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009452 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009453 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9454 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009455 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009456 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009457 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009458 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009459
Chris Mason5a3f23d2009-03-31 13:27:11 -04009460 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009461 * This can happen where we create an inode, but somebody else also
9462 * created the same inode and we need to destroy the one we already
9463 * created.
9464 */
9465 if (!root)
9466 goto free;
9467
Josef Bacik8a35d952012-05-23 14:26:42 -04009468 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9469 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009471 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009472 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009473 }
Josef Bacik7b128762008-07-24 12:17:14 -04009474
Chris Masond3977122009-01-05 21:25:51 -05009475 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009476 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9477 if (!ordered)
9478 break;
9479 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009480 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009481 "found ordered extent %llu %llu on inode cleanup",
9482 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009483 btrfs_remove_ordered_extent(inode, ordered);
9484 btrfs_put_ordered_extent(ordered);
9485 btrfs_put_ordered_extent(ordered);
9486 }
9487 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009488 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009489 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009490 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009491free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009492 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009493}
9494
Al Viro45321ac2010-06-07 13:43:19 -04009495int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009496{
9497 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009498
Naohiro Aota6379ef92013-06-06 09:56:34 +00009499 if (root == NULL)
9500 return 1;
9501
Liu Bofa6ac872013-02-20 14:10:23 +00009502 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009503 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009504 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009505 else
Al Viro45321ac2010-06-07 13:43:19 -04009506 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009507}
9508
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009509static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009510{
9511 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9512
9513 inode_init_once(&ei->vfs_inode);
9514}
9515
9516void btrfs_destroy_cachep(void)
9517{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009518 /*
9519 * Make sure all delayed rcu free inodes are flushed before we
9520 * destroy cache.
9521 */
9522 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009523 kmem_cache_destroy(btrfs_inode_cachep);
9524 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009525 kmem_cache_destroy(btrfs_path_cachep);
9526 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009527}
9528
9529int btrfs_init_cachep(void)
9530{
David Sterba837e1972012-09-07 03:00:48 -06009531 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009532 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009533 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9534 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009535 if (!btrfs_inode_cachep)
9536 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009537
David Sterba837e1972012-09-07 03:00:48 -06009538 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009539 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009540 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009541 if (!btrfs_trans_handle_cachep)
9542 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009543
David Sterba837e1972012-09-07 03:00:48 -06009544 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009545 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009546 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009547 if (!btrfs_path_cachep)
9548 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009549
David Sterba837e1972012-09-07 03:00:48 -06009550 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009551 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009552 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009553 if (!btrfs_free_space_cachep)
9554 goto fail;
9555
Chris Mason39279cc2007-06-12 06:35:45 -04009556 return 0;
9557fail:
9558 btrfs_destroy_cachep();
9559 return -ENOMEM;
9560}
9561
David Howellsa528d352017-01-31 16:46:22 +00009562static int btrfs_getattr(const struct path *path, struct kstat *stat,
9563 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009564{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009565 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009566 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009567 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009568 u32 bi_flags = BTRFS_I(inode)->flags;
9569
9570 stat->result_mask |= STATX_BTIME;
9571 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9572 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9573 if (bi_flags & BTRFS_INODE_APPEND)
9574 stat->attributes |= STATX_ATTR_APPEND;
9575 if (bi_flags & BTRFS_INODE_COMPRESS)
9576 stat->attributes |= STATX_ATTR_COMPRESSED;
9577 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9578 stat->attributes |= STATX_ATTR_IMMUTABLE;
9579 if (bi_flags & BTRFS_INODE_NODUMP)
9580 stat->attributes |= STATX_ATTR_NODUMP;
9581
9582 stat->attributes_mask |= (STATX_ATTR_APPEND |
9583 STATX_ATTR_COMPRESSED |
9584 STATX_ATTR_IMMUTABLE |
9585 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009586
Chris Mason39279cc2007-06-12 06:35:45 -04009587 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009588 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009589
9590 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009591 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009592 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009593 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009594 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009595 return 0;
9596}
9597
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598static int btrfs_rename_exchange(struct inode *old_dir,
9599 struct dentry *old_dentry,
9600 struct inode *new_dir,
9601 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009602{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009603 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009604 struct btrfs_trans_handle *trans;
9605 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009606 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 struct inode *new_inode = new_dentry->d_inode;
9608 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009609 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009610 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009611 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9612 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 u64 old_idx = 0;
9614 u64 new_idx = 0;
9615 u64 root_objectid;
9616 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 bool root_log_pinned = false;
9618 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619
9620 /* we only allow rename subvolume link between subvolumes */
9621 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9622 return -EXDEV;
9623
9624 /* close the race window with snapshot create/destroy ioctl */
9625 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009626 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009628 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009629
9630 /*
9631 * We want to reserve the absolute worst case amount of items. So if
9632 * both inodes are subvols and we need to unlink them then that would
9633 * require 4 item modifications, but if they are both normal inodes it
9634 * would require 5 item modifications, so we'll assume their normal
9635 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9636 * should cover the worst case number of items we'll modify.
9637 */
9638 trans = btrfs_start_transaction(root, 12);
9639 if (IS_ERR(trans)) {
9640 ret = PTR_ERR(trans);
9641 goto out_notrans;
9642 }
9643
9644 /*
9645 * We need to find a free sequence number both in the source and
9646 * in the destination directory for the exchange.
9647 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009648 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 if (ret)
9650 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009651 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 if (ret)
9653 goto out_fail;
9654
9655 BTRFS_I(old_inode)->dir_index = 0ULL;
9656 BTRFS_I(new_inode)->dir_index = 0ULL;
9657
9658 /* Reference for the source. */
9659 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9660 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009661 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009663 btrfs_pin_log_trans(root);
9664 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665 ret = btrfs_insert_inode_ref(trans, dest,
9666 new_dentry->d_name.name,
9667 new_dentry->d_name.len,
9668 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009669 btrfs_ino(BTRFS_I(new_dir)),
9670 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 if (ret)
9672 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 }
9674
9675 /* And now for the dest. */
9676 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9677 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009678 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009680 btrfs_pin_log_trans(dest);
9681 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 ret = btrfs_insert_inode_ref(trans, root,
9683 old_dentry->d_name.name,
9684 old_dentry->d_name.len,
9685 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009686 btrfs_ino(BTRFS_I(old_dir)),
9687 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009688 if (ret)
9689 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690 }
9691
9692 /* Update inode version and ctime/mtime. */
9693 inode_inc_iversion(old_dir);
9694 inode_inc_iversion(new_dir);
9695 inode_inc_iversion(old_inode);
9696 inode_inc_iversion(new_inode);
9697 old_dir->i_ctime = old_dir->i_mtime = ctime;
9698 new_dir->i_ctime = new_dir->i_mtime = ctime;
9699 old_inode->i_ctime = ctime;
9700 new_inode->i_ctime = ctime;
9701
9702 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009703 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9704 BTRFS_I(old_inode), 1);
9705 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9706 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 }
9708
9709 /* src is a subvolume */
9710 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9711 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9712 ret = btrfs_unlink_subvol(trans, root, old_dir,
9713 root_objectid,
9714 old_dentry->d_name.name,
9715 old_dentry->d_name.len);
9716 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009717 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9718 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 old_dentry->d_name.name,
9720 old_dentry->d_name.len);
9721 if (!ret)
9722 ret = btrfs_update_inode(trans, root, old_inode);
9723 }
9724 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009725 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009726 goto out_fail;
9727 }
9728
9729 /* dest is a subvolume */
9730 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9731 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9732 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9733 root_objectid,
9734 new_dentry->d_name.name,
9735 new_dentry->d_name.len);
9736 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009737 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9738 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739 new_dentry->d_name.name,
9740 new_dentry->d_name.len);
9741 if (!ret)
9742 ret = btrfs_update_inode(trans, dest, new_inode);
9743 }
9744 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009745 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 goto out_fail;
9747 }
9748
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009749 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 new_dentry->d_name.name,
9751 new_dentry->d_name.len, 0, old_idx);
9752 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009753 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754 goto out_fail;
9755 }
9756
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009757 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758 old_dentry->d_name.name,
9759 old_dentry->d_name.len, 0, new_idx);
9760 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009761 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009762 goto out_fail;
9763 }
9764
9765 if (old_inode->i_nlink == 1)
9766 BTRFS_I(old_inode)->dir_index = old_idx;
9767 if (new_inode->i_nlink == 1)
9768 BTRFS_I(new_inode)->dir_index = new_idx;
9769
Filipe Manana86e8aa02016-05-05 02:02:27 +01009770 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009771 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009772 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9773 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009775 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009777 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009778 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009779 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9780 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009782 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 }
9784out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009785 /*
9786 * If we have pinned a log and an error happened, we unpin tasks
9787 * trying to sync the log and force them to fallback to a transaction
9788 * commit if the log currently contains any of the inodes involved in
9789 * this rename operation (to ensure we do not persist a log with an
9790 * inconsistent state for any of these inodes or leading to any
9791 * inconsistencies when replayed). If the transaction was aborted, the
9792 * abortion reason is propagated to userspace when attempting to commit
9793 * the transaction. If the log does not contain any of these inodes, we
9794 * allow the tasks to sync it.
9795 */
9796 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009797 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9798 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9799 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009800 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009801 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009802 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009803
9804 if (root_log_pinned) {
9805 btrfs_end_log_trans(root);
9806 root_log_pinned = false;
9807 }
9808 if (dest_log_pinned) {
9809 btrfs_end_log_trans(dest);
9810 dest_log_pinned = false;
9811 }
9812 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009813 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814out_notrans:
9815 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009816 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009818 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009819
9820 return ret;
9821}
9822
9823static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9824 struct btrfs_root *root,
9825 struct inode *dir,
9826 struct dentry *dentry)
9827{
9828 int ret;
9829 struct inode *inode;
9830 u64 objectid;
9831 u64 index;
9832
9833 ret = btrfs_find_free_ino(root, &objectid);
9834 if (ret)
9835 return ret;
9836
9837 inode = btrfs_new_inode(trans, root, dir,
9838 dentry->d_name.name,
9839 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009840 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009841 objectid,
9842 S_IFCHR | WHITEOUT_MODE,
9843 &index);
9844
9845 if (IS_ERR(inode)) {
9846 ret = PTR_ERR(inode);
9847 return ret;
9848 }
9849
9850 inode->i_op = &btrfs_special_inode_operations;
9851 init_special_inode(inode, inode->i_mode,
9852 WHITEOUT_DEV);
9853
9854 ret = btrfs_init_inode_security(trans, inode, dir,
9855 &dentry->d_name);
9856 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009857 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009858
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009859 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9860 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009861 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009862 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009863
9864 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009865out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009866 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009867 if (ret)
9868 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009869 iput(inode);
9870
Filipe Mananac9901612016-05-05 01:41:57 +01009871 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009872}
9873
Chris Mason39279cc2007-06-12 06:35:45 -04009874static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009875 struct inode *new_dir, struct dentry *new_dentry,
9876 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009878 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009879 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009880 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009881 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9882 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009883 struct inode *new_inode = d_inode(new_dentry);
9884 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009885 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009887 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009888 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009889 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009890
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009891 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009892 return -EPERM;
9893
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009894 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009895 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009896 return -EXDEV;
9897
Li Zefan33345d012011-04-20 10:31:50 +08009898 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009899 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 return -ENOTEMPTY;
9901
Chris Mason39279cc2007-06-12 06:35:45 -04009902 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009903 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009904 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009905
9906
9907 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009908 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009909 new_dentry->d_name.name,
9910 new_dentry->d_name.len);
9911
9912 if (ret) {
9913 if (ret == -EEXIST) {
9914 /* we shouldn't get
9915 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309916 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009917 return ret;
9918 }
9919 } else {
9920 /* maybe -EOVERFLOW */
9921 return ret;
9922 }
9923 }
9924 ret = 0;
9925
Chris Mason5a3f23d2009-03-31 13:27:11 -04009926 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009927 * we're using rename to replace one file with another. Start IO on it
9928 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009929 */
Chris Mason8d875f92014-08-12 10:47:42 -07009930 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009931 filemap_flush(old_inode->i_mapping);
9932
Yan, Zheng76dda932009-09-21 16:00:26 -04009933 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009934 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009935 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009936 /*
9937 * We want to reserve the absolute worst case amount of items. So if
9938 * both inodes are subvols and we need to unlink them then that would
9939 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009940 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009941 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9942 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009943 * If our rename has the whiteout flag, we need more 5 units for the
9944 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9945 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009946 */
Filipe Manana5062af32016-05-05 10:26:26 +01009947 trans_num_items = 11;
9948 if (flags & RENAME_WHITEOUT)
9949 trans_num_items += 5;
9950 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009951 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009952 ret = PTR_ERR(trans);
9953 goto out_notrans;
9954 }
Chris Mason5f39d392007-10-15 16:14:19 -04009955
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009956 if (dest != root)
9957 btrfs_record_root_in_trans(trans, dest);
9958
Nikolay Borisov877574e2017-02-20 13:50:33 +02009959 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009960 if (ret)
9961 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009962
Miao Xie67de1172013-12-26 13:07:06 +08009963 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009964 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009965 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009966 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009967 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009968 btrfs_pin_log_trans(root);
9969 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009970 ret = btrfs_insert_inode_ref(trans, dest,
9971 new_dentry->d_name.name,
9972 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009973 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009974 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009975 if (ret)
9976 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009977 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009978
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009979 inode_inc_iversion(old_dir);
9980 inode_inc_iversion(new_dir);
9981 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009982 old_dir->i_ctime = old_dir->i_mtime =
9983 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009984 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009985
Chris Mason12fcfd22009-03-24 10:24:20 -04009986 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009987 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9988 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009989
Li Zefan33345d012011-04-20 10:31:50 +08009990 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009991 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9992 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9993 old_dentry->d_name.name,
9994 old_dentry->d_name.len);
9995 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009996 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9997 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009998 old_dentry->d_name.name,
9999 old_dentry->d_name.len);
10000 if (!ret)
10001 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010002 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010003 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010004 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010005 goto out_fail;
10006 }
Chris Mason39279cc2007-06-12 06:35:45 -040010007
10008 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010009 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010010 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010011 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010012 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10013 root_objectid = BTRFS_I(new_inode)->location.objectid;
10014 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10015 root_objectid,
10016 new_dentry->d_name.name,
10017 new_dentry->d_name.len);
10018 BUG_ON(new_inode->i_nlink == 0);
10019 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010020 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10021 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010022 new_dentry->d_name.name,
10023 new_dentry->d_name.len);
10024 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010025 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010026 ret = btrfs_orphan_add(trans,
10027 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010028 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010029 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010030 goto out_fail;
10031 }
Chris Mason39279cc2007-06-12 06:35:45 -040010032 }
Josef Bacikaec74772008-07-24 12:12:38 -040010033
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010034 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010035 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010036 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010037 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010038 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010039 goto out_fail;
10040 }
Chris Mason39279cc2007-06-12 06:35:45 -040010041
Miao Xie67de1172013-12-26 13:07:06 +080010042 if (old_inode->i_nlink == 1)
10043 BTRFS_I(old_inode)->dir_index = index;
10044
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010045 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010046 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010047
David Sterbaf85b7372017-01-20 14:54:07 +010010048 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10049 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010050 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010051 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010052 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010053
10054 if (flags & RENAME_WHITEOUT) {
10055 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10056 old_dentry);
10057
10058 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010059 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010060 goto out_fail;
10061 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010062 }
Chris Mason39279cc2007-06-12 06:35:45 -040010063out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010064 /*
10065 * If we have pinned the log and an error happened, we unpin tasks
10066 * trying to sync the log and force them to fallback to a transaction
10067 * commit if the log currently contains any of the inodes involved in
10068 * this rename operation (to ensure we do not persist a log with an
10069 * inconsistent state for any of these inodes or leading to any
10070 * inconsistencies when replayed). If the transaction was aborted, the
10071 * abortion reason is propagated to userspace when attempting to commit
10072 * the transaction. If the log does not contain any of these inodes, we
10073 * allow the tasks to sync it.
10074 */
10075 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010076 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10077 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10078 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010079 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010080 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010081 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010082
10083 btrfs_end_log_trans(root);
10084 log_pinned = false;
10085 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010086 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010087out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010088 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010089 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010090
Chris Mason39279cc2007-06-12 06:35:45 -040010091 return ret;
10092}
10093
Miklos Szeredi80ace852014-07-23 15:15:32 +020010094static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10095 struct inode *new_dir, struct dentry *new_dentry,
10096 unsigned int flags)
10097{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010098 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010099 return -EINVAL;
10100
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010101 if (flags & RENAME_EXCHANGE)
10102 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10103 new_dentry);
10104
10105 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010106}
10107
Miao Xie8ccf6f192012-10-25 09:28:04 +000010108static void btrfs_run_delalloc_work(struct btrfs_work *work)
10109{
10110 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010111 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010112
10113 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10114 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010115 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010116 filemap_flush(inode->i_mapping);
10117 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10118 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010119 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010120
10121 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010122 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010123 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010124 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010125 complete(&delalloc_work->completion);
10126}
10127
10128struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010129 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010130{
10131 struct btrfs_delalloc_work *work;
10132
David Sterba100d5702015-12-08 14:39:32 +010010133 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010134 if (!work)
10135 return NULL;
10136
10137 init_completion(&work->completion);
10138 INIT_LIST_HEAD(&work->list);
10139 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010140 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010141 WARN_ON_ONCE(!inode);
10142 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10143 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010144
10145 return work;
10146}
10147
10148void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10149{
10150 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010151 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152}
10153
Chris Masond352ac62008-09-29 15:18:18 -040010154/*
10155 * some fairly slow code that needs optimization. This walks the list
10156 * of all the inodes with pending delalloc and forces them to disk.
10157 */
Miao Xie6c255e62014-03-06 13:55:01 +080010158static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10159 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010160{
Chris Masonea8c2812008-08-04 23:17:27 -040010161 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010162 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010163 struct btrfs_delalloc_work *work, *next;
10164 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010165 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010166 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010167
Miao Xie8ccf6f192012-10-25 09:28:04 +000010168 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010169 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010170
Miao Xie573bfb72014-03-06 13:55:03 +080010171 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172 spin_lock(&root->delalloc_lock);
10173 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010174 while (!list_empty(&splice)) {
10175 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010176 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010177
Miao Xieeb73c1b2013-05-15 07:48:22 +000010178 list_move_tail(&binode->delalloc_inodes,
10179 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010180 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010181 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010182 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010183 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010184 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010185 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010186
David Sterba651d4942015-11-27 19:24:16 +010010187 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010188 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010189 if (delay_iput)
10190 btrfs_add_delayed_iput(inode);
10191 else
10192 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010193 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010194 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010195 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010196 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010197 btrfs_queue_work(root->fs_info->flush_workers,
10198 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010199 ret++;
10200 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010201 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010202 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010204 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010206
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010207out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010208 list_for_each_entry_safe(work, next, &works, list) {
10209 list_del_init(&work->list);
10210 btrfs_wait_and_free_delalloc_work(work);
10211 }
10212
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 if (!list_empty_careful(&splice)) {
10214 spin_lock(&root->delalloc_lock);
10215 list_splice_tail(&splice, &root->delalloc_inodes);
10216 spin_unlock(&root->delalloc_lock);
10217 }
Miao Xie573bfb72014-03-06 13:55:03 +080010218 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010219 return ret;
10220}
10221
10222int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10223{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010224 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010225 int ret;
10226
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010227 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010228 return -EROFS;
10229
Miao Xie6c255e62014-03-06 13:55:01 +080010230 ret = __start_delalloc_inodes(root, delay_iput, -1);
10231 if (ret > 0)
10232 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010233 /*
10234 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010235 * we have to make sure the IO is actually started and that
10236 * ordered extents get created before we return
10237 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010238 atomic_inc(&fs_info->async_submit_draining);
10239 while (atomic_read(&fs_info->nr_async_submits) ||
10240 atomic_read(&fs_info->async_delalloc_pages)) {
10241 wait_event(fs_info->async_submit_wait,
10242 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10243 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010244 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010245 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010246 return ret;
10247}
10248
Miao Xie6c255e62014-03-06 13:55:01 +080010249int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10250 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010251{
10252 struct btrfs_root *root;
10253 struct list_head splice;
10254 int ret;
10255
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010256 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010257 return -EROFS;
10258
10259 INIT_LIST_HEAD(&splice);
10260
Miao Xie573bfb72014-03-06 13:55:03 +080010261 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262 spin_lock(&fs_info->delalloc_root_lock);
10263 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010264 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010265 root = list_first_entry(&splice, struct btrfs_root,
10266 delalloc_root);
10267 root = btrfs_grab_fs_root(root);
10268 BUG_ON(!root);
10269 list_move_tail(&root->delalloc_root,
10270 &fs_info->delalloc_roots);
10271 spin_unlock(&fs_info->delalloc_root_lock);
10272
Miao Xie6c255e62014-03-06 13:55:01 +080010273 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010274 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010275 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 goto out;
10277
Miao Xie6c255e62014-03-06 13:55:01 +080010278 if (nr != -1) {
10279 nr -= ret;
10280 WARN_ON(nr < 0);
10281 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010282 spin_lock(&fs_info->delalloc_root_lock);
10283 }
10284 spin_unlock(&fs_info->delalloc_root_lock);
10285
Miao Xie6c255e62014-03-06 13:55:01 +080010286 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010287 atomic_inc(&fs_info->async_submit_draining);
10288 while (atomic_read(&fs_info->nr_async_submits) ||
10289 atomic_read(&fs_info->async_delalloc_pages)) {
10290 wait_event(fs_info->async_submit_wait,
10291 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10292 atomic_read(&fs_info->async_delalloc_pages) == 0));
10293 }
10294 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010295out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010296 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010297 spin_lock(&fs_info->delalloc_root_lock);
10298 list_splice_tail(&splice, &fs_info->delalloc_roots);
10299 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010300 }
Miao Xie573bfb72014-03-06 13:55:03 +080010301 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010302 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010303}
10304
Chris Mason39279cc2007-06-12 06:35:45 -040010305static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10306 const char *symname)
10307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010308 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010309 struct btrfs_trans_handle *trans;
10310 struct btrfs_root *root = BTRFS_I(dir)->root;
10311 struct btrfs_path *path;
10312 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010313 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010314 int err;
10315 int drop_inode = 0;
10316 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010317 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010318 int name_len;
10319 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010320 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010321 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010322 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010323
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010324 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010325 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010326 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010327
Josef Bacik9ed74f22009-09-11 16:12:44 -040010328 /*
10329 * 2 items for inode item and ref
10330 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010331 * 1 item for updating parent inode item
10332 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010333 * 1 item for xattr if selinux is on
10334 */
Filipe Manana9269d122015-12-31 18:16:29 +000010335 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010336 if (IS_ERR(trans))
10337 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010338
Li Zefan581bb052011-04-20 10:06:11 +080010339 err = btrfs_find_free_ino(root, &objectid);
10340 if (err)
10341 goto out_unlock;
10342
Josef Bacikaec74772008-07-24 12:12:38 -040010343 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010344 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10345 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010346 if (IS_ERR(inode)) {
10347 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010348 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010349 }
Chris Mason39279cc2007-06-12 06:35:45 -040010350
Casey Schauflerad19db72011-12-15 10:09:07 -050010351 /*
10352 * If the active LSM wants to access the inode during
10353 * d_instantiate it needs these. Smack checks to see
10354 * if the filesystem supports xattrs by looking at the
10355 * ops vector.
10356 */
10357 inode->i_fop = &btrfs_file_operations;
10358 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010359 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010360 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10361
10362 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10363 if (err)
10364 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010365
Chris Mason39279cc2007-06-12 06:35:45 -040010366 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010367 if (!path) {
10368 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010369 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010370 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010371 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010372 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010373 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010374 datasize = btrfs_file_extent_calc_inline_size(name_len);
10375 err = btrfs_insert_empty_item(trans, root, path, &key,
10376 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010377 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010378 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010379 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010380 }
Chris Mason5f39d392007-10-15 16:14:19 -040010381 leaf = path->nodes[0];
10382 ei = btrfs_item_ptr(leaf, path->slots[0],
10383 struct btrfs_file_extent_item);
10384 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10385 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010386 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010387 btrfs_set_file_extent_encryption(leaf, ei, 0);
10388 btrfs_set_file_extent_compression(leaf, ei, 0);
10389 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10390 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10391
Chris Mason39279cc2007-06-12 06:35:45 -040010392 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010393 write_extent_buffer(leaf, symname, ptr, name_len);
10394 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010395 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010396
Chris Mason39279cc2007-06-12 06:35:45 -040010397 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010398 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010399 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010400 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010401 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010402 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010403 /*
10404 * Last step, add directory indexes for our symlink inode. This is the
10405 * last step to avoid extra cleanup of these indexes if an error happens
10406 * elsewhere above.
10407 */
10408 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010409 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10410 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010411 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010412 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010413 goto out_unlock_inode;
10414 }
10415
10416 unlock_new_inode(inode);
10417 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010418
10419out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010420 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010421 if (drop_inode) {
10422 inode_dec_link_count(inode);
10423 iput(inode);
10424 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010425 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010426 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010427
10428out_unlock_inode:
10429 drop_inode = 1;
10430 unlock_new_inode(inode);
10431 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010432}
Chris Mason16432982008-04-10 10:23:21 -040010433
Josef Bacik0af3d002010-06-21 14:48:16 -040010434static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10435 u64 start, u64 num_bytes, u64 min_size,
10436 loff_t actual_len, u64 *alloc_hint,
10437 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010439 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010440 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10441 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010442 struct btrfs_root *root = BTRFS_I(inode)->root;
10443 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010444 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010445 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010446 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010447 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010448 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010449 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010450 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010451
Josef Bacik0af3d002010-06-21 14:48:16 -040010452 if (trans)
10453 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010454 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010455 if (own_trans) {
10456 trans = btrfs_start_transaction(root, 3);
10457 if (IS_ERR(trans)) {
10458 ret = PTR_ERR(trans);
10459 break;
10460 }
Yan Zhengd899e052008-10-30 14:25:28 -040010461 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010462
Byongho Leeee221842015-12-15 01:42:10 +090010463 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010464 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010465 /*
10466 * If we are severely fragmented we could end up with really
10467 * small allocations, so if the allocator is returning small
10468 * chunks lets make its job easier by only searching for those
10469 * sized chunks.
10470 */
10471 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010472 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10473 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010474 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010475 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010476 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010477 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010478 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010479 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010480
Josef Bacik0b670dc2015-09-23 17:11:16 -040010481 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010482 ret = insert_reserved_file_extent(trans, inode,
10483 cur_offset, ins.objectid,
10484 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010485 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010486 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010487 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010488 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010489 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010490 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010491 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010492 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010493 break;
10494 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010495
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010496 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010497 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010498
Josef Bacik5dc562c2012-08-17 13:14:17 -040010499 em = alloc_extent_map();
10500 if (!em) {
10501 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10502 &BTRFS_I(inode)->runtime_flags);
10503 goto next;
10504 }
10505
10506 em->start = cur_offset;
10507 em->orig_start = cur_offset;
10508 em->len = ins.offset;
10509 em->block_start = ins.objectid;
10510 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010511 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010512 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010513 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010514 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10515 em->generation = trans->transid;
10516
10517 while (1) {
10518 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010519 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010520 write_unlock(&em_tree->lock);
10521 if (ret != -EEXIST)
10522 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010523 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010524 cur_offset + ins.offset - 1,
10525 0);
10526 }
10527 free_extent_map(em);
10528next:
Yan Zhengd899e052008-10-30 14:25:28 -040010529 num_bytes -= ins.offset;
10530 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010531 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010532
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010533 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010534 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010535 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010536 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010537 (actual_len > inode->i_size) &&
10538 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010539 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010540 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010541 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010542 i_size = cur_offset;
10543 i_size_write(inode, i_size);
10544 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010545 }
10546
Yan Zhengd899e052008-10-30 14:25:28 -040010547 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010548
10549 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010550 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010551 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010552 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010553 break;
10554 }
Yan Zhengd899e052008-10-30 14:25:28 -040010555
Josef Bacik0af3d002010-06-21 14:48:16 -040010556 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010557 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010558 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010559 if (cur_offset < end)
10560 btrfs_free_reserved_data_space(inode, cur_offset,
10561 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010562 return ret;
10563}
10564
Josef Bacik0af3d002010-06-21 14:48:16 -040010565int btrfs_prealloc_file_range(struct inode *inode, int mode,
10566 u64 start, u64 num_bytes, u64 min_size,
10567 loff_t actual_len, u64 *alloc_hint)
10568{
10569 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10570 min_size, actual_len, alloc_hint,
10571 NULL);
10572}
10573
10574int btrfs_prealloc_file_range_trans(struct inode *inode,
10575 struct btrfs_trans_handle *trans, int mode,
10576 u64 start, u64 num_bytes, u64 min_size,
10577 loff_t actual_len, u64 *alloc_hint)
10578{
10579 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10580 min_size, actual_len, alloc_hint, trans);
10581}
10582
Chris Masone6dcd2d2008-07-17 12:53:50 -040010583static int btrfs_set_page_dirty(struct page *page)
10584{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010585 return __set_page_dirty_nobuffers(page);
10586}
10587
Al Viro10556cb2011-06-20 19:28:19 -040010588static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010589{
Li Zefanb83cc962010-12-20 16:04:08 +080010590 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010591 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010592
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010593 if (mask & MAY_WRITE &&
10594 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10595 if (btrfs_root_readonly(root))
10596 return -EROFS;
10597 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10598 return -EACCES;
10599 }
Al Viro2830ba72011-06-20 19:16:29 -040010600 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010601}
Chris Mason39279cc2007-06-12 06:35:45 -040010602
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010603static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10604{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010605 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010606 struct btrfs_trans_handle *trans;
10607 struct btrfs_root *root = BTRFS_I(dir)->root;
10608 struct inode *inode = NULL;
10609 u64 objectid;
10610 u64 index;
10611 int ret = 0;
10612
10613 /*
10614 * 5 units required for adding orphan entry
10615 */
10616 trans = btrfs_start_transaction(root, 5);
10617 if (IS_ERR(trans))
10618 return PTR_ERR(trans);
10619
10620 ret = btrfs_find_free_ino(root, &objectid);
10621 if (ret)
10622 goto out;
10623
10624 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010625 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010626 if (IS_ERR(inode)) {
10627 ret = PTR_ERR(inode);
10628 inode = NULL;
10629 goto out;
10630 }
10631
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010632 inode->i_fop = &btrfs_file_operations;
10633 inode->i_op = &btrfs_file_inode_operations;
10634
10635 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010636 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10637
Chris Masonb0d5d102014-09-08 13:08:51 -070010638 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10639 if (ret)
10640 goto out_inode;
10641
10642 ret = btrfs_update_inode(trans, root, inode);
10643 if (ret)
10644 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010645 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010646 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010647 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010648
Filipe Manana5762b5c2014-08-01 00:10:32 +010010649 /*
10650 * We set number of links to 0 in btrfs_new_inode(), and here we set
10651 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10652 * through:
10653 *
10654 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10655 */
10656 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010657 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010658 d_tmpfile(dentry, inode);
10659 mark_inode_dirty(inode);
10660
10661out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010662 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010663 if (ret)
10664 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010665 btrfs_balance_delayed_items(fs_info);
10666 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010667 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010668
10669out_inode:
10670 unlock_new_inode(inode);
10671 goto out;
10672
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010673}
10674
David Sterba20a7db82017-02-17 16:24:29 +010010675__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010676static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010677{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010678 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010679}
10680
Josef Bacikc6100a42017-05-05 11:57:13 -040010681static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10682{
10683 struct inode *inode = private_data;
10684 return btrfs_sb(inode->i_sb);
10685}
10686
10687static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10688 u64 start, u64 end)
10689{
10690 struct inode *inode = private_data;
10691 u64 isize;
10692
10693 isize = i_size_read(inode);
10694 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10695 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10696 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10697 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10698 }
10699}
10700
10701void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10702{
10703 struct inode *inode = private_data;
10704 unsigned long index = start >> PAGE_SHIFT;
10705 unsigned long end_index = end >> PAGE_SHIFT;
10706 struct page *page;
10707
10708 while (index <= end_index) {
10709 page = find_get_page(inode->i_mapping, index);
10710 ASSERT(page); /* Pages should be in the extent_io_tree */
10711 set_page_writeback(page);
10712 put_page(page);
10713 index++;
10714 }
10715}
10716
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010717static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010718 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010719 .lookup = btrfs_lookup,
10720 .create = btrfs_create,
10721 .unlink = btrfs_unlink,
10722 .link = btrfs_link,
10723 .mkdir = btrfs_mkdir,
10724 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010725 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010726 .symlink = btrfs_symlink,
10727 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010728 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010729 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010730 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010731 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010732 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010733 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010734 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010735};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010736static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010737 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010738 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010739 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010740};
Yan, Zheng76dda932009-09-21 16:00:26 -040010741
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010742static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010743 .llseek = generic_file_llseek,
10744 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010745 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010746 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010747#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010748 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010749#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010750 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010751 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010752};
10753
David Sterba20e55062015-11-19 11:42:28 +010010754static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010755 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010756 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010757 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010758 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010759 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010760 .tree_fs_info = iotree_fs_info,
10761 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010762
10763 /* optional callbacks */
10764 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010765 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010766 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010767 .set_bit_hook = btrfs_set_bit_hook,
10768 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010769 .merge_extent_hook = btrfs_merge_extent_hook,
10770 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010771 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010772};
10773
Chris Mason35054392009-01-21 13:11:13 -050010774/*
10775 * btrfs doesn't support the bmap operation because swapfiles
10776 * use bmap to make a mapping of extents in the file. They assume
10777 * these extents won't change over the life of the file and they
10778 * use the bmap result to do IO directly to the drive.
10779 *
10780 * the btrfs bmap call would return logical addresses that aren't
10781 * suitable for IO and they also will change frequently as COW
10782 * operations happen. So, swapfile + btrfs == corruption.
10783 *
10784 * For now we're avoiding this by dropping bmap.
10785 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010786static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010787 .readpage = btrfs_readpage,
10788 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010789 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010790 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010791 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010792 .invalidatepage = btrfs_invalidatepage,
10793 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010794 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010795 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010796};
10797
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010798static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010799 .readpage = btrfs_readpage,
10800 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010801 .invalidatepage = btrfs_invalidatepage,
10802 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010803};
10804
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010805static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010806 .getattr = btrfs_getattr,
10807 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010808 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010809 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010810 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010811 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010812 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010813 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010814};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010815static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010816 .getattr = btrfs_getattr,
10817 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010818 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010819 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010820 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010821 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010822 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010823};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010824static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010825 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010826 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010827 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010828 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010829 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010830 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010831};
Yan, Zheng76dda932009-09-21 16:00:26 -040010832
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010833const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010834 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010835 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010836};