blob: e837713d0d050f9187338cd4430386061d1725ac [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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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;
Chris Mason247e7432008-07-17 12:53:51 -04002040 struct page *page;
2041 struct inode *inode;
2042 u64 page_start;
2043 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002044 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002045
2046 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2047 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002048again:
Chris Mason247e7432008-07-17 12:53:51 -04002049 lock_page(page);
2050 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2051 ClearPageChecked(page);
2052 goto out_page;
2053 }
2054
2055 inode = page->mapping->host;
2056 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002057 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002058
David Sterbaff13db42015-12-03 14:30:40 +01002059 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002060 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002061
2062 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002063 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002064 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002065
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002066 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002067 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002068 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002069 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2070 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002071 unlock_page(page);
2072 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002073 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002074 goto again;
2075 }
Chris Mason247e7432008-07-17 12:53:51 -04002076
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002077 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002078 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002079 if (ret) {
2080 mapping_set_error(page->mapping, ret);
2081 end_extent_writepage(page, ret, page_start, page_end);
2082 ClearPageChecked(page);
2083 goto out;
2084 }
2085
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002086 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2087 0);
Chris Mason247e7432008-07-17 12:53:51 -04002088 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002089 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002090out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002091 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2092 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002093out_page:
2094 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002095 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002096 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002097}
2098
2099/*
2100 * There are a few paths in the higher layers of the kernel that directly
2101 * set the page dirty bit without asking the filesystem if it is a
2102 * good idea. This causes problems because we want to make sure COW
2103 * properly happens and the data=ordered rules are followed.
2104 *
Chris Masonc8b97812008-10-29 14:49:59 -04002105 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002106 * hasn't been properly setup for IO. We kick off an async process
2107 * to fix it up. The async helper will wait for ordered extents, set
2108 * the delalloc bit and make it safe to write the page.
2109 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002110static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002111{
2112 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002113 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002114 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002115
Chris Mason8b62b722009-09-02 16:53:46 -04002116 /* this page is properly in the ordered list */
2117 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002118 return 0;
2119
2120 if (PageChecked(page))
2121 return -EAGAIN;
2122
2123 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2124 if (!fixup)
2125 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002126
Chris Mason247e7432008-07-17 12:53:51 -04002127 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002128 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002129 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2130 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002131 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002132 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002133 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002134}
2135
Yan Zhengd899e052008-10-30 14:25:28 -04002136static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2137 struct inode *inode, u64 file_pos,
2138 u64 disk_bytenr, u64 disk_num_bytes,
2139 u64 num_bytes, u64 ram_bytes,
2140 u8 compression, u8 encryption,
2141 u16 other_encoding, int extent_type)
2142{
2143 struct btrfs_root *root = BTRFS_I(inode)->root;
2144 struct btrfs_file_extent_item *fi;
2145 struct btrfs_path *path;
2146 struct extent_buffer *leaf;
2147 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002148 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002149 int ret;
2150
2151 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002152 if (!path)
2153 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002154
Chris Masona1ed8352009-09-11 12:27:37 -04002155 /*
2156 * we may be replacing one extent in the tree with another.
2157 * The new extent is pinned in the extent map, and we don't want
2158 * to drop it from the cache until it is completely in the btree.
2159 *
2160 * So, tell btrfs_drop_extents to leave this extent in the cache.
2161 * the caller is expected to unpin it and allow it to be merged
2162 * with the others.
2163 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002164 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2165 file_pos + num_bytes, NULL, 0,
2166 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002167 if (ret)
2168 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002169
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002170 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002171 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002172 ins.offset = file_pos;
2173 ins.type = BTRFS_EXTENT_DATA_KEY;
2174
2175 path->leave_spinning = 1;
2176 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2177 sizeof(*fi));
2178 if (ret)
2179 goto out;
2180 }
Yan Zhengd899e052008-10-30 14:25:28 -04002181 leaf = path->nodes[0];
2182 fi = btrfs_item_ptr(leaf, path->slots[0],
2183 struct btrfs_file_extent_item);
2184 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2185 btrfs_set_file_extent_type(leaf, fi, extent_type);
2186 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2187 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2188 btrfs_set_file_extent_offset(leaf, fi, 0);
2189 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2190 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2191 btrfs_set_file_extent_compression(leaf, fi, compression);
2192 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2193 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002194
Yan Zhengd899e052008-10-30 14:25:28 -04002195 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002196 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002197
2198 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002199
2200 ins.objectid = disk_bytenr;
2201 ins.offset = disk_num_bytes;
2202 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002203 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002204 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002205 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002206 * Release the reserved range from inode dirty range map, as it is
2207 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002208 */
2209 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002210out:
Yan Zhengd899e052008-10-30 14:25:28 -04002211 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002212
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002213 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002214}
2215
Liu Bo38c227d2013-01-29 03:18:40 +00002216/* snapshot-aware defrag */
2217struct sa_defrag_extent_backref {
2218 struct rb_node node;
2219 struct old_sa_defrag_extent *old;
2220 u64 root_id;
2221 u64 inum;
2222 u64 file_pos;
2223 u64 extent_offset;
2224 u64 num_bytes;
2225 u64 generation;
2226};
2227
2228struct old_sa_defrag_extent {
2229 struct list_head list;
2230 struct new_sa_defrag_extent *new;
2231
2232 u64 extent_offset;
2233 u64 bytenr;
2234 u64 offset;
2235 u64 len;
2236 int count;
2237};
2238
2239struct new_sa_defrag_extent {
2240 struct rb_root root;
2241 struct list_head head;
2242 struct btrfs_path *path;
2243 struct inode *inode;
2244 u64 file_pos;
2245 u64 len;
2246 u64 bytenr;
2247 u64 disk_len;
2248 u8 compress_type;
2249};
2250
2251static int backref_comp(struct sa_defrag_extent_backref *b1,
2252 struct sa_defrag_extent_backref *b2)
2253{
2254 if (b1->root_id < b2->root_id)
2255 return -1;
2256 else if (b1->root_id > b2->root_id)
2257 return 1;
2258
2259 if (b1->inum < b2->inum)
2260 return -1;
2261 else if (b1->inum > b2->inum)
2262 return 1;
2263
2264 if (b1->file_pos < b2->file_pos)
2265 return -1;
2266 else if (b1->file_pos > b2->file_pos)
2267 return 1;
2268
2269 /*
2270 * [------------------------------] ===> (a range of space)
2271 * |<--->| |<---->| =============> (fs/file tree A)
2272 * |<---------------------------->| ===> (fs/file tree B)
2273 *
2274 * A range of space can refer to two file extents in one tree while
2275 * refer to only one file extent in another tree.
2276 *
2277 * So we may process a disk offset more than one time(two extents in A)
2278 * and locate at the same extent(one extent in B), then insert two same
2279 * backrefs(both refer to the extent in B).
2280 */
2281 return 0;
2282}
2283
2284static void backref_insert(struct rb_root *root,
2285 struct sa_defrag_extent_backref *backref)
2286{
2287 struct rb_node **p = &root->rb_node;
2288 struct rb_node *parent = NULL;
2289 struct sa_defrag_extent_backref *entry;
2290 int ret;
2291
2292 while (*p) {
2293 parent = *p;
2294 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2295
2296 ret = backref_comp(backref, entry);
2297 if (ret < 0)
2298 p = &(*p)->rb_left;
2299 else
2300 p = &(*p)->rb_right;
2301 }
2302
2303 rb_link_node(&backref->node, parent, p);
2304 rb_insert_color(&backref->node, root);
2305}
2306
2307/*
2308 * Note the backref might has changed, and in this case we just return 0.
2309 */
2310static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2311 void *ctx)
2312{
2313 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002314 struct old_sa_defrag_extent *old = ctx;
2315 struct new_sa_defrag_extent *new = old->new;
2316 struct btrfs_path *path = new->path;
2317 struct btrfs_key key;
2318 struct btrfs_root *root;
2319 struct sa_defrag_extent_backref *backref;
2320 struct extent_buffer *leaf;
2321 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002323 int slot;
2324 int ret;
2325 u64 extent_offset;
2326 u64 num_bytes;
2327
2328 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002329 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002330 return 0;
2331
2332 key.objectid = root_id;
2333 key.type = BTRFS_ROOT_ITEM_KEY;
2334 key.offset = (u64)-1;
2335
Liu Bo38c227d2013-01-29 03:18:40 +00002336 root = btrfs_read_fs_root_no_name(fs_info, &key);
2337 if (IS_ERR(root)) {
2338 if (PTR_ERR(root) == -ENOENT)
2339 return 0;
2340 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002341 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002342 inum, offset, root_id);
2343 return PTR_ERR(root);
2344 }
2345
2346 key.objectid = inum;
2347 key.type = BTRFS_EXTENT_DATA_KEY;
2348 if (offset > (u64)-1 << 32)
2349 key.offset = 0;
2350 else
2351 key.offset = offset;
2352
2353 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302354 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002355 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002356 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002357
2358 while (1) {
2359 cond_resched();
2360
2361 leaf = path->nodes[0];
2362 slot = path->slots[0];
2363
2364 if (slot >= btrfs_header_nritems(leaf)) {
2365 ret = btrfs_next_leaf(root, path);
2366 if (ret < 0) {
2367 goto out;
2368 } else if (ret > 0) {
2369 ret = 0;
2370 goto out;
2371 }
2372 continue;
2373 }
2374
2375 path->slots[0]++;
2376
2377 btrfs_item_key_to_cpu(leaf, &key, slot);
2378
2379 if (key.objectid > inum)
2380 goto out;
2381
2382 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2383 continue;
2384
2385 extent = btrfs_item_ptr(leaf, slot,
2386 struct btrfs_file_extent_item);
2387
2388 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2389 continue;
2390
Liu Boe68afa42013-07-01 22:13:26 +08002391 /*
2392 * 'offset' refers to the exact key.offset,
2393 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2394 * (key.offset - extent_offset).
2395 */
2396 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002397 continue;
2398
Liu Boe68afa42013-07-01 22:13:26 +08002399 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002400 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002401
Liu Bo38c227d2013-01-29 03:18:40 +00002402 if (extent_offset >= old->extent_offset + old->offset +
2403 old->len || extent_offset + num_bytes <=
2404 old->extent_offset + old->offset)
2405 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002406 break;
2407 }
2408
2409 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2410 if (!backref) {
2411 ret = -ENOENT;
2412 goto out;
2413 }
2414
2415 backref->root_id = root_id;
2416 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002417 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002418 backref->num_bytes = num_bytes;
2419 backref->extent_offset = extent_offset;
2420 backref->generation = btrfs_file_extent_generation(leaf, extent);
2421 backref->old = old;
2422 backref_insert(&new->root, backref);
2423 old->count++;
2424out:
2425 btrfs_release_path(path);
2426 WARN_ON(ret);
2427 return ret;
2428}
2429
2430static noinline bool record_extent_backrefs(struct btrfs_path *path,
2431 struct new_sa_defrag_extent *new)
2432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002433 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002434 struct old_sa_defrag_extent *old, *tmp;
2435 int ret;
2436
2437 new->path = path;
2438
2439 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002440 ret = iterate_inodes_from_logical(old->bytenr +
2441 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002442 path, record_one_backref,
2443 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002444 if (ret < 0 && ret != -ENOENT)
2445 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002446
2447 /* no backref to be processed for this extent */
2448 if (!old->count) {
2449 list_del(&old->list);
2450 kfree(old);
2451 }
2452 }
2453
2454 if (list_empty(&new->head))
2455 return false;
2456
2457 return true;
2458}
2459
2460static int relink_is_mergable(struct extent_buffer *leaf,
2461 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002462 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002463{
Liu Bo116e0022013-08-02 16:30:40 +08002464 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002465 return 0;
2466
2467 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2468 return 0;
2469
Liu Bo116e0022013-08-02 16:30:40 +08002470 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2471 return 0;
2472
2473 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002474 btrfs_file_extent_other_encoding(leaf, fi))
2475 return 0;
2476
2477 return 1;
2478}
2479
2480/*
2481 * Note the backref might has changed, and in this case we just return 0.
2482 */
2483static noinline int relink_extent_backref(struct btrfs_path *path,
2484 struct sa_defrag_extent_backref *prev,
2485 struct sa_defrag_extent_backref *backref)
2486{
2487 struct btrfs_file_extent_item *extent;
2488 struct btrfs_file_extent_item *item;
2489 struct btrfs_ordered_extent *ordered;
2490 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002491 struct btrfs_root *root;
2492 struct btrfs_key key;
2493 struct extent_buffer *leaf;
2494 struct old_sa_defrag_extent *old = backref->old;
2495 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002496 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002497 struct inode *inode;
2498 struct extent_state *cached = NULL;
2499 int ret = 0;
2500 u64 start;
2501 u64 len;
2502 u64 lock_start;
2503 u64 lock_end;
2504 bool merge = false;
2505 int index;
2506
2507 if (prev && prev->root_id == backref->root_id &&
2508 prev->inum == backref->inum &&
2509 prev->file_pos + prev->num_bytes == backref->file_pos)
2510 merge = true;
2511
2512 /* step 1: get root */
2513 key.objectid = backref->root_id;
2514 key.type = BTRFS_ROOT_ITEM_KEY;
2515 key.offset = (u64)-1;
2516
Liu Bo38c227d2013-01-29 03:18:40 +00002517 index = srcu_read_lock(&fs_info->subvol_srcu);
2518
2519 root = btrfs_read_fs_root_no_name(fs_info, &key);
2520 if (IS_ERR(root)) {
2521 srcu_read_unlock(&fs_info->subvol_srcu, index);
2522 if (PTR_ERR(root) == -ENOENT)
2523 return 0;
2524 return PTR_ERR(root);
2525 }
Liu Bo38c227d2013-01-29 03:18:40 +00002526
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002527 if (btrfs_root_readonly(root)) {
2528 srcu_read_unlock(&fs_info->subvol_srcu, index);
2529 return 0;
2530 }
2531
Liu Bo38c227d2013-01-29 03:18:40 +00002532 /* step 2: get inode */
2533 key.objectid = backref->inum;
2534 key.type = BTRFS_INODE_ITEM_KEY;
2535 key.offset = 0;
2536
2537 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2538 if (IS_ERR(inode)) {
2539 srcu_read_unlock(&fs_info->subvol_srcu, index);
2540 return 0;
2541 }
2542
2543 srcu_read_unlock(&fs_info->subvol_srcu, index);
2544
2545 /* step 3: relink backref */
2546 lock_start = backref->file_pos;
2547 lock_end = backref->file_pos + backref->num_bytes - 1;
2548 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002549 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002550
2551 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2552 if (ordered) {
2553 btrfs_put_ordered_extent(ordered);
2554 goto out_unlock;
2555 }
2556
2557 trans = btrfs_join_transaction(root);
2558 if (IS_ERR(trans)) {
2559 ret = PTR_ERR(trans);
2560 goto out_unlock;
2561 }
2562
2563 key.objectid = backref->inum;
2564 key.type = BTRFS_EXTENT_DATA_KEY;
2565 key.offset = backref->file_pos;
2566
2567 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2568 if (ret < 0) {
2569 goto out_free_path;
2570 } else if (ret > 0) {
2571 ret = 0;
2572 goto out_free_path;
2573 }
2574
2575 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2576 struct btrfs_file_extent_item);
2577
2578 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2579 backref->generation)
2580 goto out_free_path;
2581
2582 btrfs_release_path(path);
2583
2584 start = backref->file_pos;
2585 if (backref->extent_offset < old->extent_offset + old->offset)
2586 start += old->extent_offset + old->offset -
2587 backref->extent_offset;
2588
2589 len = min(backref->extent_offset + backref->num_bytes,
2590 old->extent_offset + old->offset + old->len);
2591 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2592
2593 ret = btrfs_drop_extents(trans, root, inode, start,
2594 start + len, 1);
2595 if (ret)
2596 goto out_free_path;
2597again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002598 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002599 key.type = BTRFS_EXTENT_DATA_KEY;
2600 key.offset = start;
2601
Liu Boa09a0a72013-03-11 09:20:58 +00002602 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002603 if (merge) {
2604 struct btrfs_file_extent_item *fi;
2605 u64 extent_len;
2606 struct btrfs_key found_key;
2607
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002608 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002609 if (ret < 0)
2610 goto out_free_path;
2611
2612 path->slots[0]--;
2613 leaf = path->nodes[0];
2614 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2615
2616 fi = btrfs_item_ptr(leaf, path->slots[0],
2617 struct btrfs_file_extent_item);
2618 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2619
Liu Bo116e0022013-08-02 16:30:40 +08002620 if (extent_len + found_key.offset == start &&
2621 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002622 btrfs_set_file_extent_num_bytes(leaf, fi,
2623 extent_len + len);
2624 btrfs_mark_buffer_dirty(leaf);
2625 inode_add_bytes(inode, len);
2626
2627 ret = 1;
2628 goto out_free_path;
2629 } else {
2630 merge = false;
2631 btrfs_release_path(path);
2632 goto again;
2633 }
2634 }
2635
2636 ret = btrfs_insert_empty_item(trans, root, path, &key,
2637 sizeof(*extent));
2638 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002639 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002640 goto out_free_path;
2641 }
2642
2643 leaf = path->nodes[0];
2644 item = btrfs_item_ptr(leaf, path->slots[0],
2645 struct btrfs_file_extent_item);
2646 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2647 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2648 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2649 btrfs_set_file_extent_num_bytes(leaf, item, len);
2650 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2651 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2652 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2653 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2654 btrfs_set_file_extent_encryption(leaf, item, 0);
2655 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2656
2657 btrfs_mark_buffer_dirty(leaf);
2658 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002659 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002660
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002661 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002662 new->disk_len, 0,
2663 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002664 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002665 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002666 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002667 goto out_free_path;
2668 }
2669
2670 ret = 1;
2671out_free_path:
2672 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002673 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002674 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002675out_unlock:
2676 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2677 &cached, GFP_NOFS);
2678 iput(inode);
2679 return ret;
2680}
2681
Liu Bo6f519562013-10-29 10:45:05 +08002682static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2683{
2684 struct old_sa_defrag_extent *old, *tmp;
2685
2686 if (!new)
2687 return;
2688
2689 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002690 kfree(old);
2691 }
2692 kfree(new);
2693}
2694
Liu Bo38c227d2013-01-29 03:18:40 +00002695static void relink_file_extents(struct new_sa_defrag_extent *new)
2696{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002697 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002698 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002699 struct sa_defrag_extent_backref *backref;
2700 struct sa_defrag_extent_backref *prev = NULL;
2701 struct inode *inode;
2702 struct btrfs_root *root;
2703 struct rb_node *node;
2704 int ret;
2705
2706 inode = new->inode;
2707 root = BTRFS_I(inode)->root;
2708
2709 path = btrfs_alloc_path();
2710 if (!path)
2711 return;
2712
2713 if (!record_extent_backrefs(path, new)) {
2714 btrfs_free_path(path);
2715 goto out;
2716 }
2717 btrfs_release_path(path);
2718
2719 while (1) {
2720 node = rb_first(&new->root);
2721 if (!node)
2722 break;
2723 rb_erase(node, &new->root);
2724
2725 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2726
2727 ret = relink_extent_backref(path, prev, backref);
2728 WARN_ON(ret < 0);
2729
2730 kfree(prev);
2731
2732 if (ret == 1)
2733 prev = backref;
2734 else
2735 prev = NULL;
2736 cond_resched();
2737 }
2738 kfree(prev);
2739
2740 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002741out:
Liu Bo6f519562013-10-29 10:45:05 +08002742 free_sa_defrag_extent(new);
2743
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002744 atomic_dec(&fs_info->defrag_running);
2745 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002746}
2747
2748static struct new_sa_defrag_extent *
2749record_old_file_extents(struct inode *inode,
2750 struct btrfs_ordered_extent *ordered)
2751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002753 struct btrfs_root *root = BTRFS_I(inode)->root;
2754 struct btrfs_path *path;
2755 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002756 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002757 struct new_sa_defrag_extent *new;
2758 int ret;
2759
2760 new = kmalloc(sizeof(*new), GFP_NOFS);
2761 if (!new)
2762 return NULL;
2763
2764 new->inode = inode;
2765 new->file_pos = ordered->file_offset;
2766 new->len = ordered->len;
2767 new->bytenr = ordered->start;
2768 new->disk_len = ordered->disk_len;
2769 new->compress_type = ordered->compress_type;
2770 new->root = RB_ROOT;
2771 INIT_LIST_HEAD(&new->head);
2772
2773 path = btrfs_alloc_path();
2774 if (!path)
2775 goto out_kfree;
2776
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002777 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002778 key.type = BTRFS_EXTENT_DATA_KEY;
2779 key.offset = new->file_pos;
2780
2781 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2782 if (ret < 0)
2783 goto out_free_path;
2784 if (ret > 0 && path->slots[0] > 0)
2785 path->slots[0]--;
2786
2787 /* find out all the old extents for the file range */
2788 while (1) {
2789 struct btrfs_file_extent_item *extent;
2790 struct extent_buffer *l;
2791 int slot;
2792 u64 num_bytes;
2793 u64 offset;
2794 u64 end;
2795 u64 disk_bytenr;
2796 u64 extent_offset;
2797
2798 l = path->nodes[0];
2799 slot = path->slots[0];
2800
2801 if (slot >= btrfs_header_nritems(l)) {
2802 ret = btrfs_next_leaf(root, path);
2803 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002804 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002805 else if (ret > 0)
2806 break;
2807 continue;
2808 }
2809
2810 btrfs_item_key_to_cpu(l, &key, slot);
2811
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002812 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002813 break;
2814 if (key.type != BTRFS_EXTENT_DATA_KEY)
2815 break;
2816 if (key.offset >= new->file_pos + new->len)
2817 break;
2818
2819 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2820
2821 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2822 if (key.offset + num_bytes < new->file_pos)
2823 goto next;
2824
2825 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2826 if (!disk_bytenr)
2827 goto next;
2828
2829 extent_offset = btrfs_file_extent_offset(l, extent);
2830
2831 old = kmalloc(sizeof(*old), GFP_NOFS);
2832 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002833 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002834
2835 offset = max(new->file_pos, key.offset);
2836 end = min(new->file_pos + new->len, key.offset + num_bytes);
2837
2838 old->bytenr = disk_bytenr;
2839 old->extent_offset = extent_offset;
2840 old->offset = offset - key.offset;
2841 old->len = end - offset;
2842 old->new = new;
2843 old->count = 0;
2844 list_add_tail(&old->list, &new->head);
2845next:
2846 path->slots[0]++;
2847 cond_resched();
2848 }
2849
2850 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002851 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002852
2853 return new;
2854
Liu Bo38c227d2013-01-29 03:18:40 +00002855out_free_path:
2856 btrfs_free_path(path);
2857out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002858 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002859 return NULL;
2860}
2861
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002862static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002863 u64 start, u64 len)
2864{
2865 struct btrfs_block_group_cache *cache;
2866
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002868 ASSERT(cache);
2869
2870 spin_lock(&cache->lock);
2871 cache->delalloc_bytes -= len;
2872 spin_unlock(&cache->lock);
2873
2874 btrfs_put_block_group(cache);
2875}
2876
Chris Masond352ac62008-09-29 15:18:18 -04002877/* as ordered data IO finishes, this gets called so we can finish
2878 * an ordered extent if the range of bytes in the file it covers are
2879 * fully written.
2880 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002881static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002882{
Josef Bacik5fd02042012-05-02 14:00:54 -04002883 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002884 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002886 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002888 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002889 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002890 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002891 int ret = 0;
2892 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002893 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002894 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002895 bool range_locked = false;
2896 bool clear_new_delalloc_bytes = false;
2897
2898 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2899 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2900 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2901 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002902
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002903 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002904
Josef Bacik5fd02042012-05-02 14:00:54 -04002905 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2906 ret = -EIO;
2907 goto out;
2908 }
2909
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002910 btrfs_free_io_failure_record(BTRFS_I(inode),
2911 ordered_extent->file_offset,
2912 ordered_extent->file_offset +
2913 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002914
Josef Bacik77cef2e2013-08-29 13:57:21 -04002915 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2916 truncated = true;
2917 logical_len = ordered_extent->truncated_len;
2918 /* Truncated the entire extent, don't bother adding */
2919 if (!logical_len)
2920 goto out;
2921 }
2922
Yan, Zhengc2167752009-11-12 09:34:21 +00002923 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002925
2926 /*
2927 * For mwrite(mmap + memset to write) case, we still reserve
2928 * space for NOCOW range.
2929 * As NOCOW won't cause a new delayed ref, just free the space
2930 */
2931 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2932 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002933 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2934 if (nolock)
2935 trans = btrfs_join_transaction_nolock(root);
2936 else
2937 trans = btrfs_join_transaction(root);
2938 if (IS_ERR(trans)) {
2939 ret = PTR_ERR(trans);
2940 trans = NULL;
2941 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002942 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002943 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002944 ret = btrfs_update_inode_fallback(trans, root, inode);
2945 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002946 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002947 goto out;
2948 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002949
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002950 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002951 lock_extent_bits(io_tree, ordered_extent->file_offset,
2952 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002953 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002954
Liu Bo38c227d2013-01-29 03:18:40 +00002955 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2956 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002957 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002958 if (ret) {
2959 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002960 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002961 /* the inode is shared */
2962 new = record_old_file_extents(inode, ordered_extent);
2963
2964 clear_extent_bit(io_tree, ordered_extent->file_offset,
2965 ordered_extent->file_offset + ordered_extent->len - 1,
2966 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2967 }
2968
Josef Bacik0cb59c92010-07-02 12:14:14 -04002969 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002970 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002971 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002972 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002973 if (IS_ERR(trans)) {
2974 ret = PTR_ERR(trans);
2975 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002976 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002977 }
Chris Masona79b7d42014-05-22 16:18:52 -07002978
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002979 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002980
Chris Masonc8b97812008-10-29 14:49:59 -04002981 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002982 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002983 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002984 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002985 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002986 ordered_extent->file_offset,
2987 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002988 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002989 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002990 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002991 ret = insert_reserved_file_extent(trans, inode,
2992 ordered_extent->file_offset,
2993 ordered_extent->start,
2994 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002995 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002996 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002997 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002998 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002999 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003000 ordered_extent->start,
3001 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003002 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003003 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3004 ordered_extent->file_offset, ordered_extent->len,
3005 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003006 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003007 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003008 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003009 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003010
David Sterbadf9f6282017-02-10 19:35:37 +01003011 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003012
Josef Bacik6c760c02012-11-09 10:53:21 -05003013 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3014 ret = btrfs_update_inode_fallback(trans, root, inode);
3015 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003016 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003017 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003018 }
3019 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003020out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003021 if (range_locked || clear_new_delalloc_bytes) {
3022 unsigned int clear_bits = 0;
3023
3024 if (range_locked)
3025 clear_bits |= EXTENT_LOCKED;
3026 if (clear_new_delalloc_bytes)
3027 clear_bits |= EXTENT_DELALLOC_NEW;
3028 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3029 ordered_extent->file_offset,
3030 ordered_extent->file_offset +
3031 ordered_extent->len - 1,
3032 clear_bits,
3033 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3034 0, &cached_state, GFP_NOFS);
3035 }
3036
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003038 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3039 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003040 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003041 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003042
Josef Bacik77cef2e2013-08-29 13:57:21 -04003043 if (ret || truncated) {
3044 u64 start, end;
3045
3046 if (truncated)
3047 start = ordered_extent->file_offset + logical_len;
3048 else
3049 start = ordered_extent->file_offset;
3050 end = ordered_extent->file_offset + ordered_extent->len - 1;
3051 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3052
3053 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003054 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003055
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003056 /*
3057 * If the ordered extent had an IOERR or something else went
3058 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003059 * back to the allocator. We only free the extent in the
3060 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003061 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003062 if ((ret || !logical_len) &&
3063 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003064 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003065 btrfs_free_reserved_extent(fs_info,
3066 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003067 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003068 }
3069
3070
Josef Bacik5fd02042012-05-02 14:00:54 -04003071 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003072 * This needs to be done to make sure anybody waiting knows we are done
3073 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003074 */
3075 btrfs_remove_ordered_extent(inode, ordered_extent);
3076
Liu Bo38c227d2013-01-29 03:18:40 +00003077 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003078 if (new) {
3079 if (ret) {
3080 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003081 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003082 } else {
3083 relink_file_extents(new);
3084 }
3085 }
Liu Bo38c227d2013-01-29 03:18:40 +00003086
Chris Masone6dcd2d2008-07-17 12:53:50 -04003087 /* once for us */
3088 btrfs_put_ordered_extent(ordered_extent);
3089 /* once for the tree */
3090 btrfs_put_ordered_extent(ordered_extent);
3091
Josef Bacik5fd02042012-05-02 14:00:54 -04003092 return ret;
3093}
3094
3095static void finish_ordered_fn(struct btrfs_work *work)
3096{
3097 struct btrfs_ordered_extent *ordered_extent;
3098 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3099 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003100}
3101
David Sterbac3988d62017-02-17 15:18:32 +01003102static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003103 struct extent_state *state, int uptodate)
3104{
Josef Bacik5fd02042012-05-02 14:00:54 -04003105 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003107 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003108 struct btrfs_workqueue *wq;
3109 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003110
liubo1abe9b82011-03-24 11:18:59 +00003111 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3112
Chris Mason8b62b722009-09-02 16:53:46 -04003113 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003114 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3115 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003116 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003117
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003118 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003119 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003120 func = btrfs_freespace_write_helper;
3121 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003122 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003123 func = btrfs_endio_write_helper;
3124 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003125
Liu Bo9e0af232014-08-15 23:36:53 +08003126 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3127 NULL);
3128 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003129}
3130
Miao Xiedc380ae2014-09-12 18:43:55 +08003131static int __readpage_endio_check(struct inode *inode,
3132 struct btrfs_io_bio *io_bio,
3133 int icsum, struct page *page,
3134 int pgoff, u64 start, size_t len)
3135{
3136 char *kaddr;
3137 u32 csum_expected;
3138 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003139
3140 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3141
3142 kaddr = kmap_atomic(page);
3143 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003144 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003145 if (csum != csum_expected)
3146 goto zeroit;
3147
3148 kunmap_atomic(kaddr);
3149 return 0;
3150zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003151 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003152 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003153 memset(kaddr + pgoff, 1, len);
3154 flush_dcache_page(page);
3155 kunmap_atomic(kaddr);
3156 if (csum_expected == 0)
3157 return 0;
3158 return -EIO;
3159}
3160
Chris Masond352ac62008-09-29 15:18:18 -04003161/*
Chris Masond352ac62008-09-29 15:18:18 -04003162 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003163 * if there's a match, we allow the bio to finish. If not, the code in
3164 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003165 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003166static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3167 u64 phy_offset, struct page *page,
3168 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003169{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003170 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003171 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003172 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003173 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003174
Chris Masond20f7042008-12-08 16:58:54 -05003175 if (PageChecked(page)) {
3176 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003177 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003178 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003179
3180 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003181 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003182
Yan Zheng17d217f2008-12-12 10:03:38 -05003183 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003184 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003185 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003186 return 0;
3187 }
3188
Miao Xiefacc8a222013-07-25 19:22:34 +08003189 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003190 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3191 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003192}
Chris Masonb888db22007-08-27 16:49:44 -04003193
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003194void btrfs_add_delayed_iput(struct inode *inode)
3195{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003196 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003197 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003198
3199 if (atomic_add_unless(&inode->i_count, -1, 1))
3200 return;
3201
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003202 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003203 if (binode->delayed_iput_count == 0) {
3204 ASSERT(list_empty(&binode->delayed_iput));
3205 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3206 } else {
3207 binode->delayed_iput_count++;
3208 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003209 spin_unlock(&fs_info->delayed_iput_lock);
3210}
3211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003212void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003213{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003214
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003215 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003216 while (!list_empty(&fs_info->delayed_iputs)) {
3217 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218
David Sterba8089fe62015-11-19 14:15:51 +01003219 inode = list_first_entry(&fs_info->delayed_iputs,
3220 struct btrfs_inode, delayed_iput);
3221 if (inode->delayed_iput_count) {
3222 inode->delayed_iput_count--;
3223 list_move_tail(&inode->delayed_iput,
3224 &fs_info->delayed_iputs);
3225 } else {
3226 list_del_init(&inode->delayed_iput);
3227 }
3228 spin_unlock(&fs_info->delayed_iput_lock);
3229 iput(&inode->vfs_inode);
3230 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003231 }
David Sterba8089fe62015-11-19 14:15:51 +01003232 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003233}
3234
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003236 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003237 * files in the subvolume, it removes orphan item and frees block_rsv
3238 * structure.
3239 */
3240void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3241 struct btrfs_root *root)
3242{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003243 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003244 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 int ret;
3246
Josef Bacik8a35d952012-05-23 14:26:42 -04003247 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3249 return;
3250
Josef Bacik90290e12011-12-02 15:44:12 -05003251 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003252 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003253 spin_unlock(&root->orphan_lock);
3254 return;
3255 }
3256
3257 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3258 spin_unlock(&root->orphan_lock);
3259 return;
3260 }
3261
3262 block_rsv = root->orphan_block_rsv;
3263 root->orphan_block_rsv = NULL;
3264 spin_unlock(&root->orphan_lock);
3265
Miao Xie27cdeb72014-04-02 19:51:05 +08003266 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003270 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003271 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003272 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003273 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3274 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 }
3276
Josef Bacik90290e12011-12-02 15:44:12 -05003277 if (block_rsv) {
3278 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003279 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 }
3281}
3282
3283/*
Josef Bacik7b128762008-07-24 12:17:14 -04003284 * This creates an orphan entry for the given inode in case something goes
3285 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 *
3287 * NOTE: caller of this function should reserve 5 units of metadata for
3288 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003289 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003290int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3291 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003292{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3294 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 struct btrfs_block_rsv *block_rsv = NULL;
3296 int reserve = 0;
3297 int insert = 0;
3298 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003299
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003301 block_rsv = btrfs_alloc_block_rsv(fs_info,
3302 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003303 if (!block_rsv)
3304 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003305 }
3306
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 spin_lock(&root->orphan_lock);
3308 if (!root->orphan_block_rsv) {
3309 root->orphan_block_rsv = block_rsv;
3310 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003311 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 block_rsv = NULL;
3313 }
Josef Bacik7b128762008-07-24 12:17:14 -04003314
Josef Bacik8a35d952012-05-23 14:26:42 -04003315 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003316 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317#if 0
3318 /*
3319 * For proper ENOSPC handling, we should do orphan
3320 * cleanup when mounting. But this introduces backward
3321 * compatibility issue.
3322 */
3323 if (!xchg(&root->orphan_item_inserted, 1))
3324 insert = 2;
3325 else
3326 insert = 1;
3327#endif
3328 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003329 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003330 }
Josef Bacik7b128762008-07-24 12:17:14 -04003331
Josef Bacik72ac3c02012-05-23 14:13:11 -04003332 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003333 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003335 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003336
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 /* grab metadata reservation from transaction handle */
3338 if (reserve) {
3339 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003340 ASSERT(!ret);
3341 if (ret) {
3342 atomic_dec(&root->orphan_inodes);
3343 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003344 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003345 if (insert)
3346 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003347 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003348 return ret;
3349 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 }
3351
3352 /* insert an orphan item to track this unlinked/truncated file */
3353 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003354 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003355 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003356 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003357 if (reserve) {
3358 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003359 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003360 btrfs_orphan_release_metadata(inode);
3361 }
3362 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003363 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003364 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003365 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003366 return ret;
3367 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003368 }
3369 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 }
3371
3372 /* insert an orphan item to track subvolume contains orphan files */
3373 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003374 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003376 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003377 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003378 return ret;
3379 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 }
3381 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003382}
3383
3384/*
3385 * We have done the truncate/delete so we can go ahead and remove the orphan
3386 * item for this particular inode.
3387 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003388static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003389 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003390{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003391 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003392 int delete_item = 0;
3393 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003394 int ret = 0;
3395
Yan, Zhengd68fc572010-05-16 10:49:58 -04003396 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003397 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003398 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003399 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003400
Josef Bacik72ac3c02012-05-23 14:13:11 -04003401 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003402 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003403 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003404 spin_unlock(&root->orphan_lock);
3405
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003406 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003407 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003408 if (trans)
3409 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003410 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003411 }
Josef Bacik7b128762008-07-24 12:17:14 -04003412
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003413 if (release_rsv)
3414 btrfs_orphan_release_metadata(inode);
3415
Josef Bacik4ef31a42013-08-13 14:10:08 -04003416 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003417}
3418
3419/*
3420 * this cleans up any orphans that may be left on the list from the last use
3421 * of this root.
3422 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003423int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003424{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003425 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003426 struct btrfs_path *path;
3427 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003428 struct btrfs_key key, found_key;
3429 struct btrfs_trans_handle *trans;
3430 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003431 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003432 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3433
Yan, Zhengd68fc572010-05-16 10:49:58 -04003434 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003435 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003436
3437 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003438 if (!path) {
3439 ret = -ENOMEM;
3440 goto out;
3441 }
David Sterbae4058b52015-11-27 16:31:35 +01003442 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003443
3444 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003445 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003446 key.offset = (u64)-1;
3447
Josef Bacik7b128762008-07-24 12:17:14 -04003448 while (1) {
3449 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003450 if (ret < 0)
3451 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003452
3453 /*
3454 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003455 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003456 * find the key and see if we have stuff that matches
3457 */
3458 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003459 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003460 if (path->slots[0] == 0)
3461 break;
3462 path->slots[0]--;
3463 }
3464
3465 /* pull out the item */
3466 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003467 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3468
3469 /* make sure the item matches what we want */
3470 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3471 break;
David Sterba962a2982014-06-04 18:41:45 +02003472 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003473 break;
3474
3475 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003476 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003477
3478 /*
3479 * this is where we are basically btrfs_lookup, without the
3480 * crossing root thing. we store the inode number in the
3481 * offset of the orphan item.
3482 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003483
3484 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003485 btrfs_err(fs_info,
3486 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003487 ret = -EINVAL;
3488 goto out;
3489 }
3490
3491 last_objectid = found_key.offset;
3492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003493 found_key.objectid = found_key.offset;
3494 found_key.type = BTRFS_INODE_ITEM_KEY;
3495 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003496 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303497 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003498 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003500
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003501 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003502 struct btrfs_root *dead_root;
3503 struct btrfs_fs_info *fs_info = root->fs_info;
3504 int is_dead_root = 0;
3505
3506 /*
3507 * this is an orphan in the tree root. Currently these
3508 * could come from 2 sources:
3509 * a) a snapshot deletion in progress
3510 * b) a free space cache inode
3511 * We need to distinguish those two, as the snapshot
3512 * orphan must not get deleted.
3513 * find_dead_roots already ran before us, so if this
3514 * is a snapshot deletion, we should find the root
3515 * in the dead_roots list
3516 */
3517 spin_lock(&fs_info->trans_lock);
3518 list_for_each_entry(dead_root, &fs_info->dead_roots,
3519 root_list) {
3520 if (dead_root->root_key.objectid ==
3521 found_key.objectid) {
3522 is_dead_root = 1;
3523 break;
3524 }
3525 }
3526 spin_unlock(&fs_info->trans_lock);
3527 if (is_dead_root) {
3528 /* prevent this orphan from being found again */
3529 key.offset = found_key.objectid - 1;
3530 continue;
3531 }
3532 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003533 /*
3534 * Inode is already gone but the orphan item is still there,
3535 * kill the orphan item.
3536 */
Filipe Manana67710892016-06-06 11:51:25 +01003537 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003538 trans = btrfs_start_transaction(root, 1);
3539 if (IS_ERR(trans)) {
3540 ret = PTR_ERR(trans);
3541 goto out;
3542 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 btrfs_debug(fs_info, "auto deleting %Lu",
3544 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003545 ret = btrfs_del_orphan_item(trans, root,
3546 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003547 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003548 if (ret)
3549 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003550 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003551 }
Josef Bacik7b128762008-07-24 12:17:14 -04003552
Josef Bacik7b128762008-07-24 12:17:14 -04003553 /*
3554 * add this inode to the orphan list so btrfs_orphan_del does
3555 * the proper thing when we hit it
3556 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003557 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3558 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003559 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003560
Josef Bacik7b128762008-07-24 12:17:14 -04003561 /* if we have links, this was a truncate, lets do that */
3562 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303563 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003564 iput(inode);
3565 continue;
3566 }
Josef Bacik7b128762008-07-24 12:17:14 -04003567 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003568
3569 /* 1 for the orphan item deletion. */
3570 trans = btrfs_start_transaction(root, 1);
3571 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003572 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003573 ret = PTR_ERR(trans);
3574 goto out;
3575 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003576 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003577 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003578 if (ret) {
3579 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003580 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003581 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003582
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003583 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003584 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003585 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003586 } else {
3587 nr_unlink++;
3588 }
3589
3590 /* this will do delete_inode and everything for us */
3591 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003592 if (ret)
3593 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003594 }
Miao Xie3254c872011-11-10 20:45:05 -05003595 /* release the path since we're done with it */
3596 btrfs_release_path(path);
3597
Yan, Zhengd68fc572010-05-16 10:49:58 -04003598 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3599
3600 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003601 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003602 (u64)-1);
3603
Miao Xie27cdeb72014-04-02 19:51:05 +08003604 if (root->orphan_block_rsv ||
3605 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003606 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003607 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003608 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003609 }
Josef Bacik7b128762008-07-24 12:17:14 -04003610
3611 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003612 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003613 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003614 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003615
3616out:
3617 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003619 btrfs_free_path(path);
3620 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003621}
3622
Chris Masond352ac62008-09-29 15:18:18 -04003623/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003624 * very simple check to peek ahead in the leaf looking for xattrs. If we
3625 * don't find any xattrs, we know there can't be any acls.
3626 *
3627 * slot is the slot the inode is in, objectid is the objectid of the inode
3628 */
3629static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003630 int slot, u64 objectid,
3631 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003632{
3633 u32 nritems = btrfs_header_nritems(leaf);
3634 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003635 static u64 xattr_access = 0;
3636 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003637 int scanned = 0;
3638
Josef Bacikf23b5a52013-06-19 10:16:26 -04003639 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003640 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3641 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3642 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3643 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003644 }
3645
Chris Mason46a53cc2009-04-27 11:47:50 -04003646 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003647 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003648 while (slot < nritems) {
3649 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3650
3651 /* we found a different objectid, there must not be acls */
3652 if (found_key.objectid != objectid)
3653 return 0;
3654
3655 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003656 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003657 if (*first_xattr_slot == -1)
3658 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003659 if (found_key.offset == xattr_access ||
3660 found_key.offset == xattr_default)
3661 return 1;
3662 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003663
3664 /*
3665 * we found a key greater than an xattr key, there can't
3666 * be any acls later on
3667 */
3668 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3669 return 0;
3670
3671 slot++;
3672 scanned++;
3673
3674 /*
3675 * it goes inode, inode backrefs, xattrs, extents,
3676 * so if there are a ton of hard links to an inode there can
3677 * be a lot of backrefs. Don't waste time searching too hard,
3678 * this is just an optimization
3679 */
3680 if (scanned >= 8)
3681 break;
3682 }
3683 /* we hit the end of the leaf before we found an xattr or
3684 * something larger than an xattr. We have to assume the inode
3685 * has acls
3686 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003687 if (*first_xattr_slot == -1)
3688 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003689 return 1;
3690}
3691
3692/*
Chris Masond352ac62008-09-29 15:18:18 -04003693 * read an inode from the btree into the in-memory inode
3694 */
Filipe Manana67710892016-06-06 11:51:25 +01003695static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003696{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003697 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003698 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003699 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003700 struct btrfs_inode_item *inode_item;
3701 struct btrfs_root *root = BTRFS_I(inode)->root;
3702 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003703 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003704 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003705 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003706 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003707 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003708 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003709
3710 ret = btrfs_fill_inode(inode, &rdev);
3711 if (!ret)
3712 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003713
3714 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003715 if (!path) {
3716 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003717 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003718 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003719
Chris Mason39279cc2007-06-12 06:35:45 -04003720 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003721
Chris Mason39279cc2007-06-12 06:35:45 -04003722 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003723 if (ret) {
3724 if (ret > 0)
3725 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003726 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003727 }
Chris Mason39279cc2007-06-12 06:35:45 -04003728
Chris Mason5f39d392007-10-15 16:14:19 -04003729 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003730
3731 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003732 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003733
Chris Mason5f39d392007-10-15 16:14:19 -04003734 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3735 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003736 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003737 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003738 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3739 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003740 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003741
David Sterbaa937b972014-12-12 17:39:12 +01003742 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3743 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
David Sterbaa937b972014-12-12 17:39:12 +01003745 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3746 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003747
David Sterbaa937b972014-12-12 17:39:12 +01003748 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3749 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003750
chandan r9cc97d62012-07-04 12:48:07 +05303751 BTRFS_I(inode)->i_otime.tv_sec =
3752 btrfs_timespec_sec(leaf, &inode_item->otime);
3753 BTRFS_I(inode)->i_otime.tv_nsec =
3754 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003755
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003756 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003757 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003758 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3759
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003760 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003761 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003762 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003763 rdev = btrfs_inode_rdev(leaf, inode_item);
3764
Josef Bacikaec74772008-07-24 12:12:38 -04003765 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003766 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003767
3768cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003769 /*
3770 * If we were modified in the current generation and evicted from memory
3771 * and then re-read we need to do a full sync since we don't have any
3772 * idea about which extents were modified before we were evicted from
3773 * cache.
3774 *
3775 * This is required for both inode re-read from disk and delayed inode
3776 * in delayed_nodes_tree.
3777 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003778 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003779 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3780 &BTRFS_I(inode)->runtime_flags);
3781
Filipe Mananabde6c242015-07-24 00:00:19 +01003782 /*
3783 * We don't persist the id of the transaction where an unlink operation
3784 * against the inode was last made. So here we assume the inode might
3785 * have been evicted, and therefore the exact value of last_unlink_trans
3786 * lost, and set it to last_trans to avoid metadata inconsistencies
3787 * between the inode and its parent if the inode is fsync'ed and the log
3788 * replayed. For example, in the scenario:
3789 *
3790 * touch mydir/foo
3791 * ln mydir/foo mydir/bar
3792 * sync
3793 * unlink mydir/bar
3794 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3795 * xfs_io -c fsync mydir/foo
3796 * <power failure>
3797 * mount fs, triggers fsync log replay
3798 *
3799 * We must make sure that when we fsync our inode foo we also log its
3800 * parent inode, otherwise after log replay the parent still has the
3801 * dentry with the "bar" name but our inode foo has a link count of 1
3802 * and doesn't have an inode ref with the name "bar" anymore.
3803 *
3804 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003805 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003806 * transaction commits on fsync if our inode is a directory, or if our
3807 * inode is not a directory, logging its parent unnecessarily.
3808 */
3809 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3810
Miao Xie67de1172013-12-26 13:07:06 +08003811 path->slots[0]++;
3812 if (inode->i_nlink != 1 ||
3813 path->slots[0] >= btrfs_header_nritems(leaf))
3814 goto cache_acl;
3815
3816 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003817 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003818 goto cache_acl;
3819
3820 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3821 if (location.type == BTRFS_INODE_REF_KEY) {
3822 struct btrfs_inode_ref *ref;
3823
3824 ref = (struct btrfs_inode_ref *)ptr;
3825 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3826 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3827 struct btrfs_inode_extref *extref;
3828
3829 extref = (struct btrfs_inode_extref *)ptr;
3830 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3831 extref);
3832 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003833cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003834 /*
3835 * try to precache a NULL acl entry for files that don't have
3836 * any xattrs or acls
3837 */
Li Zefan33345d012011-04-20 10:31:50 +08003838 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003839 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003840 if (first_xattr_slot != -1) {
3841 path->slots[0] = first_xattr_slot;
3842 ret = btrfs_load_inode_props(inode, path);
3843 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003844 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003845 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003846 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003847 root->root_key.objectid, ret);
3848 }
3849 btrfs_free_path(path);
3850
Al Viro72c04902009-06-24 16:58:48 -04003851 if (!maybe_acls)
3852 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003853
Chris Mason39279cc2007-06-12 06:35:45 -04003854 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003855 case S_IFREG:
3856 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003857 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003858 inode->i_fop = &btrfs_file_operations;
3859 inode->i_op = &btrfs_file_inode_operations;
3860 break;
3861 case S_IFDIR:
3862 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003863 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003864 break;
3865 case S_IFLNK:
3866 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003867 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003868 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3869 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003870 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003871 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003872 init_special_inode(inode, inode->i_mode, rdev);
3873 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003875
3876 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003877 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003878
3879make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003880 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003881 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003882 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003883}
3884
Chris Masond352ac62008-09-29 15:18:18 -04003885/*
3886 * given a leaf and an inode, copy the inode fields into the leaf
3887 */
Chris Masone02119d2008-09-05 16:13:11 -04003888static void fill_inode_item(struct btrfs_trans_handle *trans,
3889 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003890 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003891 struct inode *inode)
3892{
Liu Bo51fab692012-12-27 09:01:21 +00003893 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003894
Liu Bo51fab692012-12-27 09:01:21 +00003895 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003896
Liu Bo51fab692012-12-27 09:01:21 +00003897 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3898 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3899 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3900 &token);
3901 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3902 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003903
David Sterbaa937b972014-12-12 17:39:12 +01003904 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003905 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003906 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003907 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003908
David Sterbaa937b972014-12-12 17:39:12 +01003909 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003910 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003911 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003912 inode->i_mtime.tv_nsec, &token);
3913
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003916 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003917 inode->i_ctime.tv_nsec, &token);
3918
chandan r9cc97d62012-07-04 12:48:07 +05303919 btrfs_set_token_timespec_sec(leaf, &item->otime,
3920 BTRFS_I(inode)->i_otime.tv_sec, &token);
3921 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3922 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3923
Liu Bo51fab692012-12-27 09:01:21 +00003924 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3925 &token);
3926 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3927 &token);
3928 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3929 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3930 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3931 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3932 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003933}
3934
Chris Masond352ac62008-09-29 15:18:18 -04003935/*
3936 * copy everything in the in-memory inode into the btree.
3937 */
Chris Mason21151332011-11-10 20:39:08 -05003938static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003939 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003940{
3941 struct btrfs_inode_item *inode_item;
3942 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003943 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003944 int ret;
3945
3946 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003947 if (!path)
3948 return -ENOMEM;
3949
Chris Masonb9473432009-03-13 11:00:37 -04003950 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003951 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3952 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 if (ret) {
3954 if (ret > 0)
3955 ret = -ENOENT;
3956 goto failed;
3957 }
3958
Chris Mason5f39d392007-10-15 16:14:19 -04003959 leaf = path->nodes[0];
3960 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003961 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003962
Chris Masone02119d2008-09-05 16:13:11 -04003963 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003964 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003965 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003966 ret = 0;
3967failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003968 btrfs_free_path(path);
3969 return ret;
3970}
3971
Chris Masond352ac62008-09-29 15:18:18 -04003972/*
Chris Mason21151332011-11-10 20:39:08 -05003973 * copy everything in the in-memory inode into the btree.
3974 */
3975noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3976 struct btrfs_root *root, struct inode *inode)
3977{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003978 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003979 int ret;
3980
3981 /*
3982 * If the inode is a free space inode, we can deadlock during commit
3983 * if we put it into the delayed code.
3984 *
3985 * The data relocation inode should also be directly updated
3986 * without delay
3987 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003988 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003989 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003990 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003991 btrfs_update_root_times(trans, root);
3992
Chris Mason21151332011-11-10 20:39:08 -05003993 ret = btrfs_delayed_update_inode(trans, root, inode);
3994 if (!ret)
3995 btrfs_set_inode_last_trans(trans, inode);
3996 return ret;
3997 }
3998
3999 return btrfs_update_inode_item(trans, root, inode);
4000}
4001
Josef Bacikbe6aef62012-10-22 15:43:12 -04004002noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4003 struct btrfs_root *root,
4004 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004005{
4006 int ret;
4007
4008 ret = btrfs_update_inode(trans, root, inode);
4009 if (ret == -ENOSPC)
4010 return btrfs_update_inode_item(trans, root, inode);
4011 return ret;
4012}
4013
4014/*
Chris Masond352ac62008-09-29 15:18:18 -04004015 * unlink helper that gets used here in inode.c and in the tree logging
4016 * recovery code. It remove a link in a directory with a given name, and
4017 * also drops the back refs in the inode to the directory
4018 */
Al Viro92986792011-03-04 17:14:37 +00004019static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4020 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004021 struct btrfs_inode *dir,
4022 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004023 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004024{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004025 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004026 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004027 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004028 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004029 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004030 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004031 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004032 u64 ino = btrfs_ino(inode);
4033 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004034
4035 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004036 if (!path) {
4037 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004038 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004039 }
4040
Chris Masonb9473432009-03-13 11:00:37 -04004041 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004042 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004043 name, name_len, -1);
4044 if (IS_ERR(di)) {
4045 ret = PTR_ERR(di);
4046 goto err;
4047 }
4048 if (!di) {
4049 ret = -ENOENT;
4050 goto err;
4051 }
Chris Mason5f39d392007-10-15 16:14:19 -04004052 leaf = path->nodes[0];
4053 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004055 if (ret)
4056 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004057 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004058
Miao Xie67de1172013-12-26 13:07:06 +08004059 /*
4060 * If we don't have dir index, we have to get it by looking up
4061 * the inode ref, since we get the inode ref, remove it directly,
4062 * it is unnecessary to do delayed deletion.
4063 *
4064 * But if we have dir index, needn't search inode ref to get it.
4065 * Since the inode ref is close to the inode item, it is better
4066 * that we delay to delete it, and just do this deletion when
4067 * we update the inode item.
4068 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004069 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004070 ret = btrfs_delayed_delete_inode_ref(inode);
4071 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004072 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004073 goto skip_backref;
4074 }
4075 }
4076
Li Zefan33345d012011-04-20 10:31:50 +08004077 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4078 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004079 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004080 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004081 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004082 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004083 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004084 goto err;
4085 }
Miao Xie67de1172013-12-26 13:07:06 +08004086skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004087 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004089 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004090 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004091 }
Chris Mason39279cc2007-06-12 06:35:45 -04004092
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004093 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4094 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 goto err;
4098 }
Chris Masone02119d2008-09-05 16:13:11 -04004099
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004100 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4101 index);
Chris Mason6418c962010-10-30 07:34:24 -04004102 if (ret == -ENOENT)
4103 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004104 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004105 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004106err:
4107 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004108 if (ret)
4109 goto out;
4110
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004111 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004112 inode_inc_iversion(&inode->vfs_inode);
4113 inode_inc_iversion(&dir->vfs_inode);
4114 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4115 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4116 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004117out:
Chris Mason39279cc2007-06-12 06:35:45 -04004118 return ret;
4119}
4120
Al Viro92986792011-03-04 17:14:37 +00004121int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4122 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004123 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004124 const char *name, int name_len)
4125{
4126 int ret;
4127 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4128 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004129 drop_nlink(&inode->vfs_inode);
4130 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004131 }
4132 return ret;
4133}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004134
4135/*
4136 * helper to start transaction for unlink and rmdir.
4137 *
Josef Bacikd52be812013-05-29 14:54:47 -04004138 * unlink and rmdir are special in btrfs, they do not always free space, so
4139 * if we cannot make our reservations the normal way try and see if there is
4140 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4141 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004142 */
Josef Bacikd52be812013-05-29 14:54:47 -04004143static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004144{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004145 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146
Josef Bacike70bea52011-10-11 14:18:24 -04004147 /*
4148 * 1 for the possible orphan item
4149 * 1 for the dir item
4150 * 1 for the dir index
4151 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004152 * 1 for the inode
4153 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004154 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004155}
4156
Chris Mason39279cc2007-06-12 06:35:45 -04004157static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4158{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004159 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004161 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004162 int ret;
4163
Josef Bacikd52be812013-05-29 14:54:47 -04004164 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004166 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004167
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004168 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4169 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004170
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004171 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4172 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4173 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004174 if (ret)
4175 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004176
Yan, Zhenga22285a2010-05-16 10:48:46 -04004177 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004178 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004179 if (ret)
4180 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004181 }
Josef Bacik7b128762008-07-24 12:17:14 -04004182
Tsutomu Itohb5324022011-07-19 07:27:20 +00004183out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004184 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004185 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004186 return ret;
4187}
4188
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004189int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4190 struct btrfs_root *root,
4191 struct inode *dir, u64 objectid,
4192 const char *name, int name_len)
4193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004194 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004195 struct btrfs_path *path;
4196 struct extent_buffer *leaf;
4197 struct btrfs_dir_item *di;
4198 struct btrfs_key key;
4199 u64 index;
4200 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004201 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202
4203 path = btrfs_alloc_path();
4204 if (!path)
4205 return -ENOMEM;
4206
Li Zefan33345d012011-04-20 10:31:50 +08004207 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004209 if (IS_ERR_OR_NULL(di)) {
4210 if (!di)
4211 ret = -ENOENT;
4212 else
4213 ret = PTR_ERR(di);
4214 goto out;
4215 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004216
4217 leaf = path->nodes[0];
4218 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4219 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4220 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004221 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004222 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004223 goto out;
4224 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004225 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004226
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004227 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4228 root->root_key.objectid, dir_ino,
4229 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004230 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 goto out;
4234 }
Li Zefan33345d012011-04-20 10:31:50 +08004235 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004237 if (IS_ERR_OR_NULL(di)) {
4238 if (!di)
4239 ret = -ENOENT;
4240 else
4241 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 goto out;
4244 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004245
4246 leaf = path->nodes[0];
4247 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004248 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004249 index = key.offset;
4250 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004251 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004253 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004255 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004256 goto out;
4257 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004258
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004259 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004260 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004261 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004262 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004264 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004265out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004266 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004267 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268}
4269
Chris Mason39279cc2007-06-12 06:35:45 -04004270static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4271{
David Howells2b0143b2015-03-17 22:25:59 +00004272 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004273 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004274 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004275 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004276 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004277
David Sterbab3ae2442012-09-13 16:04:34 -06004278 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004279 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004280 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004281 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004282
Josef Bacikd52be812013-05-29 14:54:47 -04004283 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004284 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004285 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004286
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004287 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288 err = btrfs_unlink_subvol(trans, root, dir,
4289 BTRFS_I(inode)->location.objectid,
4290 dentry->d_name.name,
4291 dentry->d_name.len);
4292 goto out;
4293 }
4294
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004295 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004296 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004297 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004298
Filipe Manana44f714d2016-06-06 16:11:13 +01004299 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4300
Chris Mason39279cc2007-06-12 06:35:45 -04004301 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004302 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4303 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4304 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004305 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004306 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004307 /*
4308 * Propagate the last_unlink_trans value of the deleted dir to
4309 * its parent directory. This is to prevent an unrecoverable
4310 * log tree in the case we do something like this:
4311 * 1) create dir foo
4312 * 2) create snapshot under dir foo
4313 * 3) delete the snapshot
4314 * 4) rmdir foo
4315 * 5) mkdir foo
4316 * 6) fsync foo or some file inside foo
4317 */
4318 if (last_unlink_trans >= trans->transid)
4319 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4320 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004321out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004322 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004323 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004324
Chris Mason39279cc2007-06-12 06:35:45 -04004325 return err;
4326}
4327
Chris Mason28f75a02015-02-04 06:59:29 -08004328static int truncate_space_check(struct btrfs_trans_handle *trans,
4329 struct btrfs_root *root,
4330 u64 bytes_deleted)
4331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004332 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004333 int ret;
4334
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004335 /*
4336 * This is only used to apply pressure to the enospc system, we don't
4337 * intend to use this reservation at all.
4338 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004339 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004340 bytes_deleted *= fs_info->nodesize;
4341 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004342 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004343 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004344 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004345 trans->transid,
4346 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004347 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004348 }
Chris Mason28f75a02015-02-04 06:59:29 -08004349 return ret;
4350
4351}
4352
Filipe Manana0305cd52015-10-16 12:34:25 +01004353static int truncate_inline_extent(struct inode *inode,
4354 struct btrfs_path *path,
4355 struct btrfs_key *found_key,
4356 const u64 item_end,
4357 const u64 new_size)
4358{
4359 struct extent_buffer *leaf = path->nodes[0];
4360 int slot = path->slots[0];
4361 struct btrfs_file_extent_item *fi;
4362 u32 size = (u32)(new_size - found_key->offset);
4363 struct btrfs_root *root = BTRFS_I(inode)->root;
4364
4365 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4366
4367 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4368 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004369 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004370
4371 /*
4372 * Zero out the remaining of the last page of our inline extent,
4373 * instead of directly truncating our inline extent here - that
4374 * would be much more complex (decompressing all the data, then
4375 * compressing the truncated data, which might be bigger than
4376 * the size of the inline extent, resize the extent, etc).
4377 * We release the path because to get the page we might need to
4378 * read the extent item from disk (data not in the page cache).
4379 */
4380 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304381 return btrfs_truncate_block(inode, offset, page_end - offset,
4382 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004383 }
4384
4385 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4386 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004387 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004388
4389 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4390 inode_sub_bytes(inode, item_end + 1 - new_size);
4391
4392 return 0;
4393}
4394
Chris Mason323ac952008-10-01 19:05:46 -04004395/*
Chris Mason39279cc2007-06-12 06:35:45 -04004396 * this can truncate away extent items, csum items and directory items.
4397 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004398 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004399 *
4400 * csum items that cross the new i_size are truncated to the new size
4401 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004402 *
4403 * min_type is the minimum key type to truncate down to. If set to 0, this
4404 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004405 */
Yan, Zheng80825102009-11-12 09:35:36 +00004406int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4407 struct btrfs_root *root,
4408 struct inode *inode,
4409 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004411 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004413 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004414 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004415 struct btrfs_key key;
4416 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004417 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004418 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004419 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004420 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004421 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004422 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 int found_extent;
4424 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004425 int pending_del_nr = 0;
4426 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004427 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004428 int ret;
4429 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004430 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004431 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004432 bool be_nice = 0;
4433 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004434 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004435
4436 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004437
Chris Mason28ed1342014-12-17 09:41:04 -08004438 /*
4439 * for non-free space inodes and ref cows, we want to back off from
4440 * time to time
4441 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004442 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004443 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4444 be_nice = 1;
4445
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004446 path = btrfs_alloc_path();
4447 if (!path)
4448 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004449 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004450
Josef Bacik5dc562c2012-08-17 13:14:17 -04004451 /*
4452 * We want to drop from the next block forward in case this new size is
4453 * not block aligned since we will be keeping the last block of the
4454 * extent just the way it is.
4455 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004456 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004457 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004458 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004459 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004460 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004461
Miao Xie16cdcec2011-04-22 18:12:22 +08004462 /*
4463 * This function is also used to drop the items in the log tree before
4464 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4465 * it is used to drop the loged items. So we shouldn't kill the delayed
4466 * items.
4467 */
4468 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004469 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004470
Li Zefan33345d012011-04-20 10:31:50 +08004471 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004473 key.type = (u8)-1;
4474
Chris Mason85e21ba2008-01-29 15:11:36 -05004475search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004476 /*
4477 * with a 16K leaf size and 128MB extents, you can actually queue
4478 * up a huge file in a single leaf. Most of the time that
4479 * bytes_deleted is > 0, it will be huge by the time we get here
4480 */
Byongho Leeee221842015-12-15 01:42:10 +09004481 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004482 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004483 err = -EAGAIN;
4484 goto error;
4485 }
4486 }
4487
4488
Chris Masonb9473432009-03-13 11:00:37 -04004489 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004490 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004491 if (ret < 0) {
4492 err = ret;
4493 goto out;
4494 }
Chris Masond3977122009-01-05 21:25:51 -05004495
Chris Mason85e21ba2008-01-29 15:11:36 -05004496 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004497 /* there are no items in the tree for us to truncate, we're
4498 * done
4499 */
Yan, Zheng80825102009-11-12 09:35:36 +00004500 if (path->slots[0] == 0)
4501 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004502 path->slots[0]--;
4503 }
4504
Chris Masond3977122009-01-05 21:25:51 -05004505 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004506 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004507 leaf = path->nodes[0];
4508 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004509 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004510
Li Zefan33345d012011-04-20 10:31:50 +08004511 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004512 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004513
Chris Mason85e21ba2008-01-29 15:11:36 -05004514 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004515 break;
4516
Chris Mason5f39d392007-10-15 16:14:19 -04004517 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004518 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004519 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004520 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004521 extent_type = btrfs_file_extent_type(leaf, fi);
4522 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004523 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004524 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004525
4526 trace_btrfs_truncate_show_fi_regular(
4527 BTRFS_I(inode), leaf, fi,
4528 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004529 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004530 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004531 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004532
4533 trace_btrfs_truncate_show_fi_inline(
4534 BTRFS_I(inode), leaf, fi, path->slots[0],
4535 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004536 }
Yan008630c2007-11-07 13:31:09 -05004537 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004538 }
Yan, Zheng80825102009-11-12 09:35:36 +00004539 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004540 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004541 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004542 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004543 break;
4544 if (found_key.offset >= new_size)
4545 del_item = 1;
4546 else
4547 del_item = 0;
4548 }
Chris Mason39279cc2007-06-12 06:35:45 -04004549 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004550 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004551 if (found_type != BTRFS_EXTENT_DATA_KEY)
4552 goto delete;
4553
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004554 if (del_item)
4555 last_size = found_key.offset;
4556 else
4557 last_size = new_size;
4558
Chris Mason179e29e2007-11-01 11:28:41 -04004559 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004560 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004561 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004562 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004563 u64 orig_num_bytes =
4564 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004565 extent_num_bytes = ALIGN(new_size -
4566 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004567 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004568 btrfs_set_file_extent_num_bytes(leaf, fi,
4569 extent_num_bytes);
4570 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004571 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004572 if (test_bit(BTRFS_ROOT_REF_COWS,
4573 &root->state) &&
4574 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004575 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004576 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004577 } else {
Chris Masondb945352007-10-15 16:15:53 -04004578 extent_num_bytes =
4579 btrfs_file_extent_disk_num_bytes(leaf,
4580 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004581 extent_offset = found_key.offset -
4582 btrfs_file_extent_offset(leaf, fi);
4583
Chris Mason39279cc2007-06-12 06:35:45 -04004584 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004585 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004586 if (extent_start != 0) {
4587 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004588 if (test_bit(BTRFS_ROOT_REF_COWS,
4589 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004590 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004591 }
4592 }
Chris Mason90692182008-02-08 13:49:28 -05004593 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004594 /*
4595 * we can't truncate inline items that have had
4596 * special encodings
4597 */
4598 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004599 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4600 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004601
4602 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004603 * Need to release path in order to truncate a
4604 * compressed extent. So delete any accumulated
4605 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004606 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004607 if (btrfs_file_extent_compression(leaf, fi) !=
4608 BTRFS_COMPRESS_NONE && pending_del_nr) {
4609 err = btrfs_del_items(trans, root, path,
4610 pending_del_slot,
4611 pending_del_nr);
4612 if (err) {
4613 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004614 err);
4615 goto error;
4616 }
4617 pending_del_nr = 0;
4618 }
4619
4620 err = truncate_inline_extent(inode, path,
4621 &found_key,
4622 item_end,
4623 new_size);
4624 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004625 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004626 goto error;
4627 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004628 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4629 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004630 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004631 }
Chris Mason39279cc2007-06-12 06:35:45 -04004632 }
Chris Mason179e29e2007-11-01 11:28:41 -04004633delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004634 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004635 if (!pending_del_nr) {
4636 /* no pending yet, add ourselves */
4637 pending_del_slot = path->slots[0];
4638 pending_del_nr = 1;
4639 } else if (pending_del_nr &&
4640 path->slots[0] + 1 == pending_del_slot) {
4641 /* hop on the pending chunk */
4642 pending_del_nr++;
4643 pending_del_slot = path->slots[0];
4644 } else {
Chris Masond3977122009-01-05 21:25:51 -05004645 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004646 }
Chris Mason39279cc2007-06-12 06:35:45 -04004647 } else {
4648 break;
4649 }
Chris Mason28f75a02015-02-04 06:59:29 -08004650 should_throttle = 0;
4651
Miao Xie27cdeb72014-04-02 19:51:05 +08004652 if (found_extent &&
4653 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004655 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004656 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004657 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004658 extent_num_bytes, 0,
4659 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004660 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004661 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004662 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4663 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004664 trans->delayed_ref_updates * 2,
4665 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004666 if (be_nice) {
4667 if (truncate_space_check(trans, root,
4668 extent_num_bytes)) {
4669 should_end = 1;
4670 }
4671 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004672 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004673 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004674 }
Chris Mason39279cc2007-06-12 06:35:45 -04004675 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004676
Yan, Zheng80825102009-11-12 09:35:36 +00004677 if (found_type == BTRFS_INODE_ITEM_KEY)
4678 break;
4679
4680 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004681 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004682 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004683 if (pending_del_nr) {
4684 ret = btrfs_del_items(trans, root, path,
4685 pending_del_slot,
4686 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004687 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004688 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004689 goto error;
4690 }
Yan, Zheng80825102009-11-12 09:35:36 +00004691 pending_del_nr = 0;
4692 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004693 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004694 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004695 unsigned long updates = trans->delayed_ref_updates;
4696 if (updates) {
4697 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004698 ret = btrfs_run_delayed_refs(trans,
4699 fs_info,
4700 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004701 if (ret && !err)
4702 err = ret;
4703 }
4704 }
Chris Mason28f75a02015-02-04 06:59:29 -08004705 /*
4706 * if we failed to refill our space rsv, bail out
4707 * and let the transaction restart
4708 */
4709 if (should_end) {
4710 err = -EAGAIN;
4711 goto error;
4712 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004713 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004714 } else {
4715 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004716 }
Chris Mason39279cc2007-06-12 06:35:45 -04004717 }
Yan, Zheng80825102009-11-12 09:35:36 +00004718out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004719 if (pending_del_nr) {
4720 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4721 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004722 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004723 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004724 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004725error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004726 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4727 ASSERT(last_size >= new_size);
4728 if (!err && last_size > new_size)
4729 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004730 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004731 }
Chris Mason28ed1342014-12-17 09:41:04 -08004732
Chris Mason39279cc2007-06-12 06:35:45 -04004733 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004734
Byongho Leeee221842015-12-15 01:42:10 +09004735 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004736 unsigned long updates = trans->delayed_ref_updates;
4737 if (updates) {
4738 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004739 ret = btrfs_run_delayed_refs(trans, fs_info,
4740 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004741 if (ret && !err)
4742 err = ret;
4743 }
4744 }
Yan, Zheng80825102009-11-12 09:35:36 +00004745 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004746}
4747
Chris Masona52d9a82007-08-27 16:49:44 -04004748/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304749 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004750 * @inode - inode that we're zeroing
4751 * @from - the offset to start zeroing
4752 * @len - the length to zero, 0 to zero the entire range respective to the
4753 * offset
4754 * @front - zero up to the offset instead of from the offset on
4755 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304756 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004757 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004758 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004760 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004761{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004762 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004763 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004764 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4765 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004766 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004767 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004768 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004769 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304770 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004771 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004772 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004773 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 u64 block_start;
4775 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004776
Josef Bacik2aaa6652012-08-29 14:27:18 -04004777 if ((offset & (blocksize - 1)) == 0 &&
4778 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004779 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004781 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004783 if (ret)
4784 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004785
Chris Mason211c17f2008-05-15 09:13:45 -04004786again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004787 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004788 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004789 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 round_down(from, blocksize),
4791 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004792 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004793 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004794 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004795
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796 block_start = round_down(from, blocksize);
4797 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798
Chris Masona52d9a82007-08-27 16:49:44 -04004799 if (!PageUptodate(page)) {
4800 ret = btrfs_readpage(NULL, page);
4801 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004802 if (page->mapping != mapping) {
4803 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004804 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004805 goto again;
4806 }
Chris Masona52d9a82007-08-27 16:49:44 -04004807 if (!PageUptodate(page)) {
4808 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004809 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004810 }
4811 }
Chris Mason211c17f2008-05-15 09:13:45 -04004812 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004813
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004815 set_page_extent_mapped(page);
4816
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304817 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004818 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004820 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004822 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004823 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004824 btrfs_put_ordered_extent(ordered);
4825 goto again;
4826 }
4827
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004829 EXTENT_DIRTY | EXTENT_DELALLOC |
4830 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004831 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004832
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004834 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004835 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304836 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004837 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004838 goto out_unlock;
4839 }
4840
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004842 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304843 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004844 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004845 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846 memset(kaddr + (block_start - page_offset(page)),
4847 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004848 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304849 memset(kaddr + (block_start - page_offset(page)) + offset,
4850 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004851 flush_dcache_page(page);
4852 kunmap(page);
4853 }
Chris Mason247e7432008-07-17 12:53:51 -04004854 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004857 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004858
Chris Mason89642222008-07-24 09:41:53 -04004859out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004860 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304861 btrfs_delalloc_release_space(inode, block_start,
4862 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004863 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004864 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004865out:
4866 return ret;
4867}
4868
Josef Bacik16e75492013-10-22 12:18:51 -04004869static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4870 u64 offset, u64 len)
4871{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004873 struct btrfs_trans_handle *trans;
4874 int ret;
4875
4876 /*
4877 * Still need to make sure the inode looks like it's been updated so
4878 * that any holes get logged if we fsync.
4879 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004880 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4881 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004882 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4883 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4884 return 0;
4885 }
4886
4887 /*
4888 * 1 - for the one we're dropping
4889 * 1 - for the one we're adding
4890 * 1 - for updating the inode.
4891 */
4892 trans = btrfs_start_transaction(root, 3);
4893 if (IS_ERR(trans))
4894 return PTR_ERR(trans);
4895
4896 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4897 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004898 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004899 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004900 return ret;
4901 }
4902
David Sterbaf85b7372017-01-20 14:54:07 +01004903 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4904 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004905 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004906 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004907 else
4908 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004909 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004910 return ret;
4911}
4912
Josef Bacik695a0d02011-03-04 15:46:53 -05004913/*
4914 * This function puts in dummy file extents for the area we're creating a hole
4915 * for. So if we are truncating this file to a larger size we need to insert
4916 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4917 * the range between oldsize and size
4918 */
Josef Bacika41ad392011-01-31 15:30:16 -05004919int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004922 struct btrfs_root *root = BTRFS_I(inode)->root;
4923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004924 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004925 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004927 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4928 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004929 u64 last_byte;
4930 u64 cur_offset;
4931 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004932 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004933
Josef Bacika71754f2013-06-17 17:14:39 -04004934 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 * If our size started in the middle of a block we need to zero out the
4936 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004937 * expose stale data.
4938 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304939 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004940 if (err)
4941 return err;
4942
Yan Zheng9036c102008-10-30 14:19:41 -04004943 if (size <= hole_start)
4944 return 0;
4945
Yan Zheng9036c102008-10-30 14:19:41 -04004946 while (1) {
4947 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004948
David Sterbaff13db42015-12-03 14:30:40 +01004949 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004950 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004951 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004952 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004953 if (!ordered)
4954 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004955 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4956 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004957 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004958 btrfs_put_ordered_extent(ordered);
4959 }
4960
Yan Zheng9036c102008-10-30 14:19:41 -04004961 cur_offset = hole_start;
4962 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004963 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004964 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004965 if (IS_ERR(em)) {
4966 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004967 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004968 break;
4969 }
Yan Zheng9036c102008-10-30 14:19:41 -04004970 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004971 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004972 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004973 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004974 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004975
Josef Bacik16e75492013-10-22 12:18:51 -04004976 err = maybe_insert_hole(root, inode, cur_offset,
4977 hole_size);
4978 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004979 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004980 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004981 cur_offset + hole_size - 1, 0);
4982 hole_em = alloc_extent_map();
4983 if (!hole_em) {
4984 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4985 &BTRFS_I(inode)->runtime_flags);
4986 goto next;
4987 }
4988 hole_em->start = cur_offset;
4989 hole_em->len = hole_size;
4990 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004991
Josef Bacik5dc562c2012-08-17 13:14:17 -04004992 hole_em->block_start = EXTENT_MAP_HOLE;
4993 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004994 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004995 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004996 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004997 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004998 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004999
5000 while (1) {
5001 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005002 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005003 write_unlock(&em_tree->lock);
5004 if (err != -EEXIST)
5005 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005006 btrfs_drop_extent_cache(BTRFS_I(inode),
5007 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005008 cur_offset +
5009 hole_size - 1, 0);
5010 }
5011 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005012 }
Josef Bacik16e75492013-10-22 12:18:51 -04005013next:
Yan Zheng9036c102008-10-30 14:19:41 -04005014 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005015 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005016 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005017 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005018 break;
5019 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005020 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005021 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5022 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005023 return err;
5024}
5025
Eric Sandeen3972f262013-01-12 02:57:22 +00005026static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005027{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005028 struct btrfs_root *root = BTRFS_I(inode)->root;
5029 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005030 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005031 loff_t newsize = attr->ia_size;
5032 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005033 int ret;
5034
Eric Sandeen3972f262013-01-12 02:57:22 +00005035 /*
5036 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5037 * special case where we need to update the times despite not having
5038 * these flags set. For all other operations the VFS set these flags
5039 * explicitly if it wants a timestamp update.
5040 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005041 if (newsize != oldsize) {
5042 inode_inc_iversion(inode);
5043 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5044 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005045 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005046 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005047
Josef Bacika41ad392011-01-31 15:30:16 -05005048 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005049 /*
5050 * Don't do an expanding truncate while snapshoting is ongoing.
5051 * This is to ensure the snapshot captures a fully consistent
5052 * state of this file - if the snapshot captures this expanding
5053 * truncation, it must capture all writes that happened before
5054 * this truncation.
5055 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005056 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005057 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005058 if (ret) {
5059 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005060 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005061 }
Yan, Zheng80825102009-11-12 09:35:36 +00005062
Miao Xief4a2f4c2011-12-14 20:12:01 -05005063 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005064 if (IS_ERR(trans)) {
5065 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005066 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005067 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005068
5069 i_size_write(inode, newsize);
5070 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305071 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005072 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005074 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005075 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005076
Josef Bacika41ad392011-01-31 15:30:16 -05005077 /*
5078 * We're truncating a file that used to have good data down to
5079 * zero. Make sure it gets into the ordered flush list so that
5080 * any new writes get down to disk quickly.
5081 */
5082 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005083 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5084 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005085
Josef Bacikf3fe8202013-01-07 17:03:21 -05005086 /*
5087 * 1 for the orphan item we're going to add
5088 * 1 for the orphan item deletion.
5089 */
5090 trans = btrfs_start_transaction(root, 2);
5091 if (IS_ERR(trans))
5092 return PTR_ERR(trans);
5093
5094 /*
5095 * We need to do this in case we fail at _any_ point during the
5096 * actual truncate. Once we do the truncate_setsize we could
5097 * invalidate pages which forces any outstanding ordered io to
5098 * be instantly completed which will give us extents that need
5099 * to be truncated. If we fail to get an orphan inode down we
5100 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005101 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005102 * will be consistent.
5103 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005104 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005105 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005106 if (ret)
5107 return ret;
5108
Josef Bacika41ad392011-01-31 15:30:16 -05005109 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5110 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005111
5112 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005113 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005114 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005115 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005116
Josef Bacika41ad392011-01-31 15:30:16 -05005117 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005118 if (ret && inode->i_nlink) {
5119 int err;
5120
Liu Bo19fd2df2016-12-01 13:01:02 -08005121 /* To get a stable disk_i_size */
5122 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5123 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005124 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005125 return err;
5126 }
5127
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005128 /*
5129 * failed to truncate, disk_i_size is only adjusted down
5130 * as we remove extents, so it should represent the true
5131 * size of the inode, so reset the in memory size and
5132 * delete our orphan entry.
5133 */
5134 trans = btrfs_join_transaction(root);
5135 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005136 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005137 return ret;
5138 }
5139 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005140 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005141 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005142 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005143 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005144 }
Yan, Zheng80825102009-11-12 09:35:36 +00005145 }
5146
Josef Bacika41ad392011-01-31 15:30:16 -05005147 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005148}
5149
Chris Mason39279cc2007-06-12 06:35:45 -04005150static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5151{
David Howells2b0143b2015-03-17 22:25:59 +00005152 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005153 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005154 int err;
5155
Li Zefanb83cc962010-12-20 16:04:08 +08005156 if (btrfs_root_readonly(root))
5157 return -EROFS;
5158
Jan Kara31051c82016-05-26 16:55:18 +02005159 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005160 if (err)
5161 return err;
5162
Chris Mason5a3f23d2009-03-31 13:27:11 -04005163 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005164 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005165 if (err)
5166 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005167 }
Yan Zheng9036c102008-10-30 14:19:41 -04005168
Christoph Hellwig10257742010-06-04 11:30:02 +02005169 if (attr->ia_valid) {
5170 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005171 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005172 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005173
Josef Bacik22c44fe2011-11-30 10:45:38 -05005174 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005175 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005176 }
5177
Chris Mason39279cc2007-06-12 06:35:45 -04005178 return err;
5179}
Chris Mason61295eb2008-01-14 16:24:38 -05005180
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005181/*
5182 * While truncating the inode pages during eviction, we get the VFS calling
5183 * btrfs_invalidatepage() against each page of the inode. This is slow because
5184 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5185 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5186 * extent_state structures over and over, wasting lots of time.
5187 *
5188 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5189 * those expensive operations on a per page basis and do only the ordered io
5190 * finishing, while we release here the extent_map and extent_state structures,
5191 * without the excessive merging and splitting.
5192 */
5193static void evict_inode_truncate_pages(struct inode *inode)
5194{
5195 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5196 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5197 struct rb_node *node;
5198
5199 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005200 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201
5202 write_lock(&map_tree->lock);
5203 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5204 struct extent_map *em;
5205
5206 node = rb_first(&map_tree->map);
5207 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005208 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5209 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005210 remove_extent_mapping(map_tree, em);
5211 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005212 if (need_resched()) {
5213 write_unlock(&map_tree->lock);
5214 cond_resched();
5215 write_lock(&map_tree->lock);
5216 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217 }
5218 write_unlock(&map_tree->lock);
5219
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 /*
5221 * Keep looping until we have no more ranges in the io tree.
5222 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005223 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5224 * still in progress (unlocked the pages in the bio but did not yet
5225 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005226 * ranges can still be locked and eviction started because before
5227 * submitting those bios, which are executed by a separate task (work
5228 * queue kthread), inode references (inode->i_count) were not taken
5229 * (which would be dropped in the end io callback of each bio).
5230 * Therefore here we effectively end up waiting for those bios and
5231 * anyone else holding locked ranges without having bumped the inode's
5232 * reference count - if we don't do it, when they access the inode's
5233 * io_tree to unlock a range it may be too late, leading to an
5234 * use-after-free issue.
5235 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005236 spin_lock(&io_tree->lock);
5237 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5238 struct extent_state *state;
5239 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 u64 start;
5241 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242
5243 node = rb_first(&io_tree->state);
5244 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005245 start = state->start;
5246 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005247 spin_unlock(&io_tree->lock);
5248
David Sterbaff13db42015-12-03 14:30:40 +01005249 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005250
5251 /*
5252 * If still has DELALLOC flag, the extent didn't reach disk,
5253 * and its reserved space won't be freed by delayed_ref.
5254 * So we need to free its reserved space here.
5255 * (Refer to comment in btrfs_invalidatepage, case 2)
5256 *
5257 * Note, end is the bytenr of last byte, so we need + 1 here.
5258 */
5259 if (state->state & EXTENT_DELALLOC)
5260 btrfs_qgroup_free_data(inode, start, end - start + 1);
5261
Filipe Manana6ca07092015-05-26 00:55:42 +01005262 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 EXTENT_LOCKED | EXTENT_DIRTY |
5264 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5265 EXTENT_DEFRAG, 1, 1,
5266 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005267
Filipe Manana7064dd52014-08-08 02:47:05 +01005268 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269 spin_lock(&io_tree->lock);
5270 }
5271 spin_unlock(&io_tree->lock);
5272}
5273
Al Virobd555972010-06-07 11:35:40 -04005274void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005277 struct btrfs_trans_handle *trans;
5278 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005279 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005280 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005281 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005282 int ret;
5283
liubo1abe9b82011-03-24 11:18:59 +00005284 trace_btrfs_inode_evict(inode);
5285
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005286 if (!root) {
5287 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5288 return;
5289 }
5290
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005291 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005292
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005293 evict_inode_truncate_pages(inode);
5294
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005295 if (inode->i_nlink &&
5296 ((btrfs_root_refs(&root->root_item) != 0 &&
5297 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005298 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005299 goto no_delete;
5300
Chris Mason39279cc2007-06-12 06:35:45 -04005301 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005302 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005303 goto no_delete;
5304 }
Al Virobd555972010-06-07 11:35:40 -04005305 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005306 if (!special_file(inode->i_mode))
5307 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005308
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005309 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005310
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005311 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005312 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005313 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005314 goto no_delete;
5315 }
5316
Yan, Zheng76dda932009-09-21 16:00:26 -04005317 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005318 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5319 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005320 goto no_delete;
5321 }
5322
Nikolay Borisovaa790212017-01-10 20:35:40 +02005323 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005324 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005325 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005326 goto no_delete;
5327 }
5328
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005329 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005330 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005331 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005332 goto no_delete;
5333 }
Josef Bacik4a338542011-08-29 11:01:31 -04005334 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005335 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005336 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005337
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005338 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005339
Josef Bacik4289a662011-08-05 13:22:24 -04005340 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005341 * This is a bit simpler than btrfs_truncate since we've already
5342 * reserved our space for our orphan item in the unlink, so we just
5343 * need to reserve some slack space in case we add bytes and update
5344 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005345 */
Yan, Zheng80825102009-11-12 09:35:36 +00005346 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005347 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5348 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005349
Josef Bacik726c35f2011-09-26 15:46:06 -04005350 /*
5351 * Try and steal from the global reserve since we will
5352 * likely not use this space anyway, we want to try as
5353 * hard as possible to get this to work.
5354 */
5355 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005356 steal_from_global++;
5357 else
5358 steal_from_global = 0;
5359 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005360
Josef Bacik3bce8762015-02-24 12:35:51 -08005361 /*
5362 * steal_from_global == 0: we reserved stuff, hooray!
5363 * steal_from_global == 1: we didn't reserve stuff, boo!
5364 * steal_from_global == 2: we've committed, still not a lot of
5365 * room but maybe we'll have room in the global reserve this
5366 * time.
5367 * steal_from_global == 3: abandon all hope!
5368 */
5369 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005370 btrfs_warn(fs_info,
5371 "Could not get space for a delete, will truncate on mount %d",
5372 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005373 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005374 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005375 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005376 }
5377
Miao Xie0e8c36a2012-12-19 06:59:51 +00005378 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005379 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005380 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005381 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005382 goto no_delete;
5383 }
5384
Josef Bacik3bce8762015-02-24 12:35:51 -08005385 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005386 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005387 * sure there is room to do it, if not we need to commit and try
5388 * again.
5389 */
5390 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005391 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005392 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005393 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005394 else
5395 ret = -ENOSPC;
5396 }
5397
5398 /*
5399 * Couldn't steal from the global reserve, we have too much
5400 * pending stuff built up, commit the transaction and try it
5401 * again.
5402 */
5403 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005404 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005405 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005406 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005407 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005408 goto no_delete;
5409 }
5410 continue;
5411 } else {
5412 steal_from_global = 0;
5413 }
5414
Josef Bacik4289a662011-08-05 13:22:24 -04005415 trans->block_rsv = rsv;
5416
Yan, Zhengd68fc572010-05-16 10:49:58 -04005417 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005418 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005419 break;
5420
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005421 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005422 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005423 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005424 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005425 }
5426
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005427 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005428
Josef Bacik4ef31a42013-08-13 14:10:08 -04005429 /*
5430 * Errors here aren't a big deal, it just means we leave orphan items
5431 * in the tree. They will be cleaned up on the next mount.
5432 */
Yan, Zheng80825102009-11-12 09:35:36 +00005433 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005434 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005435 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005436 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005437 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005438 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005439
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005440 trans->block_rsv = &fs_info->trans_block_rsv;
5441 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005442 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005443 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005444
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005445 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005446 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005447no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005448 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005449 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005450}
5451
5452/*
5453 * this returns the key found in the dir entry in the location pointer.
5454 * If no dir entries were found, location->objectid is 0.
5455 */
5456static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5457 struct btrfs_key *location)
5458{
5459 const char *name = dentry->d_name.name;
5460 int namelen = dentry->d_name.len;
5461 struct btrfs_dir_item *di;
5462 struct btrfs_path *path;
5463 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005464 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005465
5466 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005467 if (!path)
5468 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005469
David Sterbaf85b7372017-01-20 14:54:07 +01005470 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5471 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005472 if (IS_ERR(di))
5473 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005474
David Sterbac7040052011-04-19 18:00:01 +02005475 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005476 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005477
Chris Mason5f39d392007-10-15 16:14:19 -04005478 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005479out:
Chris Mason39279cc2007-06-12 06:35:45 -04005480 btrfs_free_path(path);
5481 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005482out_err:
5483 location->objectid = 0;
5484 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005485}
5486
5487/*
5488 * when we hit a tree root in a directory, the btrfs part of the inode
5489 * needs to be changed to reflect the root directory of the tree root. This
5490 * is kind of like crossing a mount point.
5491 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005492static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 struct inode *dir,
5494 struct dentry *dentry,
5495 struct btrfs_key *location,
5496 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005497{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 struct btrfs_path *path;
5499 struct btrfs_root *new_root;
5500 struct btrfs_root_ref *ref;
5501 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005502 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 int ret;
5504 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005505
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 path = btrfs_alloc_path();
5507 if (!path) {
5508 err = -ENOMEM;
5509 goto out;
5510 }
Chris Mason39279cc2007-06-12 06:35:45 -04005511
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005513 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5514 key.type = BTRFS_ROOT_REF_KEY;
5515 key.offset = location->objectid;
5516
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005517 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 if (ret) {
5519 if (ret < 0)
5520 err = ret;
5521 goto out;
5522 }
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 leaf = path->nodes[0];
5525 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005526 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5528 goto out;
5529
5530 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5531 (unsigned long)(ref + 1),
5532 dentry->d_name.len);
5533 if (ret)
5534 goto out;
5535
David Sterbab3b4aa72011-04-21 01:20:15 +02005536 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005537
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005538 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 if (IS_ERR(new_root)) {
5540 err = PTR_ERR(new_root);
5541 goto out;
5542 }
5543
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 *sub_root = new_root;
5545 location->objectid = btrfs_root_dirid(&new_root->root_item);
5546 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005547 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 err = 0;
5549out:
5550 btrfs_free_path(path);
5551 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005552}
5553
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554static void inode_tree_add(struct inode *inode)
5555{
5556 struct btrfs_root *root = BTRFS_I(inode)->root;
5557 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005558 struct rb_node **p;
5559 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005560 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005561 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005562
Al Viro1d3382cb2010-10-23 15:19:20 -04005563 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005564 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005565 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005567 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 while (*p) {
5569 parent = *p;
5570 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5571
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005572 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005573 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005574 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005575 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 else {
5577 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005578 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005579 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005580 RB_CLEAR_NODE(parent);
5581 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005582 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583 }
5584 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005585 rb_link_node(new, parent, p);
5586 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 spin_unlock(&root->inode_lock);
5588}
5589
5590static void inode_tree_del(struct inode *inode)
5591{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005592 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005593 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005594 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005596 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005600 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005602 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005603
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005604 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005605 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005606 spin_lock(&root->inode_lock);
5607 empty = RB_EMPTY_ROOT(&root->inode_tree);
5608 spin_unlock(&root->inode_lock);
5609 if (empty)
5610 btrfs_add_dead_root(root);
5611 }
5612}
5613
Jeff Mahoney143bede2012-03-01 14:56:26 +01005614void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005615{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005616 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005617 struct rb_node *node;
5618 struct rb_node *prev;
5619 struct btrfs_inode *entry;
5620 struct inode *inode;
5621 u64 objectid = 0;
5622
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005623 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005624 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005625
5626 spin_lock(&root->inode_lock);
5627again:
5628 node = root->inode_tree.rb_node;
5629 prev = NULL;
5630 while (node) {
5631 prev = node;
5632 entry = rb_entry(node, struct btrfs_inode, rb_node);
5633
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005634 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005635 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005636 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005637 node = node->rb_right;
5638 else
5639 break;
5640 }
5641 if (!node) {
5642 while (prev) {
5643 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005644 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005645 node = prev;
5646 break;
5647 }
5648 prev = rb_next(prev);
5649 }
5650 }
5651 while (node) {
5652 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005653 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005654 inode = igrab(&entry->vfs_inode);
5655 if (inode) {
5656 spin_unlock(&root->inode_lock);
5657 if (atomic_read(&inode->i_count) > 1)
5658 d_prune_aliases(inode);
5659 /*
Al Viro45321ac2010-06-07 13:43:19 -04005660 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005661 * the inode cache when its usage count
5662 * hits zero.
5663 */
5664 iput(inode);
5665 cond_resched();
5666 spin_lock(&root->inode_lock);
5667 goto again;
5668 }
5669
5670 if (cond_resched_lock(&root->inode_lock))
5671 goto again;
5672
5673 node = rb_next(node);
5674 }
5675 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005676}
5677
Chris Masone02119d2008-09-05 16:13:11 -04005678static int btrfs_init_locked_inode(struct inode *inode, void *p)
5679{
5680 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005681 inode->i_ino = args->location->objectid;
5682 memcpy(&BTRFS_I(inode)->location, args->location,
5683 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005684 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005685 return 0;
5686}
5687
5688static int btrfs_find_actor(struct inode *inode, void *opaque)
5689{
5690 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005691 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005692 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005693}
5694
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005695static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005696 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005698{
5699 struct inode *inode;
5700 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005701 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005702
Chris Mason90d3e592014-01-09 17:28:00 -08005703 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005704 args.root = root;
5705
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005706 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005707 btrfs_init_locked_inode,
5708 (void *)&args);
5709 return inode;
5710}
5711
Balaji Rao1a54ef82008-07-21 02:01:04 +05305712/* Get an inode object given its location and corresponding root.
5713 * Returns in *is_new if the inode was read from disk
5714 */
5715struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005716 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305717{
5718 struct inode *inode;
5719
Chris Mason90d3e592014-01-09 17:28:00 -08005720 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305721 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005722 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305723
5724 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005725 int ret;
5726
5727 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005728 if (!is_bad_inode(inode)) {
5729 inode_tree_add(inode);
5730 unlock_new_inode(inode);
5731 if (new)
5732 *new = 1;
5733 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005734 unlock_new_inode(inode);
5735 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005736 ASSERT(ret < 0);
5737 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005738 }
5739 }
5740
Balaji Rao1a54ef82008-07-21 02:01:04 +05305741 return inode;
5742}
5743
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744static struct inode *new_simple_dir(struct super_block *s,
5745 struct btrfs_key *key,
5746 struct btrfs_root *root)
5747{
5748 struct inode *inode = new_inode(s);
5749
5750 if (!inode)
5751 return ERR_PTR(-ENOMEM);
5752
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005753 BTRFS_I(inode)->root = root;
5754 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005755 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
5757 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005758 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005759 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760 inode->i_fop = &simple_dir_operations;
5761 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005762 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305763 inode->i_atime = inode->i_mtime;
5764 inode->i_ctime = inode->i_mtime;
5765 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005766
5767 return inode;
5768}
5769
Chris Mason3de45862008-11-17 21:02:50 -05005770struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005772 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005773 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005774 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005775 struct btrfs_root *sub_root = root;
5776 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005777 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005778 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005779
5780 if (dentry->d_name.len > BTRFS_NAME_LEN)
5781 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005782
Jeff Layton39e3c952012-11-28 11:30:53 -05005783 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 if (ret < 0)
5785 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005786
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005787 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005788 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789
5790 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005791 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005792 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005793 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794
5795 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5796
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005797 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005798 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799 &location, &sub_root);
5800 if (ret < 0) {
5801 if (ret != -ENOENT)
5802 inode = ERR_PTR(ret);
5803 else
5804 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5805 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005806 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005808 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005809
Julia Lawall34d19ba2011-01-24 19:55:19 +00005810 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005811 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005812 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005813 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005814 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005815 if (ret) {
5816 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005817 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005818 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005819 }
5820
Chris Mason3de45862008-11-17 21:02:50 -05005821 return inode;
5822}
5823
Nick Pigginfe15ce42011-01-07 17:49:23 +11005824static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005825{
5826 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005827 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005828
Li Zefan848cce02012-02-21 17:04:28 +08005829 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005830 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005831
Li Zefan848cce02012-02-21 17:04:28 +08005832 if (inode) {
5833 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005834 if (btrfs_root_refs(&root->root_item) == 0)
5835 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005836
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005837 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005838 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005839 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005840 return 0;
5841}
5842
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005843static void btrfs_dentry_release(struct dentry *dentry)
5844{
Daeseok Youn944a4512014-04-14 15:37:02 +09005845 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005846}
5847
Chris Mason3de45862008-11-17 21:02:50 -05005848static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005849 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005850{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005851 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005852
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005853 inode = btrfs_lookup_dentry(dir, dentry);
5854 if (IS_ERR(inode)) {
5855 if (PTR_ERR(inode) == -ENOENT)
5856 inode = NULL;
5857 else
5858 return ERR_CAST(inode);
5859 }
5860
Al Viro41d28bc2014-10-12 22:24:21 -04005861 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005862}
5863
Miao Xie16cdcec2011-04-22 18:12:22 +08005864unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005865 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5866};
5867
Al Viro9cdda8d2013-05-22 16:48:09 -04005868static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005869{
Al Viro9cdda8d2013-05-22 16:48:09 -04005870 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005871 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005872 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005873 struct btrfs_dir_item *di;
5874 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005875 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005876 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005877 struct list_head ins_list;
5878 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005879 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005880 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005881 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005882 unsigned char d_type;
5883 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005884 char tmp_name[32];
5885 char *name_ptr;
5886 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005887 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005888 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005889
Al Viro9cdda8d2013-05-22 16:48:09 -04005890 if (!dir_emit_dots(file, ctx))
5891 return 0;
5892
David Woodhouse49593bf2008-08-17 17:08:36 +01005893 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005894 if (!path)
5895 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005896
David Sterbae4058b52015-11-27 16:31:35 +01005897 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005898
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005899 INIT_LIST_HEAD(&ins_list);
5900 INIT_LIST_HEAD(&del_list);
5901 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005902
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005903 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005904 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005905 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005906
Chris Mason39279cc2007-06-12 06:35:45 -04005907 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5908 if (ret < 0)
5909 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005910
5911 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005912 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005913 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005914 if (slot >= btrfs_header_nritems(leaf)) {
5915 ret = btrfs_next_leaf(root, path);
5916 if (ret < 0)
5917 goto err;
5918 else if (ret > 0)
5919 break;
5920 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005921 }
Chris Mason3de45862008-11-17 21:02:50 -05005922
Chris Mason5f39d392007-10-15 16:14:19 -04005923 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5924
5925 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005926 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005927 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005928 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005929 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005930 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005931 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005932 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005933
Al Viro9cdda8d2013-05-22 16:48:09 -04005934 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005935
Chris Mason39279cc2007-06-12 06:35:45 -04005936 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005937 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005938 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005939
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005940 name_len = btrfs_dir_name_len(leaf, di);
5941 if (name_len <= sizeof(tmp_name)) {
5942 name_ptr = tmp_name;
5943 } else {
5944 name_ptr = kmalloc(name_len, GFP_KERNEL);
5945 if (!name_ptr) {
5946 ret = -ENOMEM;
5947 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005948 }
Chris Mason39279cc2007-06-12 06:35:45 -04005949 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005950 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5951 name_len);
5952
5953 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5954 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5955
5956 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5957 d_type);
5958
5959 if (name_ptr != tmp_name)
5960 kfree(name_ptr);
5961
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005962 if (over)
5963 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005964 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005965next:
5966 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005967 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005968
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005969 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005970 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005971 goto nopos;
5972
Zach Browndb62efb2013-07-11 16:19:42 -07005973 /*
5974 * Stop new entries from being returned after we return the last
5975 * entry.
5976 *
5977 * New directory entries are assigned a strictly increasing
5978 * offset. This means that new entries created during readdir
5979 * are *guaranteed* to be seen in the future by that readdir.
5980 * This has broken buggy programs which operate on names as
5981 * they're returned by readdir. Until we re-use freed offsets
5982 * we have this hack to stop new entries from being returned
5983 * under the assumption that they'll never reach this huge
5984 * offset.
5985 *
5986 * This is being careful not to overflow 32bit loff_t unless the
5987 * last entry requires it because doing so has broken 32bit apps
5988 * in the past.
5989 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005990 if (ctx->pos >= INT_MAX)
5991 ctx->pos = LLONG_MAX;
5992 else
5993 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005994nopos:
5995 ret = 0;
5996err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005997 if (put)
5998 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005999 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006000 return ret;
6001}
6002
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006003int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006004{
6005 struct btrfs_root *root = BTRFS_I(inode)->root;
6006 struct btrfs_trans_handle *trans;
6007 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006008 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006009
Josef Bacik72ac3c02012-05-23 14:13:11 -04006010 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006011 return 0;
6012
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006013 if (btrfs_fs_closing(root->fs_info) &&
6014 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006015 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006016
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006017 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006018 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006019 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006020 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006021 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006022 if (IS_ERR(trans))
6023 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006024 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006025 }
6026 return ret;
6027}
6028
6029/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006030 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006031 * inode changes. But, it is most likely to find the inode in cache.
6032 * FIXME, needs more benchmarking...there are no reasons other than performance
6033 * to keep or drop this code.
6034 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006035static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006036{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006037 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006038 struct btrfs_root *root = BTRFS_I(inode)->root;
6039 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006040 int ret;
6041
Josef Bacik72ac3c02012-05-23 14:13:11 -04006042 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006043 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006044
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006045 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006046 if (IS_ERR(trans))
6047 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006048
6049 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006050 if (ret && ret == -ENOSPC) {
6051 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006052 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006053 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006054 if (IS_ERR(trans))
6055 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006056
Chris Mason94b60442010-05-26 11:02:00 -04006057 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006058 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006059 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006060 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006061 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006062
6063 return ret;
6064}
6065
6066/*
6067 * This is a copy of file_update_time. We need this so we can return error on
6068 * ENOSPC for updating the inode in the case of file write and mmap writes.
6069 */
Josef Bacike41f9412012-03-26 09:46:47 -04006070static int btrfs_update_time(struct inode *inode, struct timespec *now,
6071 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006072{
Alexander Block2bc5565282012-06-15 09:49:33 +02006073 struct btrfs_root *root = BTRFS_I(inode)->root;
6074
6075 if (btrfs_root_readonly(root))
6076 return -EROFS;
6077
Josef Bacike41f9412012-03-26 09:46:47 -04006078 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006079 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006080 if (flags & S_CTIME)
6081 inode->i_ctime = *now;
6082 if (flags & S_MTIME)
6083 inode->i_mtime = *now;
6084 if (flags & S_ATIME)
6085 inode->i_atime = *now;
6086 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006087}
6088
Chris Masond352ac62008-09-29 15:18:18 -04006089/*
6090 * find the highest existing sequence number in a directory
6091 * and then set the in-memory index_cnt variable to reflect
6092 * free sequence numbers
6093 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006094static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006095{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006096 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006097 struct btrfs_key key, found_key;
6098 struct btrfs_path *path;
6099 struct extent_buffer *leaf;
6100 int ret;
6101
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006102 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006103 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006104 key.offset = (u64)-1;
6105
6106 path = btrfs_alloc_path();
6107 if (!path)
6108 return -ENOMEM;
6109
6110 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6111 if (ret < 0)
6112 goto out;
6113 /* FIXME: we should be able to handle this */
6114 if (ret == 0)
6115 goto out;
6116 ret = 0;
6117
6118 /*
6119 * MAGIC NUMBER EXPLANATION:
6120 * since we search a directory based on f_pos we have to start at 2
6121 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6122 * else has to start at 2
6123 */
6124 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006125 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006126 goto out;
6127 }
6128
6129 path->slots[0]--;
6130
6131 leaf = path->nodes[0];
6132 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6133
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006134 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006135 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006136 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006137 goto out;
6138 }
6139
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006140 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006141out:
6142 btrfs_free_path(path);
6143 return ret;
6144}
6145
Chris Masond352ac62008-09-29 15:18:18 -04006146/*
6147 * helper to find a free sequence number in a given directory. This current
6148 * code is very simple, later versions will do smarter things in the btree
6149 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006150int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006151{
6152 int ret = 0;
6153
Nikolay Borisov877574e2017-02-20 13:50:33 +02006154 if (dir->index_cnt == (u64)-1) {
6155 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006156 if (ret) {
6157 ret = btrfs_set_inode_index_count(dir);
6158 if (ret)
6159 return ret;
6160 }
Josef Bacikaec74772008-07-24 12:12:38 -04006161 }
6162
Nikolay Borisov877574e2017-02-20 13:50:33 +02006163 *index = dir->index_cnt;
6164 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006165
6166 return ret;
6167}
6168
Chris Masonb0d5d102014-09-08 13:08:51 -07006169static int btrfs_insert_inode_locked(struct inode *inode)
6170{
6171 struct btrfs_iget_args args;
6172 args.location = &BTRFS_I(inode)->location;
6173 args.root = BTRFS_I(inode)->root;
6174
6175 return insert_inode_locked4(inode,
6176 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6177 btrfs_find_actor, &args);
6178}
6179
Chris Mason39279cc2007-06-12 06:35:45 -04006180static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6181 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006182 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006183 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006184 u64 ref_objectid, u64 objectid,
6185 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006186{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006187 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006188 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006189 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006190 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006191 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006192 struct btrfs_inode_ref *ref;
6193 struct btrfs_key key[2];
6194 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006195 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006196 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006197 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006198
Chris Mason5f39d392007-10-15 16:14:19 -04006199 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006200 if (!path)
6201 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006202
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006203 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006204 if (!inode) {
6205 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006206 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006207 }
Chris Mason39279cc2007-06-12 06:35:45 -04006208
Li Zefan581bb052011-04-20 10:06:11 +08006209 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006210 * O_TMPFILE, set link count to 0, so that after this point,
6211 * we fill in an inode item with the correct link count.
6212 */
6213 if (!name)
6214 set_nlink(inode, 0);
6215
6216 /*
Li Zefan581bb052011-04-20 10:06:11 +08006217 * we have to initialize this early, so we can reclaim the inode
6218 * number if we fail afterwards in this function.
6219 */
6220 inode->i_ino = objectid;
6221
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006222 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006223 trace_btrfs_inode_request(dir);
6224
Nikolay Borisov877574e2017-02-20 13:50:33 +02006225 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006226 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006227 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006228 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006229 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006230 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006231 } else if (dir) {
6232 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006233 }
6234 /*
6235 * index_cnt is ignored for everything but a dir,
6236 * btrfs_get_inode_index_count has an explanation for the magic
6237 * number
6238 */
6239 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006240 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006241 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006242 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006243 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006244
Josef Bacik5dc562c2012-08-17 13:14:17 -04006245 /*
6246 * We could have gotten an inode number from somebody who was fsynced
6247 * and then removed in this same transaction, so let's just set full
6248 * sync since it will be a full sync anyway and this will blow away the
6249 * old info in the log.
6250 */
6251 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6252
Chris Mason9c583092008-01-29 15:15:18 -05006253 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006254 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006255 key[0].offset = 0;
6256
Chris Mason9c583092008-01-29 15:15:18 -05006257 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006258
6259 if (name) {
6260 /*
6261 * Start new inodes with an inode_ref. This is slightly more
6262 * efficient for small numbers of hard links since they will
6263 * be packed into one item. Extended refs will kick in if we
6264 * add more hard links than can fit in the ref item.
6265 */
6266 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006267 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006268 key[1].offset = ref_objectid;
6269
6270 sizes[1] = name_len + sizeof(*ref);
6271 }
Chris Mason9c583092008-01-29 15:15:18 -05006272
Chris Masonb0d5d102014-09-08 13:08:51 -07006273 location = &BTRFS_I(inode)->location;
6274 location->objectid = objectid;
6275 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006276 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006277
6278 ret = btrfs_insert_inode_locked(inode);
6279 if (ret < 0)
6280 goto fail;
6281
Chris Masonb9473432009-03-13 11:00:37 -04006282 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006283 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006284 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006285 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006286
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006287 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006288 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306289
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006290 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306291 inode->i_atime = inode->i_mtime;
6292 inode->i_ctime = inode->i_mtime;
6293 BTRFS_I(inode)->i_otime = inode->i_mtime;
6294
Chris Mason5f39d392007-10-15 16:14:19 -04006295 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6296 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006297 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006298 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006299 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006300
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006301 if (name) {
6302 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6303 struct btrfs_inode_ref);
6304 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6305 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6306 ptr = (unsigned long)(ref + 1);
6307 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6308 }
Chris Mason9c583092008-01-29 15:15:18 -05006309
Chris Mason5f39d392007-10-15 16:14:19 -04006310 btrfs_mark_buffer_dirty(path->nodes[0]);
6311 btrfs_free_path(path);
6312
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006313 btrfs_inherit_iflags(inode, dir);
6314
Al Viro569254b2011-07-24 17:08:40 -04006315 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006316 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006317 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006318 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006319 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6320 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006321 }
6322
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006323 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006324
6325 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006326 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006327
Alexander Block8ea05e32012-07-25 17:35:53 +02006328 btrfs_update_root_times(trans, root);
6329
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006330 ret = btrfs_inode_inherit_props(trans, inode, dir);
6331 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006332 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006333 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006334 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006335
Chris Mason39279cc2007-06-12 06:35:45 -04006336 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006337
6338fail_unlock:
6339 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006340fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006341 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006342 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006343 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006344 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006345 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006346}
6347
6348static inline u8 btrfs_inode_type(struct inode *inode)
6349{
6350 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6351}
6352
Chris Masond352ac62008-09-29 15:18:18 -04006353/*
6354 * utility function to add 'inode' into 'parent_inode' with
6355 * a give name and a given sequence number.
6356 * if 'add_backref' is true, also insert a backref from the
6357 * inode to the parent directory.
6358 */
Chris Masone02119d2008-09-05 16:13:11 -04006359int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006360 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006361 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006362{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006363 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006364 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006365 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006366 struct btrfs_root *root = parent_inode->root;
6367 u64 ino = btrfs_ino(inode);
6368 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006369
Li Zefan33345d012011-04-20 10:31:50 +08006370 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006371 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006372 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006373 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006374 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006375 key.offset = 0;
6376 }
Chris Mason39279cc2007-06-12 06:35:45 -04006377
Li Zefan33345d012011-04-20 10:31:50 +08006378 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006379 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6380 root->root_key.objectid, parent_ino,
6381 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006382 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006383 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6384 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006385 }
6386
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006387 /* Nothing to clean up yet */
6388 if (ret)
6389 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006390
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006391 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6392 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006393 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006394 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006395 goto fail_dir_item;
6396 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006397 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006398 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006399 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006400
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006401 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006402 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006403 inode_inc_iversion(&parent_inode->vfs_inode);
6404 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6405 current_time(&parent_inode->vfs_inode);
6406 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006407 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006408 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006409 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006410
6411fail_dir_item:
6412 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6413 u64 local_index;
6414 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006415 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6416 root->root_key.objectid, parent_ino,
6417 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006418
6419 } else if (add_backref) {
6420 u64 local_index;
6421 int err;
6422
6423 err = btrfs_del_inode_ref(trans, root, name, name_len,
6424 ino, parent_ino, &local_index);
6425 }
6426 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006427}
6428
6429static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006430 struct btrfs_inode *dir, struct dentry *dentry,
6431 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006432{
Josef Bacika1b075d2010-11-19 20:36:11 +00006433 int err = btrfs_add_link(trans, dir, inode,
6434 dentry->d_name.name, dentry->d_name.len,
6435 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006436 if (err > 0)
6437 err = -EEXIST;
6438 return err;
6439}
6440
Josef Bacik618e21d2007-07-11 10:18:17 -04006441static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006442 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006443{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006444 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006445 struct btrfs_trans_handle *trans;
6446 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006447 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006448 int err;
6449 int drop_inode = 0;
6450 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006451 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006452
Josef Bacik9ed74f22009-09-11 16:12:44 -04006453 /*
6454 * 2 for inode item and ref
6455 * 2 for dir items
6456 * 1 for xattr if selinux is on
6457 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006458 trans = btrfs_start_transaction(root, 5);
6459 if (IS_ERR(trans))
6460 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006461
Li Zefan581bb052011-04-20 10:06:11 +08006462 err = btrfs_find_free_ino(root, &objectid);
6463 if (err)
6464 goto out_unlock;
6465
Josef Bacikaec74772008-07-24 12:12:38 -04006466 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006467 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6468 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006469 if (IS_ERR(inode)) {
6470 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006471 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006472 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006473
Casey Schauflerad19db72011-12-15 10:09:07 -05006474 /*
6475 * If the active LSM wants to access the inode during
6476 * d_instantiate it needs these. Smack checks to see
6477 * if the filesystem supports xattrs by looking at the
6478 * ops vector.
6479 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006480 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006481 init_special_inode(inode, inode->i_mode, rdev);
6482
6483 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006484 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006485 goto out_unlock_inode;
6486
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006487 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6488 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006489 if (err) {
6490 goto out_unlock_inode;
6491 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006492 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006493 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006494 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006495 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006496
Josef Bacik618e21d2007-07-11 10:18:17 -04006497out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006498 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006499 btrfs_balance_delayed_items(fs_info);
6500 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006501 if (drop_inode) {
6502 inode_dec_link_count(inode);
6503 iput(inode);
6504 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006505 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006506
6507out_unlock_inode:
6508 drop_inode = 1;
6509 unlock_new_inode(inode);
6510 goto out_unlock;
6511
Josef Bacik618e21d2007-07-11 10:18:17 -04006512}
6513
Chris Mason39279cc2007-06-12 06:35:45 -04006514static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006515 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006516{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006517 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006518 struct btrfs_trans_handle *trans;
6519 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006520 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006521 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006522 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006523 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006524 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006525
Josef Bacik9ed74f22009-09-11 16:12:44 -04006526 /*
6527 * 2 for inode item and ref
6528 * 2 for dir items
6529 * 1 for xattr if selinux is on
6530 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006531 trans = btrfs_start_transaction(root, 5);
6532 if (IS_ERR(trans))
6533 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006534
Li Zefan581bb052011-04-20 10:06:11 +08006535 err = btrfs_find_free_ino(root, &objectid);
6536 if (err)
6537 goto out_unlock;
6538
Josef Bacikaec74772008-07-24 12:12:38 -04006539 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006540 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6541 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006542 if (IS_ERR(inode)) {
6543 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006544 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006545 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006546 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006547 /*
6548 * If the active LSM wants to access the inode during
6549 * d_instantiate it needs these. Smack checks to see
6550 * if the filesystem supports xattrs by looking at the
6551 * ops vector.
6552 */
6553 inode->i_fop = &btrfs_file_operations;
6554 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006555 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006556
6557 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6558 if (err)
6559 goto out_unlock_inode;
6560
6561 err = btrfs_update_inode(trans, root, inode);
6562 if (err)
6563 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006564
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006565 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6566 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006567 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006568 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006569
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006570 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006571 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006572 d_instantiate(dentry, inode);
6573
Chris Mason39279cc2007-06-12 06:35:45 -04006574out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006575 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006576 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006577 inode_dec_link_count(inode);
6578 iput(inode);
6579 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006580 btrfs_balance_delayed_items(fs_info);
6581 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006582 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006583
6584out_unlock_inode:
6585 unlock_new_inode(inode);
6586 goto out_unlock;
6587
Chris Mason39279cc2007-06-12 06:35:45 -04006588}
6589
6590static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6591 struct dentry *dentry)
6592{
Filipe Manana271dba42016-01-05 16:24:05 +00006593 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006594 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006595 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006596 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006597 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006598 int err;
6599 int drop_inode = 0;
6600
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006601 /* do not allow sys_link's with other subvols of the same device */
6602 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006603 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006604
Mark Fashehf1863732012-08-08 11:32:27 -07006605 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006606 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006607
Nikolay Borisov877574e2017-02-20 13:50:33 +02006608 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006609 if (err)
6610 goto fail;
6611
Yan, Zhenga22285a2010-05-16 10:48:46 -04006612 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006613 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006614 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006615 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006616 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006617 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006618 if (IS_ERR(trans)) {
6619 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006620 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006621 goto fail;
6622 }
Chris Mason5f39d392007-10-15 16:14:19 -04006623
Miao Xie67de1172013-12-26 13:07:06 +08006624 /* There are several dir indexes for this inode, clear the cache. */
6625 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006626 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006627 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006628 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006629 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006630 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006631
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006632 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6633 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006634
Yan, Zhenga5719522009-09-24 09:17:31 -04006635 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006636 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006637 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006638 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006639 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006640 if (err)
6641 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006642 if (inode->i_nlink == 1) {
6643 /*
6644 * If new hard link count is 1, it's a file created
6645 * with open(2) O_TMPFILE flag.
6646 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006647 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006648 if (err)
6649 goto fail;
6650 }
Al Viro08c422c2011-12-23 07:58:13 -05006651 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006652 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006653 }
Chris Mason39279cc2007-06-12 06:35:45 -04006654
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006655 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006656fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006657 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006658 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006659 if (drop_inode) {
6660 inode_dec_link_count(inode);
6661 iput(inode);
6662 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006663 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006664 return err;
6665}
6666
Al Viro18bb1db2011-07-26 01:41:39 -04006667static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006668{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006669 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006670 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006671 struct btrfs_trans_handle *trans;
6672 struct btrfs_root *root = BTRFS_I(dir)->root;
6673 int err = 0;
6674 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006675 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006676 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006677
Josef Bacik9ed74f22009-09-11 16:12:44 -04006678 /*
6679 * 2 items for inode and ref
6680 * 2 items for dir items
6681 * 1 for xattr if selinux is on
6682 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006683 trans = btrfs_start_transaction(root, 5);
6684 if (IS_ERR(trans))
6685 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006686
Li Zefan581bb052011-04-20 10:06:11 +08006687 err = btrfs_find_free_ino(root, &objectid);
6688 if (err)
6689 goto out_fail;
6690
Josef Bacikaec74772008-07-24 12:12:38 -04006691 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006692 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6693 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006694 if (IS_ERR(inode)) {
6695 err = PTR_ERR(inode);
6696 goto out_fail;
6697 }
Chris Mason5f39d392007-10-15 16:14:19 -04006698
Chris Mason39279cc2007-06-12 06:35:45 -04006699 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006700 /* these must be set before we unlock the inode */
6701 inode->i_op = &btrfs_dir_inode_operations;
6702 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006703
Eric Paris2a7dba32011-02-01 11:05:39 -05006704 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006705 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006706 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006707
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006708 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006709 err = btrfs_update_inode(trans, root, inode);
6710 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006711 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006712
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006713 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6714 dentry->d_name.name,
6715 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006716 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006717 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006718
Chris Mason39279cc2007-06-12 06:35:45 -04006719 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006720 /*
6721 * mkdir is special. We're unlocking after we call d_instantiate
6722 * to avoid a race with nfsd calling d_instantiate.
6723 */
6724 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006725 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006726
6727out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006728 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006729 if (drop_on_err) {
6730 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006731 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006732 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006733 btrfs_balance_delayed_items(fs_info);
6734 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006735 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006736
6737out_fail_inode:
6738 unlock_new_inode(inode);
6739 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006740}
6741
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006742/* Find next extent map of a given extent map, caller needs to ensure locks */
6743static struct extent_map *next_extent_map(struct extent_map *em)
6744{
6745 struct rb_node *next;
6746
6747 next = rb_next(&em->rb_node);
6748 if (!next)
6749 return NULL;
6750 return container_of(next, struct extent_map, rb_node);
6751}
6752
6753static struct extent_map *prev_extent_map(struct extent_map *em)
6754{
6755 struct rb_node *prev;
6756
6757 prev = rb_prev(&em->rb_node);
6758 if (!prev)
6759 return NULL;
6760 return container_of(prev, struct extent_map, rb_node);
6761}
6762
Chris Masond352ac62008-09-29 15:18:18 -04006763/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006764 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006765 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006766 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006767 */
Chris Mason3b951512008-04-17 11:29:12 -04006768static int merge_extent_mapping(struct extent_map_tree *em_tree,
6769 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006770 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006771 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006772{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006773 struct extent_map *prev;
6774 struct extent_map *next;
6775 u64 start;
6776 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006777 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006778
Chris Masone6dcd2d2008-07-17 12:53:50 -04006779 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006780
6781 if (existing->start > map_start) {
6782 next = existing;
6783 prev = prev_extent_map(next);
6784 } else {
6785 prev = existing;
6786 next = next_extent_map(prev);
6787 }
6788
6789 start = prev ? extent_map_end(prev) : em->start;
6790 start = max_t(u64, start, em->start);
6791 end = next ? next->start : extent_map_end(em);
6792 end = min_t(u64, end, extent_map_end(em));
6793 start_diff = start - em->start;
6794 em->start = start;
6795 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006796 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6797 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006798 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006799 em->block_len -= start_diff;
6800 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006801 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006802}
6803
Chris Masonc8b97812008-10-29 14:49:59 -04006804static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006805 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006806 size_t pg_offset, u64 extent_offset,
6807 struct btrfs_file_extent_item *item)
6808{
6809 int ret;
6810 struct extent_buffer *leaf = path->nodes[0];
6811 char *tmp;
6812 size_t max_size;
6813 unsigned long inline_size;
6814 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006815 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006816
6817 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006818 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006819 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6820 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006821 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006822 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006823 if (!tmp)
6824 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006825 ptr = btrfs_file_extent_inline_start(item);
6826
6827 read_extent_buffer(leaf, tmp, ptr, inline_size);
6828
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006829 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006830 ret = btrfs_decompress(compress_type, tmp, page,
6831 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006832
6833 /*
6834 * decompression code contains a memset to fill in any space between the end
6835 * of the uncompressed data and the end of max_size in case the decompressed
6836 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6837 * the end of an inline extent and the beginning of the next block, so we
6838 * cover that region here.
6839 */
6840
6841 if (max_size + pg_offset < PAGE_SIZE) {
6842 char *map = kmap(page);
6843 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6844 kunmap(page);
6845 }
Chris Masonc8b97812008-10-29 14:49:59 -04006846 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006847 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006848}
6849
Chris Masond352ac62008-09-29 15:18:18 -04006850/*
6851 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006852 * the ugly parts come from merging extents from the disk with the in-ram
6853 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006854 * where the in-ram extents might be locked pending data=ordered completion.
6855 *
6856 * This also copies inline extents directly into the page.
6857 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006858struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6859 struct page *page,
6860 size_t pg_offset, u64 start, u64 len,
6861 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006862{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006863 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006864 int ret;
6865 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006866 u64 extent_start = 0;
6867 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006868 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006869 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006870 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006871 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006872 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006873 struct extent_buffer *leaf;
6874 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006875 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006876 struct extent_map_tree *em_tree = &inode->extent_tree;
6877 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006878 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006879 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006880
Chris Masona52d9a82007-08-27 16:49:44 -04006881again:
Chris Mason890871b2009-09-02 16:24:52 -04006882 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006883 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006884 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006885 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006886 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006887
Chris Masona52d9a82007-08-27 16:49:44 -04006888 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006889 if (em->start > start || em->start + em->len <= start)
6890 free_extent_map(em);
6891 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006892 free_extent_map(em);
6893 else
6894 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006895 }
David Sterba172ddd62011-04-21 00:48:27 +02006896 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006897 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006898 err = -ENOMEM;
6899 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006900 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006901 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006902 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006903 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006904 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006905 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006906
6907 if (!path) {
6908 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006909 if (!path) {
6910 err = -ENOMEM;
6911 goto out;
6912 }
6913 /*
6914 * Chances are we'll be called again, so go ahead and do
6915 * readahead
6916 */
David Sterbae4058b52015-11-27 16:31:35 +01006917 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006918 }
6919
Chris Mason179e29e2007-11-01 11:28:41 -04006920 ret = btrfs_lookup_file_extent(trans, root, path,
6921 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 if (ret < 0) {
6923 err = ret;
6924 goto out;
6925 }
6926
6927 if (ret != 0) {
6928 if (path->slots[0] == 0)
6929 goto not_found;
6930 path->slots[0]--;
6931 }
6932
Chris Mason5f39d392007-10-15 16:14:19 -04006933 leaf = path->nodes[0];
6934 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006935 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006936 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006937 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006938 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006939 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006940 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006941 /*
6942 * If we backup past the first extent we want to move forward
6943 * and see if there is an extent in front of us, otherwise we'll
6944 * say there is a hole for our whole search range which can
6945 * cause problems.
6946 */
6947 extent_end = start;
6948 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006949 }
6950
Chris Mason5f39d392007-10-15 16:14:19 -04006951 found_type = btrfs_file_extent_type(leaf, item);
6952 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006953 if (found_type == BTRFS_FILE_EXTENT_REG ||
6954 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006955 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006956 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006957
6958 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6959 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006960 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6961 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006962 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006963 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006964 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006965
6966 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6967 path->slots[0],
6968 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006969 }
Josef Bacik25a50342013-10-14 12:08:38 -04006970next:
Yan Zheng9036c102008-10-30 14:19:41 -04006971 if (start >= extent_end) {
6972 path->slots[0]++;
6973 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6974 ret = btrfs_next_leaf(root, path);
6975 if (ret < 0) {
6976 err = ret;
6977 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006978 }
Yan Zheng9036c102008-10-30 14:19:41 -04006979 if (ret > 0)
6980 goto not_found;
6981 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006982 }
Yan Zheng9036c102008-10-30 14:19:41 -04006983 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6984 if (found_key.objectid != objectid ||
6985 found_key.type != BTRFS_EXTENT_DATA_KEY)
6986 goto not_found;
6987 if (start + len <= found_key.offset)
6988 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006989 if (start > found_key.offset)
6990 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006991 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006992 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006993 em->len = found_key.offset - start;
6994 goto not_found_em;
6995 }
6996
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006997 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006998 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006999
Yan Zhengd899e052008-10-30 14:25:28 -04007000 if (found_type == BTRFS_FILE_EXTENT_REG ||
7001 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007002 goto insert;
7003 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007004 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007005 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007006 size_t size;
7007 size_t extent_offset;
7008 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007009
Filipe Manana7ffbb592014-06-09 03:48:05 +01007010 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007011 goto out;
Yan689f9342007-10-29 11:41:07 -04007012
Chris Mason514ac8a2014-01-03 21:07:00 -08007013 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007014 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007015 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7016 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007017 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007018 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007019 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007020 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007021 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007022 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007023 if (btrfs_file_extent_compression(leaf, item) !=
7024 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007025 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007026 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007027 if (ret) {
7028 err = ret;
7029 goto out;
7030 }
Chris Masonc8b97812008-10-29 14:49:59 -04007031 } else {
7032 map = kmap(page);
7033 read_extent_buffer(leaf, map + pg_offset, ptr,
7034 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007035 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007036 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007037 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007038 copy_size);
7039 }
Chris Masonc8b97812008-10-29 14:49:59 -04007040 kunmap(page);
7041 }
Chris Mason179e29e2007-11-01 11:28:41 -04007042 flush_dcache_page(page);
7043 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007044 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007045 if (!trans) {
7046 kunmap(page);
7047 free_extent_map(em);
7048 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007049
David Sterbab3b4aa72011-04-21 01:20:15 +02007050 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007051 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007052
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007053 if (IS_ERR(trans))
7054 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007055 goto again;
7056 }
Chris Masonc8b97812008-10-29 14:49:59 -04007057 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007058 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007059 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007060 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007061 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007062 }
Chris Masond1310b22008-01-24 16:13:08 -05007063 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007064 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007065 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007066 }
7067not_found:
7068 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007069 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007070 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007071not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007072 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007073 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007074insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007075 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007076 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007077 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007078 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7079 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007080 err = -EIO;
7081 goto out;
7082 }
Chris Masond1310b22008-01-24 16:13:08 -05007083
7084 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007085 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007086 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007087 /* it is possible that someone inserted the extent into the tree
7088 * while we had the lock dropped. It is also possible that
7089 * an overlapping map exists in the tree
7090 */
Chris Masona52d9a82007-08-27 16:49:44 -04007091 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007092 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007093
7094 ret = 0;
7095
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007096 existing = search_extent_mapping(em_tree, start, len);
7097 /*
7098 * existing will always be non-NULL, since there must be
7099 * extent causing the -EEXIST.
7100 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007101 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007102 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007103 em->block_start == existing->block_start) {
7104 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007105 * The existing extent map already encompasses the
7106 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007107 */
7108 free_extent_map(em);
7109 em = existing;
7110 err = 0;
7111
7112 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007113 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007114 /*
7115 * The existing extent map is the one nearest to
7116 * the [start, start + len) range which overlaps
7117 */
7118 err = merge_extent_mapping(em_tree, existing,
7119 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007120 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007121 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007122 free_extent_map(em);
7123 em = NULL;
7124 }
7125 } else {
7126 free_extent_map(em);
7127 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007128 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007129 }
Chris Masona52d9a82007-08-27 16:49:44 -04007130 }
Chris Mason890871b2009-09-02 16:24:52 -04007131 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007132out:
liubo1abe9b82011-03-24 11:18:59 +00007133
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007134 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007135
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007136 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007137 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007138 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007139 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007140 err = ret;
7141 }
Chris Masona52d9a82007-08-27 16:49:44 -04007142 if (err) {
7143 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007144 return ERR_PTR(err);
7145 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007146 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007147 return em;
7148}
7149
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007150struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7151 struct page *page,
7152 size_t pg_offset, u64 start, u64 len,
7153 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007154{
7155 struct extent_map *em;
7156 struct extent_map *hole_em = NULL;
7157 u64 range_start = start;
7158 u64 end;
7159 u64 found;
7160 u64 found_end;
7161 int err = 0;
7162
7163 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7164 if (IS_ERR(em))
7165 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007166 /*
7167 * If our em maps to:
7168 * - a hole or
7169 * - a pre-alloc extent,
7170 * there might actually be delalloc bytes behind it.
7171 */
7172 if (em->block_start != EXTENT_MAP_HOLE &&
7173 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7174 return em;
7175 else
7176 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007177
7178 /* check to see if we've wrapped (len == -1 or similar) */
7179 end = start + len;
7180 if (end < start)
7181 end = (u64)-1;
7182 else
7183 end -= 1;
7184
7185 em = NULL;
7186
7187 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007188 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007189 end, len, EXTENT_DELALLOC, 1);
7190 found_end = range_start + found;
7191 if (found_end < range_start)
7192 found_end = (u64)-1;
7193
7194 /*
7195 * we didn't find anything useful, return
7196 * the original results from get_extent()
7197 */
7198 if (range_start > end || found_end <= start) {
7199 em = hole_em;
7200 hole_em = NULL;
7201 goto out;
7202 }
7203
7204 /* adjust the range_start to make sure it doesn't
7205 * go backwards from the start they passed in
7206 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307207 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007208 found = found_end - range_start;
7209
7210 if (found > 0) {
7211 u64 hole_start = start;
7212 u64 hole_len = len;
7213
David Sterba172ddd62011-04-21 00:48:27 +02007214 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007215 if (!em) {
7216 err = -ENOMEM;
7217 goto out;
7218 }
7219 /*
7220 * when btrfs_get_extent can't find anything it
7221 * returns one huge hole
7222 *
7223 * make sure what it found really fits our range, and
7224 * adjust to make sure it is based on the start from
7225 * the caller
7226 */
7227 if (hole_em) {
7228 u64 calc_end = extent_map_end(hole_em);
7229
7230 if (calc_end <= start || (hole_em->start > end)) {
7231 free_extent_map(hole_em);
7232 hole_em = NULL;
7233 } else {
7234 hole_start = max(hole_em->start, start);
7235 hole_len = calc_end - hole_start;
7236 }
7237 }
7238 em->bdev = NULL;
7239 if (hole_em && range_start > hole_start) {
7240 /* our hole starts before our delalloc, so we
7241 * have to return just the parts of the hole
7242 * that go until the delalloc starts
7243 */
7244 em->len = min(hole_len,
7245 range_start - hole_start);
7246 em->start = hole_start;
7247 em->orig_start = hole_start;
7248 /*
7249 * don't adjust block start at all,
7250 * it is fixed at EXTENT_MAP_HOLE
7251 */
7252 em->block_start = hole_em->block_start;
7253 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007254 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7255 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007256 } else {
7257 em->start = range_start;
7258 em->len = found;
7259 em->orig_start = range_start;
7260 em->block_start = EXTENT_MAP_DELALLOC;
7261 em->block_len = found;
7262 }
7263 } else if (hole_em) {
7264 return hole_em;
7265 }
7266out:
7267
7268 free_extent_map(hole_em);
7269 if (err) {
7270 free_extent_map(em);
7271 return ERR_PTR(err);
7272 }
7273 return em;
7274}
7275
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007276static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7277 const u64 start,
7278 const u64 len,
7279 const u64 orig_start,
7280 const u64 block_start,
7281 const u64 block_len,
7282 const u64 orig_block_len,
7283 const u64 ram_bytes,
7284 const int type)
7285{
7286 struct extent_map *em = NULL;
7287 int ret;
7288
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007289 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007290 em = create_io_em(inode, start, len, orig_start,
7291 block_start, block_len, orig_block_len,
7292 ram_bytes,
7293 BTRFS_COMPRESS_NONE, /* compress_type */
7294 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295 if (IS_ERR(em))
7296 goto out;
7297 }
7298 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7299 len, block_len, type);
7300 if (ret) {
7301 if (em) {
7302 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007303 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007304 start + len - 1, 0);
7305 }
7306 em = ERR_PTR(ret);
7307 }
7308 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007309
7310 return em;
7311}
7312
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7314 u64 start, u64 len)
7315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007317 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007318 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007319 struct btrfs_key ins;
7320 u64 alloc_hint;
7321 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007324 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007325 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007326 if (ret)
7327 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007329 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7330 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007331 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007332 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007333 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007334 btrfs_free_reserved_extent(fs_info, ins.objectid,
7335 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007336
Josef Bacik4b46fce2010-05-23 11:00:55 -04007337 return em;
7338}
7339
Chris Mason46bfbb52010-05-26 11:04:10 -04007340/*
7341 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7342 * block must be cow'd
7343 */
Josef Bacik00361582013-08-14 14:02:47 -04007344noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007345 u64 *orig_start, u64 *orig_block_len,
7346 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007347{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007348 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007349 struct btrfs_path *path;
7350 int ret;
7351 struct extent_buffer *leaf;
7352 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007353 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007354 struct btrfs_file_extent_item *fi;
7355 struct btrfs_key key;
7356 u64 disk_bytenr;
7357 u64 backref_offset;
7358 u64 extent_end;
7359 u64 num_bytes;
7360 int slot;
7361 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007362 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007363
Chris Mason46bfbb52010-05-26 11:04:10 -04007364 path = btrfs_alloc_path();
7365 if (!path)
7366 return -ENOMEM;
7367
David Sterbaf85b7372017-01-20 14:54:07 +01007368 ret = btrfs_lookup_file_extent(NULL, root, path,
7369 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007370 if (ret < 0)
7371 goto out;
7372
7373 slot = path->slots[0];
7374 if (ret == 1) {
7375 if (slot == 0) {
7376 /* can't find the item, must cow */
7377 ret = 0;
7378 goto out;
7379 }
7380 slot--;
7381 }
7382 ret = 0;
7383 leaf = path->nodes[0];
7384 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007385 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007386 key.type != BTRFS_EXTENT_DATA_KEY) {
7387 /* not our file or wrong item type, must cow */
7388 goto out;
7389 }
7390
7391 if (key.offset > offset) {
7392 /* Wrong offset, must cow */
7393 goto out;
7394 }
7395
7396 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7397 found_type = btrfs_file_extent_type(leaf, fi);
7398 if (found_type != BTRFS_FILE_EXTENT_REG &&
7399 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7400 /* not a regular extent, must cow */
7401 goto out;
7402 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007403
7404 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7405 goto out;
7406
Miao Xiee77751a2013-12-27 21:11:50 +08007407 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7408 if (extent_end <= offset)
7409 goto out;
7410
Chris Mason46bfbb52010-05-26 11:04:10 -04007411 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007412 if (disk_bytenr == 0)
7413 goto out;
7414
7415 if (btrfs_file_extent_compression(leaf, fi) ||
7416 btrfs_file_extent_encryption(leaf, fi) ||
7417 btrfs_file_extent_other_encoding(leaf, fi))
7418 goto out;
7419
Chris Mason46bfbb52010-05-26 11:04:10 -04007420 backref_offset = btrfs_file_extent_offset(leaf, fi);
7421
Josef Bacik7ee9e442013-06-21 16:37:03 -04007422 if (orig_start) {
7423 *orig_start = key.offset - backref_offset;
7424 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7425 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7426 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007427
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007428 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007429 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007430
7431 num_bytes = min(offset + *len, extent_end) - offset;
7432 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7433 u64 range_end;
7434
Jeff Mahoneyda170662016-06-15 09:22:56 -04007435 range_end = round_up(offset + num_bytes,
7436 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007437 ret = test_range_bit(io_tree, offset, range_end,
7438 EXTENT_DELALLOC, 0, NULL);
7439 if (ret) {
7440 ret = -EAGAIN;
7441 goto out;
7442 }
7443 }
7444
Josef Bacik1bda19e2013-10-18 12:10:36 -04007445 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007446
7447 /*
7448 * look for other files referencing this extent, if we
7449 * find any we must cow
7450 */
Josef Bacik00361582013-08-14 14:02:47 -04007451
Liu Boe4c3b2d2017-01-30 12:25:28 -08007452 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007453 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007454 if (ret) {
7455 ret = 0;
7456 goto out;
7457 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007458
7459 /*
7460 * adjust disk_bytenr and num_bytes to cover just the bytes
7461 * in this extent we are about to write. If there
7462 * are any csums in that range we have to cow in order
7463 * to keep the csums correct
7464 */
7465 disk_bytenr += backref_offset;
7466 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007467 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7468 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007469 /*
7470 * all of the above have passed, it is safe to overwrite this extent
7471 * without cow
7472 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007473 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 ret = 1;
7475out:
7476 btrfs_free_path(path);
7477 return ret;
7478}
7479
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007480bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7481{
7482 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007483 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007484 void **pagep = NULL;
7485 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007486 unsigned long start_idx;
7487 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007488
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007489 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007490
7491 /*
7492 * end is the last byte in the last page. end == start is legal
7493 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007494 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007495
7496 rcu_read_lock();
7497
7498 /* Most of the code in this while loop is lifted from
7499 * find_get_page. It's been modified to begin searching from a
7500 * page and return just the first page found in that range. If the
7501 * found idx is less than or equal to the end idx then we know that
7502 * a page exists. If no pages are found or if those pages are
7503 * outside of the range then we're fine (yay!) */
7504 while (page == NULL &&
7505 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7506 page = radix_tree_deref_slot(pagep);
7507 if (unlikely(!page))
7508 break;
7509
7510 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007511 if (radix_tree_deref_retry(page)) {
7512 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007513 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007514 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007515 /*
7516 * Otherwise, shmem/tmpfs must be storing a swap entry
7517 * here as an exceptional entry: so return it without
7518 * attempting to raise page count.
7519 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007520 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007521 break; /* TODO: Is this relevant for this use case? */
7522 }
7523
Filipe Manana91405152014-06-05 13:22:24 +01007524 if (!page_cache_get_speculative(page)) {
7525 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007526 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007527 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007528
7529 /*
7530 * Has the page moved?
7531 * This is part of the lockless pagecache protocol. See
7532 * include/linux/pagemap.h for details.
7533 */
7534 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007535 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007536 page = NULL;
7537 }
7538 }
7539
7540 if (page) {
7541 if (page->index <= end_idx)
7542 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007543 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007544 }
7545
7546 rcu_read_unlock();
7547 return found;
7548}
7549
Josef Bacikeb838e72012-07-31 16:28:48 -04007550static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7551 struct extent_state **cached_state, int writing)
7552{
7553 struct btrfs_ordered_extent *ordered;
7554 int ret = 0;
7555
7556 while (1) {
7557 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007558 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007559 /*
7560 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007561 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 * extents in this range.
7563 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007564 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 lockend - lockstart + 1);
7566
7567 /*
7568 * We need to make sure there are no buffered pages in this
7569 * range either, we could have raced between the invalidate in
7570 * generic_file_direct_write and locking the extent. The
7571 * invalidate needs to happen so that reads after a write do not
7572 * get stale data.
7573 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007574 if (!ordered &&
7575 (!writing ||
7576 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007577 break;
7578
7579 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7580 cached_state, GFP_NOFS);
7581
7582 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007583 /*
7584 * If we are doing a DIO read and the ordered extent we
7585 * found is for a buffered write, we can not wait for it
7586 * to complete and retry, because if we do so we can
7587 * deadlock with concurrent buffered writes on page
7588 * locks. This happens only if our DIO read covers more
7589 * than one extent map, if at this point has already
7590 * created an ordered extent for a previous extent map
7591 * and locked its range in the inode's io tree, and a
7592 * concurrent write against that previous extent map's
7593 * range and this range started (we unlock the ranges
7594 * in the io tree only when the bios complete and
7595 * buffered writes always lock pages before attempting
7596 * to lock range in the io tree).
7597 */
7598 if (writing ||
7599 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7600 btrfs_start_ordered_extent(inode, ordered, 1);
7601 else
7602 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007603 btrfs_put_ordered_extent(ordered);
7604 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007605 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007606 * We could trigger writeback for this range (and wait
7607 * for it to complete) and then invalidate the pages for
7608 * this range (through invalidate_inode_pages2_range()),
7609 * but that can lead us to a deadlock with a concurrent
7610 * call to readpages() (a buffered read or a defrag call
7611 * triggered a readahead) on a page lock due to an
7612 * ordered dio extent we created before but did not have
7613 * yet a corresponding bio submitted (whence it can not
7614 * complete), which makes readpages() wait for that
7615 * ordered extent to complete while holding a lock on
7616 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007617 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007618 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007619 }
7620
Filipe Mananaade77022016-02-18 14:28:55 +00007621 if (ret)
7622 break;
7623
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 cond_resched();
7625 }
7626
7627 return ret;
7628}
7629
Liu Bo6f9994d2017-01-31 07:50:22 -08007630/* The callers of this must take lock_extent() */
7631static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7632 u64 orig_start, u64 block_start,
7633 u64 block_len, u64 orig_block_len,
7634 u64 ram_bytes, int compress_type,
7635 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007636{
7637 struct extent_map_tree *em_tree;
7638 struct extent_map *em;
7639 struct btrfs_root *root = BTRFS_I(inode)->root;
7640 int ret;
7641
Liu Bo6f9994d2017-01-31 07:50:22 -08007642 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7643 type == BTRFS_ORDERED_COMPRESSED ||
7644 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007645 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007646
Josef Bacik69ffb542012-09-11 15:40:07 -04007647 em_tree = &BTRFS_I(inode)->extent_tree;
7648 em = alloc_extent_map();
7649 if (!em)
7650 return ERR_PTR(-ENOMEM);
7651
7652 em->start = start;
7653 em->orig_start = orig_start;
7654 em->len = len;
7655 em->block_len = block_len;
7656 em->block_start = block_start;
7657 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007658 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007659 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007660 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007661 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007662 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007663 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007664 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007665 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7666 em->compress_type = compress_type;
7667 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007668
7669 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007670 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007671 em->start + em->len - 1, 0);
7672 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007673 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007674 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007675 /*
7676 * The caller has taken lock_extent(), who could race with us
7677 * to add em?
7678 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007679 } while (ret == -EEXIST);
7680
7681 if (ret) {
7682 free_extent_map(em);
7683 return ERR_PTR(ret);
7684 }
7685
Liu Bo6f9994d2017-01-31 07:50:22 -08007686 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007687 return em;
7688}
7689
Filipe Manana9c9464c2015-11-04 09:52:04 +00007690static void adjust_dio_outstanding_extents(struct inode *inode,
7691 struct btrfs_dio_data *dio_data,
7692 const u64 len)
7693{
David Sterba823bb202017-01-04 11:09:51 +01007694 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007695
Filipe Manana9c9464c2015-11-04 09:52:04 +00007696 /*
7697 * If we have an outstanding_extents count still set then we're
7698 * within our reservation, otherwise we need to adjust our inode
7699 * counter appropriately.
7700 */
Liu Boc2931662016-12-22 17:13:54 -08007701 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007702 dio_data->outstanding_extents -= num_extents;
7703 } else {
Liu Boc2931662016-12-22 17:13:54 -08007704 /*
7705 * If dio write length has been split due to no large enough
7706 * contiguous space, we need to compensate our inode counter
7707 * appropriately.
7708 */
7709 u64 num_needed = num_extents - dio_data->outstanding_extents;
7710
Filipe Manana9c9464c2015-11-04 09:52:04 +00007711 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007712 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007713 spin_unlock(&BTRFS_I(inode)->lock);
7714 }
7715}
7716
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7718 struct buffer_head *bh_result, int create)
7719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007720 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007722 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307723 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007725 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007728 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007729
Miao Xie172a5042013-02-21 02:48:22 -07007730 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007731 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007732 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007733 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007734
Josef Bacikc3298612012-08-03 16:49:19 -04007735 lockstart = start;
7736 lockend = start + len - 1;
7737
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007738 if (current->journal_info) {
7739 /*
7740 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007741 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007742 * confused.
7743 */
chandan50745b02015-08-28 21:10:13 +05307744 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007745 current->journal_info = NULL;
7746 }
7747
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 /*
7749 * If this errors out it's because we couldn't invalidate pagecache for
7750 * this range and we need to fallback to buffered.
7751 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007752 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7753 create)) {
7754 ret = -ENOTBLK;
7755 goto err;
7756 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007757
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007758 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007759 if (IS_ERR(em)) {
7760 ret = PTR_ERR(em);
7761 goto unlock_err;
7762 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007763
7764 /*
7765 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7766 * io. INLINE is special, and we could probably kludge it in here, but
7767 * it's still buffered so for safety lets just fall back to the generic
7768 * buffered path.
7769 *
7770 * For COMPRESSED we _have_ to read the entire extent in so we can
7771 * decompress it, so there will be buffering required no matter what we
7772 * do, so go ahead and fallback to buffered.
7773 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007774 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007775 * to buffered IO. Don't blame me, this is the price we pay for using
7776 * the generic code.
7777 */
7778 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7779 em->block_start == EXTENT_MAP_INLINE) {
7780 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007781 ret = -ENOTBLK;
7782 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 }
7784
7785 /* Just a good old fashioned hole, return */
7786 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7787 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7788 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007789 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007790 }
7791
7792 /*
7793 * We don't allocate a new extent in the following cases
7794 *
7795 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7796 * existing extent.
7797 * 2) The extent is marked as PREALLOC. We're good to go here and can
7798 * just use the extent.
7799 *
7800 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007801 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007802 len = min(len, em->len - (start - em->start));
7803 lockstart = start + len;
7804 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007805 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007806
7807 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7808 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7809 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007810 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007811 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007812
7813 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7814 type = BTRFS_ORDERED_PREALLOC;
7815 else
7816 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007817 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007818 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007819
Josef Bacik00361582013-08-14 14:02:47 -04007820 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007821 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007822 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007823 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007824
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007825 em2 = btrfs_create_dio_extent(inode, start, len,
7826 orig_start, block_start,
7827 len, orig_block_len,
7828 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007829 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007830 if (type == BTRFS_ORDERED_PREALLOC) {
7831 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007832 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007833 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007834 if (em2 && IS_ERR(em2)) {
7835 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007836 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007837 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007838 /*
7839 * For inode marked NODATACOW or extent marked PREALLOC,
7840 * use the existing or preallocated extent, so does not
7841 * need to adjust btrfs_space_info's bytes_may_use.
7842 */
7843 btrfs_free_reserved_data_space_noquota(inode,
7844 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007845 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847 }
Josef Bacik00361582013-08-14 14:02:47 -04007848
Chris Mason46bfbb52010-05-26 11:04:10 -04007849 /*
7850 * this will cow the extent, reset the len in case we changed
7851 * it above
7852 */
7853 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007854 free_extent_map(em);
7855 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007856 if (IS_ERR(em)) {
7857 ret = PTR_ERR(em);
7858 goto unlock_err;
7859 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007860 len = min(len, em->len - (start - em->start));
7861unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007862 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7863 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007864 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007865 bh_result->b_bdev = em->bdev;
7866 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007867 if (create) {
7868 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7869 set_buffer_new(bh_result);
7870
7871 /*
7872 * Need to update the i_size under the extent lock so buffered
7873 * readers will get the updated i_size when we unlock.
7874 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007875 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007876 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007877
Filipe Manana9c9464c2015-11-04 09:52:04 +00007878 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307879 WARN_ON(dio_data->reserve < len);
7880 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007881 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307882 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007883 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007884
Josef Bacikeb838e72012-07-31 16:28:48 -04007885 /*
7886 * In the case of write we need to clear and unlock the entire range,
7887 * in the case of read we need to unlock only the end area that we
7888 * aren't using if there is any left over space.
7889 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007890 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007891 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7892 lockend, unlock_bits, 1, 0,
7893 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007894 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007895 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007896 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007897
Josef Bacik4b46fce2010-05-23 11:00:55 -04007898 free_extent_map(em);
7899
7900 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007901
7902unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007903 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7904 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007905err:
chandan50745b02015-08-28 21:10:13 +05307906 if (dio_data)
7907 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007908 /*
7909 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7910 * write less data then expected, so that we don't underflow our inode's
7911 * outstanding extents counter.
7912 */
7913 if (create && dio_data)
7914 adjust_dio_outstanding_extents(inode, dio_data, len);
7915
Josef Bacikeb838e72012-07-31 16:28:48 -04007916 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007917}
7918
Miao Xie8b110e32014-09-12 18:44:03 +08007919static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007920 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007921{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007922 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007923 int ret;
7924
Mike Christie37226b22016-06-05 14:31:52 -05007925 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007926
7927 bio_get(bio);
7928
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007929 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007930 if (ret)
7931 goto err;
7932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007933 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007934err:
7935 bio_put(bio);
7936 return ret;
7937}
7938
7939static int btrfs_check_dio_repairable(struct inode *inode,
7940 struct bio *failed_bio,
7941 struct io_failure_record *failrec,
7942 int failed_mirror)
7943{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007945 int num_copies;
7946
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007947 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007948 if (num_copies == 1) {
7949 /*
7950 * we only have a single copy of the data, so don't bother with
7951 * all the retry and error correction code that follows. no
7952 * matter what the error is, it is very likely to persist.
7953 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007954 btrfs_debug(fs_info,
7955 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7956 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007957 return 0;
7958 }
7959
7960 failrec->failed_mirror = failed_mirror;
7961 failrec->this_mirror++;
7962 if (failrec->this_mirror == failed_mirror)
7963 failrec->this_mirror++;
7964
7965 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007966 btrfs_debug(fs_info,
7967 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7968 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007969 return 0;
7970 }
7971
7972 return 1;
7973}
7974
7975static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 struct page *page, unsigned int pgoff,
7977 u64 start, u64 end, int failed_mirror,
7978 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007979{
7980 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007981 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7982 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007983 struct bio *bio;
7984 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007985 int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007986 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007987 int ret;
7988
Mike Christie37226b22016-06-05 14:31:52 -05007989 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007990
7991 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7992 if (ret)
7993 return ret;
7994
7995 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7996 failed_mirror);
7997 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007998 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007999 return -EIO;
8000 }
8001
Liu Bo17347ce2017-05-15 15:33:27 -07008002 segs = bio_segments(failed_bio);
8003 if (segs > 1 ||
8004 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008005 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008006
8007 isector = start - btrfs_io_bio(failed_bio)->logical;
8008 isector >>= inode->i_sb->s_blocksize_bits;
8009 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308010 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008011 if (!bio) {
Josef Bacik7870d082017-05-05 11:57:15 -04008012 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008013 return -EIO;
8014 }
Mike Christie37226b22016-06-05 14:31:52 -05008015 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008016
8017 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8018 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8019 read_mode, failrec->this_mirror, failrec->in_validation);
8020
Mike Christie81a75f672016-06-05 14:31:54 -05008021 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008022 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008023 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008024 bio_put(bio);
8025 }
8026
8027 return ret;
8028}
8029
8030struct btrfs_retry_complete {
8031 struct completion done;
8032 struct inode *inode;
8033 u64 start;
8034 int uptodate;
8035};
8036
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008037static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008038{
8039 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008040 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008041 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008042 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008043 int i;
8044
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008045 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008046 goto end;
8047
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308048 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008049 io_tree = &BTRFS_I(inode)->io_tree;
8050 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8051 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052
Miao Xie8b110e32014-09-12 18:44:03 +08008053 done->uptodate = 1;
8054 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008055 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8056 io_tree, done->start, bvec->bv_page,
8057 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008058end:
8059 complete(&done->done);
8060 bio_put(bio);
8061}
8062
8063static int __btrfs_correct_data_nocsum(struct inode *inode,
8064 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008065{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008067 struct bio_vec bvec;
8068 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008069 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008070 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 unsigned int pgoff;
8072 u32 sectorsize;
8073 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008074 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008075 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008076
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008078 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308079
Miao Xiec1dc0892014-09-12 18:43:56 +08008080 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008081 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008082 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008083
Liu Bo17347ce2017-05-15 15:33:27 -07008084 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8085 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8086 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308087
8088next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008089 done.uptodate = 0;
8090 done.start = start;
8091 init_completion(&done.done);
8092
Liu Bo17347ce2017-05-15 15:33:27 -07008093 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308094 pgoff, start, start + sectorsize - 1,
8095 io_bio->mirror_num,
8096 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008097 if (ret) {
8098 err = ret;
8099 goto next;
8100 }
Miao Xie8b110e32014-09-12 18:44:03 +08008101
8102 wait_for_completion(&done.done);
8103
8104 if (!done.uptodate) {
8105 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308106 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008107 }
8108
Liu Bo629ebf42017-05-15 17:20:07 -07008109next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110 start += sectorsize;
8111
Liu Bo97bf5a52017-04-07 13:11:10 -07008112 nr_sectors--;
8113 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008115 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116 goto next_block_or_try_again;
8117 }
Miao Xie8b110e32014-09-12 18:44:03 +08008118 }
8119
Liu Bo629ebf42017-05-15 17:20:07 -07008120 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008121}
8122
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008123static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008124{
8125 struct btrfs_retry_complete *done = bio->bi_private;
8126 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008127 struct extent_io_tree *io_tree, *failure_tree;
8128 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008129 struct bio_vec *bvec;
8130 int uptodate;
8131 int ret;
8132 int i;
8133
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008134 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008135 goto end;
8136
8137 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308138
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308139 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008140 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308141
Josef Bacik7870d082017-05-05 11:57:15 -04008142 io_tree = &BTRFS_I(inode)->io_tree;
8143 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8144
Miao Xie8b110e32014-09-12 18:44:03 +08008145 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008146 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8147 bvec->bv_offset, done->start,
8148 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008149 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008150 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8151 failure_tree, io_tree, done->start,
8152 bvec->bv_page,
8153 btrfs_ino(BTRFS_I(inode)),
8154 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008155 else
8156 uptodate = 0;
8157 }
8158
8159 done->uptodate = uptodate;
8160end:
8161 complete(&done->done);
8162 bio_put(bio);
8163}
8164
8165static int __btrfs_subio_endio_read(struct inode *inode,
8166 struct btrfs_io_bio *io_bio, int err)
8167{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308168 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008169 struct bio_vec bvec;
8170 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008171 struct btrfs_retry_complete done;
8172 u64 start;
8173 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308174 u32 sectorsize;
8175 int nr_sectors;
8176 unsigned int pgoff;
8177 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008178 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008179 int ret;
8180
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308181 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008182 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308183
Miao Xie8b110e32014-09-12 18:44:03 +08008184 err = 0;
8185 start = io_bio->logical;
8186 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008187 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008188
Liu Bo17347ce2017-05-15 15:33:27 -07008189 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8190 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308191
Liu Bo17347ce2017-05-15 15:33:27 -07008192 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308193next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008194 if (uptodate) {
8195 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8196 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8197 bvec.bv_page, pgoff, start, sectorsize);
8198 if (likely(!ret))
8199 goto next;
8200 }
Miao Xie8b110e32014-09-12 18:44:03 +08008201try_again:
8202 done.uptodate = 0;
8203 done.start = start;
8204 init_completion(&done.done);
8205
Liu Bo17347ce2017-05-15 15:33:27 -07008206 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308207 pgoff, start, start + sectorsize - 1,
8208 io_bio->mirror_num,
8209 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008210 if (ret) {
8211 err = ret;
8212 goto next;
8213 }
8214
8215 wait_for_completion(&done.done);
8216
8217 if (!done.uptodate) {
8218 /* We might have another mirror, so try again */
8219 goto try_again;
8220 }
8221next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308222 offset += sectorsize;
8223 start += sectorsize;
8224
8225 ASSERT(nr_sectors);
8226
Liu Bo97bf5a52017-04-07 13:11:10 -07008227 nr_sectors--;
8228 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308229 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008230 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308231 goto next_block;
8232 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008233 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008234
8235 return err;
8236}
8237
Miao Xie8b110e32014-09-12 18:44:03 +08008238static int btrfs_subio_endio_read(struct inode *inode,
8239 struct btrfs_io_bio *io_bio, int err)
8240{
8241 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8242
8243 if (skip_csum) {
8244 if (unlikely(err))
8245 return __btrfs_correct_data_nocsum(inode, io_bio);
8246 else
8247 return 0;
8248 } else {
8249 return __btrfs_subio_endio_read(inode, io_bio, err);
8250 }
8251}
8252
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008253static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008254{
8255 struct btrfs_dio_private *dip = bio->bi_private;
8256 struct inode *inode = dip->inode;
8257 struct bio *dio_bio;
8258 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008259 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008260
Liu Boe3d37fa2017-05-17 15:42:00 -06008261 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) {
Miao Xie8b110e32014-09-12 18:44:03 +08008262 err = btrfs_subio_endio_read(inode, io_bio, err);
Liu Boe3d37fa2017-05-17 15:42:00 -06008263 if (!err)
8264 bio->bi_error = 0;
8265 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008266
Josef Bacik4b46fce2010-05-23 11:00:55 -04008267 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008268 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008269 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008270
Josef Bacik4b46fce2010-05-23 11:00:55 -04008271 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008272
Filipe Manana1636d1d2016-02-15 16:20:26 +00008273 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008274 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008275
8276 if (io_bio->end_io)
8277 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008278 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008279}
8280
Qu Wenruo524272602017-03-08 10:25:52 +08008281static void __endio_write_update_ordered(struct inode *inode,
8282 const u64 offset, const u64 bytes,
8283 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008285 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008286 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008287 struct btrfs_workqueue *wq;
8288 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008289 u64 ordered_offset = offset;
8290 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008291 int ret;
8292
Qu Wenruo524272602017-03-08 10:25:52 +08008293 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8294 wq = fs_info->endio_freespace_worker;
8295 func = btrfs_freespace_write_helper;
8296 } else {
8297 wq = fs_info->endio_write_workers;
8298 func = btrfs_endio_write_helper;
8299 }
8300
Chris Mason163cf092010-11-28 19:56:33 -05008301again:
8302 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8303 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008304 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008305 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008306 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008307 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008308
Qu Wenruo524272602017-03-08 10:25:52 +08008309 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8310 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008311out_test:
8312 /*
8313 * our bio might span multiple ordered extents. If we haven't
8314 * completed the accounting for the whole dio, go back and try again
8315 */
Filipe Manana14543772015-11-24 16:23:54 +00008316 if (ordered_offset < offset + bytes) {
8317 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008318 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008319 goto again;
8320 }
Filipe Manana14543772015-11-24 16:23:54 +00008321}
8322
8323static void btrfs_endio_direct_write(struct bio *bio)
8324{
8325 struct btrfs_dio_private *dip = bio->bi_private;
8326 struct bio *dio_bio = dip->dio_bio;
8327
Qu Wenruo524272602017-03-08 10:25:52 +08008328 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8329 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008330
Josef Bacik4b46fce2010-05-23 11:00:55 -04008331 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008332
Filipe Manana1636d1d2016-02-15 16:20:26 +00008333 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008334 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008335 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008336}
8337
Josef Bacikc6100a42017-05-05 11:57:13 -04008338static int __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008339 struct bio *bio, int mirror_num,
8340 unsigned long bio_flags, u64 offset)
8341{
Josef Bacikc6100a42017-05-05 11:57:13 -04008342 struct inode *inode = private_data;
Chris Masoneaf25d92010-05-25 09:48:28 -04008343 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008344 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008345 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008346 return 0;
8347}
8348
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008349static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008350{
8351 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008352 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008353
Miao Xie8b110e32014-09-12 18:44:03 +08008354 if (err)
8355 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008356 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008357 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8358 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008359 (unsigned long long)bio->bi_iter.bi_sector,
8360 bio->bi_iter.bi_size, err);
8361
8362 if (dip->subio_endio)
8363 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008364
8365 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008366 dip->errors = 1;
8367
8368 /*
8369 * before atomic variable goto zero, we must make sure
8370 * dip->errors is perceived to be set.
8371 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008372 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008373 }
8374
8375 /* if there are more bios still pending for this dio, just exit */
8376 if (!atomic_dec_and_test(&dip->pending_bios))
8377 goto out;
8378
Chris Mason9be33952013-05-17 18:30:14 -04008379 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008380 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008381 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008382 dip->dio_bio->bi_error = 0;
8383 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008384 }
8385out:
8386 bio_put(bio);
8387}
8388
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008389static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008390 struct btrfs_dio_private *dip,
8391 struct bio *bio,
8392 u64 file_offset)
8393{
8394 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8395 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8396 int ret;
8397
8398 /*
8399 * We load all the csum data we need when we submit
8400 * the first bio to reduce the csum tree search and
8401 * contention.
8402 */
8403 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008404 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008405 file_offset);
8406 if (ret)
8407 return ret;
8408 }
8409
8410 if (bio == dip->orig_bio)
8411 return 0;
8412
8413 file_offset -= dip->logical_offset;
8414 file_offset >>= inode->i_sb->s_blocksize_bits;
8415 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8416
8417 return 0;
8418}
8419
Miao Xiee65e1532010-11-22 03:04:43 +00008420static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008421 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008422 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008425 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008426 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008427 int ret;
8428
Josef Bacikb812ce22012-11-16 13:56:32 -05008429 if (async_submit)
8430 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8431
Miao Xiee65e1532010-11-22 03:04:43 +00008432 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008433
8434 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008435 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008436 if (ret)
8437 goto err;
8438 }
Miao Xiee65e1532010-11-22 03:04:43 +00008439
Josef Bacik1ae39932011-04-06 14:41:34 -04008440 if (skip_sum)
8441 goto map;
8442
8443 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008444 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8445 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008446 __btrfs_submit_bio_start_direct_io,
8447 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008448 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008449 } else if (write) {
8450 /*
8451 * If we aren't doing async submit, calculate the csum of the
8452 * bio now.
8453 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008454 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008455 if (ret)
8456 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008457 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008458 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008459 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008460 if (ret)
8461 goto err;
8462 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008463map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008464 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008465err:
8466 bio_put(bio);
8467 return ret;
8468}
8469
Mike Christie81a75f672016-06-05 14:31:54 -05008470static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008471 int skip_sum)
8472{
8473 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008474 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008475 struct bio *bio;
8476 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008477 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008478 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008479 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008480 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008481 u64 submit_len;
8482 int clone_offset = 0;
8483 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308484 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008485
Kent Overstreet4f024f32013-10-11 15:44:27 -07008486 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008487 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008488 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8489 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008490 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008491 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008492
Liu Bo725130b2017-05-16 09:51:39 -07008493 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008494 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008495 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008496 goto submit;
8497 }
8498
David Woodhouse53b381b2013-01-29 18:40:14 -05008499 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008500 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008501 async_submit = 0;
8502 else
8503 async_submit = 1;
8504
Liu Bo725130b2017-05-16 09:51:39 -07008505 /* bio split */
8506 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008507 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008508 do {
Liu Bo725130b2017-05-16 09:51:39 -07008509 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008510
Liu Bo725130b2017-05-16 09:51:39 -07008511 /*
8512 * This will never fail as it's passing GPF_NOFS and
8513 * the allocation is backed by btrfs_bioset.
8514 */
Liu Boe4770942017-05-16 10:57:14 -07008515 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008516 clone_len);
8517 bio->bi_private = dip;
8518 bio->bi_end_io = btrfs_end_dio_bio;
8519 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008520
Liu Bo725130b2017-05-16 09:51:39 -07008521 ASSERT(submit_len >= clone_len);
8522 submit_len -= clone_len;
8523 if (submit_len == 0)
8524 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008525
Liu Bo725130b2017-05-16 09:51:39 -07008526 /*
8527 * Increase the count before we submit the bio so we know
8528 * the end IO handler won't happen before we increase the
8529 * count. Otherwise, the dip might get freed before we're
8530 * done setting it up.
8531 */
8532 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008533
Liu Bo725130b2017-05-16 09:51:39 -07008534 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8535 async_submit);
8536 if (ret) {
8537 bio_put(bio);
8538 atomic_dec(&dip->pending_bios);
8539 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008540 }
Liu Bo725130b2017-05-16 09:51:39 -07008541
8542 clone_offset += clone_len;
8543 start_sector += clone_len >> 9;
8544 file_offset += clone_len;
8545
8546 map_length = submit_len;
8547 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8548 start_sector << 9, &map_length, NULL, 0);
8549 if (ret)
8550 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008551 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008552
Josef Bacik02f57c72011-04-06 14:25:44 -04008553submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008554 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008555 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008556 if (!ret)
8557 return 0;
8558
8559 bio_put(bio);
8560out_err:
8561 dip->errors = 1;
8562 /*
8563 * before atomic variable goto zero, we must
8564 * make sure dip->errors is perceived to be set.
8565 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008566 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008567 if (atomic_dec_and_test(&dip->pending_bios))
8568 bio_io_error(dip->orig_bio);
8569
8570 /* bio_end_io() will handle error, so we needn't return it */
8571 return 0;
8572}
8573
Mike Christie8a4c1e42016-06-05 14:31:50 -05008574static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8575 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008576{
Filipe Manana61de7182015-07-01 12:13:10 +01008577 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008578 struct bio *bio = NULL;
8579 struct btrfs_io_bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008580 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008581 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008582 int ret = 0;
8583
8584 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8585
David Sterba8b6c1d52017-06-02 17:48:13 +02008586 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008587
Miao Xiec1dc0892014-09-12 18:43:56 +08008588 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008589 if (!dip) {
8590 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008591 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008592 }
8593
8594 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595 dip->inode = inode;
8596 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008597 dip->bytes = dio_bio->bi_iter.bi_size;
8598 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008599 bio->bi_private = dip;
8600 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008601 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008602 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008603 io_bio = btrfs_io_bio(bio);
8604 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008605
Miao Xiec1dc0892014-09-12 18:43:56 +08008606 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008607 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008608 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008609 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008610 dip->subio_endio = btrfs_subio_endio_read;
8611 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008612
Filipe Mananaf28a4922015-12-08 19:23:20 +00008613 /*
8614 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8615 * even if we fail to submit a bio, because in such case we do the
8616 * corresponding error handling below and it must not be done a second
8617 * time by btrfs_direct_IO().
8618 */
8619 if (write) {
8620 struct btrfs_dio_data *dio_data = current->journal_info;
8621
8622 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8623 dip->bytes;
8624 dio_data->unsubmitted_oe_range_start =
8625 dio_data->unsubmitted_oe_range_end;
8626 }
8627
Mike Christie81a75f672016-06-05 14:31:54 -05008628 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008629 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008630 return;
Chris Mason9be33952013-05-17 18:30:14 -04008631
Liu Bo3892ac92017-04-17 15:00:28 -07008632 if (io_bio->end_io)
8633 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008634
Josef Bacik4b46fce2010-05-23 11:00:55 -04008635free_ordered:
8636 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008637 * If we arrived here it means either we failed to submit the dip
8638 * or we either failed to clone the dio_bio or failed to allocate the
8639 * dip. If we cloned the dio_bio and allocated the dip, we can just
8640 * call bio_endio against our io_bio so that we get proper resource
8641 * cleanup if we fail to submit the dip, otherwise, we must do the
8642 * same as btrfs_endio_direct_[write|read] because we can't call these
8643 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008644 */
Liu Bo3892ac92017-04-17 15:00:28 -07008645 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008646 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008647 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008648 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008649 * and all the cleanup and final put for dio_bio (through
8650 * dio_end_io()).
8651 */
8652 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008653 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008654 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008655 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008656 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008657 file_offset,
8658 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008659 false);
Filipe Manana14543772015-11-24 16:23:54 +00008660 else
Filipe Manana61de7182015-07-01 12:13:10 +01008661 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8662 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008663
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008664 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008665 /*
8666 * Releases and cleans up our dio_bio, no need to bio_put()
8667 * nor bio_endio()/bio_io_error() against dio_bio.
8668 */
8669 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008670 }
Liu Bo3892ac92017-04-17 15:00:28 -07008671 if (bio)
8672 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008673 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008674}
8675
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008676static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8677 struct kiocb *iocb,
8678 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008679{
8680 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008681 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008682 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008683 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008684
8685 if (offset & blocksize_mask)
8686 goto out;
8687
Al Viro28060d52014-03-22 05:15:17 -04008688 if (iov_iter_alignment(iter) & blocksize_mask)
8689 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008690
Al Viro28060d52014-03-22 05:15:17 -04008691 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008692 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008693 return 0;
8694 /*
8695 * Check to make sure we don't have duplicate iov_base's in this
8696 * iovec, if so return EINVAL, otherwise we'll get csum errors
8697 * when reading back.
8698 */
8699 for (seg = 0; seg < iter->nr_segs; seg++) {
8700 for (i = seg + 1; i < iter->nr_segs; i++) {
8701 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008702 goto out;
8703 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008704 }
8705 retval = 0;
8706out:
8707 return retval;
8708}
Josef Bacikeb838e72012-07-31 16:28:48 -04008709
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008710static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008711{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008712 struct file *file = iocb->ki_filp;
8713 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308715 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008716 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008717 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008718 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008719 bool wakeup = true;
8720 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008721 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008722
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008723 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008724 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008725
Jens Axboefe0f07d2015-04-15 17:05:48 -06008726 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008727 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008728
Josef Bacik0e267c42013-07-02 10:38:02 -04008729 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008730 * The generic stuff only does filemap_write_and_wait_range, which
8731 * isn't enough if we've written compressed pages to this area, so
8732 * we need to flush the dirty pages again to make absolutely sure
8733 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008734 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008735 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008736 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8737 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008738 filemap_fdatawrite_range(inode->i_mapping, offset,
8739 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008740
Omar Sandoval6f673762015-03-16 04:33:52 -07008741 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008742 /*
8743 * If the write DIO is beyond the EOF, we need update
8744 * the isize, but it is protected by i_mutex. So we can
8745 * not unlock the i_mutex at this case.
8746 */
8747 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008748 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008749 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008750 relock = true;
8751 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008752 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008753 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008754 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008755 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008756
8757 /*
8758 * We need to know how many extents we reserved so that we can
8759 * do the accounting properly if we go over the number we
8760 * originally calculated. Abuse current->journal_info for this.
8761 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008762 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008763 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008764 dio_data.unsubmitted_oe_range_start = (u64)offset;
8765 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308766 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308767 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008768 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8769 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008770 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008771 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8772 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008773 }
8774
Omar Sandoval17f8c842015-03-16 04:33:50 -07008775 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008776 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008777 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008778 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008779 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308780 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008781 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008782 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308783 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008784 btrfs_delalloc_release_space(inode, offset,
8785 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008786 /*
8787 * On error we might have left some ordered extents
8788 * without submitting corresponding bios for them, so
8789 * cleanup them up to avoid other tasks getting them
8790 * and waiting for them to complete forever.
8791 */
8792 if (dio_data.unsubmitted_oe_range_start <
8793 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008794 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008795 dio_data.unsubmitted_oe_range_start,
8796 dio_data.unsubmitted_oe_range_end -
8797 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008798 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008799 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008800 btrfs_delalloc_release_space(inode, offset,
8801 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008802 }
Miao Xie38851cc2013-02-08 07:04:11 +00008803out:
Miao Xie2e60a512013-02-08 07:01:08 +00008804 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008805 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008806 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008807 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008808
8809 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008810}
8811
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008812#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8813
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008814static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8815 __u64 start, __u64 len)
8816{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008817 int ret;
8818
8819 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8820 if (ret)
8821 return ret;
8822
Chris Masonec29ed52011-02-23 16:23:20 -05008823 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008824}
8825
Chris Mason9ebefb182007-06-15 13:50:00 -04008826int btrfs_readpage(struct file *file, struct page *page)
8827{
Chris Masond1310b22008-01-24 16:13:08 -05008828 struct extent_io_tree *tree;
8829 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008830 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008831}
Chris Mason1832a6d2007-12-21 16:27:21 -05008832
Chris Mason39279cc2007-06-12 06:35:45 -04008833static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8834{
Chris Masond1310b22008-01-24 16:13:08 -05008835 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008836 struct inode *inode = page->mapping->host;
8837 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008838
8839 if (current->flags & PF_MEMALLOC) {
8840 redirty_page_for_writepage(wbc, page);
8841 unlock_page(page);
8842 return 0;
8843 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008844
8845 /*
8846 * If we are under memory pressure we will call this directly from the
8847 * VM, we need to make sure we have the inode referenced for the ordered
8848 * extent. If not just return like we didn't do anything.
8849 */
8850 if (!igrab(inode)) {
8851 redirty_page_for_writepage(wbc, page);
8852 return AOP_WRITEPAGE_ACTIVATE;
8853 }
Chris Masond1310b22008-01-24 16:13:08 -05008854 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008855 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8856 btrfs_add_delayed_iput(inode);
8857 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008858}
Chris Mason39279cc2007-06-12 06:35:45 -04008859
Eric Sandeen48a3b632013-04-25 20:41:01 +00008860static int btrfs_writepages(struct address_space *mapping,
8861 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008862{
Chris Masond1310b22008-01-24 16:13:08 -05008863 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008864
Chris Masond1310b22008-01-24 16:13:08 -05008865 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008866 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8867}
8868
Chris Mason3ab2fb52007-11-08 10:59:22 -05008869static int
8870btrfs_readpages(struct file *file, struct address_space *mapping,
8871 struct list_head *pages, unsigned nr_pages)
8872{
Chris Masond1310b22008-01-24 16:13:08 -05008873 struct extent_io_tree *tree;
8874 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008875 return extent_readpages(tree, mapping, pages, nr_pages,
8876 btrfs_get_extent);
8877}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008878static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008879{
Chris Masond1310b22008-01-24 16:13:08 -05008880 struct extent_io_tree *tree;
8881 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008882 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008883
Chris Masond1310b22008-01-24 16:13:08 -05008884 tree = &BTRFS_I(page->mapping->host)->io_tree;
8885 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008886 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008887 if (ret == 1) {
8888 ClearPagePrivate(page);
8889 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008890 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008891 }
8892 return ret;
8893}
Chris Mason39279cc2007-06-12 06:35:45 -04008894
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8896{
Chris Mason98509cf2008-09-11 15:51:43 -04008897 if (PageWriteback(page) || PageDirty(page))
8898 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008899 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008900}
8901
Lukas Czernerd47992f2013-05-21 23:17:23 -04008902static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8903 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008904{
Josef Bacik5fd02042012-05-02 14:00:54 -04008905 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008906 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008908 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008909 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008910 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308911 u64 start;
8912 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008913 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008914
Chris Mason8b62b722009-09-02 16:53:46 -04008915 /*
8916 * we have the page locked, so new writeback can't start,
8917 * and the dirty bit won't be cleared while we are here.
8918 *
8919 * Wait for IO on this page so that we can safely clear
8920 * the PagePrivate2 bit and do ordered accounting
8921 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008923
Josef Bacik5fd02042012-05-02 14:00:54 -04008924 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008925 if (offset) {
8926 btrfs_releasepage(page, GFP_NOFS);
8927 return;
8928 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008929
8930 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008931 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308932again:
8933 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008934 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308935 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008936 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308937 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008938 /*
8939 * IO on this page will never be started, so we need
8940 * to account for any ordered extents now
8941 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008942 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308943 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008944 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008945 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008946 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8947 EXTENT_DEFRAG, 1, 0, &cached_state,
8948 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008949 /*
8950 * whoever cleared the private bit is responsible
8951 * for the finish_ordered_io
8952 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008953 if (TestClearPagePrivate2(page)) {
8954 struct btrfs_ordered_inode_tree *tree;
8955 u64 new_len;
8956
8957 tree = &BTRFS_I(inode)->ordered_tree;
8958
8959 spin_lock_irq(&tree->lock);
8960 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308961 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008962 if (new_len < ordered->truncated_len)
8963 ordered->truncated_len = new_len;
8964 spin_unlock_irq(&tree->lock);
8965
8966 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308967 start,
8968 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008969 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008970 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008972 if (!inode_evicting) {
8973 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308974 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008975 &cached_state);
8976 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308977
8978 start = end + 1;
8979 if (start < page_end)
8980 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008981 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008982
Qu Wenruob9d0b382015-09-29 10:35:16 +08008983 /*
8984 * Qgroup reserved space handler
8985 * Page here will be either
8986 * 1) Already written to disk
8987 * In this case, its reserved space is released from data rsv map
8988 * and will be freed by delayed_ref handler finally.
8989 * So even we call qgroup_free_data(), it won't decrease reserved
8990 * space.
8991 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008992 * This means the reserved space should be freed here. However,
8993 * if a truncate invalidates the page (by clearing PageDirty)
8994 * and the page is accounted for while allocating extent
8995 * in btrfs_check_data_free_space() we let delayed_ref to
8996 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008997 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008998 if (PageDirty(page))
8999 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009000 if (!inode_evicting) {
9001 clear_extent_bit(tree, page_start, page_end,
9002 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009003 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9004 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009005 &cached_state, GFP_NOFS);
9006
9007 __btrfs_releasepage(page, GFP_NOFS);
9008 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009
Chris Mason4a096752008-07-21 10:29:44 -04009010 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009011 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009012 ClearPagePrivate(page);
9013 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009014 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009015 }
Chris Mason39279cc2007-06-12 06:35:45 -04009016}
9017
Chris Mason9ebefb182007-06-15 13:50:00 -04009018/*
9019 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9020 * called from a page fault handler when a page is first dirtied. Hence we must
9021 * be careful to check for EOF conditions here. We set the page up correctly
9022 * for a written page which means we get ENOSPC checking when writing into
9023 * holes and correct delalloc and unwritten extent mapping on filesystems that
9024 * support these features.
9025 *
9026 * We are not allowed to take the i_mutex here so we have to play games to
9027 * protect against truncate races as the page could now be beyond EOF. Because
9028 * vmtruncate() writes the inode size before removing pages, once we have the
9029 * page lock we can determine safely if the page is beyond EOF. If it is not
9030 * beyond EOF, then the page is guaranteed safe against truncation until we
9031 * unlock the page.
9032 */
Dave Jiang11bac802017-02-24 14:56:41 -08009033int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009034{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009035 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009036 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009037 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009038 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9039 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009040 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009041 char *kaddr;
9042 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009043 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009044 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009045 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309046 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009047 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009048 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309049 u64 end;
9050
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009051 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009052
Jan Karab2b5ef52012-06-12 16:20:45 +02009053 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009054 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009055 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309056 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009057
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309058 /*
9059 * Reserving delalloc space after obtaining the page lock can lead to
9060 * deadlock. For example, if a dirty page is locked by this function
9061 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9062 * dirty page write out, then the btrfs_writepage() function could
9063 * end up waiting indefinitely to get a lock on the page currently
9064 * being processed by btrfs_page_mkwrite() function.
9065 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009066 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309067 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009068 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009069 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009070 reserved = 1;
9071 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009072 if (ret) {
9073 if (ret == -ENOMEM)
9074 ret = VM_FAULT_OOM;
9075 else /* -ENOSPC, -EIO, etc */
9076 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009077 if (reserved)
9078 goto out;
9079 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009080 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009081
Nick Piggin56a76f82009-03-31 15:23:23 -07009082 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009084 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009085 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009086
Chris Mason9ebefb182007-06-15 13:50:00 -04009087 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009088 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009089 /* page got truncated out from underneath us */
9090 goto out_unlock;
9091 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009092 wait_on_page_writeback(page);
9093
David Sterbaff13db42015-12-03 14:30:40 +01009094 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009095 set_page_extent_mapped(page);
9096
Chris Masoneb84ae02008-07-17 13:53:27 -04009097 /*
9098 * we can't set the delalloc bits if there are pending ordered
9099 * extents. Drop our locks and wait for them to finish
9100 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009101 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9102 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009103 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009104 unlock_extent_cached(io_tree, page_start, page_end,
9105 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009106 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009107 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009108 btrfs_put_ordered_extent(ordered);
9109 goto again;
9110 }
9111
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009112 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009113 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009114 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009115 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309116 end = page_start + reserved_space - 1;
9117 spin_lock(&BTRFS_I(inode)->lock);
9118 BTRFS_I(inode)->outstanding_extents++;
9119 spin_unlock(&BTRFS_I(inode)->lock);
9120 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009121 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309122 }
9123 }
9124
Josef Bacikfbf19082009-10-01 17:10:23 -04009125 /*
Liu Bo54160342016-12-13 12:15:19 -08009126 * page_mkwrite gets called when the page is firstly dirtied after it's
9127 * faulted in, but write(2) could also dirty a page and set delalloc
9128 * bits, thus in this case for space account reason, we still need to
9129 * clear any delalloc bits within this page range since we have to
9130 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009131 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309132 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009133 EXTENT_DIRTY | EXTENT_DELALLOC |
9134 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009135 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009136
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309137 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009138 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009139 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009140 unlock_extent_cached(io_tree, page_start, page_end,
9141 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009142 ret = VM_FAULT_SIGBUS;
9143 goto out_unlock;
9144 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009145 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009146
9147 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009148 if (page_start + PAGE_SIZE > size)
9149 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009150 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009151 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009152
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009153 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009154 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009155 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009156 flush_dcache_page(page);
9157 kunmap(page);
9158 }
Chris Mason247e7432008-07-17 12:53:51 -04009159 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009160 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009161 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009162
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009163 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009164 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009165 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009166
Josef Bacik2ac55d42010-02-03 19:33:23 +00009167 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009168
9169out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009170 if (!ret) {
9171 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009172 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009173 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009174 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009175out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309176 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009177out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009178 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009179 return ret;
9180}
9181
Josef Bacika41ad392011-01-31 15:30:16 -05009182static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009185 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009186 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009187 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009188 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009189 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009190 u64 mask = fs_info->sectorsize - 1;
9191 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009192
Josef Bacik0ef8b722013-10-25 16:13:35 -04009193 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9194 (u64)-1);
9195 if (ret)
9196 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009197
Josef Bacikfcb80c22011-05-03 10:40:22 -04009198 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009199 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009200 * 3 things going on here
9201 *
9202 * 1) We need to reserve space for our orphan item and the space to
9203 * delete our orphan item. Lord knows we don't want to have a dangling
9204 * orphan item because we didn't reserve space to remove it.
9205 *
9206 * 2) We need to reserve space to update our inode.
9207 *
9208 * 3) We need to have something to cache all the space that is going to
9209 * be free'd up by the truncate operation, but also have some slack
9210 * space reserved in case it uses space during the truncate (thank you
9211 * very much snapshotting).
9212 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009213 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009214 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009215 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009216 * doesn't end up using space reserved for updating the inode or
9217 * removing the orphan item. We also need to be able to stop the
9218 * transaction and start a new one, which means we need to be able to
9219 * update the inode several times, and we have no idea of knowing how
9220 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009221 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009222 * Then there is the orphan item, which does indeed need to be held on
9223 * to for the whole operation, and we need nobody to touch this reserved
9224 * space except the orphan code.
9225 *
9226 * So that leaves us with
9227 *
9228 * 1) root->orphan_block_rsv - for the orphan deletion.
9229 * 2) rsv - for the truncate reservation, which we will steal from the
9230 * transaction reservation.
9231 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9232 * updating the inode.
9233 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009234 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009235 if (!rsv)
9236 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009237 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009238 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009239
Josef Bacik907cbce2011-08-08 13:46:15 -04009240 /*
Josef Bacik07127182011-08-19 10:29:59 -04009241 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009242 * 1 for updating the inode.
9243 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009244 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009245 if (IS_ERR(trans)) {
9246 err = PTR_ERR(trans);
9247 goto out;
9248 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009249
Josef Bacik907cbce2011-08-08 13:46:15 -04009250 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009251 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009252 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009253 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009254
Chris Mason5a3f23d2009-03-31 13:27:11 -04009255 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009256 * So if we truncate and then write and fsync we normally would just
9257 * write the extents that changed, which is a problem if we need to
9258 * first truncate that entire inode. So set this flag so we write out
9259 * all of the extents in the inode to the sync log so we're completely
9260 * safe.
9261 */
9262 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009263 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009264
Yan, Zheng80825102009-11-12 09:35:36 +00009265 while (1) {
9266 ret = btrfs_truncate_inode_items(trans, root, inode,
9267 inode->i_size,
9268 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009269 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009270 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009271 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009272 }
Chris Mason39279cc2007-06-12 06:35:45 -04009273
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009274 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009275 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009276 if (ret) {
9277 err = ret;
9278 break;
9279 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009280
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009281 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009282 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009283
9284 trans = btrfs_start_transaction(root, 2);
9285 if (IS_ERR(trans)) {
9286 ret = err = PTR_ERR(trans);
9287 trans = NULL;
9288 break;
9289 }
9290
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009291 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009292 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009293 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009294 BUG_ON(ret); /* shouldn't happen */
9295 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009296 }
9297
9298 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009299 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009300 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009301 if (ret)
9302 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009303 }
9304
Chris Mason917c16b2011-11-08 14:49:59 -05009305 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009306 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009307 ret = btrfs_update_inode(trans, root, inode);
9308 if (ret && !err)
9309 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009310
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009311 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009312 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009313 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009314out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009315 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009316
Josef Bacik3893e332011-01-31 16:03:11 -05009317 if (ret && !err)
9318 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009319
Josef Bacik3893e332011-01-31 16:03:11 -05009320 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009321}
9322
Sven Wegener3b963622008-06-09 21:57:42 -04009323/*
Chris Masond352ac62008-09-29 15:18:18 -04009324 * create a new subvolume directory/inode (helper for the ioctl).
9325 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009326int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009327 struct btrfs_root *new_root,
9328 struct btrfs_root *parent_root,
9329 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009330{
Chris Mason39279cc2007-06-12 06:35:45 -04009331 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009332 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009333 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009334
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009335 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9336 new_dirid, new_dirid,
9337 S_IFDIR | (~current_umask() & S_IRWXUGO),
9338 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009339 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009340 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009341 inode->i_op = &btrfs_dir_inode_operations;
9342 inode->i_fop = &btrfs_dir_file_operations;
9343
Miklos Szeredibfe86842011-10-28 14:13:29 +02009344 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009345 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009346 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009347
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009348 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9349 if (err)
9350 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009351 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009352 new_root->root_key.objectid, err);
9353
Yan, Zheng76dda932009-09-21 16:00:26 -04009354 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009355
Yan, Zheng76dda932009-09-21 16:00:26 -04009356 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009357 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009358}
9359
Chris Mason39279cc2007-06-12 06:35:45 -04009360struct inode *btrfs_alloc_inode(struct super_block *sb)
9361{
9362 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009363 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009364
9365 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9366 if (!ei)
9367 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009368
9369 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009370 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009371 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009372 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009373 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009374 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009375 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009376 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009377 ei->disk_i_size = 0;
9378 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009379 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009380 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009381 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009382 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009383 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009384 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009385
Josef Bacik9e0baf62011-07-15 15:16:44 +00009386 spin_lock_init(&ei->lock);
9387 ei->outstanding_extents = 0;
9388 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009389
Josef Bacik72ac3c02012-05-23 14:13:11 -04009390 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009391 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009392
Miao Xie16cdcec2011-04-22 18:12:22 +08009393 ei->delayed_node = NULL;
9394
chandan r9cc97d62012-07-04 12:48:07 +05309395 ei->i_otime.tv_sec = 0;
9396 ei->i_otime.tv_nsec = 0;
9397
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009398 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009399 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009400 extent_io_tree_init(&ei->io_tree, inode);
9401 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009402 ei->io_tree.track_uptodate = 1;
9403 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009404 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009405 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009406 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009407 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009408 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009409 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009410 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009411 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009412
9413 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009414}
9415
Josef Bacikaaedb552013-10-11 14:44:09 -04009416#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9417void btrfs_test_destroy_inode(struct inode *inode)
9418{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009419 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009420 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9421}
9422#endif
9423
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009424static void btrfs_i_callback(struct rcu_head *head)
9425{
9426 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009427 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9428}
9429
Chris Mason39279cc2007-06-12 06:35:45 -04009430void btrfs_destroy_inode(struct inode *inode)
9431{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009432 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009433 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009434 struct btrfs_root *root = BTRFS_I(inode)->root;
9435
Al Virob3d9b7a2012-06-09 13:51:19 -04009436 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009437 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009438 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9439 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009440 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009441 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009442 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009443 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009444
Chris Mason5a3f23d2009-03-31 13:27:11 -04009445 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009446 * This can happen where we create an inode, but somebody else also
9447 * created the same inode and we need to destroy the one we already
9448 * created.
9449 */
9450 if (!root)
9451 goto free;
9452
Josef Bacik8a35d952012-05-23 14:26:42 -04009453 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9454 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009455 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009456 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009457 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009458 }
Josef Bacik7b128762008-07-24 12:17:14 -04009459
Chris Masond3977122009-01-05 21:25:51 -05009460 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009461 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9462 if (!ordered)
9463 break;
9464 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009465 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009466 "found ordered extent %llu %llu on inode cleanup",
9467 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009468 btrfs_remove_ordered_extent(inode, ordered);
9469 btrfs_put_ordered_extent(ordered);
9470 btrfs_put_ordered_extent(ordered);
9471 }
9472 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009473 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009474 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009475 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009476free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009477 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009478}
9479
Al Viro45321ac2010-06-07 13:43:19 -04009480int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009481{
9482 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009483
Naohiro Aota6379ef92013-06-06 09:56:34 +00009484 if (root == NULL)
9485 return 1;
9486
Liu Bofa6ac872013-02-20 14:10:23 +00009487 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009488 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009489 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009490 else
Al Viro45321ac2010-06-07 13:43:19 -04009491 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009492}
9493
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009494static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009495{
9496 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9497
9498 inode_init_once(&ei->vfs_inode);
9499}
9500
9501void btrfs_destroy_cachep(void)
9502{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009503 /*
9504 * Make sure all delayed rcu free inodes are flushed before we
9505 * destroy cache.
9506 */
9507 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009508 kmem_cache_destroy(btrfs_inode_cachep);
9509 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009510 kmem_cache_destroy(btrfs_path_cachep);
9511 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009512}
9513
9514int btrfs_init_cachep(void)
9515{
David Sterba837e1972012-09-07 03:00:48 -06009516 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009517 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009518 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9519 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009520 if (!btrfs_inode_cachep)
9521 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009522
David Sterba837e1972012-09-07 03:00:48 -06009523 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009524 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009525 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009526 if (!btrfs_trans_handle_cachep)
9527 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009528
David Sterba837e1972012-09-07 03:00:48 -06009529 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009530 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009531 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009532 if (!btrfs_path_cachep)
9533 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009534
David Sterba837e1972012-09-07 03:00:48 -06009535 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009536 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009537 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009538 if (!btrfs_free_space_cachep)
9539 goto fail;
9540
Chris Mason39279cc2007-06-12 06:35:45 -04009541 return 0;
9542fail:
9543 btrfs_destroy_cachep();
9544 return -ENOMEM;
9545}
9546
David Howellsa528d352017-01-31 16:46:22 +00009547static int btrfs_getattr(const struct path *path, struct kstat *stat,
9548 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009549{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009550 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009551 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009552 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009553 u32 bi_flags = BTRFS_I(inode)->flags;
9554
9555 stat->result_mask |= STATX_BTIME;
9556 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9557 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9558 if (bi_flags & BTRFS_INODE_APPEND)
9559 stat->attributes |= STATX_ATTR_APPEND;
9560 if (bi_flags & BTRFS_INODE_COMPRESS)
9561 stat->attributes |= STATX_ATTR_COMPRESSED;
9562 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9563 stat->attributes |= STATX_ATTR_IMMUTABLE;
9564 if (bi_flags & BTRFS_INODE_NODUMP)
9565 stat->attributes |= STATX_ATTR_NODUMP;
9566
9567 stat->attributes_mask |= (STATX_ATTR_APPEND |
9568 STATX_ATTR_COMPRESSED |
9569 STATX_ATTR_IMMUTABLE |
9570 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009571
Chris Mason39279cc2007-06-12 06:35:45 -04009572 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009573 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009574
9575 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009576 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009577 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009578 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009579 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009580 return 0;
9581}
9582
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583static int btrfs_rename_exchange(struct inode *old_dir,
9584 struct dentry *old_dentry,
9585 struct inode *new_dir,
9586 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009588 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009589 struct btrfs_trans_handle *trans;
9590 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009591 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 struct inode *new_inode = new_dentry->d_inode;
9593 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009594 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009596 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9597 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 u64 old_idx = 0;
9599 u64 new_idx = 0;
9600 u64 root_objectid;
9601 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009602 bool root_log_pinned = false;
9603 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604
9605 /* we only allow rename subvolume link between subvolumes */
9606 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9607 return -EXDEV;
9608
9609 /* close the race window with snapshot create/destroy ioctl */
9610 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009611 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009613 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614
9615 /*
9616 * We want to reserve the absolute worst case amount of items. So if
9617 * both inodes are subvols and we need to unlink them then that would
9618 * require 4 item modifications, but if they are both normal inodes it
9619 * would require 5 item modifications, so we'll assume their normal
9620 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9621 * should cover the worst case number of items we'll modify.
9622 */
9623 trans = btrfs_start_transaction(root, 12);
9624 if (IS_ERR(trans)) {
9625 ret = PTR_ERR(trans);
9626 goto out_notrans;
9627 }
9628
9629 /*
9630 * We need to find a free sequence number both in the source and
9631 * in the destination directory for the exchange.
9632 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009633 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 if (ret)
9635 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009636 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 if (ret)
9638 goto out_fail;
9639
9640 BTRFS_I(old_inode)->dir_index = 0ULL;
9641 BTRFS_I(new_inode)->dir_index = 0ULL;
9642
9643 /* Reference for the source. */
9644 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9645 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009646 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009648 btrfs_pin_log_trans(root);
9649 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 ret = btrfs_insert_inode_ref(trans, dest,
9651 new_dentry->d_name.name,
9652 new_dentry->d_name.len,
9653 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009654 btrfs_ino(BTRFS_I(new_dir)),
9655 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 if (ret)
9657 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 }
9659
9660 /* And now for the dest. */
9661 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9662 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009663 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009665 btrfs_pin_log_trans(dest);
9666 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009667 ret = btrfs_insert_inode_ref(trans, root,
9668 old_dentry->d_name.name,
9669 old_dentry->d_name.len,
9670 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009671 btrfs_ino(BTRFS_I(old_dir)),
9672 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 if (ret)
9674 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009675 }
9676
9677 /* Update inode version and ctime/mtime. */
9678 inode_inc_iversion(old_dir);
9679 inode_inc_iversion(new_dir);
9680 inode_inc_iversion(old_inode);
9681 inode_inc_iversion(new_inode);
9682 old_dir->i_ctime = old_dir->i_mtime = ctime;
9683 new_dir->i_ctime = new_dir->i_mtime = ctime;
9684 old_inode->i_ctime = ctime;
9685 new_inode->i_ctime = ctime;
9686
9687 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009688 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9689 BTRFS_I(old_inode), 1);
9690 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9691 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 }
9693
9694 /* src is a subvolume */
9695 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9696 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9697 ret = btrfs_unlink_subvol(trans, root, old_dir,
9698 root_objectid,
9699 old_dentry->d_name.name,
9700 old_dentry->d_name.len);
9701 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009702 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9703 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 old_dentry->d_name.name,
9705 old_dentry->d_name.len);
9706 if (!ret)
9707 ret = btrfs_update_inode(trans, root, old_inode);
9708 }
9709 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009710 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 goto out_fail;
9712 }
9713
9714 /* dest is a subvolume */
9715 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9716 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9717 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9718 root_objectid,
9719 new_dentry->d_name.name,
9720 new_dentry->d_name.len);
9721 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009722 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9723 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009724 new_dentry->d_name.name,
9725 new_dentry->d_name.len);
9726 if (!ret)
9727 ret = btrfs_update_inode(trans, dest, new_inode);
9728 }
9729 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009730 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009731 goto out_fail;
9732 }
9733
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009734 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009735 new_dentry->d_name.name,
9736 new_dentry->d_name.len, 0, old_idx);
9737 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009738 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739 goto out_fail;
9740 }
9741
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009742 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743 old_dentry->d_name.name,
9744 old_dentry->d_name.len, 0, new_idx);
9745 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009746 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747 goto out_fail;
9748 }
9749
9750 if (old_inode->i_nlink == 1)
9751 BTRFS_I(old_inode)->dir_index = old_idx;
9752 if (new_inode->i_nlink == 1)
9753 BTRFS_I(new_inode)->dir_index = new_idx;
9754
Filipe Manana86e8aa02016-05-05 02:02:27 +01009755 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009757 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9758 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009760 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009761 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009762 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009763 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009764 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9765 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009766 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009767 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009768 }
9769out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009770 /*
9771 * If we have pinned a log and an error happened, we unpin tasks
9772 * trying to sync the log and force them to fallback to a transaction
9773 * commit if the log currently contains any of the inodes involved in
9774 * this rename operation (to ensure we do not persist a log with an
9775 * inconsistent state for any of these inodes or leading to any
9776 * inconsistencies when replayed). If the transaction was aborted, the
9777 * abortion reason is propagated to userspace when attempting to commit
9778 * the transaction. If the log does not contain any of these inodes, we
9779 * allow the tasks to sync it.
9780 */
9781 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009782 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9783 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9784 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009785 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009786 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009787 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009788
9789 if (root_log_pinned) {
9790 btrfs_end_log_trans(root);
9791 root_log_pinned = false;
9792 }
9793 if (dest_log_pinned) {
9794 btrfs_end_log_trans(dest);
9795 dest_log_pinned = false;
9796 }
9797 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009798 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799out_notrans:
9800 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009801 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009803 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009804
9805 return ret;
9806}
9807
9808static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9809 struct btrfs_root *root,
9810 struct inode *dir,
9811 struct dentry *dentry)
9812{
9813 int ret;
9814 struct inode *inode;
9815 u64 objectid;
9816 u64 index;
9817
9818 ret = btrfs_find_free_ino(root, &objectid);
9819 if (ret)
9820 return ret;
9821
9822 inode = btrfs_new_inode(trans, root, dir,
9823 dentry->d_name.name,
9824 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009825 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009826 objectid,
9827 S_IFCHR | WHITEOUT_MODE,
9828 &index);
9829
9830 if (IS_ERR(inode)) {
9831 ret = PTR_ERR(inode);
9832 return ret;
9833 }
9834
9835 inode->i_op = &btrfs_special_inode_operations;
9836 init_special_inode(inode, inode->i_mode,
9837 WHITEOUT_DEV);
9838
9839 ret = btrfs_init_inode_security(trans, inode, dir,
9840 &dentry->d_name);
9841 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009842 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009843
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009844 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9845 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009846 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009847 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848
9849 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009850out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009852 if (ret)
9853 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009854 iput(inode);
9855
Filipe Mananac9901612016-05-05 01:41:57 +01009856 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009857}
9858
Chris Mason39279cc2007-06-12 06:35:45 -04009859static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009860 struct inode *new_dir, struct dentry *new_dentry,
9861 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009863 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009864 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009865 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009866 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9867 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009868 struct inode *new_inode = d_inode(new_dentry);
9869 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009870 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009871 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009872 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009873 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009874 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009875
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009876 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009877 return -EPERM;
9878
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009879 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009880 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009881 return -EXDEV;
9882
Li Zefan33345d012011-04-20 10:31:50 +08009883 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009884 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 return -ENOTEMPTY;
9886
Chris Mason39279cc2007-06-12 06:35:45 -04009887 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009888 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009889 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009890
9891
9892 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009893 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009894 new_dentry->d_name.name,
9895 new_dentry->d_name.len);
9896
9897 if (ret) {
9898 if (ret == -EEXIST) {
9899 /* we shouldn't get
9900 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309901 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009902 return ret;
9903 }
9904 } else {
9905 /* maybe -EOVERFLOW */
9906 return ret;
9907 }
9908 }
9909 ret = 0;
9910
Chris Mason5a3f23d2009-03-31 13:27:11 -04009911 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009912 * we're using rename to replace one file with another. Start IO on it
9913 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009914 */
Chris Mason8d875f92014-08-12 10:47:42 -07009915 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009916 filemap_flush(old_inode->i_mapping);
9917
Yan, Zheng76dda932009-09-21 16:00:26 -04009918 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009919 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009920 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009921 /*
9922 * We want to reserve the absolute worst case amount of items. So if
9923 * both inodes are subvols and we need to unlink them then that would
9924 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009925 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009926 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9927 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009928 * If our rename has the whiteout flag, we need more 5 units for the
9929 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9930 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009931 */
Filipe Manana5062af32016-05-05 10:26:26 +01009932 trans_num_items = 11;
9933 if (flags & RENAME_WHITEOUT)
9934 trans_num_items += 5;
9935 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009936 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009937 ret = PTR_ERR(trans);
9938 goto out_notrans;
9939 }
Chris Mason5f39d392007-10-15 16:14:19 -04009940
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009941 if (dest != root)
9942 btrfs_record_root_in_trans(trans, dest);
9943
Nikolay Borisov877574e2017-02-20 13:50:33 +02009944 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009945 if (ret)
9946 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009947
Miao Xie67de1172013-12-26 13:07:06 +08009948 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009949 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009950 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009951 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009952 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009953 btrfs_pin_log_trans(root);
9954 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009955 ret = btrfs_insert_inode_ref(trans, dest,
9956 new_dentry->d_name.name,
9957 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009958 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009959 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009960 if (ret)
9961 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009962 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009963
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009964 inode_inc_iversion(old_dir);
9965 inode_inc_iversion(new_dir);
9966 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009967 old_dir->i_ctime = old_dir->i_mtime =
9968 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009969 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009970
Chris Mason12fcfd22009-03-24 10:24:20 -04009971 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009972 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9973 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009974
Li Zefan33345d012011-04-20 10:31:50 +08009975 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009976 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9977 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9978 old_dentry->d_name.name,
9979 old_dentry->d_name.len);
9980 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009981 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9982 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009983 old_dentry->d_name.name,
9984 old_dentry->d_name.len);
9985 if (!ret)
9986 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009987 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009988 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009989 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009990 goto out_fail;
9991 }
Chris Mason39279cc2007-06-12 06:35:45 -04009992
9993 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009994 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009995 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009996 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009997 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9998 root_objectid = BTRFS_I(new_inode)->location.objectid;
9999 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10000 root_objectid,
10001 new_dentry->d_name.name,
10002 new_dentry->d_name.len);
10003 BUG_ON(new_inode->i_nlink == 0);
10004 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010005 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10006 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010007 new_dentry->d_name.name,
10008 new_dentry->d_name.len);
10009 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010010 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010011 ret = btrfs_orphan_add(trans,
10012 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010013 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010014 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010015 goto out_fail;
10016 }
Chris Mason39279cc2007-06-12 06:35:45 -040010017 }
Josef Bacikaec74772008-07-24 12:12:38 -040010018
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010019 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010020 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010021 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010022 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010023 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010024 goto out_fail;
10025 }
Chris Mason39279cc2007-06-12 06:35:45 -040010026
Miao Xie67de1172013-12-26 13:07:06 +080010027 if (old_inode->i_nlink == 1)
10028 BTRFS_I(old_inode)->dir_index = index;
10029
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010030 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010031 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010032
David Sterbaf85b7372017-01-20 14:54:07 +010010033 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10034 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010035 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010036 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010037 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010038
10039 if (flags & RENAME_WHITEOUT) {
10040 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10041 old_dentry);
10042
10043 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010044 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010045 goto out_fail;
10046 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010047 }
Chris Mason39279cc2007-06-12 06:35:45 -040010048out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010049 /*
10050 * If we have pinned the log and an error happened, we unpin tasks
10051 * trying to sync the log and force them to fallback to a transaction
10052 * commit if the log currently contains any of the inodes involved in
10053 * this rename operation (to ensure we do not persist a log with an
10054 * inconsistent state for any of these inodes or leading to any
10055 * inconsistencies when replayed). If the transaction was aborted, the
10056 * abortion reason is propagated to userspace when attempting to commit
10057 * the transaction. If the log does not contain any of these inodes, we
10058 * allow the tasks to sync it.
10059 */
10060 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010061 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10062 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10063 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010064 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010065 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010066 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010067
10068 btrfs_end_log_trans(root);
10069 log_pinned = false;
10070 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010071 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010072out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010073 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010075
Chris Mason39279cc2007-06-12 06:35:45 -040010076 return ret;
10077}
10078
Miklos Szeredi80ace852014-07-23 15:15:32 +020010079static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10080 struct inode *new_dir, struct dentry *new_dentry,
10081 unsigned int flags)
10082{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010083 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010084 return -EINVAL;
10085
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010086 if (flags & RENAME_EXCHANGE)
10087 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10088 new_dentry);
10089
10090 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010091}
10092
Miao Xie8ccf6f192012-10-25 09:28:04 +000010093static void btrfs_run_delalloc_work(struct btrfs_work *work)
10094{
10095 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010096 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010097
10098 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10099 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010100 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010101 filemap_flush(inode->i_mapping);
10102 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10103 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010104 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010105
10106 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010107 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010108 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010109 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010110 complete(&delalloc_work->completion);
10111}
10112
10113struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010114 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010115{
10116 struct btrfs_delalloc_work *work;
10117
David Sterba100d5702015-12-08 14:39:32 +010010118 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010119 if (!work)
10120 return NULL;
10121
10122 init_completion(&work->completion);
10123 INIT_LIST_HEAD(&work->list);
10124 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010125 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010126 WARN_ON_ONCE(!inode);
10127 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10128 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129
10130 return work;
10131}
10132
10133void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10134{
10135 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010136 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010137}
10138
Chris Masond352ac62008-09-29 15:18:18 -040010139/*
10140 * some fairly slow code that needs optimization. This walks the list
10141 * of all the inodes with pending delalloc and forces them to disk.
10142 */
Miao Xie6c255e62014-03-06 13:55:01 +080010143static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10144 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010145{
Chris Masonea8c2812008-08-04 23:17:27 -040010146 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010147 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010148 struct btrfs_delalloc_work *work, *next;
10149 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010150 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010151 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010152
Miao Xie8ccf6f192012-10-25 09:28:04 +000010153 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010154 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010155
Miao Xie573bfb72014-03-06 13:55:03 +080010156 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010157 spin_lock(&root->delalloc_lock);
10158 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010159 while (!list_empty(&splice)) {
10160 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010161 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010162
Miao Xieeb73c1b2013-05-15 07:48:22 +000010163 list_move_tail(&binode->delalloc_inodes,
10164 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010165 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010166 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010167 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010168 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010169 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010171
David Sterba651d4942015-11-27 19:24:16 +010010172 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010173 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010174 if (delay_iput)
10175 btrfs_add_delayed_iput(inode);
10176 else
10177 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010178 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010179 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010180 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010181 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010182 btrfs_queue_work(root->fs_info->flush_workers,
10183 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010184 ret++;
10185 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010186 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010187 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010188 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010189 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010190 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010191
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010192out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010193 list_for_each_entry_safe(work, next, &works, list) {
10194 list_del_init(&work->list);
10195 btrfs_wait_and_free_delalloc_work(work);
10196 }
10197
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 if (!list_empty_careful(&splice)) {
10199 spin_lock(&root->delalloc_lock);
10200 list_splice_tail(&splice, &root->delalloc_inodes);
10201 spin_unlock(&root->delalloc_lock);
10202 }
Miao Xie573bfb72014-03-06 13:55:03 +080010203 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010204 return ret;
10205}
10206
10207int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10208{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010209 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010210 int ret;
10211
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010212 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 return -EROFS;
10214
Miao Xie6c255e62014-03-06 13:55:01 +080010215 ret = __start_delalloc_inodes(root, delay_iput, -1);
10216 if (ret > 0)
10217 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 /*
10219 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010220 * we have to make sure the IO is actually started and that
10221 * ordered extents get created before we return
10222 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010223 atomic_inc(&fs_info->async_submit_draining);
10224 while (atomic_read(&fs_info->nr_async_submits) ||
10225 atomic_read(&fs_info->async_delalloc_pages)) {
10226 wait_event(fs_info->async_submit_wait,
10227 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10228 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010229 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010230 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010231 return ret;
10232}
10233
Miao Xie6c255e62014-03-06 13:55:01 +080010234int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10235 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236{
10237 struct btrfs_root *root;
10238 struct list_head splice;
10239 int ret;
10240
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010241 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010242 return -EROFS;
10243
10244 INIT_LIST_HEAD(&splice);
10245
Miao Xie573bfb72014-03-06 13:55:03 +080010246 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 spin_lock(&fs_info->delalloc_root_lock);
10248 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010249 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010250 root = list_first_entry(&splice, struct btrfs_root,
10251 delalloc_root);
10252 root = btrfs_grab_fs_root(root);
10253 BUG_ON(!root);
10254 list_move_tail(&root->delalloc_root,
10255 &fs_info->delalloc_roots);
10256 spin_unlock(&fs_info->delalloc_root_lock);
10257
Miao Xie6c255e62014-03-06 13:55:01 +080010258 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010259 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010260 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010261 goto out;
10262
Miao Xie6c255e62014-03-06 13:55:01 +080010263 if (nr != -1) {
10264 nr -= ret;
10265 WARN_ON(nr < 0);
10266 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010267 spin_lock(&fs_info->delalloc_root_lock);
10268 }
10269 spin_unlock(&fs_info->delalloc_root_lock);
10270
Miao Xie6c255e62014-03-06 13:55:01 +080010271 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010272 atomic_inc(&fs_info->async_submit_draining);
10273 while (atomic_read(&fs_info->nr_async_submits) ||
10274 atomic_read(&fs_info->async_delalloc_pages)) {
10275 wait_event(fs_info->async_submit_wait,
10276 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10277 atomic_read(&fs_info->async_delalloc_pages) == 0));
10278 }
10279 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010280out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010281 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010282 spin_lock(&fs_info->delalloc_root_lock);
10283 list_splice_tail(&splice, &fs_info->delalloc_roots);
10284 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010285 }
Miao Xie573bfb72014-03-06 13:55:03 +080010286 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010287 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010288}
10289
Chris Mason39279cc2007-06-12 06:35:45 -040010290static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10291 const char *symname)
10292{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010293 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010294 struct btrfs_trans_handle *trans;
10295 struct btrfs_root *root = BTRFS_I(dir)->root;
10296 struct btrfs_path *path;
10297 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010298 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010299 int err;
10300 int drop_inode = 0;
10301 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010302 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010303 int name_len;
10304 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010305 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010306 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010307 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010308
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010309 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010310 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010311 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010312
Josef Bacik9ed74f22009-09-11 16:12:44 -040010313 /*
10314 * 2 items for inode item and ref
10315 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010316 * 1 item for updating parent inode item
10317 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010318 * 1 item for xattr if selinux is on
10319 */
Filipe Manana9269d122015-12-31 18:16:29 +000010320 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010321 if (IS_ERR(trans))
10322 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010323
Li Zefan581bb052011-04-20 10:06:11 +080010324 err = btrfs_find_free_ino(root, &objectid);
10325 if (err)
10326 goto out_unlock;
10327
Josef Bacikaec74772008-07-24 12:12:38 -040010328 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010329 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10330 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010331 if (IS_ERR(inode)) {
10332 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010333 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010334 }
Chris Mason39279cc2007-06-12 06:35:45 -040010335
Casey Schauflerad19db72011-12-15 10:09:07 -050010336 /*
10337 * If the active LSM wants to access the inode during
10338 * d_instantiate it needs these. Smack checks to see
10339 * if the filesystem supports xattrs by looking at the
10340 * ops vector.
10341 */
10342 inode->i_fop = &btrfs_file_operations;
10343 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010344 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010345 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10346
10347 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10348 if (err)
10349 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010350
Chris Mason39279cc2007-06-12 06:35:45 -040010351 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010352 if (!path) {
10353 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010354 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010355 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010356 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010357 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010358 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010359 datasize = btrfs_file_extent_calc_inline_size(name_len);
10360 err = btrfs_insert_empty_item(trans, root, path, &key,
10361 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010362 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010363 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010364 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010365 }
Chris Mason5f39d392007-10-15 16:14:19 -040010366 leaf = path->nodes[0];
10367 ei = btrfs_item_ptr(leaf, path->slots[0],
10368 struct btrfs_file_extent_item);
10369 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10370 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010371 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010372 btrfs_set_file_extent_encryption(leaf, ei, 0);
10373 btrfs_set_file_extent_compression(leaf, ei, 0);
10374 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10375 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10376
Chris Mason39279cc2007-06-12 06:35:45 -040010377 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010378 write_extent_buffer(leaf, symname, ptr, name_len);
10379 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010380 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010381
Chris Mason39279cc2007-06-12 06:35:45 -040010382 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010383 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010384 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010385 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010386 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010387 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010388 /*
10389 * Last step, add directory indexes for our symlink inode. This is the
10390 * last step to avoid extra cleanup of these indexes if an error happens
10391 * elsewhere above.
10392 */
10393 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010394 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10395 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010396 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010397 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010398 goto out_unlock_inode;
10399 }
10400
10401 unlock_new_inode(inode);
10402 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010403
10404out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010405 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010406 if (drop_inode) {
10407 inode_dec_link_count(inode);
10408 iput(inode);
10409 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010410 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010411 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010412
10413out_unlock_inode:
10414 drop_inode = 1;
10415 unlock_new_inode(inode);
10416 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010417}
Chris Mason16432982008-04-10 10:23:21 -040010418
Josef Bacik0af3d002010-06-21 14:48:16 -040010419static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10420 u64 start, u64 num_bytes, u64 min_size,
10421 loff_t actual_len, u64 *alloc_hint,
10422 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010425 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10426 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010427 struct btrfs_root *root = BTRFS_I(inode)->root;
10428 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010429 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010430 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010431 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010432 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010433 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010434 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010435 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010436
Josef Bacik0af3d002010-06-21 14:48:16 -040010437 if (trans)
10438 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010439 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010440 if (own_trans) {
10441 trans = btrfs_start_transaction(root, 3);
10442 if (IS_ERR(trans)) {
10443 ret = PTR_ERR(trans);
10444 break;
10445 }
Yan Zhengd899e052008-10-30 14:25:28 -040010446 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010447
Byongho Leeee221842015-12-15 01:42:10 +090010448 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010449 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010450 /*
10451 * If we are severely fragmented we could end up with really
10452 * small allocations, so if the allocator is returning small
10453 * chunks lets make its job easier by only searching for those
10454 * sized chunks.
10455 */
10456 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010457 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10458 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010459 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010460 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010461 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010462 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010463 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010464 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010465
Josef Bacik0b670dc2015-09-23 17:11:16 -040010466 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010467 ret = insert_reserved_file_extent(trans, inode,
10468 cur_offset, ins.objectid,
10469 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010470 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010471 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010472 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010473 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010474 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010475 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010476 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010477 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010478 break;
10479 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010480
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010481 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010482 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010483
Josef Bacik5dc562c2012-08-17 13:14:17 -040010484 em = alloc_extent_map();
10485 if (!em) {
10486 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10487 &BTRFS_I(inode)->runtime_flags);
10488 goto next;
10489 }
10490
10491 em->start = cur_offset;
10492 em->orig_start = cur_offset;
10493 em->len = ins.offset;
10494 em->block_start = ins.objectid;
10495 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010496 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010497 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010498 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010499 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10500 em->generation = trans->transid;
10501
10502 while (1) {
10503 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010504 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010505 write_unlock(&em_tree->lock);
10506 if (ret != -EEXIST)
10507 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010508 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010509 cur_offset + ins.offset - 1,
10510 0);
10511 }
10512 free_extent_map(em);
10513next:
Yan Zhengd899e052008-10-30 14:25:28 -040010514 num_bytes -= ins.offset;
10515 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010516 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010517
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010518 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010519 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010520 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010521 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010522 (actual_len > inode->i_size) &&
10523 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010524 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010525 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010526 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010527 i_size = cur_offset;
10528 i_size_write(inode, i_size);
10529 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010530 }
10531
Yan Zhengd899e052008-10-30 14:25:28 -040010532 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010533
10534 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010535 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010536 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010537 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010538 break;
10539 }
Yan Zhengd899e052008-10-30 14:25:28 -040010540
Josef Bacik0af3d002010-06-21 14:48:16 -040010541 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010542 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010543 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010544 if (cur_offset < end)
10545 btrfs_free_reserved_data_space(inode, cur_offset,
10546 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010547 return ret;
10548}
10549
Josef Bacik0af3d002010-06-21 14:48:16 -040010550int btrfs_prealloc_file_range(struct inode *inode, int mode,
10551 u64 start, u64 num_bytes, u64 min_size,
10552 loff_t actual_len, u64 *alloc_hint)
10553{
10554 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10555 min_size, actual_len, alloc_hint,
10556 NULL);
10557}
10558
10559int btrfs_prealloc_file_range_trans(struct inode *inode,
10560 struct btrfs_trans_handle *trans, int mode,
10561 u64 start, u64 num_bytes, u64 min_size,
10562 loff_t actual_len, u64 *alloc_hint)
10563{
10564 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10565 min_size, actual_len, alloc_hint, trans);
10566}
10567
Chris Masone6dcd2d2008-07-17 12:53:50 -040010568static int btrfs_set_page_dirty(struct page *page)
10569{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010570 return __set_page_dirty_nobuffers(page);
10571}
10572
Al Viro10556cb2011-06-20 19:28:19 -040010573static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010574{
Li Zefanb83cc962010-12-20 16:04:08 +080010575 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010576 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010577
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010578 if (mask & MAY_WRITE &&
10579 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10580 if (btrfs_root_readonly(root))
10581 return -EROFS;
10582 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10583 return -EACCES;
10584 }
Al Viro2830ba72011-06-20 19:16:29 -040010585 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010586}
Chris Mason39279cc2007-06-12 06:35:45 -040010587
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010588static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10589{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010590 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010591 struct btrfs_trans_handle *trans;
10592 struct btrfs_root *root = BTRFS_I(dir)->root;
10593 struct inode *inode = NULL;
10594 u64 objectid;
10595 u64 index;
10596 int ret = 0;
10597
10598 /*
10599 * 5 units required for adding orphan entry
10600 */
10601 trans = btrfs_start_transaction(root, 5);
10602 if (IS_ERR(trans))
10603 return PTR_ERR(trans);
10604
10605 ret = btrfs_find_free_ino(root, &objectid);
10606 if (ret)
10607 goto out;
10608
10609 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010610 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010611 if (IS_ERR(inode)) {
10612 ret = PTR_ERR(inode);
10613 inode = NULL;
10614 goto out;
10615 }
10616
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010617 inode->i_fop = &btrfs_file_operations;
10618 inode->i_op = &btrfs_file_inode_operations;
10619
10620 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010621 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10622
Chris Masonb0d5d102014-09-08 13:08:51 -070010623 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10624 if (ret)
10625 goto out_inode;
10626
10627 ret = btrfs_update_inode(trans, root, inode);
10628 if (ret)
10629 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010630 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010631 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010632 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633
Filipe Manana5762b5c2014-08-01 00:10:32 +010010634 /*
10635 * We set number of links to 0 in btrfs_new_inode(), and here we set
10636 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10637 * through:
10638 *
10639 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10640 */
10641 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010642 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010643 d_tmpfile(dentry, inode);
10644 mark_inode_dirty(inode);
10645
10646out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010647 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010648 if (ret)
10649 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010650 btrfs_balance_delayed_items(fs_info);
10651 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010652 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010653
10654out_inode:
10655 unlock_new_inode(inode);
10656 goto out;
10657
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010658}
10659
David Sterba20a7db82017-02-17 16:24:29 +010010660__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010661static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010662{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010663 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010664}
10665
Josef Bacikc6100a42017-05-05 11:57:13 -040010666static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10667{
10668 struct inode *inode = private_data;
10669 return btrfs_sb(inode->i_sb);
10670}
10671
10672static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10673 u64 start, u64 end)
10674{
10675 struct inode *inode = private_data;
10676 u64 isize;
10677
10678 isize = i_size_read(inode);
10679 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10680 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10681 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10682 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10683 }
10684}
10685
10686void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10687{
10688 struct inode *inode = private_data;
10689 unsigned long index = start >> PAGE_SHIFT;
10690 unsigned long end_index = end >> PAGE_SHIFT;
10691 struct page *page;
10692
10693 while (index <= end_index) {
10694 page = find_get_page(inode->i_mapping, index);
10695 ASSERT(page); /* Pages should be in the extent_io_tree */
10696 set_page_writeback(page);
10697 put_page(page);
10698 index++;
10699 }
10700}
10701
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010702static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010703 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010704 .lookup = btrfs_lookup,
10705 .create = btrfs_create,
10706 .unlink = btrfs_unlink,
10707 .link = btrfs_link,
10708 .mkdir = btrfs_mkdir,
10709 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010710 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010711 .symlink = btrfs_symlink,
10712 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010713 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010714 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010715 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010716 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010717 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010718 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010719 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010720};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010721static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010722 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010723 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010724 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010725};
Yan, Zheng76dda932009-09-21 16:00:26 -040010726
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010727static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010728 .llseek = generic_file_llseek,
10729 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010730 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010731 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010732#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010733 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010734#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010735 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010736 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010737};
10738
David Sterba20e55062015-11-19 11:42:28 +010010739static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010740 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010741 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010742 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010743 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010744 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010745 .tree_fs_info = iotree_fs_info,
10746 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010747
10748 /* optional callbacks */
10749 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010750 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010751 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010752 .set_bit_hook = btrfs_set_bit_hook,
10753 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010754 .merge_extent_hook = btrfs_merge_extent_hook,
10755 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010756 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010757};
10758
Chris Mason35054392009-01-21 13:11:13 -050010759/*
10760 * btrfs doesn't support the bmap operation because swapfiles
10761 * use bmap to make a mapping of extents in the file. They assume
10762 * these extents won't change over the life of the file and they
10763 * use the bmap result to do IO directly to the drive.
10764 *
10765 * the btrfs bmap call would return logical addresses that aren't
10766 * suitable for IO and they also will change frequently as COW
10767 * operations happen. So, swapfile + btrfs == corruption.
10768 *
10769 * For now we're avoiding this by dropping bmap.
10770 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010771static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010772 .readpage = btrfs_readpage,
10773 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010774 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010775 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010776 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010777 .invalidatepage = btrfs_invalidatepage,
10778 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010779 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010780 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010781};
10782
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010783static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010784 .readpage = btrfs_readpage,
10785 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010786 .invalidatepage = btrfs_invalidatepage,
10787 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010788};
10789
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010790static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010791 .getattr = btrfs_getattr,
10792 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010793 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010794 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010795 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010796 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010797 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010798 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010799};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010800static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010801 .getattr = btrfs_getattr,
10802 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010803 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010804 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010805 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010806 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010807 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010808};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010809static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010810 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010811 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010812 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010813 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010814 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010815 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010816};
Yan, Zheng76dda932009-09-21 16:00:26 -040010817
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010818const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010819 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010820 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010821};