blob: f53470112670b2de73343e635e4304fd08816468 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050046#include <linux/iversion.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000062#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080063#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080064#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040065
66struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080067 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040068 struct btrfs_root *root;
69};
70
Filipe Mananaf28a4922015-12-08 19:23:20 +000071struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000072 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080075 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000076};
77
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070078static const struct inode_operations btrfs_dir_inode_operations;
79static const struct inode_operations btrfs_symlink_inode_operations;
80static const struct inode_operations btrfs_dir_ro_inode_operations;
81static const struct inode_operations btrfs_special_inode_operations;
82static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070083static const struct address_space_operations btrfs_aops;
84static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070085static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010086static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040087
88static struct kmem_cache *btrfs_inode_cachep;
89struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Qu Wenruo524272602017-03-08 10:25:52 +0800118static void __endio_write_update_ordered(struct inode *inode,
119 const u64 offset, const u64 bytes,
120 const bool uptodate);
121
122/*
123 * Cleanup all submitted ordered extents in specified range to handle errors
124 * from the fill_dellaloc() callback.
125 *
126 * NOTE: caller must ensure that when an error happens, it can not call
127 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
128 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
129 * to be released, which we want to happen only when finishing the ordered
130 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
131 * fill_delalloc() callback already does proper cleanup for the first page of
132 * the range, that is, it invokes the callback writepage_end_io_hook() for the
133 * range of the first page.
134 */
135static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
136 const u64 offset,
137 const u64 bytes)
138{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900139 unsigned long index = offset >> PAGE_SHIFT;
140 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
141 struct page *page;
142
143 while (index <= end_index) {
144 page = find_get_page(inode->i_mapping, index);
145 index++;
146 if (!page)
147 continue;
148 ClearPagePrivate2(page);
149 put_page(page);
150 }
Qu Wenruo524272602017-03-08 10:25:52 +0800151 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
152 bytes - PAGE_SIZE, false);
153}
154
Eric Sandeen48a3b632013-04-25 20:41:01 +0000155static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400156
Josef Bacik6a3891c2015-03-16 17:38:52 -0400157#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
158void btrfs_test_inode_set_ops(struct inode *inode)
159{
160 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
161}
162#endif
163
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000164static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500165 struct inode *inode, struct inode *dir,
166 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500167{
168 int err;
169
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000170 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500171 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500172 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500173 return err;
174}
175
Chris Masond352ac62008-09-29 15:18:18 -0400176/*
Chris Masonc8b97812008-10-29 14:49:59 -0400177 * this does all the hard work for inserting an inline extent into
178 * the btree. The caller should have done a btrfs_drop_extents so that
179 * no overlapping inline items exist in the btree
180 */
Chris Mason40f76582014-05-21 13:35:51 -0700181static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000182 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 struct btrfs_root *root, struct inode *inode,
184 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400186 struct page **compressed_pages)
187{
Chris Masonc8b97812008-10-29 14:49:59 -0400188 struct extent_buffer *leaf;
189 struct page *page = NULL;
190 char *kaddr;
191 unsigned long ptr;
192 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400193 int ret;
194 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400195 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400196
Li Zefanfe3f5662011-03-28 08:30:38 +0000197 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400198 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400199
Chris Masonc8b97812008-10-29 14:49:59 -0400200 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201
202 if (!extent_inserted) {
203 struct btrfs_key key;
204 size_t datasize;
205
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200206 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000207 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200208 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000209
210 datasize = btrfs_file_extent_calc_inline_size(cur_size);
211 path->leave_spinning = 1;
212 ret = btrfs_insert_empty_item(trans, root, path, &key,
213 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200214 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000215 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 }
217 leaf = path->nodes[0];
218 ei = btrfs_item_ptr(leaf, path->slots[0],
219 struct btrfs_file_extent_item);
220 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
221 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
222 btrfs_set_file_extent_encryption(leaf, ei, 0);
223 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
224 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
225 ptr = btrfs_file_extent_inline_start(ei);
226
Li Zefan261507a02010-12-17 14:21:50 +0800227 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400228 struct page *cpage;
229 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500230 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400231 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500232 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400234
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400238
239 i++;
240 ptr += cur_size;
241 compressed_size -= cur_size;
242 }
243 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800244 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 } else {
246 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300247 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400248 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800249 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300250 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400251 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800252 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300253 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400254 }
255 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000256 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Yan, Zhengc2167752009-11-12 09:34:21 +0000258 /*
259 * we're an inline extent, so nobody can
260 * extend the file past i_size without locking
261 * a page we already have locked.
262 *
263 * We must do any isize and inode updates
264 * before we unlock the pages. Otherwise we
265 * could end up racing with unlink.
266 */
Chris Masonc8b97812008-10-29 14:49:59 -0400267 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000269
Chris Masonc8b97812008-10-29 14:49:59 -0400270fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200271 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400272}
273
274
275/*
276 * conditionally insert an inline extent into the file. This
277 * does the checks required to make sure the data is small enough
278 * to fit as an inline extent.
279 */
Josef Bacik00361582013-08-14 14:02:47 -0400280static noinline int cow_file_range_inline(struct btrfs_root *root,
281 struct inode *inode, u64 start,
282 u64 end, size_t compressed_size,
283 int compress_type,
284 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400287 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 isize = i_size_read(inode);
289 u64 actual_end = min(end + 1, isize);
290 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400291 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400292 u64 data_len = inline_len;
293 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000294 struct btrfs_path *path;
295 int extent_inserted = 0;
296 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400297
298 if (compressed_size)
299 data_len = compressed_size;
300
301 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 actual_end > fs_info->sectorsize ||
303 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400304 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400305 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400306 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400307 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400308 return 1;
309 }
310
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 path = btrfs_alloc_path();
312 if (!path)
313 return -ENOMEM;
314
Josef Bacik00361582013-08-14 14:02:47 -0400315 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000316 if (IS_ERR(trans)) {
317 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400318 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000319 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400320 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400321
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000322 if (compressed_size && compressed_pages)
323 extent_item_size = btrfs_file_extent_calc_inline_size(
324 compressed_size);
325 else
326 extent_item_size = btrfs_file_extent_calc_inline_size(
327 inline_len);
328
329 ret = __btrfs_drop_extents(trans, root, inode, path,
330 start, aligned_end, NULL,
331 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400332 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400333 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400334 goto out;
335 }
Chris Masonc8b97812008-10-29 14:49:59 -0400336
337 if (isize > actual_end)
338 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000339 ret = insert_inline_extent(trans, path, extent_inserted,
340 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400341 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000342 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400343 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400344 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400345 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400346 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400347 ret = 1;
348 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100349 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400350
Josef Bacikbdc20e62013-02-28 13:23:38 -0500351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200352 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400353out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800354 /*
355 * Don't forget to free the reserved space, as for inlined extent
356 * it won't count as data extent, free them directly here.
357 * And at reserve time, it's always aligned to page size, so
358 * just free one page here.
359 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800360 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000361 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400362 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400363 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400364}
365
Chris Mason771ed682008-11-06 22:02:51 -0500366struct async_extent {
367 u64 start;
368 u64 ram_size;
369 u64 compressed_size;
370 struct page **pages;
371 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800372 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct list_head list;
374};
375
376struct async_cow {
377 struct inode *inode;
378 struct btrfs_root *root;
379 struct page *locked_page;
380 u64 start;
381 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600382 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500383 struct list_head extents;
384 struct btrfs_work work;
385};
386
387static noinline int add_async_extent(struct async_cow *cow,
388 u64 start, u64 ram_size,
389 u64 compressed_size,
390 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800391 unsigned long nr_pages,
392 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500393{
394 struct async_extent *async_extent;
395
396 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100397 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500398 async_extent->start = start;
399 async_extent->ram_size = ram_size;
400 async_extent->compressed_size = compressed_size;
401 async_extent->pages = pages;
402 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800403 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500404 list_add_tail(&async_extent->list, &cow->extents);
405 return 0;
406}
407
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300408static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800409{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800411
412 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400413 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800414 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200415 /* defrag ioctl */
416 if (BTRFS_I(inode)->defrag_compress)
417 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800418 /* bad compression ratios */
419 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
420 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400421 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800422 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200423 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300424 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800425 return 0;
426}
427
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200428static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800429 u64 start, u64 end, u64 num_bytes, u64 small_write)
430{
431 /* If this is a small write inside eof, kick off a defrag */
432 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200433 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800434 btrfs_add_inode_defrag(NULL, inode);
435}
436
Chris Masonc8b97812008-10-29 14:49:59 -0400437/*
Chris Mason771ed682008-11-06 22:02:51 -0500438 * we create compressed extents in two phases. The first
439 * phase compresses a range of pages that have already been
440 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400441 *
Chris Mason771ed682008-11-06 22:02:51 -0500442 * This is done inside an ordered work queue, and the compression
443 * is spread across many cpus. The actual IO submission is step
444 * two, and the ordered work queue takes care of making sure that
445 * happens in the same order things were put onto the queue by
446 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400447 *
Chris Mason771ed682008-11-06 22:02:51 -0500448 * If this code finds it can't get good compression, it puts an
449 * entry onto the work queue to write the uncompressed bytes. This
450 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300451 * are written in the same order that the flusher thread sent them
452 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400453 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100454static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500455 struct page *locked_page,
456 u64 start, u64 end,
457 struct async_cow *async_cow,
458 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400461 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400463 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500464 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400465 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400466 struct page **pages = NULL;
467 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400468 unsigned long total_compressed = 0;
469 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400470 int i;
471 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400473 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400474
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200475 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
476 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400477
Chris Mason42dc7ba2008-12-15 11:44:56 -0500478 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400479again:
480 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300481 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100482 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
483 nr_pages = min_t(unsigned long, nr_pages,
484 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400485
Chris Masonf03d9301f2009-02-04 09:31:06 -0500486 /*
487 * we don't want to send crud past the end of i_size through
488 * compression, that's just a waste of CPU time. So, if the
489 * end of the file is before the start of our current
490 * requested range of bytes, we bail out to the uncompressed
491 * cleanup code that can deal with all of this.
492 *
493 * It isn't really the fastest way to fix things, but this is a
494 * very uncommon corner.
495 */
496 if (actual_end <= start)
497 goto cleanup_and_bail_uncompressed;
498
Chris Masonc8b97812008-10-29 14:49:59 -0400499 total_compressed = actual_end - start;
500
Shilong Wang4bcbb332014-10-07 18:44:35 -0400501 /*
502 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400503 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400504 */
505 if (total_compressed <= blocksize &&
506 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
507 goto cleanup_and_bail_uncompressed;
508
David Sterba069eac72017-02-14 19:36:54 +0100509 total_compressed = min_t(unsigned long, total_compressed,
510 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400511 total_in = 0;
512 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400513
Chris Mason771ed682008-11-06 22:02:51 -0500514 /*
515 * we do compression for mount -o compress and when the
516 * inode has not been flagged as nocompress. This flag can
517 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300519 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400520 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100521 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800522 if (!pages) {
523 /* just bail out to the uncompressed code */
524 goto cont;
525 }
Chris Mason179e29e2007-11-01 11:28:41 -0400526
David Sterbaeec63c62017-07-17 19:41:31 +0200527 if (BTRFS_I(inode)->defrag_compress)
528 compress_type = BTRFS_I(inode)->defrag_compress;
529 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200530 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800531
Chris Mason4adaa612013-03-26 13:07:00 -0400532 /*
533 * we need to call clear_page_dirty_for_io on each
534 * page in the range. Otherwise applications with the file
535 * mmap'd can wander in and change the page contents while
536 * we are compressing them.
537 *
538 * If the compression fails for any reason, we set the pages
539 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300540 *
541 * Note that the remaining part is redirtied, the start pointer
542 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400543 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300544 if (!redirty) {
545 extent_range_clear_dirty_for_io(inode, start, end);
546 redirty = 1;
547 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200548
549 /* Compression level is applied here and only here */
550 ret = btrfs_compress_pages(
551 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800552 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100553 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100554 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800555 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100556 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400557
558 if (!ret) {
559 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300560 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100561 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400562 char *kaddr;
563
564 /* zero the tail end of the last page, we might be
565 * sending it down to disk
566 */
567 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800568 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400569 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300570 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800571 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 }
573 will_compress = 1;
574 }
575 }
Li Zefan560f7d72011-09-08 10:22:01 +0800576cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400577 if (start == 0) {
578 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300579 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400580 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500581 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400582 */
Josef Bacik00361582013-08-14 14:02:47 -0400583 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800584 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400585 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500586 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400587 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000588 total_compressed,
589 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400590 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100591 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400592 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400593 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
594 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100595 unsigned long page_error_op;
596
Filipe Mananae6eb4312014-10-10 10:45:12 +0100597 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400598
Chris Mason771ed682008-11-06 22:02:51 -0500599 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100600 * inline extent creation worked or returned error,
601 * we don't need to create any more async work items.
602 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400603 *
604 * We use DO_ACCOUNTING here because we need the
605 * delalloc_release_metadata to be done _after_ we drop
606 * our outstanding extent for clearing delalloc for this
607 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500608 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800609 extent_clear_unlock_delalloc(inode, start, end, end,
610 NULL, clear_flags,
611 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400612 PAGE_CLEAR_DIRTY |
613 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100614 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400615 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400616 goto free_pages_out;
617 }
618 }
619
620 if (will_compress) {
621 /*
622 * we aren't doing an inline extent round the compressed size
623 * up to a block size boundary so the allocator does sane
624 * things
625 */
Qu Wenruofda28322013-02-26 08:10:22 +0000626 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400627
628 /*
629 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300630 * win, compare the page count read with the blocks on disk,
631 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400632 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300633 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300634 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700635 *num_added += 1;
636
637 /*
638 * The async work queues will take care of doing actual
639 * allocation on disk for these compressed pages, and
640 * will submit them to the elevator.
641 */
Timofey Titovets11708622017-10-03 18:06:01 +0300642 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100643 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700644 compress_type);
645
Timofey Titovets11708622017-10-03 18:06:01 +0300646 if (start + total_in < end) {
647 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700648 pages = NULL;
649 cond_resched();
650 goto again;
651 }
652 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400653 }
654 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700655 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400656 /*
657 * the compression code ran but failed to make things smaller,
658 * free any pages it allocated and our page pointer array
659 */
David Sterba4d3a8002017-02-14 19:04:07 +0100660 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400661 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300662 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400663 }
664 kfree(pages);
665 pages = NULL;
666 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100667 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400668
669 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400670 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200671 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500672 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500673 }
Chris Masonc8b97812008-10-29 14:49:59 -0400674 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500675cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700676 /*
677 * No compression, but we still need to write the pages in the file
678 * we've been given so far. redirty the locked page if it corresponds
679 * to our extent and set things up for the async work queue to run
680 * cow_file_range to do the normal delalloc dance.
681 */
682 if (page_offset(locked_page) >= start &&
683 page_offset(locked_page) <= end)
684 __set_page_dirty_nobuffers(locked_page);
685 /* unlocked later on in the async handlers */
686
687 if (redirty)
688 extent_range_redirty_for_io(inode, start, end);
689 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
690 BTRFS_COMPRESS_NONE);
691 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500692
Filipe Mananac44f6492014-10-09 21:15:44 +0100693 return;
Chris Mason771ed682008-11-06 22:02:51 -0500694
695free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100696 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500697 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300698 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500699 }
Chris Masond3977122009-01-05 21:25:51 -0500700 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500701}
Chris Mason771ed682008-11-06 22:02:51 -0500702
Filipe Manana40ae8372014-10-06 22:14:24 +0100703static void free_async_extent_pages(struct async_extent *async_extent)
704{
705 int i;
706
707 if (!async_extent->pages)
708 return;
709
710 for (i = 0; i < async_extent->nr_pages; i++) {
711 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300712 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100713 }
714 kfree(async_extent->pages);
715 async_extent->nr_pages = 0;
716 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500717}
718
719/*
720 * phase two of compressed writeback. This is the ordered portion
721 * of the code, which only gets called in the order the work was
722 * queued. We walk all the async extents created by compress_file_range
723 * and send them down to the disk.
724 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100725static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500726 struct async_cow *async_cow)
727{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400728 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500729 struct async_extent *async_extent;
730 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500731 struct btrfs_key ins;
732 struct extent_map *em;
733 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500734 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500735 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500736
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500737again:
Chris Masond3977122009-01-05 21:25:51 -0500738 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500739 async_extent = list_entry(async_cow->extents.next,
740 struct async_extent, list);
741 list_del(&async_extent->list);
742
743 io_tree = &BTRFS_I(inode)->io_tree;
744
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500745retry:
Chris Mason771ed682008-11-06 22:02:51 -0500746 /* did the compression code fall back to uncompressed IO? */
747 if (!async_extent->pages) {
748 int page_started = 0;
749 unsigned long nr_written = 0;
750
751 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000752 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100753 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500754
755 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756 ret = cow_file_range(inode, async_cow->locked_page,
757 async_extent->start,
758 async_extent->start +
759 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800760 async_extent->start +
761 async_extent->ram_size - 1,
762 &page_started, &nr_written, 0,
763 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500764
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100765 /* JDM XXX */
766
Chris Mason771ed682008-11-06 22:02:51 -0500767 /*
768 * if page_started, cow_file_range inserted an
769 * inline extent and took care of all the unlocking
770 * and IO for us. Otherwise, we need to submit
771 * all those pages down to the drive.
772 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200774 extent_write_locked_range(inode,
775 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500776 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500777 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500778 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500779 else if (ret)
780 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500781 kfree(async_extent);
782 cond_resched();
783 continue;
784 }
785
786 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100787 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500788
Wang Xiaoguang18513092016-07-25 15:51:40 +0800789 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500790 async_extent->compressed_size,
791 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800792 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500793 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100794 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400796 if (ret == -ENOSPC) {
797 unlock_extent(io_tree, async_extent->start,
798 async_extent->start +
799 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800800
801 /*
802 * we need to redirty the pages if we decide to
803 * fallback to uncompressed IO, otherwise we
804 * will not submit these pages down to lower
805 * layers.
806 */
807 extent_range_redirty_for_io(inode,
808 async_extent->start,
809 async_extent->start +
810 async_extent->ram_size - 1);
811
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100812 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400813 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500815 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000816 /*
817 * here we're doing allocation and writeback of the
818 * compressed pages
819 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800820 em = create_io_em(inode, async_extent->start,
821 async_extent->ram_size, /* len */
822 async_extent->start, /* orig_start */
823 ins.objectid, /* block_start */
824 ins.offset, /* block_len */
825 ins.offset, /* orig_block_len */
826 async_extent->ram_size, /* ram_bytes */
827 async_extent->compress_type,
828 BTRFS_ORDERED_COMPRESSED);
829 if (IS_ERR(em))
830 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500831 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800832 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500833
Li Zefan261507a02010-12-17 14:21:50 +0800834 ret = btrfs_add_ordered_extent_compress(inode,
835 async_extent->start,
836 ins.objectid,
837 async_extent->ram_size,
838 ins.offset,
839 BTRFS_ORDERED_COMPRESSED,
840 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100841 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200842 btrfs_drop_extent_cache(BTRFS_I(inode),
843 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100844 async_extent->start +
845 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500846 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100847 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400848 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500849
Chris Mason771ed682008-11-06 22:02:51 -0500850 /*
851 * clear dirty, set writeback and unlock the pages.
852 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400853 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400854 async_extent->start +
855 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800856 async_extent->start +
857 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400858 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
859 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400860 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200861 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500862 async_extent->start,
863 async_extent->ram_size,
864 ins.objectid,
865 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600866 async_extent->nr_pages,
867 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100868 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
869 struct page *p = async_extent->pages[0];
870 const u64 start = async_extent->start;
871 const u64 end = start + async_extent->ram_size - 1;
872
873 p->mapping = inode->i_mapping;
874 tree->ops->writepage_end_io_hook(p, start, end,
875 NULL, 0);
876 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800877 extent_clear_unlock_delalloc(inode, start, end, end,
878 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100879 PAGE_END_WRITEBACK |
880 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100881 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100882 }
Chris Mason771ed682008-11-06 22:02:51 -0500883 alloc_hint = ins.objectid + ins.offset;
884 kfree(async_extent);
885 cond_resched();
886 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100887 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400889 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400890 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400892 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500893 async_extent->start +
894 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800895 async_extent->start +
896 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400897 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100898 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400899 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
900 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100901 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
902 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100903 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500905 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500906}
907
Josef Bacik4b46fce2010-05-23 11:00:55 -0400908static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
909 u64 num_bytes)
910{
911 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
912 struct extent_map *em;
913 u64 alloc_hint = 0;
914
915 read_lock(&em_tree->lock);
916 em = search_extent_mapping(em_tree, start, num_bytes);
917 if (em) {
918 /*
919 * if block start isn't an actual block number then find the
920 * first block in this inode and use that as a hint. If that
921 * block is also bogus then just don't worry about it.
922 */
923 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
924 free_extent_map(em);
925 em = search_extent_mapping(em_tree, 0, 0);
926 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
927 alloc_hint = em->block_start;
928 if (em)
929 free_extent_map(em);
930 } else {
931 alloc_hint = em->block_start;
932 free_extent_map(em);
933 }
934 }
935 read_unlock(&em_tree->lock);
936
937 return alloc_hint;
938}
939
Chris Mason771ed682008-11-06 22:02:51 -0500940/*
941 * when extent_io.c finds a delayed allocation range in the file,
942 * the call backs end up in this code. The basic idea is to
943 * allocate extents on disk for the range, and create ordered data structs
944 * in ram to track those extents.
945 *
946 * locked_page is the page that writepage had locked already. We use
947 * it to make sure we don't do extra locks or unlocks.
948 *
949 * *page_started is set to one if we unlock locked_page and do everything
950 * required to start IO on it. It may be clean and already done with
951 * IO when we return.
952 */
Josef Bacik00361582013-08-14 14:02:47 -0400953static noinline int cow_file_range(struct inode *inode,
954 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800955 u64 start, u64 end, u64 delalloc_end,
956 int *page_started, unsigned long *nr_written,
957 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500958{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400959 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400960 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500961 u64 alloc_hint = 0;
962 u64 num_bytes;
963 unsigned long ram_size;
964 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000965 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400966 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500967 struct btrfs_key ins;
968 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000969 unsigned clear_bits;
970 unsigned long page_ops;
971 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500972 int ret = 0;
973
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200974 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400975 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500976 ret = -EINVAL;
977 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400978 }
Chris Mason771ed682008-11-06 22:02:51 -0500979
Qu Wenruofda28322013-02-26 08:10:22 +0000980 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500981 num_bytes = max(blocksize, num_bytes);
982 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500983
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200984 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400985
Chris Mason771ed682008-11-06 22:02:51 -0500986 if (start == 0) {
987 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800988 ret = cow_file_range_inline(root, inode, start, end, 0,
989 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500990 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400991 /*
992 * We use DO_ACCOUNTING here because we need the
993 * delalloc_release_metadata to be run _after_ we drop
994 * our outstanding extent for clearing delalloc for this
995 * range.
996 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800997 extent_clear_unlock_delalloc(inode, start, end,
998 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400999 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001000 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1001 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001002 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1003 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001004 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001005 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001006 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001007 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001010 }
1011 }
Chris Masonc8b97812008-10-29 14:49:59 -04001012
1013 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001014 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001015
Josef Bacik4b46fce2010-05-23 11:00:55 -04001016 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001017 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1018 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001019
Chris Masond3977122009-01-05 21:25:51 -05001020 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001021 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001022 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001023 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001024 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001025 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001026 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001027 cur_alloc_size = ins.offset;
1028 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001029
Chris Mason771ed682008-11-06 22:02:51 -05001030 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001031 em = create_io_em(inode, start, ins.offset, /* len */
1032 start, /* orig_start */
1033 ins.objectid, /* block_start */
1034 ins.offset, /* block_len */
1035 ins.offset, /* orig_block_len */
1036 ram_size, /* ram_bytes */
1037 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001038 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001039 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001040 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001041 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001042
Chris Masone6dcd2d2008-07-17 12:53:50 -04001043 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001044 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001045 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001046 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001047
Yan Zheng17d217f2008-12-12 10:03:38 -05001048 if (root->root_key.objectid ==
1049 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1050 ret = btrfs_reloc_clone_csums(inode, start,
1051 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001052 /*
1053 * Only drop cache here, and process as normal.
1054 *
1055 * We must not allow extent_clear_unlock_delalloc()
1056 * at out_unlock label to free meta of this ordered
1057 * extent, as its meta should be freed by
1058 * btrfs_finish_ordered_io().
1059 *
1060 * So we must continue until @start is increased to
1061 * skip current ordered extent.
1062 */
Josef Bacik00361582013-08-14 14:02:47 -04001063 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001064 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1065 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001066 }
1067
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001068 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001069
Chris Masonc8b97812008-10-29 14:49:59 -04001070 /* we're not doing compressed IO, don't unlock the first
1071 * page (which the caller expects to stay locked), don't
1072 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001073 *
1074 * Do set the Private2 bit so we know this page was properly
1075 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001076 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001077 page_ops = unlock ? PAGE_UNLOCK : 0;
1078 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001079
Josef Bacikc2790a22013-07-29 11:20:47 -04001080 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001081 start + ram_size - 1,
1082 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001083 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001084 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001085 if (disk_num_bytes < cur_alloc_size)
1086 disk_num_bytes = 0;
1087 else
1088 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001089 num_bytes -= cur_alloc_size;
1090 alloc_hint = ins.objectid + ins.offset;
1091 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001092 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001093
1094 /*
1095 * btrfs_reloc_clone_csums() error, since start is increased
1096 * extent_clear_unlock_delalloc() at out_unlock label won't
1097 * free metadata of current ordered extent, we're OK to exit.
1098 */
1099 if (ret)
1100 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001101 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001102out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001103 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001104
Filipe Mananad9f85962014-08-25 10:43:00 +01001105out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001106 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001107out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001108 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001109 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001110out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001111 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1112 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001113 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1114 PAGE_END_WRITEBACK;
1115 /*
1116 * If we reserved an extent for our delalloc range (or a subrange) and
1117 * failed to create the respective ordered extent, then it means that
1118 * when we reserved the extent we decremented the extent's size from
1119 * the data space_info's bytes_may_use counter and incremented the
1120 * space_info's bytes_reserved counter by the same amount. We must make
1121 * sure extent_clear_unlock_delalloc() does not try to decrement again
1122 * the data space_info's bytes_may_use counter, therefore we do not pass
1123 * it the flag EXTENT_CLEAR_DATA_RESV.
1124 */
1125 if (extent_reserved) {
1126 extent_clear_unlock_delalloc(inode, start,
1127 start + cur_alloc_size,
1128 start + cur_alloc_size,
1129 locked_page,
1130 clear_bits,
1131 page_ops);
1132 start += cur_alloc_size;
1133 if (start >= end)
1134 goto out;
1135 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001136 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1137 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001138 clear_bits | EXTENT_CLEAR_DATA_RESV,
1139 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001140 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001141}
Chris Masonc8b97812008-10-29 14:49:59 -04001142
Chris Mason771ed682008-11-06 22:02:51 -05001143/*
1144 * work queue call back to started compression on a file and pages
1145 */
1146static noinline void async_cow_start(struct btrfs_work *work)
1147{
1148 struct async_cow *async_cow;
1149 int num_added = 0;
1150 async_cow = container_of(work, struct async_cow, work);
1151
1152 compress_file_range(async_cow->inode, async_cow->locked_page,
1153 async_cow->start, async_cow->end, async_cow,
1154 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001155 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001156 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001157 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001158 }
Chris Mason771ed682008-11-06 22:02:51 -05001159}
1160
1161/*
1162 * work queue call back to submit previously compressed pages
1163 */
1164static noinline void async_cow_submit(struct btrfs_work *work)
1165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001166 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001167 struct async_cow *async_cow;
1168 struct btrfs_root *root;
1169 unsigned long nr_pages;
1170
1171 async_cow = container_of(work, struct async_cow, work);
1172
1173 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001174 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001175 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1176 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001177
David Sterbaee863952015-02-16 19:41:40 +01001178 /*
1179 * atomic_sub_return implies a barrier for waitqueue_active
1180 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001181 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001182 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001183 waitqueue_active(&fs_info->async_submit_wait))
1184 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001185
Chris Masond3977122009-01-05 21:25:51 -05001186 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001187 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001188}
Chris Masonc8b97812008-10-29 14:49:59 -04001189
Chris Mason771ed682008-11-06 22:02:51 -05001190static noinline void async_cow_free(struct btrfs_work *work)
1191{
1192 struct async_cow *async_cow;
1193 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001194 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001195 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001196 kfree(async_cow);
1197}
1198
1199static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1200 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001201 unsigned long *nr_written,
1202 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001204 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001205 struct async_cow *async_cow;
1206 struct btrfs_root *root = BTRFS_I(inode)->root;
1207 unsigned long nr_pages;
1208 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001209
Chris Masona3429ab2009-10-08 12:30:20 -04001210 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001211 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001212 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001213 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001215 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001216 async_cow->root = root;
1217 async_cow->locked_page = locked_page;
1218 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001219 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001220
Wang Shilongf79707b2014-07-17 11:44:09 +08001221 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001222 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001223 cur_end = end;
1224 else
Byongho Leeee221842015-12-15 01:42:10 +09001225 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001226
1227 async_cow->end = cur_end;
1228 INIT_LIST_HEAD(&async_cow->extents);
1229
Liu Bo9e0af232014-08-15 23:36:53 +08001230 btrfs_init_work(&async_cow->work,
1231 btrfs_delalloc_helper,
1232 async_cow_start, async_cow_submit,
1233 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001234
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001235 nr_pages = (cur_end - start + PAGE_SIZE) >>
1236 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001237 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001238
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001239 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001240
Chris Mason771ed682008-11-06 22:02:51 -05001241 *nr_written += nr_pages;
1242 start = cur_end + 1;
1243 }
1244 *page_started = 1;
1245 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001246}
1247
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001248static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001249 u64 bytenr, u64 num_bytes)
1250{
1251 int ret;
1252 struct btrfs_ordered_sum *sums;
1253 LIST_HEAD(list);
1254
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001255 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001256 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001257 if (ret == 0 && list_empty(&list))
1258 return 0;
1259
1260 while (!list_empty(&list)) {
1261 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1262 list_del(&sums->list);
1263 kfree(sums);
1264 }
1265 return 1;
1266}
1267
Chris Masond352ac62008-09-29 15:18:18 -04001268/*
1269 * when nowcow writeback call back. This checks for snapshots or COW copies
1270 * of the extents that exist in the file, and COWs the file as required.
1271 *
1272 * If no cow copies or snapshots exist, we write directly to the existing
1273 * blocks on disk
1274 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001275static noinline int run_delalloc_nocow(struct inode *inode,
1276 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001277 u64 start, u64 end, int *page_started, int force,
1278 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001281 struct btrfs_root *root = BTRFS_I(inode)->root;
1282 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001283 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001285 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001286 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 u64 cow_start;
1288 u64 cur_offset;
1289 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 u64 disk_bytenr;
1292 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001293 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001294 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001296 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001297 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 int nocow;
1299 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001300 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001301 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
1303 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001304 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001305 extent_clear_unlock_delalloc(inode, start, end, end,
1306 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001307 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001308 EXTENT_DO_ACCOUNTING |
1309 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001310 PAGE_CLEAR_DIRTY |
1311 PAGE_SET_WRITEBACK |
1312 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001313 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001314 }
Li Zefan82d59022011-04-20 10:33:24 +08001315
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001316 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001317
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 cow_start = (u64)-1;
1319 cur_offset = start;
1320 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001321 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001323 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001324 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1326 leaf = path->nodes[0];
1327 btrfs_item_key_to_cpu(leaf, &found_key,
1328 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001329 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 found_key.type == BTRFS_EXTENT_DATA_KEY)
1331 path->slots[0]--;
1332 }
1333 check_prev = 0;
1334next_slot:
1335 leaf = path->nodes[0];
1336 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1337 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001338 if (ret < 0) {
1339 if (cow_start != (u64)-1)
1340 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001341 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001342 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 if (ret > 0)
1344 break;
1345 leaf = path->nodes[0];
1346 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 nocow = 0;
1349 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001350 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001352
Filipe Manana1d512cb2015-11-09 00:33:58 +00001353 if (found_key.objectid > ino)
1354 break;
1355 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1356 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1357 path->slots[0]++;
1358 goto next_slot;
1359 }
1360 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 found_key.offset > end)
1362 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001363
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (found_key.offset > cur_offset) {
1365 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001366 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 goto out_check;
1368 }
Chris Masonc31f8832008-01-08 15:46:31 -05001369
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 fi = btrfs_item_ptr(leaf, path->slots[0],
1371 struct btrfs_file_extent_item);
1372 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001373
Josef Bacikcc95bef2013-04-04 14:31:27 -04001374 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1376 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 extent_end = found_key.offset +
1380 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001381 disk_num_bytes =
1382 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 if (extent_end <= start) {
1384 path->slots[0]++;
1385 goto next_slot;
1386 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001387 if (disk_bytenr == 0)
1388 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001389 if (btrfs_file_extent_compression(leaf, fi) ||
1390 btrfs_file_extent_encryption(leaf, fi) ||
1391 btrfs_file_extent_other_encoding(leaf, fi))
1392 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001393 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1394 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001395 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001397 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001398 found_key.offset -
1399 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001400 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 disk_bytenr += cur_offset - found_key.offset;
1403 num_bytes = min(end + 1, extent_end) - cur_offset;
1404 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 * if there are pending snapshots for this root,
1406 * we fall into common COW way.
1407 */
1408 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001409 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001410 if (!err)
1411 goto out_check;
1412 }
1413 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001414 * force cow if csum exists in the range.
1415 * this ensure that csum for a given extent are
1416 * either valid or do not exist.
1417 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001418 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 num_bytes)) {
1420 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001421 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001422 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001423 }
1424 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1425 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001426 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001427 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001428 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 nocow = 1;
1430 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1431 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001432 btrfs_file_extent_inline_len(leaf,
1433 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001434 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001435 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 } else {
1437 BUG_ON(1);
1438 }
1439out_check:
1440 if (extent_end <= start) {
1441 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001442 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001443 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001444 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001445 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001446 goto next_slot;
1447 }
1448 if (!nocow) {
1449 if (cow_start == (u64)-1)
1450 cow_start = cur_offset;
1451 cur_offset = extent_end;
1452 if (cur_offset > end)
1453 break;
1454 path->slots[0]++;
1455 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001456 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001457
David Sterbab3b4aa72011-04-21 01:20:15 +02001458 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001460 ret = cow_file_range(inode, locked_page,
1461 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001462 end, page_started, nr_written, 1,
1463 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001464 if (ret) {
1465 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001466 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001467 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001468 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001469 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001470 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001471 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001472 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001473 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001474
Yan Zhengd899e052008-10-30 14:25:28 -04001475 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001476 u64 orig_start = found_key.offset - extent_offset;
1477
1478 em = create_io_em(inode, cur_offset, num_bytes,
1479 orig_start,
1480 disk_bytenr, /* block_start */
1481 num_bytes, /* block_len */
1482 disk_num_bytes, /* orig_block_len */
1483 ram_bytes, BTRFS_COMPRESS_NONE,
1484 BTRFS_ORDERED_PREALLOC);
1485 if (IS_ERR(em)) {
1486 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001487 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001488 if (nocow)
1489 btrfs_dec_nocow_writers(fs_info,
1490 disk_bytenr);
1491 ret = PTR_ERR(em);
1492 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001493 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001494 free_extent_map(em);
1495 }
1496
1497 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001498 type = BTRFS_ORDERED_PREALLOC;
1499 } else {
1500 type = BTRFS_ORDERED_NOCOW;
1501 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001502
1503 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001504 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001505 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001506 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001507 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001508
Yan, Zhengefa56462010-05-16 10:49:59 -04001509 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001510 BTRFS_DATA_RELOC_TREE_OBJECTID)
1511 /*
1512 * Error handled later, as we must prevent
1513 * extent_clear_unlock_delalloc() in error handler
1514 * from freeing metadata of created ordered extent.
1515 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001516 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1517 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001518
Josef Bacikc2790a22013-07-29 11:20:47 -04001519 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001520 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001521 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001522 EXTENT_DELALLOC |
1523 EXTENT_CLEAR_DATA_RESV,
1524 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1525
Wang Shilonge9894fd2014-03-27 11:12:25 +08001526 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001527 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001529
1530 /*
1531 * btrfs_reloc_clone_csums() error, now we're OK to call error
1532 * handler, as metadata for created ordered extent will only
1533 * be freed by btrfs_finish_ordered_io().
1534 */
1535 if (ret)
1536 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001537 if (cur_offset > end)
1538 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001539 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001540 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001541
Josef Bacik17ca04a2012-05-31 15:58:55 -04001542 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001544 cur_offset = end;
1545 }
1546
Yan Zheng80ff3852008-10-30 14:20:02 -04001547 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001548 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1549 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001550 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001551 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 }
1553
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001554error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001555 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001556 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001557 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001558 EXTENT_DELALLOC | EXTENT_DEFRAG |
1559 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1560 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001561 PAGE_SET_WRITEBACK |
1562 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001563 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001565}
1566
Wang Shilong47059d92014-07-03 18:22:07 +08001567static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1568{
1569
1570 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1571 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1572 return 0;
1573
1574 /*
1575 * @defrag_bytes is a hint value, no spinlock held here,
1576 * if is not zero, it means the file is defragging.
1577 * Force cow if given extent needs to be defragged.
1578 */
1579 if (BTRFS_I(inode)->defrag_bytes &&
1580 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1581 EXTENT_DEFRAG, 0, NULL))
1582 return 1;
1583
1584 return 0;
1585}
1586
Chris Masond352ac62008-09-29 15:18:18 -04001587/*
1588 * extent_io.c call back to do delayed allocation processing
1589 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001590static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001591 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001592 unsigned long *nr_written,
1593 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001594{
Josef Bacikc6100a42017-05-05 11:57:13 -04001595 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001596 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001597 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001598 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001599
Wang Shilong47059d92014-07-03 18:22:07 +08001600 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001601 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001602 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001603 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001604 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001605 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001606 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001607 ret = cow_file_range(inode, locked_page, start, end, end,
1608 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001609 } else {
1610 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1611 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001612 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001613 page_started, nr_written,
1614 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001615 }
Qu Wenruo524272602017-03-08 10:25:52 +08001616 if (ret)
1617 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001618 return ret;
1619}
1620
Josef Bacikc6100a42017-05-05 11:57:13 -04001621static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001622 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001623{
Josef Bacikc6100a42017-05-05 11:57:13 -04001624 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001625 u64 size;
1626
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001627 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001628 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001629 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001630
Josef Bacikdcab6a32015-02-11 15:08:59 -05001631 size = orig->end - orig->start + 1;
1632 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001633 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634 u64 new_size;
1635
1636 /*
Josef Bacikba117212015-03-13 15:01:24 -04001637 * See the explanation in btrfs_merge_extent_hook, the same
1638 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639 */
1640 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001641 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001642 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001643 num_extents += count_max_extents(new_size);
1644 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001645 return;
1646 }
1647
Josef Bacik9e0baf62011-07-15 15:16:44 +00001648 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001649 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001650 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651}
1652
1653/*
1654 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1655 * extents so we can keep track of new extents that are just merged onto old
1656 * extents, such as when we are doing sequential writes, so we can properly
1657 * account for the metadata space we'll need.
1658 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001659static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001660 struct extent_state *new,
1661 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662{
Josef Bacikc6100a42017-05-05 11:57:13 -04001663 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001664 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001665 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001666
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667 /* not delalloc, ignore it */
1668 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001669 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001670
Josef Bacik8461a3d2015-03-13 15:12:08 -04001671 if (new->start > other->start)
1672 new_size = new->end - other->start + 1;
1673 else
1674 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001675
1676 /* we're not bigger than the max, unreserve the space and go */
1677 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1678 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001679 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001680 spin_unlock(&BTRFS_I(inode)->lock);
1681 return;
1682 }
1683
1684 /*
Josef Bacikba117212015-03-13 15:01:24 -04001685 * We have to add up either side to figure out how many extents were
1686 * accounted for before we merged into one big extent. If the number of
1687 * extents we accounted for is <= the amount we need for the new range
1688 * then we can return, otherwise drop. Think of it like this
1689 *
1690 * [ 4k][MAX_SIZE]
1691 *
1692 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1693 * need 2 outstanding extents, on one side we have 1 and the other side
1694 * we have 1 so they are == and we can return. But in this case
1695 *
1696 * [MAX_SIZE+4k][MAX_SIZE+4k]
1697 *
1698 * Each range on their own accounts for 2 extents, but merged together
1699 * they are only 3 extents worth of accounting, so we need to drop in
1700 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001701 */
Josef Bacikba117212015-03-13 15:01:24 -04001702 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001703 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001704 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001705 num_extents += count_max_extents(old_size);
1706 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001707 return;
1708
Josef Bacik9e0baf62011-07-15 15:16:44 +00001709 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001710 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001711 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001712}
1713
Miao Xieeb73c1b2013-05-15 07:48:22 +00001714static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1715 struct inode *inode)
1716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1718
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 spin_lock(&root->delalloc_lock);
1720 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1721 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1722 &root->delalloc_inodes);
1723 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1724 &BTRFS_I(inode)->runtime_flags);
1725 root->nr_delalloc_inodes++;
1726 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001727 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 BUG_ON(!list_empty(&root->delalloc_root));
1729 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001730 &fs_info->delalloc_roots);
1731 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732 }
1733 }
1734 spin_unlock(&root->delalloc_lock);
1735}
1736
1737static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001738 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001740 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741
Miao Xieeb73c1b2013-05-15 07:48:22 +00001742 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001743 if (!list_empty(&inode->delalloc_inodes)) {
1744 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001746 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001747 root->nr_delalloc_inodes--;
1748 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001749 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001750 BUG_ON(list_empty(&root->delalloc_root));
1751 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001752 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001753 }
1754 }
1755 spin_unlock(&root->delalloc_lock);
1756}
1757
Chris Masond352ac62008-09-29 15:18:18 -04001758/*
1759 * extent_io.c set_bit_hook, used to track delayed allocation
1760 * bytes in this file, and to maintain the list of inodes that
1761 * have pending delalloc work to be done.
1762 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001763static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001764 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001765{
Josef Bacikc6100a42017-05-05 11:57:13 -04001766 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001767
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1769
Wang Shilong47059d92014-07-03 18:22:07 +08001770 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1771 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001772 /*
1773 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001774 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001775 * bit, which is only set or cleared with irqs on
1776 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001778 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001779 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001780 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001781 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001782
Josef Bacik8b62f872017-10-19 14:15:55 -04001783 spin_lock(&BTRFS_I(inode)->lock);
1784 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1785 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001786
Josef Bacik6a3891c2015-03-16 17:38:52 -04001787 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001788 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001789 return;
1790
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001791 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1792 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001793 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001794 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001795 if (*bits & EXTENT_DEFRAG)
1796 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001797 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001798 &BTRFS_I(inode)->runtime_flags))
1799 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001800 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001801 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001802
1803 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1804 (*bits & EXTENT_DELALLOC_NEW)) {
1805 spin_lock(&BTRFS_I(inode)->lock);
1806 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1807 state->start;
1808 spin_unlock(&BTRFS_I(inode)->lock);
1809 }
Chris Mason291d6732008-01-29 15:55:23 -05001810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
1813 * extent_io.c clear_bit_hook, see set_bit_hook for why
1814 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001815static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001816 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001817 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001818{
Josef Bacikc6100a42017-05-05 11:57:13 -04001819 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001821 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001822 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001823
Filipe Manana4a4b9642017-07-27 19:52:55 +01001824 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1825 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001826 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001827 spin_unlock(&inode->lock);
1828 }
Wang Shilong47059d92014-07-03 18:22:07 +08001829
Chris Mason75eff682008-12-15 15:54:40 -05001830 /*
1831 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001832 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001833 * bit, which is only set or cleared with irqs on
1834 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001835 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001836 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001837 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001838
Josef Bacik8b62f872017-10-19 14:15:55 -04001839 spin_lock(&inode->lock);
1840 btrfs_mod_outstanding_extents(inode, -num_extents);
1841 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001842
Josef Bacikb6d08f02013-09-27 14:57:43 -04001843 /*
1844 * We don't reserve metadata space for space cache inodes so we
1845 * don't need to call dellalloc_release_metadata if there is an
1846 * error.
1847 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001848 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001850 btrfs_delalloc_release_metadata(inode, len);
1851
Josef Bacik6a3891c2015-03-16 17:38:52 -04001852 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001853 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001854 return;
1855
Filipe Mananaa315e682017-03-06 23:04:20 +00001856 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1857 do_list && !(state->state & EXTENT_NORESERVE) &&
1858 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001859 btrfs_free_reserved_data_space_noquota(
1860 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001861 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001862
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001863 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1864 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001865 spin_lock(&inode->lock);
1866 inode->delalloc_bytes -= len;
1867 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001868 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001869 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001870 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001871 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001872 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001873
1874 if ((state->state & EXTENT_DELALLOC_NEW) &&
1875 (*bits & EXTENT_DELALLOC_NEW)) {
1876 spin_lock(&inode->lock);
1877 ASSERT(inode->new_delalloc_bytes >= len);
1878 inode->new_delalloc_bytes -= len;
1879 spin_unlock(&inode->lock);
1880 }
Chris Mason291d6732008-01-29 15:55:23 -05001881}
1882
Chris Masond352ac62008-09-29 15:18:18 -04001883/*
1884 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1885 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001886 *
1887 * return 1 if page cannot be merged to bio
1888 * return 0 if page can be merged to bio
1889 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001890 */
Mike Christie81a75f672016-06-05 14:31:54 -05001891int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001892 size_t size, struct bio *bio,
1893 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001894{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001895 struct inode *inode = page->mapping->host;
1896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 u64 length = 0;
1899 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001900 int ret;
1901
Chris Mason771ed682008-11-06 22:02:51 -05001902 if (bio_flags & EXTENT_BIO_COMPRESSED)
1903 return 0;
1904
Kent Overstreet4f024f32013-10-11 15:44:27 -07001905 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001906 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001907 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1908 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001909 if (ret < 0)
1910 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001911 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001912 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001913 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001914}
1915
Chris Masond352ac62008-09-29 15:18:18 -04001916/*
1917 * in order to insert checksums into the metadata in large chunks,
1918 * we wait until bio submission time. All the pages in the bio are
1919 * checksummed and sums are attached onto the ordered extent record.
1920 *
1921 * At IO completion time the cums attached on the ordered extent record
1922 * are inserted into the btree
1923 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001924static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001925 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001926 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001927{
Josef Bacikc6100a42017-05-05 11:57:13 -04001928 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001929 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001930
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001931 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001933 return 0;
1934}
Chris Masone0156402008-04-16 11:15:20 -04001935
Chris Mason4a69a412008-11-06 22:03:00 -05001936/*
1937 * in order to insert checksums into the metadata in large chunks,
1938 * we wait until bio submission time. All the pages in the bio are
1939 * checksummed and sums are attached onto the ordered extent record.
1940 *
1941 * At IO completion time the cums attached on the ordered extent record
1942 * are inserted into the btree
1943 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001944static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001945 int mirror_num, unsigned long bio_flags,
1946 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001947{
Josef Bacikc6100a42017-05-05 11:57:13 -04001948 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001949 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001951
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001952 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001953 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001954 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001955 bio_endio(bio);
1956 }
Stefan Behrens61891922012-11-05 18:51:52 +01001957 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001958}
1959
Chris Masond352ac62008-09-29 15:18:18 -04001960/*
Chris Masoncad321a2008-12-17 14:51:42 -05001961 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001962 * on write, or reading the csums from the tree before a read.
1963 *
1964 * Rules about async/sync submit,
1965 * a) read: sync submit
1966 *
1967 * b) write without checksum: sync submit
1968 *
1969 * c) write with checksum:
1970 * c-1) if bio is issued by fsync: sync submit
1971 * (sync_writers != 0)
1972 *
1973 * c-2) if root is reloc root: sync submit
1974 * (only in case of buffered IO)
1975 *
1976 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001977 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001978static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001979 int mirror_num, unsigned long bio_flags,
1980 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001981{
Josef Bacikc6100a42017-05-05 11:57:13 -04001982 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001983 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001984 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301985 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001986 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001987 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001988 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001989
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001990 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001991
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001992 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301993 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001994
Mike Christie37226b22016-06-05 14:31:52 -05001995 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001996 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001997 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001998 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001999
Chris Masond20f7042008-12-08 16:58:54 -05002000 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002001 ret = btrfs_submit_compressed_read(inode, bio,
2002 mirror_num,
2003 bio_flags);
2004 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002005 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002006 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002007 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002008 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002009 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002010 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002011 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002012 /* csum items have already been cloned */
2013 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2014 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002015 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002016 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2017 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002018 __btrfs_submit_bio_start,
2019 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002020 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002021 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002022 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002023 if (ret)
2024 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002025 }
2026
Chris Mason0b86a832008-03-24 15:01:56 -04002027mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002028 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002029
2030out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002031 if (ret) {
2032 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002033 bio_endio(bio);
2034 }
Stefan Behrens61891922012-11-05 18:51:52 +01002035 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002036}
Chris Mason6885f302008-02-20 16:11:05 -05002037
Chris Masond352ac62008-09-29 15:18:18 -04002038/*
2039 * given a list of ordered sums record them in the inode. This happens
2040 * at IO completion time based on sums calculated at bio submission time.
2041 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002042static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002043 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002044{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002045 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002046 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002047
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002048 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002049 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002050 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002051 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002052 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002053 if (ret)
2054 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002055 }
2056 return 0;
2057}
2058
Josef Bacik2ac55d42010-02-03 19:33:23 +00002059int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002060 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002061 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002062{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002063 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002064 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002065 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002066}
2067
Chris Masond352ac62008-09-29 15:18:18 -04002068/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002069struct btrfs_writepage_fixup {
2070 struct page *page;
2071 struct btrfs_work work;
2072};
2073
Christoph Hellwigb2950862008-12-02 09:54:17 -05002074static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002075{
2076 struct btrfs_writepage_fixup *fixup;
2077 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002078 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002079 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002080 struct page *page;
2081 struct inode *inode;
2082 u64 page_start;
2083 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002084 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002085
2086 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2087 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002088again:
Chris Mason247e7432008-07-17 12:53:51 -04002089 lock_page(page);
2090 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2091 ClearPageChecked(page);
2092 goto out_page;
2093 }
2094
2095 inode = page->mapping->host;
2096 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002097 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002098
David Sterbaff13db42015-12-03 14:30:40 +01002099 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002100 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002101
2102 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002103 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002104 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002105
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002106 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002107 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002108 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002109 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002110 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002111 unlock_page(page);
2112 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002113 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002114 goto again;
2115 }
Chris Mason247e7432008-07-17 12:53:51 -04002116
Qu Wenruo364ecf32017-02-27 15:10:38 +08002117 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002119 if (ret) {
2120 mapping_set_error(page->mapping, ret);
2121 end_extent_writepage(page, ret, page_start, page_end);
2122 ClearPageChecked(page);
2123 goto out;
2124 }
2125
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002126 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2127 &cached_state, 0);
2128 if (ret) {
2129 mapping_set_error(page->mapping, ret);
2130 end_extent_writepage(page, ret, page_start, page_end);
2131 ClearPageChecked(page);
2132 goto out;
2133 }
2134
Chris Mason247e7432008-07-17 12:53:51 -04002135 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002136 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002137 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002138out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002139 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002140 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002141out_page:
2142 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002143 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002144 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002145 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002146}
2147
2148/*
2149 * There are a few paths in the higher layers of the kernel that directly
2150 * set the page dirty bit without asking the filesystem if it is a
2151 * good idea. This causes problems because we want to make sure COW
2152 * properly happens and the data=ordered rules are followed.
2153 *
Chris Masonc8b97812008-10-29 14:49:59 -04002154 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002155 * hasn't been properly setup for IO. We kick off an async process
2156 * to fix it up. The async helper will wait for ordered extents, set
2157 * the delalloc bit and make it safe to write the page.
2158 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002159static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002160{
2161 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002163 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002164
Chris Mason8b62b722009-09-02 16:53:46 -04002165 /* this page is properly in the ordered list */
2166 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002167 return 0;
2168
2169 if (PageChecked(page))
2170 return -EAGAIN;
2171
2172 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2173 if (!fixup)
2174 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002175
Chris Mason247e7432008-07-17 12:53:51 -04002176 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002177 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002178 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2179 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002180 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002181 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002182 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002183}
2184
Yan Zhengd899e052008-10-30 14:25:28 -04002185static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2186 struct inode *inode, u64 file_pos,
2187 u64 disk_bytenr, u64 disk_num_bytes,
2188 u64 num_bytes, u64 ram_bytes,
2189 u8 compression, u8 encryption,
2190 u16 other_encoding, int extent_type)
2191{
2192 struct btrfs_root *root = BTRFS_I(inode)->root;
2193 struct btrfs_file_extent_item *fi;
2194 struct btrfs_path *path;
2195 struct extent_buffer *leaf;
2196 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002197 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002198 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002199 int ret;
2200
2201 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002202 if (!path)
2203 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002204
Chris Masona1ed8352009-09-11 12:27:37 -04002205 /*
2206 * we may be replacing one extent in the tree with another.
2207 * The new extent is pinned in the extent map, and we don't want
2208 * to drop it from the cache until it is completely in the btree.
2209 *
2210 * So, tell btrfs_drop_extents to leave this extent in the cache.
2211 * the caller is expected to unpin it and allow it to be merged
2212 * with the others.
2213 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002214 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2215 file_pos + num_bytes, NULL, 0,
2216 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002217 if (ret)
2218 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002219
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002220 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002221 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002222 ins.offset = file_pos;
2223 ins.type = BTRFS_EXTENT_DATA_KEY;
2224
2225 path->leave_spinning = 1;
2226 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2227 sizeof(*fi));
2228 if (ret)
2229 goto out;
2230 }
Yan Zhengd899e052008-10-30 14:25:28 -04002231 leaf = path->nodes[0];
2232 fi = btrfs_item_ptr(leaf, path->slots[0],
2233 struct btrfs_file_extent_item);
2234 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2235 btrfs_set_file_extent_type(leaf, fi, extent_type);
2236 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2237 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2238 btrfs_set_file_extent_offset(leaf, fi, 0);
2239 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2240 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2241 btrfs_set_file_extent_compression(leaf, fi, compression);
2242 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2243 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002244
Yan Zhengd899e052008-10-30 14:25:28 -04002245 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002246 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002247
2248 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002249
2250 ins.objectid = disk_bytenr;
2251 ins.offset = disk_num_bytes;
2252 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002253
Qu Wenruo297d7502015-09-08 17:08:37 +08002254 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002255 * Release the reserved range from inode dirty range map, as it is
2256 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002257 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002258 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2259 if (ret < 0)
2260 goto out;
2261 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002262 ret = btrfs_alloc_reserved_file_extent(trans, root,
2263 btrfs_ino(BTRFS_I(inode)),
2264 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002265out:
Yan Zhengd899e052008-10-30 14:25:28 -04002266 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002267
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002268 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002269}
2270
Liu Bo38c227d2013-01-29 03:18:40 +00002271/* snapshot-aware defrag */
2272struct sa_defrag_extent_backref {
2273 struct rb_node node;
2274 struct old_sa_defrag_extent *old;
2275 u64 root_id;
2276 u64 inum;
2277 u64 file_pos;
2278 u64 extent_offset;
2279 u64 num_bytes;
2280 u64 generation;
2281};
2282
2283struct old_sa_defrag_extent {
2284 struct list_head list;
2285 struct new_sa_defrag_extent *new;
2286
2287 u64 extent_offset;
2288 u64 bytenr;
2289 u64 offset;
2290 u64 len;
2291 int count;
2292};
2293
2294struct new_sa_defrag_extent {
2295 struct rb_root root;
2296 struct list_head head;
2297 struct btrfs_path *path;
2298 struct inode *inode;
2299 u64 file_pos;
2300 u64 len;
2301 u64 bytenr;
2302 u64 disk_len;
2303 u8 compress_type;
2304};
2305
2306static int backref_comp(struct sa_defrag_extent_backref *b1,
2307 struct sa_defrag_extent_backref *b2)
2308{
2309 if (b1->root_id < b2->root_id)
2310 return -1;
2311 else if (b1->root_id > b2->root_id)
2312 return 1;
2313
2314 if (b1->inum < b2->inum)
2315 return -1;
2316 else if (b1->inum > b2->inum)
2317 return 1;
2318
2319 if (b1->file_pos < b2->file_pos)
2320 return -1;
2321 else if (b1->file_pos > b2->file_pos)
2322 return 1;
2323
2324 /*
2325 * [------------------------------] ===> (a range of space)
2326 * |<--->| |<---->| =============> (fs/file tree A)
2327 * |<---------------------------->| ===> (fs/file tree B)
2328 *
2329 * A range of space can refer to two file extents in one tree while
2330 * refer to only one file extent in another tree.
2331 *
2332 * So we may process a disk offset more than one time(two extents in A)
2333 * and locate at the same extent(one extent in B), then insert two same
2334 * backrefs(both refer to the extent in B).
2335 */
2336 return 0;
2337}
2338
2339static void backref_insert(struct rb_root *root,
2340 struct sa_defrag_extent_backref *backref)
2341{
2342 struct rb_node **p = &root->rb_node;
2343 struct rb_node *parent = NULL;
2344 struct sa_defrag_extent_backref *entry;
2345 int ret;
2346
2347 while (*p) {
2348 parent = *p;
2349 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2350
2351 ret = backref_comp(backref, entry);
2352 if (ret < 0)
2353 p = &(*p)->rb_left;
2354 else
2355 p = &(*p)->rb_right;
2356 }
2357
2358 rb_link_node(&backref->node, parent, p);
2359 rb_insert_color(&backref->node, root);
2360}
2361
2362/*
2363 * Note the backref might has changed, and in this case we just return 0.
2364 */
2365static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2366 void *ctx)
2367{
2368 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002369 struct old_sa_defrag_extent *old = ctx;
2370 struct new_sa_defrag_extent *new = old->new;
2371 struct btrfs_path *path = new->path;
2372 struct btrfs_key key;
2373 struct btrfs_root *root;
2374 struct sa_defrag_extent_backref *backref;
2375 struct extent_buffer *leaf;
2376 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002378 int slot;
2379 int ret;
2380 u64 extent_offset;
2381 u64 num_bytes;
2382
2383 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002384 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002385 return 0;
2386
2387 key.objectid = root_id;
2388 key.type = BTRFS_ROOT_ITEM_KEY;
2389 key.offset = (u64)-1;
2390
Liu Bo38c227d2013-01-29 03:18:40 +00002391 root = btrfs_read_fs_root_no_name(fs_info, &key);
2392 if (IS_ERR(root)) {
2393 if (PTR_ERR(root) == -ENOENT)
2394 return 0;
2395 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002396 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002397 inum, offset, root_id);
2398 return PTR_ERR(root);
2399 }
2400
2401 key.objectid = inum;
2402 key.type = BTRFS_EXTENT_DATA_KEY;
2403 if (offset > (u64)-1 << 32)
2404 key.offset = 0;
2405 else
2406 key.offset = offset;
2407
2408 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302409 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002410 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002411 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002412
2413 while (1) {
2414 cond_resched();
2415
2416 leaf = path->nodes[0];
2417 slot = path->slots[0];
2418
2419 if (slot >= btrfs_header_nritems(leaf)) {
2420 ret = btrfs_next_leaf(root, path);
2421 if (ret < 0) {
2422 goto out;
2423 } else if (ret > 0) {
2424 ret = 0;
2425 goto out;
2426 }
2427 continue;
2428 }
2429
2430 path->slots[0]++;
2431
2432 btrfs_item_key_to_cpu(leaf, &key, slot);
2433
2434 if (key.objectid > inum)
2435 goto out;
2436
2437 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2438 continue;
2439
2440 extent = btrfs_item_ptr(leaf, slot,
2441 struct btrfs_file_extent_item);
2442
2443 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2444 continue;
2445
Liu Boe68afa42013-07-01 22:13:26 +08002446 /*
2447 * 'offset' refers to the exact key.offset,
2448 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2449 * (key.offset - extent_offset).
2450 */
2451 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002452 continue;
2453
Liu Boe68afa42013-07-01 22:13:26 +08002454 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002455 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002456
Liu Bo38c227d2013-01-29 03:18:40 +00002457 if (extent_offset >= old->extent_offset + old->offset +
2458 old->len || extent_offset + num_bytes <=
2459 old->extent_offset + old->offset)
2460 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002461 break;
2462 }
2463
2464 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2465 if (!backref) {
2466 ret = -ENOENT;
2467 goto out;
2468 }
2469
2470 backref->root_id = root_id;
2471 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002472 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002473 backref->num_bytes = num_bytes;
2474 backref->extent_offset = extent_offset;
2475 backref->generation = btrfs_file_extent_generation(leaf, extent);
2476 backref->old = old;
2477 backref_insert(&new->root, backref);
2478 old->count++;
2479out:
2480 btrfs_release_path(path);
2481 WARN_ON(ret);
2482 return ret;
2483}
2484
2485static noinline bool record_extent_backrefs(struct btrfs_path *path,
2486 struct new_sa_defrag_extent *new)
2487{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002489 struct old_sa_defrag_extent *old, *tmp;
2490 int ret;
2491
2492 new->path = path;
2493
2494 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002495 ret = iterate_inodes_from_logical(old->bytenr +
2496 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002497 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002498 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002499 if (ret < 0 && ret != -ENOENT)
2500 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002501
2502 /* no backref to be processed for this extent */
2503 if (!old->count) {
2504 list_del(&old->list);
2505 kfree(old);
2506 }
2507 }
2508
2509 if (list_empty(&new->head))
2510 return false;
2511
2512 return true;
2513}
2514
2515static int relink_is_mergable(struct extent_buffer *leaf,
2516 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002517 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002518{
Liu Bo116e0022013-08-02 16:30:40 +08002519 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002520 return 0;
2521
2522 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2523 return 0;
2524
Liu Bo116e0022013-08-02 16:30:40 +08002525 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2526 return 0;
2527
2528 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002529 btrfs_file_extent_other_encoding(leaf, fi))
2530 return 0;
2531
2532 return 1;
2533}
2534
2535/*
2536 * Note the backref might has changed, and in this case we just return 0.
2537 */
2538static noinline int relink_extent_backref(struct btrfs_path *path,
2539 struct sa_defrag_extent_backref *prev,
2540 struct sa_defrag_extent_backref *backref)
2541{
2542 struct btrfs_file_extent_item *extent;
2543 struct btrfs_file_extent_item *item;
2544 struct btrfs_ordered_extent *ordered;
2545 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002546 struct btrfs_root *root;
2547 struct btrfs_key key;
2548 struct extent_buffer *leaf;
2549 struct old_sa_defrag_extent *old = backref->old;
2550 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002551 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002552 struct inode *inode;
2553 struct extent_state *cached = NULL;
2554 int ret = 0;
2555 u64 start;
2556 u64 len;
2557 u64 lock_start;
2558 u64 lock_end;
2559 bool merge = false;
2560 int index;
2561
2562 if (prev && prev->root_id == backref->root_id &&
2563 prev->inum == backref->inum &&
2564 prev->file_pos + prev->num_bytes == backref->file_pos)
2565 merge = true;
2566
2567 /* step 1: get root */
2568 key.objectid = backref->root_id;
2569 key.type = BTRFS_ROOT_ITEM_KEY;
2570 key.offset = (u64)-1;
2571
Liu Bo38c227d2013-01-29 03:18:40 +00002572 index = srcu_read_lock(&fs_info->subvol_srcu);
2573
2574 root = btrfs_read_fs_root_no_name(fs_info, &key);
2575 if (IS_ERR(root)) {
2576 srcu_read_unlock(&fs_info->subvol_srcu, index);
2577 if (PTR_ERR(root) == -ENOENT)
2578 return 0;
2579 return PTR_ERR(root);
2580 }
Liu Bo38c227d2013-01-29 03:18:40 +00002581
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002582 if (btrfs_root_readonly(root)) {
2583 srcu_read_unlock(&fs_info->subvol_srcu, index);
2584 return 0;
2585 }
2586
Liu Bo38c227d2013-01-29 03:18:40 +00002587 /* step 2: get inode */
2588 key.objectid = backref->inum;
2589 key.type = BTRFS_INODE_ITEM_KEY;
2590 key.offset = 0;
2591
2592 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2593 if (IS_ERR(inode)) {
2594 srcu_read_unlock(&fs_info->subvol_srcu, index);
2595 return 0;
2596 }
2597
2598 srcu_read_unlock(&fs_info->subvol_srcu, index);
2599
2600 /* step 3: relink backref */
2601 lock_start = backref->file_pos;
2602 lock_end = backref->file_pos + backref->num_bytes - 1;
2603 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002604 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002605
2606 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2607 if (ordered) {
2608 btrfs_put_ordered_extent(ordered);
2609 goto out_unlock;
2610 }
2611
2612 trans = btrfs_join_transaction(root);
2613 if (IS_ERR(trans)) {
2614 ret = PTR_ERR(trans);
2615 goto out_unlock;
2616 }
2617
2618 key.objectid = backref->inum;
2619 key.type = BTRFS_EXTENT_DATA_KEY;
2620 key.offset = backref->file_pos;
2621
2622 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2623 if (ret < 0) {
2624 goto out_free_path;
2625 } else if (ret > 0) {
2626 ret = 0;
2627 goto out_free_path;
2628 }
2629
2630 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2631 struct btrfs_file_extent_item);
2632
2633 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2634 backref->generation)
2635 goto out_free_path;
2636
2637 btrfs_release_path(path);
2638
2639 start = backref->file_pos;
2640 if (backref->extent_offset < old->extent_offset + old->offset)
2641 start += old->extent_offset + old->offset -
2642 backref->extent_offset;
2643
2644 len = min(backref->extent_offset + backref->num_bytes,
2645 old->extent_offset + old->offset + old->len);
2646 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2647
2648 ret = btrfs_drop_extents(trans, root, inode, start,
2649 start + len, 1);
2650 if (ret)
2651 goto out_free_path;
2652again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002653 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002654 key.type = BTRFS_EXTENT_DATA_KEY;
2655 key.offset = start;
2656
Liu Boa09a0a72013-03-11 09:20:58 +00002657 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002658 if (merge) {
2659 struct btrfs_file_extent_item *fi;
2660 u64 extent_len;
2661 struct btrfs_key found_key;
2662
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002663 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002664 if (ret < 0)
2665 goto out_free_path;
2666
2667 path->slots[0]--;
2668 leaf = path->nodes[0];
2669 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2670
2671 fi = btrfs_item_ptr(leaf, path->slots[0],
2672 struct btrfs_file_extent_item);
2673 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2674
Liu Bo116e0022013-08-02 16:30:40 +08002675 if (extent_len + found_key.offset == start &&
2676 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002677 btrfs_set_file_extent_num_bytes(leaf, fi,
2678 extent_len + len);
2679 btrfs_mark_buffer_dirty(leaf);
2680 inode_add_bytes(inode, len);
2681
2682 ret = 1;
2683 goto out_free_path;
2684 } else {
2685 merge = false;
2686 btrfs_release_path(path);
2687 goto again;
2688 }
2689 }
2690
2691 ret = btrfs_insert_empty_item(trans, root, path, &key,
2692 sizeof(*extent));
2693 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002694 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002695 goto out_free_path;
2696 }
2697
2698 leaf = path->nodes[0];
2699 item = btrfs_item_ptr(leaf, path->slots[0],
2700 struct btrfs_file_extent_item);
2701 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2702 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2703 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2704 btrfs_set_file_extent_num_bytes(leaf, item, len);
2705 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2706 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2707 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2708 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2709 btrfs_set_file_extent_encryption(leaf, item, 0);
2710 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2711
2712 btrfs_mark_buffer_dirty(leaf);
2713 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002714 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002715
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002716 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002717 new->disk_len, 0,
2718 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002719 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002720 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002721 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002722 goto out_free_path;
2723 }
2724
2725 ret = 1;
2726out_free_path:
2727 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002728 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002729 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002730out_unlock:
2731 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002732 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002733 iput(inode);
2734 return ret;
2735}
2736
Liu Bo6f519562013-10-29 10:45:05 +08002737static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2738{
2739 struct old_sa_defrag_extent *old, *tmp;
2740
2741 if (!new)
2742 return;
2743
2744 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002745 kfree(old);
2746 }
2747 kfree(new);
2748}
2749
Liu Bo38c227d2013-01-29 03:18:40 +00002750static void relink_file_extents(struct new_sa_defrag_extent *new)
2751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002752 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002753 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002754 struct sa_defrag_extent_backref *backref;
2755 struct sa_defrag_extent_backref *prev = NULL;
2756 struct inode *inode;
2757 struct btrfs_root *root;
2758 struct rb_node *node;
2759 int ret;
2760
2761 inode = new->inode;
2762 root = BTRFS_I(inode)->root;
2763
2764 path = btrfs_alloc_path();
2765 if (!path)
2766 return;
2767
2768 if (!record_extent_backrefs(path, new)) {
2769 btrfs_free_path(path);
2770 goto out;
2771 }
2772 btrfs_release_path(path);
2773
2774 while (1) {
2775 node = rb_first(&new->root);
2776 if (!node)
2777 break;
2778 rb_erase(node, &new->root);
2779
2780 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2781
2782 ret = relink_extent_backref(path, prev, backref);
2783 WARN_ON(ret < 0);
2784
2785 kfree(prev);
2786
2787 if (ret == 1)
2788 prev = backref;
2789 else
2790 prev = NULL;
2791 cond_resched();
2792 }
2793 kfree(prev);
2794
2795 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002796out:
Liu Bo6f519562013-10-29 10:45:05 +08002797 free_sa_defrag_extent(new);
2798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002799 atomic_dec(&fs_info->defrag_running);
2800 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002801}
2802
2803static struct new_sa_defrag_extent *
2804record_old_file_extents(struct inode *inode,
2805 struct btrfs_ordered_extent *ordered)
2806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002808 struct btrfs_root *root = BTRFS_I(inode)->root;
2809 struct btrfs_path *path;
2810 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002811 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002812 struct new_sa_defrag_extent *new;
2813 int ret;
2814
2815 new = kmalloc(sizeof(*new), GFP_NOFS);
2816 if (!new)
2817 return NULL;
2818
2819 new->inode = inode;
2820 new->file_pos = ordered->file_offset;
2821 new->len = ordered->len;
2822 new->bytenr = ordered->start;
2823 new->disk_len = ordered->disk_len;
2824 new->compress_type = ordered->compress_type;
2825 new->root = RB_ROOT;
2826 INIT_LIST_HEAD(&new->head);
2827
2828 path = btrfs_alloc_path();
2829 if (!path)
2830 goto out_kfree;
2831
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002832 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002833 key.type = BTRFS_EXTENT_DATA_KEY;
2834 key.offset = new->file_pos;
2835
2836 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2837 if (ret < 0)
2838 goto out_free_path;
2839 if (ret > 0 && path->slots[0] > 0)
2840 path->slots[0]--;
2841
2842 /* find out all the old extents for the file range */
2843 while (1) {
2844 struct btrfs_file_extent_item *extent;
2845 struct extent_buffer *l;
2846 int slot;
2847 u64 num_bytes;
2848 u64 offset;
2849 u64 end;
2850 u64 disk_bytenr;
2851 u64 extent_offset;
2852
2853 l = path->nodes[0];
2854 slot = path->slots[0];
2855
2856 if (slot >= btrfs_header_nritems(l)) {
2857 ret = btrfs_next_leaf(root, path);
2858 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002859 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002860 else if (ret > 0)
2861 break;
2862 continue;
2863 }
2864
2865 btrfs_item_key_to_cpu(l, &key, slot);
2866
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002867 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002868 break;
2869 if (key.type != BTRFS_EXTENT_DATA_KEY)
2870 break;
2871 if (key.offset >= new->file_pos + new->len)
2872 break;
2873
2874 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2875
2876 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2877 if (key.offset + num_bytes < new->file_pos)
2878 goto next;
2879
2880 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2881 if (!disk_bytenr)
2882 goto next;
2883
2884 extent_offset = btrfs_file_extent_offset(l, extent);
2885
2886 old = kmalloc(sizeof(*old), GFP_NOFS);
2887 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002888 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002889
2890 offset = max(new->file_pos, key.offset);
2891 end = min(new->file_pos + new->len, key.offset + num_bytes);
2892
2893 old->bytenr = disk_bytenr;
2894 old->extent_offset = extent_offset;
2895 old->offset = offset - key.offset;
2896 old->len = end - offset;
2897 old->new = new;
2898 old->count = 0;
2899 list_add_tail(&old->list, &new->head);
2900next:
2901 path->slots[0]++;
2902 cond_resched();
2903 }
2904
2905 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002907
2908 return new;
2909
Liu Bo38c227d2013-01-29 03:18:40 +00002910out_free_path:
2911 btrfs_free_path(path);
2912out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002913 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002914 return NULL;
2915}
2916
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002917static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002918 u64 start, u64 len)
2919{
2920 struct btrfs_block_group_cache *cache;
2921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002923 ASSERT(cache);
2924
2925 spin_lock(&cache->lock);
2926 cache->delalloc_bytes -= len;
2927 spin_unlock(&cache->lock);
2928
2929 btrfs_put_block_group(cache);
2930}
2931
Chris Masond352ac62008-09-29 15:18:18 -04002932/* as ordered data IO finishes, this gets called so we can finish
2933 * an ordered extent if the range of bytes in the file it covers are
2934 * fully written.
2935 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002936static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002937{
Josef Bacik5fd02042012-05-02 14:00:54 -04002938 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002939 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002940 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002941 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002942 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002943 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002944 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002945 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002946 int ret = 0;
2947 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002948 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002949 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002950 bool range_locked = false;
2951 bool clear_new_delalloc_bytes = false;
2952
2953 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2954 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2955 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2956 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002957
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002958 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002959
Josef Bacik5fd02042012-05-02 14:00:54 -04002960 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2961 ret = -EIO;
2962 goto out;
2963 }
2964
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002965 btrfs_free_io_failure_record(BTRFS_I(inode),
2966 ordered_extent->file_offset,
2967 ordered_extent->file_offset +
2968 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002969
Josef Bacik77cef2e2013-08-29 13:57:21 -04002970 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2971 truncated = true;
2972 logical_len = ordered_extent->truncated_len;
2973 /* Truncated the entire extent, don't bother adding */
2974 if (!logical_len)
2975 goto out;
2976 }
2977
Yan, Zhengc2167752009-11-12 09:34:21 +00002978 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002980
2981 /*
2982 * For mwrite(mmap + memset to write) case, we still reserve
2983 * space for NOCOW range.
2984 * As NOCOW won't cause a new delayed ref, just free the space
2985 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002986 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002987 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002988 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2989 if (nolock)
2990 trans = btrfs_join_transaction_nolock(root);
2991 else
2992 trans = btrfs_join_transaction(root);
2993 if (IS_ERR(trans)) {
2994 ret = PTR_ERR(trans);
2995 trans = NULL;
2996 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002997 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002998 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002999 ret = btrfs_update_inode_fallback(trans, root, inode);
3000 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003001 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003002 goto out;
3003 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003004
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003005 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003006 lock_extent_bits(io_tree, ordered_extent->file_offset,
3007 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003008 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009
Liu Bo38c227d2013-01-29 03:18:40 +00003010 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3011 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003012 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003013 if (ret) {
3014 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003015 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003016 /* the inode is shared */
3017 new = record_old_file_extents(inode, ordered_extent);
3018
3019 clear_extent_bit(io_tree, ordered_extent->file_offset,
3020 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003021 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003022 }
3023
Josef Bacik0cb59c92010-07-02 12:14:14 -04003024 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003025 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003026 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003027 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003028 if (IS_ERR(trans)) {
3029 ret = PTR_ERR(trans);
3030 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003031 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 }
Chris Masona79b7d42014-05-22 16:18:52 -07003033
Josef Bacik69fe2d72017-10-19 14:15:57 -04003034 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003035
Chris Masonc8b97812008-10-29 14:49:59 -04003036 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003037 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003038 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003039 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003040 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3041 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003042 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003043 ordered_extent->file_offset,
3044 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003045 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003046 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003047 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003048 ret = insert_reserved_file_extent(trans, inode,
3049 ordered_extent->file_offset,
3050 ordered_extent->start,
3051 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003052 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003053 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003054 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003055 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003056 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003057 ordered_extent->start,
3058 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003059 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003060 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3061 ordered_extent->file_offset, ordered_extent->len,
3062 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003063 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003064 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003065 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003066 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003067
Nikolay Borisovac01f262018-01-08 10:59:43 +02003068 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3069 if (ret) {
3070 btrfs_abort_transaction(trans, ret);
3071 goto out;
3072 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003073
Josef Bacik6c760c02012-11-09 10:53:21 -05003074 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3075 ret = btrfs_update_inode_fallback(trans, root, inode);
3076 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003077 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003078 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003079 }
3080 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003081out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003082 if (range_locked || clear_new_delalloc_bytes) {
3083 unsigned int clear_bits = 0;
3084
3085 if (range_locked)
3086 clear_bits |= EXTENT_LOCKED;
3087 if (clear_new_delalloc_bytes)
3088 clear_bits |= EXTENT_DELALLOC_NEW;
3089 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3090 ordered_extent->file_offset,
3091 ordered_extent->file_offset +
3092 ordered_extent->len - 1,
3093 clear_bits,
3094 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003095 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003096 }
3097
Miao Xiea698d0752012-09-20 01:51:59 -06003098 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003099 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003100
Josef Bacik77cef2e2013-08-29 13:57:21 -04003101 if (ret || truncated) {
3102 u64 start, end;
3103
3104 if (truncated)
3105 start = ordered_extent->file_offset + logical_len;
3106 else
3107 start = ordered_extent->file_offset;
3108 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003109 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003110
3111 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003112 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003113
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003114 /*
3115 * If the ordered extent had an IOERR or something else went
3116 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003117 * back to the allocator. We only free the extent in the
3118 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003119 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003120 if ((ret || !logical_len) &&
3121 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003122 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003123 btrfs_free_reserved_extent(fs_info,
3124 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003125 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003126 }
3127
3128
Josef Bacik5fd02042012-05-02 14:00:54 -04003129 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003130 * This needs to be done to make sure anybody waiting knows we are done
3131 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003132 */
3133 btrfs_remove_ordered_extent(inode, ordered_extent);
3134
Liu Bo38c227d2013-01-29 03:18:40 +00003135 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003136 if (new) {
3137 if (ret) {
3138 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003139 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003140 } else {
3141 relink_file_extents(new);
3142 }
3143 }
Liu Bo38c227d2013-01-29 03:18:40 +00003144
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145 /* once for us */
3146 btrfs_put_ordered_extent(ordered_extent);
3147 /* once for the tree */
3148 btrfs_put_ordered_extent(ordered_extent);
3149
Josef Bacik5fd02042012-05-02 14:00:54 -04003150 return ret;
3151}
3152
3153static void finish_ordered_fn(struct btrfs_work *work)
3154{
3155 struct btrfs_ordered_extent *ordered_extent;
3156 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3157 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003158}
3159
David Sterbac3988d62017-02-17 15:18:32 +01003160static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003161 struct extent_state *state, int uptodate)
3162{
Josef Bacik5fd02042012-05-02 14:00:54 -04003163 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003164 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003165 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003166 struct btrfs_workqueue *wq;
3167 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003168
liubo1abe9b82011-03-24 11:18:59 +00003169 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3170
Chris Mason8b62b722009-09-02 16:53:46 -04003171 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003172 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3173 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003174 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003175
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003176 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003177 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003178 func = btrfs_freespace_write_helper;
3179 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003180 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003181 func = btrfs_endio_write_helper;
3182 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003183
Liu Bo9e0af232014-08-15 23:36:53 +08003184 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3185 NULL);
3186 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003187}
3188
Miao Xiedc380ae2014-09-12 18:43:55 +08003189static int __readpage_endio_check(struct inode *inode,
3190 struct btrfs_io_bio *io_bio,
3191 int icsum, struct page *page,
3192 int pgoff, u64 start, size_t len)
3193{
3194 char *kaddr;
3195 u32 csum_expected;
3196 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003197
3198 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3199
3200 kaddr = kmap_atomic(page);
3201 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003202 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003203 if (csum != csum_expected)
3204 goto zeroit;
3205
3206 kunmap_atomic(kaddr);
3207 return 0;
3208zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003209 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003210 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003211 memset(kaddr + pgoff, 1, len);
3212 flush_dcache_page(page);
3213 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003214 return -EIO;
3215}
3216
Chris Masond352ac62008-09-29 15:18:18 -04003217/*
Chris Masond352ac62008-09-29 15:18:18 -04003218 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003219 * if there's a match, we allow the bio to finish. If not, the code in
3220 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003221 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003222static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3223 u64 phy_offset, struct page *page,
3224 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003225{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003226 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003227 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003228 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003229 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003230
Chris Masond20f7042008-12-08 16:58:54 -05003231 if (PageChecked(page)) {
3232 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003233 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003234 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003235
3236 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003237 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003238
Yan Zheng17d217f2008-12-12 10:03:38 -05003239 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003240 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003241 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003242 return 0;
3243 }
3244
Miao Xiefacc8a222013-07-25 19:22:34 +08003245 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003246 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3247 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003248}
Chris Masonb888db2b2007-08-27 16:49:44 -04003249
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003250void btrfs_add_delayed_iput(struct inode *inode)
3251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003253 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254
3255 if (atomic_add_unless(&inode->i_count, -1, 1))
3256 return;
3257
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003258 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003259 if (binode->delayed_iput_count == 0) {
3260 ASSERT(list_empty(&binode->delayed_iput));
3261 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3262 } else {
3263 binode->delayed_iput_count++;
3264 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003265 spin_unlock(&fs_info->delayed_iput_lock);
3266}
3267
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003268void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003270
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003271 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003272 while (!list_empty(&fs_info->delayed_iputs)) {
3273 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003274
David Sterba8089fe62015-11-19 14:15:51 +01003275 inode = list_first_entry(&fs_info->delayed_iputs,
3276 struct btrfs_inode, delayed_iput);
3277 if (inode->delayed_iput_count) {
3278 inode->delayed_iput_count--;
3279 list_move_tail(&inode->delayed_iput,
3280 &fs_info->delayed_iputs);
3281 } else {
3282 list_del_init(&inode->delayed_iput);
3283 }
3284 spin_unlock(&fs_info->delayed_iput_lock);
3285 iput(&inode->vfs_inode);
3286 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003287 }
David Sterba8089fe62015-11-19 14:15:51 +01003288 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003289}
3290
Yan, Zhengd68fc572010-05-16 10:49:58 -04003291/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003292 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 * files in the subvolume, it removes orphan item and frees block_rsv
3294 * structure.
3295 */
3296void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3297 struct btrfs_root *root)
3298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003299 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003300 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 int ret;
3302
Josef Bacik8a35d952012-05-23 14:26:42 -04003303 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003304 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3305 return;
3306
Josef Bacik90290e12011-12-02 15:44:12 -05003307 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003308 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003309 spin_unlock(&root->orphan_lock);
3310 return;
3311 }
3312
3313 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3314 spin_unlock(&root->orphan_lock);
3315 return;
3316 }
3317
3318 block_rsv = root->orphan_block_rsv;
3319 root->orphan_block_rsv = NULL;
3320 spin_unlock(&root->orphan_lock);
3321
Miao Xie27cdeb72014-04-02 19:51:05 +08003322 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003323 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003324 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003326 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003327 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003328 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003329 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3330 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003331 }
3332
Josef Bacik90290e12011-12-02 15:44:12 -05003333 if (block_rsv) {
3334 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003335 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003336 }
3337}
3338
3339/*
Josef Bacik7b128762008-07-24 12:17:14 -04003340 * This creates an orphan entry for the given inode in case something goes
3341 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 *
3343 * NOTE: caller of this function should reserve 5 units of metadata for
3344 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003345 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003346int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3347 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003348{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003349 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3350 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 struct btrfs_block_rsv *block_rsv = NULL;
3352 int reserve = 0;
3353 int insert = 0;
3354 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003355
Yan, Zhengd68fc572010-05-16 10:49:58 -04003356 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003357 block_rsv = btrfs_alloc_block_rsv(fs_info,
3358 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003359 if (!block_rsv)
3360 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003361 }
3362
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 spin_lock(&root->orphan_lock);
3364 if (!root->orphan_block_rsv) {
3365 root->orphan_block_rsv = block_rsv;
3366 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003367 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368 block_rsv = NULL;
3369 }
Josef Bacik7b128762008-07-24 12:17:14 -04003370
Josef Bacik8a35d952012-05-23 14:26:42 -04003371 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003372 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003373#if 0
3374 /*
3375 * For proper ENOSPC handling, we should do orphan
3376 * cleanup when mounting. But this introduces backward
3377 * compatibility issue.
3378 */
3379 if (!xchg(&root->orphan_item_inserted, 1))
3380 insert = 2;
3381 else
3382 insert = 1;
3383#endif
3384 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003385 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003386 }
Josef Bacik7b128762008-07-24 12:17:14 -04003387
Josef Bacik72ac3c02012-05-23 14:13:11 -04003388 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003389 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003390 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003391 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003392
Yan, Zhengd68fc572010-05-16 10:49:58 -04003393 /* grab metadata reservation from transaction handle */
3394 if (reserve) {
3395 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003396 ASSERT(!ret);
3397 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003398 /*
3399 * dec doesn't need spin_lock as ->orphan_block_rsv
3400 * would be released only if ->orphan_inodes is
3401 * zero.
3402 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003403 atomic_dec(&root->orphan_inodes);
3404 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003405 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003406 if (insert)
3407 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003408 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003409 return ret;
3410 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003411 }
3412
3413 /* insert an orphan item to track this unlinked/truncated file */
3414 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003415 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003416 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003417 if (reserve) {
3418 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003419 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003420 btrfs_orphan_release_metadata(inode);
3421 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003422 /*
3423 * btrfs_orphan_commit_root may race with us and set
3424 * ->orphan_block_rsv to zero, in order to avoid that,
3425 * decrease ->orphan_inodes after everything is done.
3426 */
3427 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003428 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003429 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003430 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003431 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003432 return ret;
3433 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003434 }
3435 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003436 }
3437
3438 /* insert an orphan item to track subvolume contains orphan files */
3439 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003440 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003442 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003443 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003444 return ret;
3445 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003446 }
3447 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003448}
3449
3450/*
3451 * We have done the truncate/delete so we can go ahead and remove the orphan
3452 * item for this particular inode.
3453 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003454static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003455 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003456{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003457 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003458 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003459 int ret = 0;
3460
Josef Bacik8a35d952012-05-23 14:26:42 -04003461 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003462 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003463 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003464
Liu Bo1a932ef2018-01-25 11:02:54 -07003465 if (delete_item && trans)
3466 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3467
Josef Bacik72ac3c02012-05-23 14:13:11 -04003468 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003469 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003470 btrfs_orphan_release_metadata(inode);
3471
Liu Bo1a932ef2018-01-25 11:02:54 -07003472 /*
3473 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3474 * to zero, in order to avoid that, decrease ->orphan_inodes after
3475 * everything is done.
3476 */
3477 if (delete_item)
3478 atomic_dec(&root->orphan_inodes);
3479
Josef Bacik4ef31a42013-08-13 14:10:08 -04003480 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003481}
3482
3483/*
3484 * this cleans up any orphans that may be left on the list from the last use
3485 * of this root.
3486 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003488{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003489 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003490 struct btrfs_path *path;
3491 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003492 struct btrfs_key key, found_key;
3493 struct btrfs_trans_handle *trans;
3494 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003495 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003496 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3497
Yan, Zhengd68fc572010-05-16 10:49:58 -04003498 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003500
3501 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003502 if (!path) {
3503 ret = -ENOMEM;
3504 goto out;
3505 }
David Sterbae4058b52015-11-27 16:31:35 +01003506 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003507
3508 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003509 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003510 key.offset = (u64)-1;
3511
Josef Bacik7b128762008-07-24 12:17:14 -04003512 while (1) {
3513 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003514 if (ret < 0)
3515 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003516
3517 /*
3518 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003519 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003520 * find the key and see if we have stuff that matches
3521 */
3522 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003523 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003524 if (path->slots[0] == 0)
3525 break;
3526 path->slots[0]--;
3527 }
3528
3529 /* pull out the item */
3530 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003531 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3532
3533 /* make sure the item matches what we want */
3534 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3535 break;
David Sterba962a2982014-06-04 18:41:45 +02003536 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003537 break;
3538
3539 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003540 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003541
3542 /*
3543 * this is where we are basically btrfs_lookup, without the
3544 * crossing root thing. we store the inode number in the
3545 * offset of the orphan item.
3546 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003547
3548 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003549 btrfs_err(fs_info,
3550 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003551 ret = -EINVAL;
3552 goto out;
3553 }
3554
3555 last_objectid = found_key.offset;
3556
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003557 found_key.objectid = found_key.offset;
3558 found_key.type = BTRFS_INODE_ITEM_KEY;
3559 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303561 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003562 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003563 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003565 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003566 struct btrfs_root *dead_root;
3567 struct btrfs_fs_info *fs_info = root->fs_info;
3568 int is_dead_root = 0;
3569
3570 /*
3571 * this is an orphan in the tree root. Currently these
3572 * could come from 2 sources:
3573 * a) a snapshot deletion in progress
3574 * b) a free space cache inode
3575 * We need to distinguish those two, as the snapshot
3576 * orphan must not get deleted.
3577 * find_dead_roots already ran before us, so if this
3578 * is a snapshot deletion, we should find the root
3579 * in the dead_roots list
3580 */
3581 spin_lock(&fs_info->trans_lock);
3582 list_for_each_entry(dead_root, &fs_info->dead_roots,
3583 root_list) {
3584 if (dead_root->root_key.objectid ==
3585 found_key.objectid) {
3586 is_dead_root = 1;
3587 break;
3588 }
3589 }
3590 spin_unlock(&fs_info->trans_lock);
3591 if (is_dead_root) {
3592 /* prevent this orphan from being found again */
3593 key.offset = found_key.objectid - 1;
3594 continue;
3595 }
3596 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003597 /*
3598 * Inode is already gone but the orphan item is still there,
3599 * kill the orphan item.
3600 */
Filipe Manana67710892016-06-06 11:51:25 +01003601 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003602 trans = btrfs_start_transaction(root, 1);
3603 if (IS_ERR(trans)) {
3604 ret = PTR_ERR(trans);
3605 goto out;
3606 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003607 btrfs_debug(fs_info, "auto deleting %Lu",
3608 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003609 ret = btrfs_del_orphan_item(trans, root,
3610 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003611 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003612 if (ret)
3613 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003614 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003615 }
Josef Bacik7b128762008-07-24 12:17:14 -04003616
Josef Bacik7b128762008-07-24 12:17:14 -04003617 /*
3618 * add this inode to the orphan list so btrfs_orphan_del does
3619 * the proper thing when we hit it
3620 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003621 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3622 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003623 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003624
Josef Bacik7b128762008-07-24 12:17:14 -04003625 /* if we have links, this was a truncate, lets do that */
3626 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303627 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003628 iput(inode);
3629 continue;
3630 }
Josef Bacik7b128762008-07-24 12:17:14 -04003631 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003632
3633 /* 1 for the orphan item deletion. */
3634 trans = btrfs_start_transaction(root, 1);
3635 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003636 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003637 ret = PTR_ERR(trans);
3638 goto out;
3639 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003640 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003641 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003642 if (ret) {
3643 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003644 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003645 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003646
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003647 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003648 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003649 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003650 } else {
3651 nr_unlink++;
3652 }
3653
3654 /* this will do delete_inode and everything for us */
3655 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003656 if (ret)
3657 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003658 }
Miao Xie3254c872011-11-10 20:45:05 -05003659 /* release the path since we're done with it */
3660 btrfs_release_path(path);
3661
Yan, Zhengd68fc572010-05-16 10:49:58 -04003662 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3663
3664 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003665 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003666 (u64)-1);
3667
Miao Xie27cdeb72014-04-02 19:51:05 +08003668 if (root->orphan_block_rsv ||
3669 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003670 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003671 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003672 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003673 }
Josef Bacik7b128762008-07-24 12:17:14 -04003674
3675 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003676 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003677 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003678 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003679
3680out:
3681 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003682 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003683 btrfs_free_path(path);
3684 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003685}
3686
Chris Masond352ac62008-09-29 15:18:18 -04003687/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003688 * very simple check to peek ahead in the leaf looking for xattrs. If we
3689 * don't find any xattrs, we know there can't be any acls.
3690 *
3691 * slot is the slot the inode is in, objectid is the objectid of the inode
3692 */
3693static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003694 int slot, u64 objectid,
3695 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003696{
3697 u32 nritems = btrfs_header_nritems(leaf);
3698 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003699 static u64 xattr_access = 0;
3700 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003701 int scanned = 0;
3702
Josef Bacikf23b5a52013-06-19 10:16:26 -04003703 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003704 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3705 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3706 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3707 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003708 }
3709
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003711 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003712 while (slot < nritems) {
3713 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3714
3715 /* we found a different objectid, there must not be acls */
3716 if (found_key.objectid != objectid)
3717 return 0;
3718
3719 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003720 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003721 if (*first_xattr_slot == -1)
3722 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003723 if (found_key.offset == xattr_access ||
3724 found_key.offset == xattr_default)
3725 return 1;
3726 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003727
3728 /*
3729 * we found a key greater than an xattr key, there can't
3730 * be any acls later on
3731 */
3732 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3733 return 0;
3734
3735 slot++;
3736 scanned++;
3737
3738 /*
3739 * it goes inode, inode backrefs, xattrs, extents,
3740 * so if there are a ton of hard links to an inode there can
3741 * be a lot of backrefs. Don't waste time searching too hard,
3742 * this is just an optimization
3743 */
3744 if (scanned >= 8)
3745 break;
3746 }
3747 /* we hit the end of the leaf before we found an xattr or
3748 * something larger than an xattr. We have to assume the inode
3749 * has acls
3750 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003751 if (*first_xattr_slot == -1)
3752 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003753 return 1;
3754}
3755
3756/*
Chris Masond352ac62008-09-29 15:18:18 -04003757 * read an inode from the btree into the in-memory inode
3758 */
Filipe Manana67710892016-06-06 11:51:25 +01003759static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003761 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003762 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003763 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003764 struct btrfs_inode_item *inode_item;
3765 struct btrfs_root *root = BTRFS_I(inode)->root;
3766 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003767 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003768 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003769 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003770 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003771 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003772 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003773
3774 ret = btrfs_fill_inode(inode, &rdev);
3775 if (!ret)
3776 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003777
3778 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003779 if (!path) {
3780 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003781 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003782 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003783
Chris Mason39279cc2007-06-12 06:35:45 -04003784 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003785
Chris Mason39279cc2007-06-12 06:35:45 -04003786 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003787 if (ret) {
3788 if (ret > 0)
3789 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003790 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003791 }
Chris Mason39279cc2007-06-12 06:35:45 -04003792
Chris Mason5f39d392007-10-15 16:14:19 -04003793 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003794
3795 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003796 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003797
Chris Mason5f39d392007-10-15 16:14:19 -04003798 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3799 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003800 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003801 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003802 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3803 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003804 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003805
David Sterbaa937b972014-12-12 17:39:12 +01003806 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3807 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003808
David Sterbaa937b972014-12-12 17:39:12 +01003809 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3810 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003811
David Sterbaa937b972014-12-12 17:39:12 +01003812 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3813 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003814
chandan r9cc97d62012-07-04 12:48:07 +05303815 BTRFS_I(inode)->i_otime.tv_sec =
3816 btrfs_timespec_sec(leaf, &inode_item->otime);
3817 BTRFS_I(inode)->i_otime.tv_nsec =
3818 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003819
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003820 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003821 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003822 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3823
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003824 inode_set_iversion_queried(inode,
3825 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003826 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003827 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003828 rdev = btrfs_inode_rdev(leaf, inode_item);
3829
Josef Bacikaec74772008-07-24 12:12:38 -04003830 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003831 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003832
3833cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003834 /*
3835 * If we were modified in the current generation and evicted from memory
3836 * and then re-read we need to do a full sync since we don't have any
3837 * idea about which extents were modified before we were evicted from
3838 * cache.
3839 *
3840 * This is required for both inode re-read from disk and delayed inode
3841 * in delayed_nodes_tree.
3842 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003843 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003844 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3845 &BTRFS_I(inode)->runtime_flags);
3846
Filipe Mananabde6c242015-07-24 00:00:19 +01003847 /*
3848 * We don't persist the id of the transaction where an unlink operation
3849 * against the inode was last made. So here we assume the inode might
3850 * have been evicted, and therefore the exact value of last_unlink_trans
3851 * lost, and set it to last_trans to avoid metadata inconsistencies
3852 * between the inode and its parent if the inode is fsync'ed and the log
3853 * replayed. For example, in the scenario:
3854 *
3855 * touch mydir/foo
3856 * ln mydir/foo mydir/bar
3857 * sync
3858 * unlink mydir/bar
3859 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3860 * xfs_io -c fsync mydir/foo
3861 * <power failure>
3862 * mount fs, triggers fsync log replay
3863 *
3864 * We must make sure that when we fsync our inode foo we also log its
3865 * parent inode, otherwise after log replay the parent still has the
3866 * dentry with the "bar" name but our inode foo has a link count of 1
3867 * and doesn't have an inode ref with the name "bar" anymore.
3868 *
3869 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003870 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003871 * transaction commits on fsync if our inode is a directory, or if our
3872 * inode is not a directory, logging its parent unnecessarily.
3873 */
3874 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3875
Miao Xie67de1172013-12-26 13:07:06 +08003876 path->slots[0]++;
3877 if (inode->i_nlink != 1 ||
3878 path->slots[0] >= btrfs_header_nritems(leaf))
3879 goto cache_acl;
3880
3881 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003882 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003883 goto cache_acl;
3884
3885 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3886 if (location.type == BTRFS_INODE_REF_KEY) {
3887 struct btrfs_inode_ref *ref;
3888
3889 ref = (struct btrfs_inode_ref *)ptr;
3890 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3891 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3892 struct btrfs_inode_extref *extref;
3893
3894 extref = (struct btrfs_inode_extref *)ptr;
3895 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3896 extref);
3897 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003898cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003899 /*
3900 * try to precache a NULL acl entry for files that don't have
3901 * any xattrs or acls
3902 */
Li Zefan33345d012011-04-20 10:31:50 +08003903 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003904 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003905 if (first_xattr_slot != -1) {
3906 path->slots[0] = first_xattr_slot;
3907 ret = btrfs_load_inode_props(inode, path);
3908 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003909 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003910 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003911 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003912 root->root_key.objectid, ret);
3913 }
3914 btrfs_free_path(path);
3915
Al Viro72c04902009-06-24 16:58:48 -04003916 if (!maybe_acls)
3917 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003918
Chris Mason39279cc2007-06-12 06:35:45 -04003919 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003920 case S_IFREG:
3921 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003922 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003923 inode->i_fop = &btrfs_file_operations;
3924 inode->i_op = &btrfs_file_inode_operations;
3925 break;
3926 case S_IFDIR:
3927 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003928 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003929 break;
3930 case S_IFLNK:
3931 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003932 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003933 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3934 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003935 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003936 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003937 init_special_inode(inode, inode->i_mode, rdev);
3938 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003939 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003940
3941 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003942 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003943
3944make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003945 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003946 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003947 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003948}
3949
Chris Masond352ac62008-09-29 15:18:18 -04003950/*
3951 * given a leaf and an inode, copy the inode fields into the leaf
3952 */
Chris Masone02119d2008-09-05 16:13:11 -04003953static void fill_inode_item(struct btrfs_trans_handle *trans,
3954 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003955 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003956 struct inode *inode)
3957{
Liu Bo51fab692012-12-27 09:01:21 +00003958 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003959
Liu Bo51fab692012-12-27 09:01:21 +00003960 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003961
Liu Bo51fab692012-12-27 09:01:21 +00003962 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3963 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3964 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3965 &token);
3966 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3967 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003968
David Sterbaa937b972014-12-12 17:39:12 +01003969 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003970 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003971 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003972 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003973
David Sterbaa937b972014-12-12 17:39:12 +01003974 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003975 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003976 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003977 inode->i_mtime.tv_nsec, &token);
3978
David Sterbaa937b972014-12-12 17:39:12 +01003979 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003980 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003981 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003982 inode->i_ctime.tv_nsec, &token);
3983
chandan r9cc97d62012-07-04 12:48:07 +05303984 btrfs_set_token_timespec_sec(leaf, &item->otime,
3985 BTRFS_I(inode)->i_otime.tv_sec, &token);
3986 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3987 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3988
Liu Bo51fab692012-12-27 09:01:21 +00003989 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3990 &token);
3991 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3992 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003993 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3994 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003995 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3996 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3997 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3998 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003999}
4000
Chris Masond352ac62008-09-29 15:18:18 -04004001/*
4002 * copy everything in the in-memory inode into the btree.
4003 */
Chris Mason21151332011-11-10 20:39:08 -05004004static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004005 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004006{
4007 struct btrfs_inode_item *inode_item;
4008 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004009 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004010 int ret;
4011
4012 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004013 if (!path)
4014 return -ENOMEM;
4015
Chris Masonb9473432009-03-13 11:00:37 -04004016 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004017 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4018 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004019 if (ret) {
4020 if (ret > 0)
4021 ret = -ENOENT;
4022 goto failed;
4023 }
4024
Chris Mason5f39d392007-10-15 16:14:19 -04004025 leaf = path->nodes[0];
4026 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004027 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004028
Chris Masone02119d2008-09-05 16:13:11 -04004029 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004030 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004031 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004032 ret = 0;
4033failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004034 btrfs_free_path(path);
4035 return ret;
4036}
4037
Chris Masond352ac62008-09-29 15:18:18 -04004038/*
Chris Mason21151332011-11-10 20:39:08 -05004039 * copy everything in the in-memory inode into the btree.
4040 */
4041noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4042 struct btrfs_root *root, struct inode *inode)
4043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004044 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004045 int ret;
4046
4047 /*
4048 * If the inode is a free space inode, we can deadlock during commit
4049 * if we put it into the delayed code.
4050 *
4051 * The data relocation inode should also be directly updated
4052 * without delay
4053 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004054 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004055 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004056 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004057 btrfs_update_root_times(trans, root);
4058
Chris Mason21151332011-11-10 20:39:08 -05004059 ret = btrfs_delayed_update_inode(trans, root, inode);
4060 if (!ret)
4061 btrfs_set_inode_last_trans(trans, inode);
4062 return ret;
4063 }
4064
4065 return btrfs_update_inode_item(trans, root, inode);
4066}
4067
Josef Bacikbe6aef62012-10-22 15:43:12 -04004068noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4069 struct btrfs_root *root,
4070 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004071{
4072 int ret;
4073
4074 ret = btrfs_update_inode(trans, root, inode);
4075 if (ret == -ENOSPC)
4076 return btrfs_update_inode_item(trans, root, inode);
4077 return ret;
4078}
4079
4080/*
Chris Masond352ac62008-09-29 15:18:18 -04004081 * unlink helper that gets used here in inode.c and in the tree logging
4082 * recovery code. It remove a link in a directory with a given name, and
4083 * also drops the back refs in the inode to the directory
4084 */
Al Viro92986792011-03-04 17:14:37 +00004085static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4086 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004087 struct btrfs_inode *dir,
4088 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004089 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004090{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004091 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004092 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004094 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004095 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004096 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004097 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004098 u64 ino = btrfs_ino(inode);
4099 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004100
4101 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004102 if (!path) {
4103 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004104 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004105 }
4106
Chris Masonb9473432009-03-13 11:00:37 -04004107 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004108 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004109 name, name_len, -1);
4110 if (IS_ERR(di)) {
4111 ret = PTR_ERR(di);
4112 goto err;
4113 }
4114 if (!di) {
4115 ret = -ENOENT;
4116 goto err;
4117 }
Chris Mason5f39d392007-10-15 16:14:19 -04004118 leaf = path->nodes[0];
4119 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004120 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004121 if (ret)
4122 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004123 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004124
Miao Xie67de1172013-12-26 13:07:06 +08004125 /*
4126 * If we don't have dir index, we have to get it by looking up
4127 * the inode ref, since we get the inode ref, remove it directly,
4128 * it is unnecessary to do delayed deletion.
4129 *
4130 * But if we have dir index, needn't search inode ref to get it.
4131 * Since the inode ref is close to the inode item, it is better
4132 * that we delay to delete it, and just do this deletion when
4133 * we update the inode item.
4134 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004135 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004136 ret = btrfs_delayed_delete_inode_ref(inode);
4137 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004139 goto skip_backref;
4140 }
4141 }
4142
Li Zefan33345d012011-04-20 10:31:50 +08004143 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4144 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004145 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004146 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004147 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004148 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004149 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004150 goto err;
4151 }
Miao Xie67de1172013-12-26 13:07:06 +08004152skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004153 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004155 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004156 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004157 }
Chris Mason39279cc2007-06-12 06:35:45 -04004158
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004159 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4160 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004162 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163 goto err;
4164 }
Chris Masone02119d2008-09-05 16:13:11 -04004165
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004166 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4167 index);
Chris Mason6418c962010-10-30 07:34:24 -04004168 if (ret == -ENOENT)
4169 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004170 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004171 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004172err:
4173 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004174 if (ret)
4175 goto out;
4176
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004177 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 inode_inc_iversion(&inode->vfs_inode);
4179 inode_inc_iversion(&dir->vfs_inode);
4180 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4181 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4182 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004183out:
Chris Mason39279cc2007-06-12 06:35:45 -04004184 return ret;
4185}
4186
Al Viro92986792011-03-04 17:14:37 +00004187int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4188 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004189 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004190 const char *name, int name_len)
4191{
4192 int ret;
4193 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4194 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004195 drop_nlink(&inode->vfs_inode);
4196 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004197 }
4198 return ret;
4199}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004200
4201/*
4202 * helper to start transaction for unlink and rmdir.
4203 *
Josef Bacikd52be812013-05-29 14:54:47 -04004204 * unlink and rmdir are special in btrfs, they do not always free space, so
4205 * if we cannot make our reservations the normal way try and see if there is
4206 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4207 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208 */
Josef Bacikd52be812013-05-29 14:54:47 -04004209static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004211 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004212
Josef Bacike70bea52011-10-11 14:18:24 -04004213 /*
4214 * 1 for the possible orphan item
4215 * 1 for the dir item
4216 * 1 for the dir index
4217 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004218 * 1 for the inode
4219 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004220 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004221}
4222
Chris Mason39279cc2007-06-12 06:35:45 -04004223static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4224{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004225 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004226 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004227 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004228 int ret;
4229
Josef Bacikd52be812013-05-29 14:54:47 -04004230 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004231 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004232 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004233
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004234 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4235 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004236
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004237 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4238 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4239 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004240 if (ret)
4241 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004242
Yan, Zhenga22285a2010-05-16 10:48:46 -04004243 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004244 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004245 if (ret)
4246 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004247 }
Josef Bacik7b128762008-07-24 12:17:14 -04004248
Tsutomu Itohb5324022011-07-19 07:27:20 +00004249out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004250 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004251 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004252 return ret;
4253}
4254
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4256 struct btrfs_root *root,
4257 struct inode *dir, u64 objectid,
4258 const char *name, int name_len)
4259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004260 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261 struct btrfs_path *path;
4262 struct extent_buffer *leaf;
4263 struct btrfs_dir_item *di;
4264 struct btrfs_key key;
4265 u64 index;
4266 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004267 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268
4269 path = btrfs_alloc_path();
4270 if (!path)
4271 return -ENOMEM;
4272
Li Zefan33345d012011-04-20 10:31:50 +08004273 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275 if (IS_ERR_OR_NULL(di)) {
4276 if (!di)
4277 ret = -ENOENT;
4278 else
4279 ret = PTR_ERR(di);
4280 goto out;
4281 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004282
4283 leaf = path->nodes[0];
4284 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4285 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4286 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004288 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004289 goto out;
4290 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004291 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004292
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004293 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4294 root->root_key.objectid, dir_ino,
4295 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004296 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004298 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 goto out;
4300 }
Li Zefan33345d012011-04-20 10:31:50 +08004301 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004302 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303 if (IS_ERR_OR_NULL(di)) {
4304 if (!di)
4305 ret = -ENOENT;
4306 else
4307 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004308 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004309 goto out;
4310 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004311
4312 leaf = path->nodes[0];
4313 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004314 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004315 index = key.offset;
4316 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004317 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004318
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004319 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004320 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004321 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004322 goto out;
4323 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004324
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004325 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004326 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004327 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004328 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004330 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004331out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004332 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004333 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004334}
4335
Chris Mason39279cc2007-06-12 06:35:45 -04004336static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4337{
David Howells2b0143b2015-03-17 22:25:59 +00004338 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004339 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004340 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004341 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004342 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004343
David Sterbab3ae2442012-09-13 16:04:34 -06004344 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004345 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004346 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004347 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004348
Josef Bacikd52be812013-05-29 14:54:47 -04004349 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004350 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004351 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004352
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004353 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004354 err = btrfs_unlink_subvol(trans, root, dir,
4355 BTRFS_I(inode)->location.objectid,
4356 dentry->d_name.name,
4357 dentry->d_name.len);
4358 goto out;
4359 }
4360
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004361 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004362 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004363 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004364
Filipe Manana44f714d2016-06-06 16:11:13 +01004365 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4366
Chris Mason39279cc2007-06-12 06:35:45 -04004367 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004368 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4369 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4370 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004371 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004372 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004373 /*
4374 * Propagate the last_unlink_trans value of the deleted dir to
4375 * its parent directory. This is to prevent an unrecoverable
4376 * log tree in the case we do something like this:
4377 * 1) create dir foo
4378 * 2) create snapshot under dir foo
4379 * 3) delete the snapshot
4380 * 4) rmdir foo
4381 * 5) mkdir foo
4382 * 6) fsync foo or some file inside foo
4383 */
4384 if (last_unlink_trans >= trans->transid)
4385 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4386 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004387out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004388 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004389 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004390
Chris Mason39279cc2007-06-12 06:35:45 -04004391 return err;
4392}
4393
Chris Mason28f75a02015-02-04 06:59:29 -08004394static int truncate_space_check(struct btrfs_trans_handle *trans,
4395 struct btrfs_root *root,
4396 u64 bytes_deleted)
4397{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004398 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004399 int ret;
4400
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004401 /*
4402 * This is only used to apply pressure to the enospc system, we don't
4403 * intend to use this reservation at all.
4404 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004405 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004406 bytes_deleted *= fs_info->nodesize;
4407 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004408 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004409 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004410 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004411 trans->transid,
4412 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004413 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004414 }
Chris Mason28f75a02015-02-04 06:59:29 -08004415 return ret;
4416
4417}
4418
Josef Bacikddfae632017-10-19 14:16:02 -04004419/*
4420 * Return this if we need to call truncate_block for the last bit of the
4421 * truncate.
4422 */
4423#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004424
Chris Mason323ac952008-10-01 19:05:46 -04004425/*
Chris Mason39279cc2007-06-12 06:35:45 -04004426 * this can truncate away extent items, csum items and directory items.
4427 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004428 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004429 *
4430 * csum items that cross the new i_size are truncated to the new size
4431 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004432 *
4433 * min_type is the minimum key type to truncate down to. If set to 0, this
4434 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004435 */
Yan, Zheng80825102009-11-12 09:35:36 +00004436int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4437 struct btrfs_root *root,
4438 struct inode *inode,
4439 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004441 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004442 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004443 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004444 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004445 struct btrfs_key key;
4446 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004447 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004448 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004449 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004450 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004451 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004452 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004453 int found_extent;
4454 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004455 int pending_del_nr = 0;
4456 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004457 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004458 int ret;
4459 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004460 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004461 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004462 bool be_nice = false;
4463 bool should_throttle = false;
4464 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004465
4466 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004467
Chris Mason28ed1342014-12-17 09:41:04 -08004468 /*
4469 * for non-free space inodes and ref cows, we want to back off from
4470 * time to time
4471 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004472 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004473 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004474 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004475
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004476 path = btrfs_alloc_path();
4477 if (!path)
4478 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004479 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004480
Josef Bacik5dc562c2012-08-17 13:14:17 -04004481 /*
4482 * We want to drop from the next block forward in case this new size is
4483 * not block aligned since we will be keeping the last block of the
4484 * extent just the way it is.
4485 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004486 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004488 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004489 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004490 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004491
Miao Xie16cdcec2011-04-22 18:12:22 +08004492 /*
4493 * This function is also used to drop the items in the log tree before
4494 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4495 * it is used to drop the loged items. So we shouldn't kill the delayed
4496 * items.
4497 */
4498 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004499 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004500
Li Zefan33345d012011-04-20 10:31:50 +08004501 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004502 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004503 key.type = (u8)-1;
4504
Chris Mason85e21ba2008-01-29 15:11:36 -05004505search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004506 /*
4507 * with a 16K leaf size and 128MB extents, you can actually queue
4508 * up a huge file in a single leaf. Most of the time that
4509 * bytes_deleted is > 0, it will be huge by the time we get here
4510 */
Byongho Leeee221842015-12-15 01:42:10 +09004511 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004512 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004513 err = -EAGAIN;
4514 goto error;
4515 }
4516 }
4517
4518
Chris Masonb9473432009-03-13 11:00:37 -04004519 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004520 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004521 if (ret < 0) {
4522 err = ret;
4523 goto out;
4524 }
Chris Masond3977122009-01-05 21:25:51 -05004525
Chris Mason85e21ba2008-01-29 15:11:36 -05004526 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004527 /* there are no items in the tree for us to truncate, we're
4528 * done
4529 */
Yan, Zheng80825102009-11-12 09:35:36 +00004530 if (path->slots[0] == 0)
4531 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004532 path->slots[0]--;
4533 }
4534
Chris Masond3977122009-01-05 21:25:51 -05004535 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004536 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004537 leaf = path->nodes[0];
4538 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004539 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004540
Li Zefan33345d012011-04-20 10:31:50 +08004541 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004542 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004543
Chris Mason85e21ba2008-01-29 15:11:36 -05004544 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004545 break;
4546
Chris Mason5f39d392007-10-15 16:14:19 -04004547 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004548 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004549 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004550 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004551 extent_type = btrfs_file_extent_type(leaf, fi);
4552 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004553 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004554 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004555
4556 trace_btrfs_truncate_show_fi_regular(
4557 BTRFS_I(inode), leaf, fi,
4558 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004559 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004560 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004561 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004562
4563 trace_btrfs_truncate_show_fi_inline(
4564 BTRFS_I(inode), leaf, fi, path->slots[0],
4565 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004566 }
Yan008630c2007-11-07 13:31:09 -05004567 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004568 }
Yan, Zheng80825102009-11-12 09:35:36 +00004569 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004570 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004571 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004572 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004573 break;
4574 if (found_key.offset >= new_size)
4575 del_item = 1;
4576 else
4577 del_item = 0;
4578 }
Chris Mason39279cc2007-06-12 06:35:45 -04004579 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004580 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004581 if (found_type != BTRFS_EXTENT_DATA_KEY)
4582 goto delete;
4583
4584 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004585 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004586 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004587 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004588 u64 orig_num_bytes =
4589 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004590 extent_num_bytes = ALIGN(new_size -
4591 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004592 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004593 btrfs_set_file_extent_num_bytes(leaf, fi,
4594 extent_num_bytes);
4595 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004596 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004597 if (test_bit(BTRFS_ROOT_REF_COWS,
4598 &root->state) &&
4599 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004600 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004601 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004602 } else {
Chris Masondb945352007-10-15 16:15:53 -04004603 extent_num_bytes =
4604 btrfs_file_extent_disk_num_bytes(leaf,
4605 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004606 extent_offset = found_key.offset -
4607 btrfs_file_extent_offset(leaf, fi);
4608
Chris Mason39279cc2007-06-12 06:35:45 -04004609 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004610 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 if (extent_start != 0) {
4612 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004613 if (test_bit(BTRFS_ROOT_REF_COWS,
4614 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004615 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 }
4617 }
Chris Mason90692182008-02-08 13:49:28 -05004618 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004619 /*
4620 * we can't truncate inline items that have had
4621 * special encodings
4622 */
4623 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004624 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004625 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4626 btrfs_file_extent_compression(leaf, fi) == 0) {
4627 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004628
Josef Bacikddfae632017-10-19 14:16:02 -04004629 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4630 size = btrfs_file_extent_calc_inline_size(size);
4631 btrfs_truncate_item(root->fs_info, path, size, 1);
4632 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004633 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004634 * We have to bail so the last_size is set to
4635 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004636 */
Josef Bacikddfae632017-10-19 14:16:02 -04004637 err = NEED_TRUNCATE_BLOCK;
4638 break;
Chris Mason90692182008-02-08 13:49:28 -05004639 }
Josef Bacikddfae632017-10-19 14:16:02 -04004640
4641 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4642 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004643 }
Chris Mason179e29e2007-11-01 11:28:41 -04004644delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004645 if (del_item)
4646 last_size = found_key.offset;
4647 else
4648 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004649 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004650 if (!pending_del_nr) {
4651 /* no pending yet, add ourselves */
4652 pending_del_slot = path->slots[0];
4653 pending_del_nr = 1;
4654 } else if (pending_del_nr &&
4655 path->slots[0] + 1 == pending_del_slot) {
4656 /* hop on the pending chunk */
4657 pending_del_nr++;
4658 pending_del_slot = path->slots[0];
4659 } else {
Chris Masond3977122009-01-05 21:25:51 -05004660 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004661 }
Chris Mason39279cc2007-06-12 06:35:45 -04004662 } else {
4663 break;
4664 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004665 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004666
Miao Xie27cdeb72014-04-02 19:51:05 +08004667 if (found_extent &&
4668 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004669 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004670 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004671 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004672 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004673 extent_num_bytes, 0,
4674 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004675 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004676 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004677 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4678 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004679 trans->delayed_ref_updates * 2,
4680 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004681 if (be_nice) {
4682 if (truncate_space_check(trans, root,
4683 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004684 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004685 }
4686 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004687 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004688 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004689 }
Chris Mason39279cc2007-06-12 06:35:45 -04004690 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004691
Yan, Zheng80825102009-11-12 09:35:36 +00004692 if (found_type == BTRFS_INODE_ITEM_KEY)
4693 break;
4694
4695 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004696 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004697 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004698 if (pending_del_nr) {
4699 ret = btrfs_del_items(trans, root, path,
4700 pending_del_slot,
4701 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004702 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004703 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004704 goto error;
4705 }
Yan, Zheng80825102009-11-12 09:35:36 +00004706 pending_del_nr = 0;
4707 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004708 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004709 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004710 unsigned long updates = trans->delayed_ref_updates;
4711 if (updates) {
4712 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004713 ret = btrfs_run_delayed_refs(trans,
4714 fs_info,
4715 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004716 if (ret && !err)
4717 err = ret;
4718 }
4719 }
Chris Mason28f75a02015-02-04 06:59:29 -08004720 /*
4721 * if we failed to refill our space rsv, bail out
4722 * and let the transaction restart
4723 */
4724 if (should_end) {
4725 err = -EAGAIN;
4726 goto error;
4727 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004729 } else {
4730 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004731 }
Chris Mason39279cc2007-06-12 06:35:45 -04004732 }
Yan, Zheng80825102009-11-12 09:35:36 +00004733out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004734 if (pending_del_nr) {
4735 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4736 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004737 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004738 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004739 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004740error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004741 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4742 ASSERT(last_size >= new_size);
4743 if (!err && last_size > new_size)
4744 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004745 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004746 }
Chris Mason28ed1342014-12-17 09:41:04 -08004747
Chris Mason39279cc2007-06-12 06:35:45 -04004748 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004749
Byongho Leeee221842015-12-15 01:42:10 +09004750 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004751 unsigned long updates = trans->delayed_ref_updates;
4752 if (updates) {
4753 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004754 ret = btrfs_run_delayed_refs(trans, fs_info,
4755 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004756 if (ret && !err)
4757 err = ret;
4758 }
4759 }
Yan, Zheng80825102009-11-12 09:35:36 +00004760 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004761}
4762
Chris Masona52d9a82007-08-27 16:49:44 -04004763/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304764 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004765 * @inode - inode that we're zeroing
4766 * @from - the offset to start zeroing
4767 * @len - the length to zero, 0 to zero the entire range respective to the
4768 * offset
4769 * @front - zero up to the offset instead of from the offset on
4770 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304771 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004773 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004775 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004778 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004779 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4780 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004781 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004782 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004784 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004785 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004787 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004788 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004789 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 u64 block_start;
4791 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004792
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004793 if (IS_ALIGNED(offset, blocksize) &&
4794 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004795 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796
Josef Bacik8b62f872017-10-19 14:15:55 -04004797 block_start = round_down(from, blocksize);
4798 block_end = block_start + blocksize - 1;
4799
Qu Wenruo364ecf32017-02-27 15:10:38 +08004800 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004801 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004802 if (ret)
4803 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004804
Chris Mason211c17f2008-05-15 09:13:45 -04004805again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004806 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004807 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004808 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004809 block_start, blocksize);
4810 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004811 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004812 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004813 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004814
Chris Masona52d9a82007-08-27 16:49:44 -04004815 if (!PageUptodate(page)) {
4816 ret = btrfs_readpage(NULL, page);
4817 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004818 if (page->mapping != mapping) {
4819 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004820 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004821 goto again;
4822 }
Chris Masona52d9a82007-08-27 16:49:44 -04004823 if (!PageUptodate(page)) {
4824 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004825 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004826 }
4827 }
Chris Mason211c17f2008-05-15 09:13:45 -04004828 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004831 set_page_extent_mapped(page);
4832
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004834 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304835 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004836 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004837 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004838 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004839 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004840 btrfs_put_ordered_extent(ordered);
4841 goto again;
4842 }
4843
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004845 EXTENT_DIRTY | EXTENT_DELALLOC |
4846 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004847 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004848
Filipe Mananae3b8a482017-11-04 00:16:59 +00004849 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004850 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004851 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304852 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004853 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004854 goto out_unlock;
4855 }
4856
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004858 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004860 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004861 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304862 memset(kaddr + (block_start - page_offset(page)),
4863 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004864 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304865 memset(kaddr + (block_start - page_offset(page)) + offset,
4866 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004867 flush_dcache_page(page);
4868 kunmap(page);
4869 }
Chris Mason247e7432008-07-17 12:53:51 -04004870 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004871 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004872 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004873
Chris Mason89642222008-07-24 09:41:53 -04004874out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004875 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004876 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304877 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004878 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004879 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004880 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004881out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004882 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004883 return ret;
4884}
4885
Josef Bacik16e75492013-10-22 12:18:51 -04004886static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4887 u64 offset, u64 len)
4888{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004890 struct btrfs_trans_handle *trans;
4891 int ret;
4892
4893 /*
4894 * Still need to make sure the inode looks like it's been updated so
4895 * that any holes get logged if we fsync.
4896 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004897 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4898 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004899 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4900 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4901 return 0;
4902 }
4903
4904 /*
4905 * 1 - for the one we're dropping
4906 * 1 - for the one we're adding
4907 * 1 - for updating the inode.
4908 */
4909 trans = btrfs_start_transaction(root, 3);
4910 if (IS_ERR(trans))
4911 return PTR_ERR(trans);
4912
4913 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004915 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004916 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004917 return ret;
4918 }
4919
David Sterbaf85b7372017-01-20 14:54:07 +01004920 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4921 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004922 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004923 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004924 else
4925 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004926 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004927 return ret;
4928}
4929
Josef Bacik695a0d02011-03-04 15:46:53 -05004930/*
4931 * This function puts in dummy file extents for the area we're creating a hole
4932 * for. So if we are truncating this file to a larger size we need to insert
4933 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4934 * the range between oldsize and size
4935 */
Josef Bacika41ad392011-01-31 15:30:16 -05004936int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004938 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004939 struct btrfs_root *root = BTRFS_I(inode)->root;
4940 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004941 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004942 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004944 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4945 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004946 u64 last_byte;
4947 u64 cur_offset;
4948 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004949 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004950
Josef Bacika71754f2013-06-17 17:14:39 -04004951 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304952 * If our size started in the middle of a block we need to zero out the
4953 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004954 * expose stale data.
4955 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304956 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004957 if (err)
4958 return err;
4959
Yan Zheng9036c102008-10-30 14:19:41 -04004960 if (size <= hole_start)
4961 return 0;
4962
Yan Zheng9036c102008-10-30 14:19:41 -04004963 while (1) {
4964 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004965
David Sterbaff13db42015-12-03 14:30:40 +01004966 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004967 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004968 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004969 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004970 if (!ordered)
4971 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004972 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004973 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004974 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004975 btrfs_put_ordered_extent(ordered);
4976 }
4977
Yan Zheng9036c102008-10-30 14:19:41 -04004978 cur_offset = hole_start;
4979 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004980 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004981 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004982 if (IS_ERR(em)) {
4983 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004984 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004985 break;
4986 }
Yan Zheng9036c102008-10-30 14:19:41 -04004987 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004988 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004989 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004990 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004991 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004992
Josef Bacik16e75492013-10-22 12:18:51 -04004993 err = maybe_insert_hole(root, inode, cur_offset,
4994 hole_size);
4995 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004996 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004997 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004998 cur_offset + hole_size - 1, 0);
4999 hole_em = alloc_extent_map();
5000 if (!hole_em) {
5001 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5002 &BTRFS_I(inode)->runtime_flags);
5003 goto next;
5004 }
5005 hole_em->start = cur_offset;
5006 hole_em->len = hole_size;
5007 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005008
Josef Bacik5dc562c2012-08-17 13:14:17 -04005009 hole_em->block_start = EXTENT_MAP_HOLE;
5010 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005011 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005012 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005013 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005014 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005015 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005016
5017 while (1) {
5018 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005019 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005020 write_unlock(&em_tree->lock);
5021 if (err != -EEXIST)
5022 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005023 btrfs_drop_extent_cache(BTRFS_I(inode),
5024 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005025 cur_offset +
5026 hole_size - 1, 0);
5027 }
5028 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005029 }
Josef Bacik16e75492013-10-22 12:18:51 -04005030next:
Yan Zheng9036c102008-10-30 14:19:41 -04005031 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005032 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005033 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005034 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005035 break;
5036 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005037 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005038 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005039 return err;
5040}
5041
Eric Sandeen3972f262013-01-12 02:57:22 +00005042static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005043{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005044 struct btrfs_root *root = BTRFS_I(inode)->root;
5045 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005046 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005047 loff_t newsize = attr->ia_size;
5048 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005049 int ret;
5050
Eric Sandeen3972f262013-01-12 02:57:22 +00005051 /*
5052 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5053 * special case where we need to update the times despite not having
5054 * these flags set. For all other operations the VFS set these flags
5055 * explicitly if it wants a timestamp update.
5056 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005057 if (newsize != oldsize) {
5058 inode_inc_iversion(inode);
5059 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5060 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005061 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005062 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005063
Josef Bacika41ad392011-01-31 15:30:16 -05005064 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005065 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005066 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005067 * This is to ensure the snapshot captures a fully consistent
5068 * state of this file - if the snapshot captures this expanding
5069 * truncation, it must capture all writes that happened before
5070 * this truncation.
5071 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005072 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005073 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005074 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005075 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005076 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005077 }
Yan, Zheng80825102009-11-12 09:35:36 +00005078
Miao Xief4a2f4c2011-12-14 20:12:01 -05005079 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005080 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005081 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005082 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005083 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005084
5085 i_size_write(inode, newsize);
5086 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305087 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005088 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005089 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005090 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005091 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005092
Josef Bacika41ad392011-01-31 15:30:16 -05005093 /*
5094 * We're truncating a file that used to have good data down to
5095 * zero. Make sure it gets into the ordered flush list so that
5096 * any new writes get down to disk quickly.
5097 */
5098 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005099 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5100 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005101
Josef Bacikf3fe8202013-01-07 17:03:21 -05005102 /*
5103 * 1 for the orphan item we're going to add
5104 * 1 for the orphan item deletion.
5105 */
5106 trans = btrfs_start_transaction(root, 2);
5107 if (IS_ERR(trans))
5108 return PTR_ERR(trans);
5109
5110 /*
5111 * We need to do this in case we fail at _any_ point during the
5112 * actual truncate. Once we do the truncate_setsize we could
5113 * invalidate pages which forces any outstanding ordered io to
5114 * be instantly completed which will give us extents that need
5115 * to be truncated. If we fail to get an orphan inode down we
5116 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005117 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005118 * will be consistent.
5119 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005120 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005121 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005122 if (ret)
5123 return ret;
5124
Josef Bacika41ad392011-01-31 15:30:16 -05005125 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5126 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005127
5128 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005129 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005130 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005131 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005132
Josef Bacika41ad392011-01-31 15:30:16 -05005133 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005134 if (ret && inode->i_nlink) {
5135 int err;
5136
Liu Bo19fd2df2016-12-01 13:01:02 -08005137 /* To get a stable disk_i_size */
5138 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5139 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005140 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005141 return err;
5142 }
5143
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005144 /*
5145 * failed to truncate, disk_i_size is only adjusted down
5146 * as we remove extents, so it should represent the true
5147 * size of the inode, so reset the in memory size and
5148 * delete our orphan entry.
5149 */
5150 trans = btrfs_join_transaction(root);
5151 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005152 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005153 return ret;
5154 }
5155 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005156 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005157 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005158 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005159 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005160 }
Yan, Zheng80825102009-11-12 09:35:36 +00005161 }
5162
Josef Bacika41ad392011-01-31 15:30:16 -05005163 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005164}
5165
Chris Mason39279cc2007-06-12 06:35:45 -04005166static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5167{
David Howells2b0143b2015-03-17 22:25:59 +00005168 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005169 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005170 int err;
5171
Li Zefanb83cc962010-12-20 16:04:08 +08005172 if (btrfs_root_readonly(root))
5173 return -EROFS;
5174
Jan Kara31051c82016-05-26 16:55:18 +02005175 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005176 if (err)
5177 return err;
5178
Chris Mason5a3f23d2009-03-31 13:27:11 -04005179 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005180 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005181 if (err)
5182 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005183 }
Yan Zheng9036c102008-10-30 14:19:41 -04005184
Christoph Hellwig10257742010-06-04 11:30:02 +02005185 if (attr->ia_valid) {
5186 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005187 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005188 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005189
Josef Bacik22c44fe2011-11-30 10:45:38 -05005190 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005191 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005192 }
5193
Chris Mason39279cc2007-06-12 06:35:45 -04005194 return err;
5195}
Chris Mason61295eb2008-01-14 16:24:38 -05005196
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005197/*
5198 * While truncating the inode pages during eviction, we get the VFS calling
5199 * btrfs_invalidatepage() against each page of the inode. This is slow because
5200 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5201 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5202 * extent_state structures over and over, wasting lots of time.
5203 *
5204 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5205 * those expensive operations on a per page basis and do only the ordered io
5206 * finishing, while we release here the extent_map and extent_state structures,
5207 * without the excessive merging and splitting.
5208 */
5209static void evict_inode_truncate_pages(struct inode *inode)
5210{
5211 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5212 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5213 struct rb_node *node;
5214
5215 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005216 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217
5218 write_lock(&map_tree->lock);
5219 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5220 struct extent_map *em;
5221
5222 node = rb_first(&map_tree->map);
5223 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005224 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5225 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005226 remove_extent_mapping(map_tree, em);
5227 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005228 if (need_resched()) {
5229 write_unlock(&map_tree->lock);
5230 cond_resched();
5231 write_lock(&map_tree->lock);
5232 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 }
5234 write_unlock(&map_tree->lock);
5235
Filipe Manana6ca07092015-05-26 00:55:42 +01005236 /*
5237 * Keep looping until we have no more ranges in the io tree.
5238 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005239 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5240 * still in progress (unlocked the pages in the bio but did not yet
5241 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005242 * ranges can still be locked and eviction started because before
5243 * submitting those bios, which are executed by a separate task (work
5244 * queue kthread), inode references (inode->i_count) were not taken
5245 * (which would be dropped in the end io callback of each bio).
5246 * Therefore here we effectively end up waiting for those bios and
5247 * anyone else holding locked ranges without having bumped the inode's
5248 * reference count - if we don't do it, when they access the inode's
5249 * io_tree to unlock a range it may be too late, leading to an
5250 * use-after-free issue.
5251 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005252 spin_lock(&io_tree->lock);
5253 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5254 struct extent_state *state;
5255 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005256 u64 start;
5257 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005258
5259 node = rb_first(&io_tree->state);
5260 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005261 start = state->start;
5262 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 spin_unlock(&io_tree->lock);
5264
David Sterbaff13db42015-12-03 14:30:40 +01005265 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005266
5267 /*
5268 * If still has DELALLOC flag, the extent didn't reach disk,
5269 * and its reserved space won't be freed by delayed_ref.
5270 * So we need to free its reserved space here.
5271 * (Refer to comment in btrfs_invalidatepage, case 2)
5272 *
5273 * Note, end is the bytenr of last byte, so we need + 1 here.
5274 */
5275 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005276 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005277
Filipe Manana6ca07092015-05-26 00:55:42 +01005278 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279 EXTENT_LOCKED | EXTENT_DIRTY |
5280 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005281 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005282
Filipe Manana7064dd52014-08-08 02:47:05 +01005283 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005284 spin_lock(&io_tree->lock);
5285 }
5286 spin_unlock(&io_tree->lock);
5287}
5288
Al Virobd555972010-06-07 11:35:40 -04005289void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005290{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005292 struct btrfs_trans_handle *trans;
5293 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005294 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005295 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005296 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005297 int ret;
5298
liubo1abe9b82011-03-24 11:18:59 +00005299 trace_btrfs_inode_evict(inode);
5300
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005301 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005302 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005303 return;
5304 }
5305
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005306 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005307
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005308 evict_inode_truncate_pages(inode);
5309
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005310 if (inode->i_nlink &&
5311 ((btrfs_root_refs(&root->root_item) != 0 &&
5312 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005313 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005314 goto no_delete;
5315
Chris Mason39279cc2007-06-12 06:35:45 -04005316 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005317 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005318 goto no_delete;
5319 }
Al Virobd555972010-06-07 11:35:40 -04005320 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005321 if (!special_file(inode->i_mode))
5322 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005323
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005324 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005325
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005326 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005327 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005328 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005329 goto no_delete;
5330 }
5331
Yan, Zheng76dda932009-09-21 16:00:26 -04005332 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005333 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5334 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005335 goto no_delete;
5336 }
5337
Nikolay Borisovaa790212017-01-10 20:35:40 +02005338 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005339 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005340 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005341 goto no_delete;
5342 }
5343
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005344 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005345 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005346 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005347 goto no_delete;
5348 }
Josef Bacik4a338542011-08-29 11:01:31 -04005349 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005350 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005351 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005352
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005353 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005354
Josef Bacik4289a662011-08-05 13:22:24 -04005355 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005356 * This is a bit simpler than btrfs_truncate since we've already
5357 * reserved our space for our orphan item in the unlink, so we just
5358 * need to reserve some slack space in case we add bytes and update
5359 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005360 */
Yan, Zheng80825102009-11-12 09:35:36 +00005361 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005362 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5363 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005364
Josef Bacik726c35f2011-09-26 15:46:06 -04005365 /*
5366 * Try and steal from the global reserve since we will
5367 * likely not use this space anyway, we want to try as
5368 * hard as possible to get this to work.
5369 */
5370 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005371 steal_from_global++;
5372 else
5373 steal_from_global = 0;
5374 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005375
Josef Bacik3bce8762015-02-24 12:35:51 -08005376 /*
5377 * steal_from_global == 0: we reserved stuff, hooray!
5378 * steal_from_global == 1: we didn't reserve stuff, boo!
5379 * steal_from_global == 2: we've committed, still not a lot of
5380 * room but maybe we'll have room in the global reserve this
5381 * time.
5382 * steal_from_global == 3: abandon all hope!
5383 */
5384 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005385 btrfs_warn(fs_info,
5386 "Could not get space for a delete, will truncate on mount %d",
5387 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005388 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005389 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005390 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005391 }
5392
Miao Xie0e8c36a2012-12-19 06:59:51 +00005393 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005394 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005395 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005396 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005397 goto no_delete;
5398 }
5399
Josef Bacik3bce8762015-02-24 12:35:51 -08005400 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005401 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005402 * sure there is room to do it, if not we need to commit and try
5403 * again.
5404 */
5405 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005406 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005407 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005408 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005409 else
5410 ret = -ENOSPC;
5411 }
5412
5413 /*
5414 * Couldn't steal from the global reserve, we have too much
5415 * pending stuff built up, commit the transaction and try it
5416 * again.
5417 */
5418 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005419 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005420 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005421 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005422 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005423 goto no_delete;
5424 }
5425 continue;
5426 } else {
5427 steal_from_global = 0;
5428 }
5429
Josef Bacik4289a662011-08-05 13:22:24 -04005430 trans->block_rsv = rsv;
5431
Yan, Zhengd68fc572010-05-16 10:49:58 -04005432 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005433 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005434 break;
5435
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005436 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005437 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005438 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005439 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005440 }
5441
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005442 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005443
Josef Bacik4ef31a42013-08-13 14:10:08 -04005444 /*
5445 * Errors here aren't a big deal, it just means we leave orphan items
5446 * in the tree. They will be cleaned up on the next mount.
5447 */
Yan, Zheng80825102009-11-12 09:35:36 +00005448 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005449 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005450 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005451 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005452 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005453 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005454
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005455 trans->block_rsv = &fs_info->trans_block_rsv;
5456 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005457 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005458 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005459
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005460 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005461 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005462no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005463 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005464 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005465}
5466
5467/*
5468 * this returns the key found in the dir entry in the location pointer.
5469 * If no dir entries were found, location->objectid is 0.
5470 */
5471static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5472 struct btrfs_key *location)
5473{
5474 const char *name = dentry->d_name.name;
5475 int namelen = dentry->d_name.len;
5476 struct btrfs_dir_item *di;
5477 struct btrfs_path *path;
5478 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005479 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005480
5481 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005482 if (!path)
5483 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005484
David Sterbaf85b7372017-01-20 14:54:07 +01005485 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5486 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005487 if (IS_ERR(di))
5488 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005489
David Sterbac7040052011-04-19 18:00:01 +02005490 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005491 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005492
Chris Mason5f39d392007-10-15 16:14:19 -04005493 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005494 if (location->type != BTRFS_INODE_ITEM_KEY &&
5495 location->type != BTRFS_ROOT_ITEM_KEY) {
5496 btrfs_warn(root->fs_info,
5497"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5498 __func__, name, btrfs_ino(BTRFS_I(dir)),
5499 location->objectid, location->type, location->offset);
5500 goto out_err;
5501 }
Chris Mason39279cc2007-06-12 06:35:45 -04005502out:
Chris Mason39279cc2007-06-12 06:35:45 -04005503 btrfs_free_path(path);
5504 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005505out_err:
5506 location->objectid = 0;
5507 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005508}
5509
5510/*
5511 * when we hit a tree root in a directory, the btrfs part of the inode
5512 * needs to be changed to reflect the root directory of the tree root. This
5513 * is kind of like crossing a mount point.
5514 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005515static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 struct inode *dir,
5517 struct dentry *dentry,
5518 struct btrfs_key *location,
5519 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005520{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 struct btrfs_path *path;
5522 struct btrfs_root *new_root;
5523 struct btrfs_root_ref *ref;
5524 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005525 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 int ret;
5527 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005528
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 path = btrfs_alloc_path();
5530 if (!path) {
5531 err = -ENOMEM;
5532 goto out;
5533 }
Chris Mason39279cc2007-06-12 06:35:45 -04005534
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005535 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005536 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5537 key.type = BTRFS_ROOT_REF_KEY;
5538 key.offset = location->objectid;
5539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005540 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 if (ret) {
5542 if (ret < 0)
5543 err = ret;
5544 goto out;
5545 }
Chris Mason39279cc2007-06-12 06:35:45 -04005546
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 leaf = path->nodes[0];
5548 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005549 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5551 goto out;
5552
5553 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5554 (unsigned long)(ref + 1),
5555 dentry->d_name.len);
5556 if (ret)
5557 goto out;
5558
David Sterbab3b4aa72011-04-21 01:20:15 +02005559 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005560
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005561 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005562 if (IS_ERR(new_root)) {
5563 err = PTR_ERR(new_root);
5564 goto out;
5565 }
5566
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005567 *sub_root = new_root;
5568 location->objectid = btrfs_root_dirid(&new_root->root_item);
5569 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005570 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005571 err = 0;
5572out:
5573 btrfs_free_path(path);
5574 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005575}
5576
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577static void inode_tree_add(struct inode *inode)
5578{
5579 struct btrfs_root *root = BTRFS_I(inode)->root;
5580 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005581 struct rb_node **p;
5582 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005583 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005584 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585
Al Viro1d3382cb2010-10-23 15:19:20 -04005586 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005587 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005588 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005590 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591 while (*p) {
5592 parent = *p;
5593 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5594
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005595 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005596 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005597 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005598 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 else {
5600 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005601 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005602 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005603 RB_CLEAR_NODE(parent);
5604 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005605 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 }
5607 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005608 rb_link_node(new, parent, p);
5609 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 spin_unlock(&root->inode_lock);
5611}
5612
5613static void inode_tree_del(struct inode *inode)
5614{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005615 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005617 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005619 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005623 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005625 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005626
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005627 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005628 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005629 spin_lock(&root->inode_lock);
5630 empty = RB_EMPTY_ROOT(&root->inode_tree);
5631 spin_unlock(&root->inode_lock);
5632 if (empty)
5633 btrfs_add_dead_root(root);
5634 }
5635}
5636
Jeff Mahoney143bede2012-03-01 14:56:26 +01005637void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005638{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005639 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005640 struct rb_node *node;
5641 struct rb_node *prev;
5642 struct btrfs_inode *entry;
5643 struct inode *inode;
5644 u64 objectid = 0;
5645
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005646 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005647 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005648
5649 spin_lock(&root->inode_lock);
5650again:
5651 node = root->inode_tree.rb_node;
5652 prev = NULL;
5653 while (node) {
5654 prev = node;
5655 entry = rb_entry(node, struct btrfs_inode, rb_node);
5656
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005657 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005658 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005659 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005660 node = node->rb_right;
5661 else
5662 break;
5663 }
5664 if (!node) {
5665 while (prev) {
5666 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005667 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005668 node = prev;
5669 break;
5670 }
5671 prev = rb_next(prev);
5672 }
5673 }
5674 while (node) {
5675 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005676 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005677 inode = igrab(&entry->vfs_inode);
5678 if (inode) {
5679 spin_unlock(&root->inode_lock);
5680 if (atomic_read(&inode->i_count) > 1)
5681 d_prune_aliases(inode);
5682 /*
Al Viro45321ac2010-06-07 13:43:19 -04005683 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005684 * the inode cache when its usage count
5685 * hits zero.
5686 */
5687 iput(inode);
5688 cond_resched();
5689 spin_lock(&root->inode_lock);
5690 goto again;
5691 }
5692
5693 if (cond_resched_lock(&root->inode_lock))
5694 goto again;
5695
5696 node = rb_next(node);
5697 }
5698 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699}
5700
Chris Masone02119d2008-09-05 16:13:11 -04005701static int btrfs_init_locked_inode(struct inode *inode, void *p)
5702{
5703 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005704 inode->i_ino = args->location->objectid;
5705 memcpy(&BTRFS_I(inode)->location, args->location,
5706 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005707 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005708 return 0;
5709}
5710
5711static int btrfs_find_actor(struct inode *inode, void *opaque)
5712{
5713 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005714 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005715 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005716}
5717
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005718static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005719 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005720 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005721{
5722 struct inode *inode;
5723 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005724 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005725
Chris Mason90d3e592014-01-09 17:28:00 -08005726 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005727 args.root = root;
5728
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005729 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005730 btrfs_init_locked_inode,
5731 (void *)&args);
5732 return inode;
5733}
5734
Balaji Rao1a54ef82008-07-21 02:01:04 +05305735/* Get an inode object given its location and corresponding root.
5736 * Returns in *is_new if the inode was read from disk
5737 */
5738struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005739 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305740{
5741 struct inode *inode;
5742
Chris Mason90d3e592014-01-09 17:28:00 -08005743 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305744 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005745 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305746
5747 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005748 int ret;
5749
5750 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005751 if (!is_bad_inode(inode)) {
5752 inode_tree_add(inode);
5753 unlock_new_inode(inode);
5754 if (new)
5755 *new = 1;
5756 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005757 unlock_new_inode(inode);
5758 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005759 ASSERT(ret < 0);
5760 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005761 }
5762 }
5763
Balaji Rao1a54ef82008-07-21 02:01:04 +05305764 return inode;
5765}
5766
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767static struct inode *new_simple_dir(struct super_block *s,
5768 struct btrfs_key *key,
5769 struct btrfs_root *root)
5770{
5771 struct inode *inode = new_inode(s);
5772
5773 if (!inode)
5774 return ERR_PTR(-ENOMEM);
5775
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776 BTRFS_I(inode)->root = root;
5777 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005778 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005779
5780 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005781 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005782 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005783 inode->i_fop = &simple_dir_operations;
5784 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005785 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305786 inode->i_atime = inode->i_mtime;
5787 inode->i_ctime = inode->i_mtime;
5788 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789
5790 return inode;
5791}
5792
Chris Mason3de45862008-11-17 21:02:50 -05005793struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005794{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005795 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005796 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005797 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005798 struct btrfs_root *sub_root = root;
5799 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005800 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005801 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005802
5803 if (dentry->d_name.len > BTRFS_NAME_LEN)
5804 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005805
Jeff Layton39e3c952012-11-28 11:30:53 -05005806 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 if (ret < 0)
5808 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005809
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005810 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005811 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005812
5813 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005814 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005815 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005816 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005817
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005818 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005819 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005820 &location, &sub_root);
5821 if (ret < 0) {
5822 if (ret != -ENOENT)
5823 inode = ERR_PTR(ret);
5824 else
5825 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5826 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005827 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005828 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005829 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005830
Julia Lawall34d19ba2011-01-24 19:55:19 +00005831 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005832 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005833 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005834 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005835 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005836 if (ret) {
5837 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005838 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005839 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005840 }
5841
Chris Mason3de45862008-11-17 21:02:50 -05005842 return inode;
5843}
5844
Nick Pigginfe15ce42011-01-07 17:49:23 +11005845static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005846{
5847 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005848 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005849
Li Zefan848cce02012-02-21 17:04:28 +08005850 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005851 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005852
Li Zefan848cce02012-02-21 17:04:28 +08005853 if (inode) {
5854 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005855 if (btrfs_root_refs(&root->root_item) == 0)
5856 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005857
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005858 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005859 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005860 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005861 return 0;
5862}
5863
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005864static void btrfs_dentry_release(struct dentry *dentry)
5865{
Daeseok Youn944a4512014-04-14 15:37:02 +09005866 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005867}
5868
Chris Mason3de45862008-11-17 21:02:50 -05005869static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005870 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005871{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005872 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005873
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005874 inode = btrfs_lookup_dentry(dir, dentry);
5875 if (IS_ERR(inode)) {
5876 if (PTR_ERR(inode) == -ENOENT)
5877 inode = NULL;
5878 else
5879 return ERR_CAST(inode);
5880 }
5881
Al Viro41d28bc2014-10-12 22:24:21 -04005882 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005883}
5884
Miao Xie16cdcec2011-04-22 18:12:22 +08005885unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005886 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5887};
5888
Josef Bacik23b5ec72017-07-24 15:14:25 -04005889/*
5890 * All this infrastructure exists because dir_emit can fault, and we are holding
5891 * the tree lock when doing readdir. For now just allocate a buffer and copy
5892 * our information into that, and then dir_emit from the buffer. This is
5893 * similar to what NFS does, only we don't keep the buffer around in pagecache
5894 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5895 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5896 * tree lock.
5897 */
5898static int btrfs_opendir(struct inode *inode, struct file *file)
5899{
5900 struct btrfs_file_private *private;
5901
5902 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5903 if (!private)
5904 return -ENOMEM;
5905 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5906 if (!private->filldir_buf) {
5907 kfree(private);
5908 return -ENOMEM;
5909 }
5910 file->private_data = private;
5911 return 0;
5912}
5913
5914struct dir_entry {
5915 u64 ino;
5916 u64 offset;
5917 unsigned type;
5918 int name_len;
5919};
5920
5921static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5922{
5923 while (entries--) {
5924 struct dir_entry *entry = addr;
5925 char *name = (char *)(entry + 1);
5926
5927 ctx->pos = entry->offset;
5928 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5929 entry->type))
5930 return 1;
5931 addr += sizeof(struct dir_entry) + entry->name_len;
5932 ctx->pos++;
5933 }
5934 return 0;
5935}
5936
Al Viro9cdda8d2013-05-22 16:48:09 -04005937static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005938{
Al Viro9cdda8d2013-05-22 16:48:09 -04005939 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005940 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005941 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005942 struct btrfs_dir_item *di;
5943 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005944 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005945 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005946 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005947 struct list_head ins_list;
5948 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005949 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005950 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005951 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005952 char *name_ptr;
5953 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005954 int entries = 0;
5955 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005956 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005957 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005958
Al Viro9cdda8d2013-05-22 16:48:09 -04005959 if (!dir_emit_dots(file, ctx))
5960 return 0;
5961
David Woodhouse49593bf2008-08-17 17:08:36 +01005962 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005963 if (!path)
5964 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005965
Josef Bacik23b5ec72017-07-24 15:14:25 -04005966 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005967 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005968
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005969 INIT_LIST_HEAD(&ins_list);
5970 INIT_LIST_HEAD(&del_list);
5971 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005972
Josef Bacik23b5ec72017-07-24 15:14:25 -04005973again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005974 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005975 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005976 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005977
Chris Mason39279cc2007-06-12 06:35:45 -04005978 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5979 if (ret < 0)
5980 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005981
5982 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983 struct dir_entry *entry;
5984
Chris Mason5f39d392007-10-15 16:14:19 -04005985 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005986 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005987 if (slot >= btrfs_header_nritems(leaf)) {
5988 ret = btrfs_next_leaf(root, path);
5989 if (ret < 0)
5990 goto err;
5991 else if (ret > 0)
5992 break;
5993 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005994 }
Chris Mason3de45862008-11-17 21:02:50 -05005995
Chris Mason5f39d392007-10-15 16:14:19 -04005996 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5997
5998 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005999 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006000 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006001 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006002 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006003 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006004 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006005 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006006 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006007 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006008 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6009 PAGE_SIZE) {
6010 btrfs_release_path(path);
6011 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6012 if (ret)
6013 goto nopos;
6014 addr = private->filldir_buf;
6015 entries = 0;
6016 total_len = 0;
6017 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006018 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006019
6020 entry = addr;
6021 entry->name_len = name_len;
6022 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006023 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6024 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006025 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006026 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006027 entry->ino = location.objectid;
6028 entry->offset = found_key.offset;
6029 entries++;
6030 addr += sizeof(struct dir_entry) + name_len;
6031 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006032next:
6033 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006034 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006035 btrfs_release_path(path);
6036
6037 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6038 if (ret)
6039 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006040
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006041 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006042 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006043 goto nopos;
6044
Zach Browndb62efb2013-07-11 16:19:42 -07006045 /*
6046 * Stop new entries from being returned after we return the last
6047 * entry.
6048 *
6049 * New directory entries are assigned a strictly increasing
6050 * offset. This means that new entries created during readdir
6051 * are *guaranteed* to be seen in the future by that readdir.
6052 * This has broken buggy programs which operate on names as
6053 * they're returned by readdir. Until we re-use freed offsets
6054 * we have this hack to stop new entries from being returned
6055 * under the assumption that they'll never reach this huge
6056 * offset.
6057 *
6058 * This is being careful not to overflow 32bit loff_t unless the
6059 * last entry requires it because doing so has broken 32bit apps
6060 * in the past.
6061 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006062 if (ctx->pos >= INT_MAX)
6063 ctx->pos = LLONG_MAX;
6064 else
6065 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006066nopos:
6067 ret = 0;
6068err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006069 if (put)
6070 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006071 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006072 return ret;
6073}
6074
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006075int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006076{
6077 struct btrfs_root *root = BTRFS_I(inode)->root;
6078 struct btrfs_trans_handle *trans;
6079 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006080 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006081
Josef Bacik72ac3c02012-05-23 14:13:11 -04006082 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006083 return 0;
6084
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006085 if (btrfs_fs_closing(root->fs_info) &&
6086 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006087 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006088
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006089 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006090 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006091 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006092 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006093 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006094 if (IS_ERR(trans))
6095 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006096 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006097 }
6098 return ret;
6099}
6100
6101/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006102 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006103 * inode changes. But, it is most likely to find the inode in cache.
6104 * FIXME, needs more benchmarking...there are no reasons other than performance
6105 * to keep or drop this code.
6106 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006107static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006108{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006109 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006110 struct btrfs_root *root = BTRFS_I(inode)->root;
6111 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006112 int ret;
6113
Josef Bacik72ac3c02012-05-23 14:13:11 -04006114 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006115 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006116
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006117 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006118 if (IS_ERR(trans))
6119 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006120
6121 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006122 if (ret && ret == -ENOSPC) {
6123 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006124 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006125 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006126 if (IS_ERR(trans))
6127 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006128
Chris Mason94b60442010-05-26 11:02:00 -04006129 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006130 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006131 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006132 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006133 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006134
6135 return ret;
6136}
6137
6138/*
6139 * This is a copy of file_update_time. We need this so we can return error on
6140 * ENOSPC for updating the inode in the case of file write and mmap writes.
6141 */
Josef Bacike41f9412012-03-26 09:46:47 -04006142static int btrfs_update_time(struct inode *inode, struct timespec *now,
6143 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006144{
Alexander Block2bc5565282012-06-15 09:49:33 +02006145 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006146 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006147
6148 if (btrfs_root_readonly(root))
6149 return -EROFS;
6150
Josef Bacike41f9412012-03-26 09:46:47 -04006151 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006152 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006153 if (flags & S_CTIME)
6154 inode->i_ctime = *now;
6155 if (flags & S_MTIME)
6156 inode->i_mtime = *now;
6157 if (flags & S_ATIME)
6158 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006159 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006160}
6161
Chris Masond352ac62008-09-29 15:18:18 -04006162/*
6163 * find the highest existing sequence number in a directory
6164 * and then set the in-memory index_cnt variable to reflect
6165 * free sequence numbers
6166 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006167static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006168{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006169 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006170 struct btrfs_key key, found_key;
6171 struct btrfs_path *path;
6172 struct extent_buffer *leaf;
6173 int ret;
6174
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006175 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006176 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006177 key.offset = (u64)-1;
6178
6179 path = btrfs_alloc_path();
6180 if (!path)
6181 return -ENOMEM;
6182
6183 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6184 if (ret < 0)
6185 goto out;
6186 /* FIXME: we should be able to handle this */
6187 if (ret == 0)
6188 goto out;
6189 ret = 0;
6190
6191 /*
6192 * MAGIC NUMBER EXPLANATION:
6193 * since we search a directory based on f_pos we have to start at 2
6194 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6195 * else has to start at 2
6196 */
6197 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006198 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006199 goto out;
6200 }
6201
6202 path->slots[0]--;
6203
6204 leaf = path->nodes[0];
6205 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6206
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006207 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006208 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006209 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006210 goto out;
6211 }
6212
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006213 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006214out:
6215 btrfs_free_path(path);
6216 return ret;
6217}
6218
Chris Masond352ac62008-09-29 15:18:18 -04006219/*
6220 * helper to find a free sequence number in a given directory. This current
6221 * code is very simple, later versions will do smarter things in the btree
6222 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006223int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006224{
6225 int ret = 0;
6226
Nikolay Borisov877574e2017-02-20 13:50:33 +02006227 if (dir->index_cnt == (u64)-1) {
6228 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006229 if (ret) {
6230 ret = btrfs_set_inode_index_count(dir);
6231 if (ret)
6232 return ret;
6233 }
Josef Bacikaec74772008-07-24 12:12:38 -04006234 }
6235
Nikolay Borisov877574e2017-02-20 13:50:33 +02006236 *index = dir->index_cnt;
6237 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006238
6239 return ret;
6240}
6241
Chris Masonb0d5d102014-09-08 13:08:51 -07006242static int btrfs_insert_inode_locked(struct inode *inode)
6243{
6244 struct btrfs_iget_args args;
6245 args.location = &BTRFS_I(inode)->location;
6246 args.root = BTRFS_I(inode)->root;
6247
6248 return insert_inode_locked4(inode,
6249 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6250 btrfs_find_actor, &args);
6251}
6252
Anand Jain19aee8d2017-07-18 17:37:05 +08006253/*
6254 * Inherit flags from the parent inode.
6255 *
6256 * Currently only the compression flags and the cow flags are inherited.
6257 */
6258static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6259{
6260 unsigned int flags;
6261
6262 if (!dir)
6263 return;
6264
6265 flags = BTRFS_I(dir)->flags;
6266
6267 if (flags & BTRFS_INODE_NOCOMPRESS) {
6268 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6269 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6270 } else if (flags & BTRFS_INODE_COMPRESS) {
6271 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6273 }
6274
6275 if (flags & BTRFS_INODE_NODATACOW) {
6276 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6277 if (S_ISREG(inode->i_mode))
6278 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6279 }
6280
6281 btrfs_update_iflags(inode);
6282}
6283
Chris Mason39279cc2007-06-12 06:35:45 -04006284static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6285 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006286 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006287 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006288 u64 ref_objectid, u64 objectid,
6289 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006290{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006292 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006293 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006294 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006295 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006296 struct btrfs_inode_ref *ref;
6297 struct btrfs_key key[2];
6298 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006300 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006301 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006302
Chris Mason5f39d392007-10-15 16:14:19 -04006303 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006304 if (!path)
6305 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006307 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006308 if (!inode) {
6309 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006310 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006311 }
Chris Mason39279cc2007-06-12 06:35:45 -04006312
Li Zefan581bb052011-04-20 10:06:11 +08006313 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006314 * O_TMPFILE, set link count to 0, so that after this point,
6315 * we fill in an inode item with the correct link count.
6316 */
6317 if (!name)
6318 set_nlink(inode, 0);
6319
6320 /*
Li Zefan581bb052011-04-20 10:06:11 +08006321 * we have to initialize this early, so we can reclaim the inode
6322 * number if we fail afterwards in this function.
6323 */
6324 inode->i_ino = objectid;
6325
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006326 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006327 trace_btrfs_inode_request(dir);
6328
Nikolay Borisov877574e2017-02-20 13:50:33 +02006329 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006330 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006331 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006332 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006333 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006334 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006335 } else if (dir) {
6336 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006337 }
6338 /*
6339 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006340 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006341 * number
6342 */
6343 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006344 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006345 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006346 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006347 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006348
Josef Bacik5dc562c2012-08-17 13:14:17 -04006349 /*
6350 * We could have gotten an inode number from somebody who was fsynced
6351 * and then removed in this same transaction, so let's just set full
6352 * sync since it will be a full sync anyway and this will blow away the
6353 * old info in the log.
6354 */
6355 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6356
Chris Mason9c583092008-01-29 15:15:18 -05006357 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006358 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006359 key[0].offset = 0;
6360
Chris Mason9c583092008-01-29 15:15:18 -05006361 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006362
6363 if (name) {
6364 /*
6365 * Start new inodes with an inode_ref. This is slightly more
6366 * efficient for small numbers of hard links since they will
6367 * be packed into one item. Extended refs will kick in if we
6368 * add more hard links than can fit in the ref item.
6369 */
6370 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006371 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006372 key[1].offset = ref_objectid;
6373
6374 sizes[1] = name_len + sizeof(*ref);
6375 }
Chris Mason9c583092008-01-29 15:15:18 -05006376
Chris Masonb0d5d102014-09-08 13:08:51 -07006377 location = &BTRFS_I(inode)->location;
6378 location->objectid = objectid;
6379 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006380 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006381
6382 ret = btrfs_insert_inode_locked(inode);
6383 if (ret < 0)
6384 goto fail;
6385
Chris Masonb9473432009-03-13 11:00:37 -04006386 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006387 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006388 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006389 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006390
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006391 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006392 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306393
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006394 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306395 inode->i_atime = inode->i_mtime;
6396 inode->i_ctime = inode->i_mtime;
6397 BTRFS_I(inode)->i_otime = inode->i_mtime;
6398
Chris Mason5f39d392007-10-15 16:14:19 -04006399 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6400 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006401 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006402 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006403 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006404
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006405 if (name) {
6406 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6407 struct btrfs_inode_ref);
6408 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6409 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6410 ptr = (unsigned long)(ref + 1);
6411 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6412 }
Chris Mason9c583092008-01-29 15:15:18 -05006413
Chris Mason5f39d392007-10-15 16:14:19 -04006414 btrfs_mark_buffer_dirty(path->nodes[0]);
6415 btrfs_free_path(path);
6416
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006417 btrfs_inherit_iflags(inode, dir);
6418
Al Viro569254b2011-07-24 17:08:40 -04006419 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006420 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006421 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006422 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006423 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6424 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006425 }
6426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006427 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006428
6429 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006430 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006431
Alexander Block8ea05e32012-07-25 17:35:53 +02006432 btrfs_update_root_times(trans, root);
6433
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006434 ret = btrfs_inode_inherit_props(trans, inode, dir);
6435 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006436 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006437 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006438 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006439
Chris Mason39279cc2007-06-12 06:35:45 -04006440 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006441
6442fail_unlock:
6443 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006444fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006445 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006446 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006447 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006448 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006449 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006450}
6451
6452static inline u8 btrfs_inode_type(struct inode *inode)
6453{
6454 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6455}
6456
Chris Masond352ac62008-09-29 15:18:18 -04006457/*
6458 * utility function to add 'inode' into 'parent_inode' with
6459 * a give name and a given sequence number.
6460 * if 'add_backref' is true, also insert a backref from the
6461 * inode to the parent directory.
6462 */
Chris Masone02119d2008-09-05 16:13:11 -04006463int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006464 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006465 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006466{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006467 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006468 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006469 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006470 struct btrfs_root *root = parent_inode->root;
6471 u64 ino = btrfs_ino(inode);
6472 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006473
Li Zefan33345d012011-04-20 10:31:50 +08006474 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006475 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006476 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006477 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006478 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006479 key.offset = 0;
6480 }
Chris Mason39279cc2007-06-12 06:35:45 -04006481
Li Zefan33345d012011-04-20 10:31:50 +08006482 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006483 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6484 root->root_key.objectid, parent_ino,
6485 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006486 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006487 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6488 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006489 }
6490
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006491 /* Nothing to clean up yet */
6492 if (ret)
6493 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006494
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006495 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6496 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006497 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006498 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006499 goto fail_dir_item;
6500 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006501 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006502 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006503 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006504
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006505 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006506 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006507 inode_inc_iversion(&parent_inode->vfs_inode);
6508 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6509 current_time(&parent_inode->vfs_inode);
6510 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006511 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006512 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006513 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006514
6515fail_dir_item:
6516 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6517 u64 local_index;
6518 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006519 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6520 root->root_key.objectid, parent_ino,
6521 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006522
6523 } else if (add_backref) {
6524 u64 local_index;
6525 int err;
6526
6527 err = btrfs_del_inode_ref(trans, root, name, name_len,
6528 ino, parent_ino, &local_index);
6529 }
6530 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006531}
6532
6533static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006534 struct btrfs_inode *dir, struct dentry *dentry,
6535 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006536{
Josef Bacika1b075d2010-11-19 20:36:11 +00006537 int err = btrfs_add_link(trans, dir, inode,
6538 dentry->d_name.name, dentry->d_name.len,
6539 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006540 if (err > 0)
6541 err = -EEXIST;
6542 return err;
6543}
6544
Josef Bacik618e21d2007-07-11 10:18:17 -04006545static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006546 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006547{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006548 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006549 struct btrfs_trans_handle *trans;
6550 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006551 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006552 int err;
6553 int drop_inode = 0;
6554 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006555 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006556
Josef Bacik9ed74f22009-09-11 16:12:44 -04006557 /*
6558 * 2 for inode item and ref
6559 * 2 for dir items
6560 * 1 for xattr if selinux is on
6561 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006562 trans = btrfs_start_transaction(root, 5);
6563 if (IS_ERR(trans))
6564 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006565
Li Zefan581bb052011-04-20 10:06:11 +08006566 err = btrfs_find_free_ino(root, &objectid);
6567 if (err)
6568 goto out_unlock;
6569
Josef Bacikaec74772008-07-24 12:12:38 -04006570 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006571 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6572 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006573 if (IS_ERR(inode)) {
6574 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006575 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006576 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006577
Casey Schauflerad19db72011-12-15 10:09:07 -05006578 /*
6579 * If the active LSM wants to access the inode during
6580 * d_instantiate it needs these. Smack checks to see
6581 * if the filesystem supports xattrs by looking at the
6582 * ops vector.
6583 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006584 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006585 init_special_inode(inode, inode->i_mode, rdev);
6586
6587 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006588 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006589 goto out_unlock_inode;
6590
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006591 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6592 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006593 if (err) {
6594 goto out_unlock_inode;
6595 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006596 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006597 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006598 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006600
Josef Bacik618e21d2007-07-11 10:18:17 -04006601out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006602 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006603 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006604 if (drop_inode) {
6605 inode_dec_link_count(inode);
6606 iput(inode);
6607 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006608 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006609
6610out_unlock_inode:
6611 drop_inode = 1;
6612 unlock_new_inode(inode);
6613 goto out_unlock;
6614
Josef Bacik618e21d2007-07-11 10:18:17 -04006615}
6616
Chris Mason39279cc2007-06-12 06:35:45 -04006617static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006618 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006619{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006620 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006621 struct btrfs_trans_handle *trans;
6622 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006623 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006624 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006625 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006626 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006627 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006628
Josef Bacik9ed74f22009-09-11 16:12:44 -04006629 /*
6630 * 2 for inode item and ref
6631 * 2 for dir items
6632 * 1 for xattr if selinux is on
6633 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006634 trans = btrfs_start_transaction(root, 5);
6635 if (IS_ERR(trans))
6636 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006637
Li Zefan581bb052011-04-20 10:06:11 +08006638 err = btrfs_find_free_ino(root, &objectid);
6639 if (err)
6640 goto out_unlock;
6641
Josef Bacikaec74772008-07-24 12:12:38 -04006642 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006643 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6644 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006645 if (IS_ERR(inode)) {
6646 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006647 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006648 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006649 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006650 /*
6651 * If the active LSM wants to access the inode during
6652 * d_instantiate it needs these. Smack checks to see
6653 * if the filesystem supports xattrs by looking at the
6654 * ops vector.
6655 */
6656 inode->i_fop = &btrfs_file_operations;
6657 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006658 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006659
6660 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6661 if (err)
6662 goto out_unlock_inode;
6663
6664 err = btrfs_update_inode(trans, root, inode);
6665 if (err)
6666 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006667
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006668 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6669 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006670 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006671 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006672
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006673 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006674 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006675 d_instantiate(dentry, inode);
6676
Chris Mason39279cc2007-06-12 06:35:45 -04006677out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006678 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006679 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006680 inode_dec_link_count(inode);
6681 iput(inode);
6682 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006683 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006685
6686out_unlock_inode:
6687 unlock_new_inode(inode);
6688 goto out_unlock;
6689
Chris Mason39279cc2007-06-12 06:35:45 -04006690}
6691
6692static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6693 struct dentry *dentry)
6694{
Filipe Manana271dba452016-01-05 16:24:05 +00006695 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006696 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006697 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006698 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006699 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006700 int err;
6701 int drop_inode = 0;
6702
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006703 /* do not allow sys_link's with other subvols of the same device */
6704 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006705 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006706
Mark Fashehf1863732012-08-08 11:32:27 -07006707 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006708 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006709
Nikolay Borisov877574e2017-02-20 13:50:33 +02006710 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006711 if (err)
6712 goto fail;
6713
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006715 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006717 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006718 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006719 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006720 if (IS_ERR(trans)) {
6721 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006722 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006723 goto fail;
6724 }
Chris Mason5f39d392007-10-15 16:14:19 -04006725
Miao Xie67de1172013-12-26 13:07:06 +08006726 /* There are several dir indexes for this inode, clear the cache. */
6727 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006728 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006729 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006730 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006731 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006732 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006733
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006734 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6735 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006736
Yan, Zhenga5719522009-09-24 09:17:31 -04006737 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006738 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006739 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006740 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006741 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006742 if (err)
6743 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006744 if (inode->i_nlink == 1) {
6745 /*
6746 * If new hard link count is 1, it's a file created
6747 * with open(2) O_TMPFILE flag.
6748 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006749 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006750 if (err)
6751 goto fail;
6752 }
Al Viro08c422c2011-12-23 07:58:13 -05006753 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006754 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006755 }
Chris Mason39279cc2007-06-12 06:35:45 -04006756
Chris Mason1832a6d2007-12-21 16:27:21 -05006757fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006758 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006759 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006760 if (drop_inode) {
6761 inode_dec_link_count(inode);
6762 iput(inode);
6763 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006764 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006765 return err;
6766}
6767
Al Viro18bb1db2011-07-26 01:41:39 -04006768static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006769{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006770 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006771 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006772 struct btrfs_trans_handle *trans;
6773 struct btrfs_root *root = BTRFS_I(dir)->root;
6774 int err = 0;
6775 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006776 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006777 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006778
Josef Bacik9ed74f22009-09-11 16:12:44 -04006779 /*
6780 * 2 items for inode and ref
6781 * 2 items for dir items
6782 * 1 for xattr if selinux is on
6783 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006784 trans = btrfs_start_transaction(root, 5);
6785 if (IS_ERR(trans))
6786 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006787
Li Zefan581bb052011-04-20 10:06:11 +08006788 err = btrfs_find_free_ino(root, &objectid);
6789 if (err)
6790 goto out_fail;
6791
Josef Bacikaec74772008-07-24 12:12:38 -04006792 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006793 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6794 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006795 if (IS_ERR(inode)) {
6796 err = PTR_ERR(inode);
6797 goto out_fail;
6798 }
Chris Mason5f39d392007-10-15 16:14:19 -04006799
Chris Mason39279cc2007-06-12 06:35:45 -04006800 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006801 /* these must be set before we unlock the inode */
6802 inode->i_op = &btrfs_dir_inode_operations;
6803 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006804
Eric Paris2a7dba32011-02-01 11:05:39 -05006805 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006806 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006807 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006808
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006809 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006810 err = btrfs_update_inode(trans, root, inode);
6811 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006812 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006813
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006814 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6815 dentry->d_name.name,
6816 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006817 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006818 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006819
Chris Mason39279cc2007-06-12 06:35:45 -04006820 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006821 /*
6822 * mkdir is special. We're unlocking after we call d_instantiate
6823 * to avoid a race with nfsd calling d_instantiate.
6824 */
6825 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006827
6828out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006829 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006830 if (drop_on_err) {
6831 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006832 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006833 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006834 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006835 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006836
6837out_fail_inode:
6838 unlock_new_inode(inode);
6839 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006840}
6841
Chris Masonc8b97812008-10-29 14:49:59 -04006842static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006843 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006844 size_t pg_offset, u64 extent_offset,
6845 struct btrfs_file_extent_item *item)
6846{
6847 int ret;
6848 struct extent_buffer *leaf = path->nodes[0];
6849 char *tmp;
6850 size_t max_size;
6851 unsigned long inline_size;
6852 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006853 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006854
6855 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006856 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006857 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6858 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006859 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006860 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006861 if (!tmp)
6862 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006863 ptr = btrfs_file_extent_inline_start(item);
6864
6865 read_extent_buffer(leaf, tmp, ptr, inline_size);
6866
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006867 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006868 ret = btrfs_decompress(compress_type, tmp, page,
6869 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006870
6871 /*
6872 * decompression code contains a memset to fill in any space between the end
6873 * of the uncompressed data and the end of max_size in case the decompressed
6874 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6875 * the end of an inline extent and the beginning of the next block, so we
6876 * cover that region here.
6877 */
6878
6879 if (max_size + pg_offset < PAGE_SIZE) {
6880 char *map = kmap(page);
6881 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6882 kunmap(page);
6883 }
Chris Masonc8b97812008-10-29 14:49:59 -04006884 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006885 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006886}
6887
Chris Masond352ac62008-09-29 15:18:18 -04006888/*
6889 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006890 * the ugly parts come from merging extents from the disk with the in-ram
6891 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006892 * where the in-ram extents might be locked pending data=ordered completion.
6893 *
6894 * This also copies inline extents directly into the page.
6895 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006896struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6897 struct page *page,
6898 size_t pg_offset, u64 start, u64 len,
6899 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006900{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006902 int ret;
6903 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006904 u64 extent_start = 0;
6905 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006906 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006907 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006908 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006909 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006910 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006911 struct extent_buffer *leaf;
6912 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006913 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006914 struct extent_map_tree *em_tree = &inode->extent_tree;
6915 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006916 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006917
Chris Mason890871b2009-09-02 16:24:52 -04006918 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006919 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006920 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006921 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006922 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006923
Chris Masona52d9a82007-08-27 16:49:44 -04006924 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006925 if (em->start > start || em->start + em->len <= start)
6926 free_extent_map(em);
6927 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006928 free_extent_map(em);
6929 else
6930 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006931 }
David Sterba172ddd62011-04-21 00:48:27 +02006932 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006933 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006934 err = -ENOMEM;
6935 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006936 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006937 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006938 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006939 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006940 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006941 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006942
6943 if (!path) {
6944 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006945 if (!path) {
6946 err = -ENOMEM;
6947 goto out;
6948 }
6949 /*
6950 * Chances are we'll be called again, so go ahead and do
6951 * readahead
6952 */
David Sterbae4058b52015-11-27 16:31:35 +01006953 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006954 }
6955
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006956 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006957 if (ret < 0) {
6958 err = ret;
6959 goto out;
6960 }
6961
6962 if (ret != 0) {
6963 if (path->slots[0] == 0)
6964 goto not_found;
6965 path->slots[0]--;
6966 }
6967
Chris Mason5f39d392007-10-15 16:14:19 -04006968 leaf = path->nodes[0];
6969 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006970 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006971 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006972 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006973 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006974 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006975 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006976 /*
6977 * If we backup past the first extent we want to move forward
6978 * and see if there is an extent in front of us, otherwise we'll
6979 * say there is a hole for our whole search range which can
6980 * cause problems.
6981 */
6982 extent_end = start;
6983 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006984 }
6985
Chris Mason5f39d392007-10-15 16:14:19 -04006986 found_type = btrfs_file_extent_type(leaf, item);
6987 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006988 if (found_type == BTRFS_FILE_EXTENT_REG ||
6989 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006990 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006991 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006992
6993 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6994 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006995 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6996 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006997 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006998 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006999 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007000
7001 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7002 path->slots[0],
7003 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007004 }
Josef Bacik25a50342013-10-14 12:08:38 -04007005next:
Yan Zheng9036c102008-10-30 14:19:41 -04007006 if (start >= extent_end) {
7007 path->slots[0]++;
7008 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7009 ret = btrfs_next_leaf(root, path);
7010 if (ret < 0) {
7011 err = ret;
7012 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007013 }
Yan Zheng9036c102008-10-30 14:19:41 -04007014 if (ret > 0)
7015 goto not_found;
7016 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007017 }
Yan Zheng9036c102008-10-30 14:19:41 -04007018 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7019 if (found_key.objectid != objectid ||
7020 found_key.type != BTRFS_EXTENT_DATA_KEY)
7021 goto not_found;
7022 if (start + len <= found_key.offset)
7023 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007024 if (start > found_key.offset)
7025 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007026 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007027 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007028 em->len = found_key.offset - start;
7029 goto not_found_em;
7030 }
7031
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007032 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007033 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007034
Yan Zhengd899e052008-10-30 14:25:28 -04007035 if (found_type == BTRFS_FILE_EXTENT_REG ||
7036 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007037 goto insert;
7038 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007039 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007040 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007041 size_t size;
7042 size_t extent_offset;
7043 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007044
Filipe Manana7ffbb592014-06-09 03:48:05 +01007045 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007046 goto out;
Yan689f9342007-10-29 11:41:07 -04007047
Chris Mason514ac8a2014-01-03 21:07:00 -08007048 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007049 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007050 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7051 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007052 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007053 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007054 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007055 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007056 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007057 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007058 if (btrfs_file_extent_compression(leaf, item) !=
7059 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007060 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007061 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007062 if (ret) {
7063 err = ret;
7064 goto out;
7065 }
Chris Masonc8b97812008-10-29 14:49:59 -04007066 } else {
7067 map = kmap(page);
7068 read_extent_buffer(leaf, map + pg_offset, ptr,
7069 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007070 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007071 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007072 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007073 copy_size);
7074 }
Chris Masonc8b97812008-10-29 14:49:59 -04007075 kunmap(page);
7076 }
Chris Mason179e29e2007-11-01 11:28:41 -04007077 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007078 }
Chris Masond1310b22008-01-24 16:13:08 -05007079 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007080 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007081 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007082 }
7083not_found:
7084 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007085 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007086 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007087not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007088 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007089insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007090 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007091 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007092 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007093 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7094 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007095 err = -EIO;
7096 goto out;
7097 }
Chris Masond1310b22008-01-24 16:13:08 -05007098
7099 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007100 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007101 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007102 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007103out:
liubo1abe9b82011-03-24 11:18:59 +00007104
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007105 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007106
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007107 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007108 if (err) {
7109 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007110 return ERR_PTR(err);
7111 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007112 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007113 return em;
7114}
7115
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007116struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7117 struct page *page,
7118 size_t pg_offset, u64 start, u64 len,
7119 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007120{
7121 struct extent_map *em;
7122 struct extent_map *hole_em = NULL;
7123 u64 range_start = start;
7124 u64 end;
7125 u64 found;
7126 u64 found_end;
7127 int err = 0;
7128
7129 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7130 if (IS_ERR(em))
7131 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007132 /*
7133 * If our em maps to:
7134 * - a hole or
7135 * - a pre-alloc extent,
7136 * there might actually be delalloc bytes behind it.
7137 */
7138 if (em->block_start != EXTENT_MAP_HOLE &&
7139 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7140 return em;
7141 else
7142 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007143
7144 /* check to see if we've wrapped (len == -1 or similar) */
7145 end = start + len;
7146 if (end < start)
7147 end = (u64)-1;
7148 else
7149 end -= 1;
7150
7151 em = NULL;
7152
7153 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007154 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007155 end, len, EXTENT_DELALLOC, 1);
7156 found_end = range_start + found;
7157 if (found_end < range_start)
7158 found_end = (u64)-1;
7159
7160 /*
7161 * we didn't find anything useful, return
7162 * the original results from get_extent()
7163 */
7164 if (range_start > end || found_end <= start) {
7165 em = hole_em;
7166 hole_em = NULL;
7167 goto out;
7168 }
7169
7170 /* adjust the range_start to make sure it doesn't
7171 * go backwards from the start they passed in
7172 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307173 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007174 found = found_end - range_start;
7175
7176 if (found > 0) {
7177 u64 hole_start = start;
7178 u64 hole_len = len;
7179
David Sterba172ddd62011-04-21 00:48:27 +02007180 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007181 if (!em) {
7182 err = -ENOMEM;
7183 goto out;
7184 }
7185 /*
7186 * when btrfs_get_extent can't find anything it
7187 * returns one huge hole
7188 *
7189 * make sure what it found really fits our range, and
7190 * adjust to make sure it is based on the start from
7191 * the caller
7192 */
7193 if (hole_em) {
7194 u64 calc_end = extent_map_end(hole_em);
7195
7196 if (calc_end <= start || (hole_em->start > end)) {
7197 free_extent_map(hole_em);
7198 hole_em = NULL;
7199 } else {
7200 hole_start = max(hole_em->start, start);
7201 hole_len = calc_end - hole_start;
7202 }
7203 }
7204 em->bdev = NULL;
7205 if (hole_em && range_start > hole_start) {
7206 /* our hole starts before our delalloc, so we
7207 * have to return just the parts of the hole
7208 * that go until the delalloc starts
7209 */
7210 em->len = min(hole_len,
7211 range_start - hole_start);
7212 em->start = hole_start;
7213 em->orig_start = hole_start;
7214 /*
7215 * don't adjust block start at all,
7216 * it is fixed at EXTENT_MAP_HOLE
7217 */
7218 em->block_start = hole_em->block_start;
7219 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007220 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7221 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007222 } else {
7223 em->start = range_start;
7224 em->len = found;
7225 em->orig_start = range_start;
7226 em->block_start = EXTENT_MAP_DELALLOC;
7227 em->block_len = found;
7228 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007229 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007230 return hole_em;
7231 }
7232out:
7233
7234 free_extent_map(hole_em);
7235 if (err) {
7236 free_extent_map(em);
7237 return ERR_PTR(err);
7238 }
7239 return em;
7240}
7241
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007242static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7243 const u64 start,
7244 const u64 len,
7245 const u64 orig_start,
7246 const u64 block_start,
7247 const u64 block_len,
7248 const u64 orig_block_len,
7249 const u64 ram_bytes,
7250 const int type)
7251{
7252 struct extent_map *em = NULL;
7253 int ret;
7254
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007255 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007256 em = create_io_em(inode, start, len, orig_start,
7257 block_start, block_len, orig_block_len,
7258 ram_bytes,
7259 BTRFS_COMPRESS_NONE, /* compress_type */
7260 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007261 if (IS_ERR(em))
7262 goto out;
7263 }
7264 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7265 len, block_len, type);
7266 if (ret) {
7267 if (em) {
7268 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007269 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007270 start + len - 1, 0);
7271 }
7272 em = ERR_PTR(ret);
7273 }
7274 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007275
7276 return em;
7277}
7278
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7280 u64 start, u64 len)
7281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007282 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007284 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285 struct btrfs_key ins;
7286 u64 alloc_hint;
7287 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007288
Josef Bacik4b46fce2010-05-23 11:00:55 -04007289 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007290 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007291 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007292 if (ret)
7293 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7296 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007297 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007298 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007299 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007300 btrfs_free_reserved_extent(fs_info, ins.objectid,
7301 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007302
Josef Bacik4b46fce2010-05-23 11:00:55 -04007303 return em;
7304}
7305
Chris Mason46bfbb52010-05-26 11:04:10 -04007306/*
7307 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7308 * block must be cow'd
7309 */
Josef Bacik00361582013-08-14 14:02:47 -04007310noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007311 u64 *orig_start, u64 *orig_block_len,
7312 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007313{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007314 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 struct btrfs_path *path;
7316 int ret;
7317 struct extent_buffer *leaf;
7318 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007319 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007320 struct btrfs_file_extent_item *fi;
7321 struct btrfs_key key;
7322 u64 disk_bytenr;
7323 u64 backref_offset;
7324 u64 extent_end;
7325 u64 num_bytes;
7326 int slot;
7327 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007328 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007329
Chris Mason46bfbb52010-05-26 11:04:10 -04007330 path = btrfs_alloc_path();
7331 if (!path)
7332 return -ENOMEM;
7333
David Sterbaf85b7372017-01-20 14:54:07 +01007334 ret = btrfs_lookup_file_extent(NULL, root, path,
7335 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007336 if (ret < 0)
7337 goto out;
7338
7339 slot = path->slots[0];
7340 if (ret == 1) {
7341 if (slot == 0) {
7342 /* can't find the item, must cow */
7343 ret = 0;
7344 goto out;
7345 }
7346 slot--;
7347 }
7348 ret = 0;
7349 leaf = path->nodes[0];
7350 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007351 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007352 key.type != BTRFS_EXTENT_DATA_KEY) {
7353 /* not our file or wrong item type, must cow */
7354 goto out;
7355 }
7356
7357 if (key.offset > offset) {
7358 /* Wrong offset, must cow */
7359 goto out;
7360 }
7361
7362 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7363 found_type = btrfs_file_extent_type(leaf, fi);
7364 if (found_type != BTRFS_FILE_EXTENT_REG &&
7365 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7366 /* not a regular extent, must cow */
7367 goto out;
7368 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007369
7370 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7371 goto out;
7372
Miao Xiee77751a2013-12-27 21:11:50 +08007373 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7374 if (extent_end <= offset)
7375 goto out;
7376
Chris Mason46bfbb52010-05-26 11:04:10 -04007377 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007378 if (disk_bytenr == 0)
7379 goto out;
7380
7381 if (btrfs_file_extent_compression(leaf, fi) ||
7382 btrfs_file_extent_encryption(leaf, fi) ||
7383 btrfs_file_extent_other_encoding(leaf, fi))
7384 goto out;
7385
Chris Mason46bfbb52010-05-26 11:04:10 -04007386 backref_offset = btrfs_file_extent_offset(leaf, fi);
7387
Josef Bacik7ee9e442013-06-21 16:37:03 -04007388 if (orig_start) {
7389 *orig_start = key.offset - backref_offset;
7390 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7391 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7392 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007393
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007394 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007395 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007396
7397 num_bytes = min(offset + *len, extent_end) - offset;
7398 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7399 u64 range_end;
7400
Jeff Mahoneyda170662016-06-15 09:22:56 -04007401 range_end = round_up(offset + num_bytes,
7402 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007403 ret = test_range_bit(io_tree, offset, range_end,
7404 EXTENT_DELALLOC, 0, NULL);
7405 if (ret) {
7406 ret = -EAGAIN;
7407 goto out;
7408 }
7409 }
7410
Josef Bacik1bda19e2013-10-18 12:10:36 -04007411 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007412
7413 /*
7414 * look for other files referencing this extent, if we
7415 * find any we must cow
7416 */
Josef Bacik00361582013-08-14 14:02:47 -04007417
Liu Boe4c3b2d2017-01-30 12:25:28 -08007418 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007419 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007420 if (ret) {
7421 ret = 0;
7422 goto out;
7423 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007424
7425 /*
7426 * adjust disk_bytenr and num_bytes to cover just the bytes
7427 * in this extent we are about to write. If there
7428 * are any csums in that range we have to cow in order
7429 * to keep the csums correct
7430 */
7431 disk_bytenr += backref_offset;
7432 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007433 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7434 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007435 /*
7436 * all of the above have passed, it is safe to overwrite this extent
7437 * without cow
7438 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007439 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007440 ret = 1;
7441out:
7442 btrfs_free_path(path);
7443 return ret;
7444}
7445
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007446bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7447{
7448 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007449 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007450 void **pagep = NULL;
7451 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007452 unsigned long start_idx;
7453 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007454
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007455 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007456
7457 /*
7458 * end is the last byte in the last page. end == start is legal
7459 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007460 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007461
7462 rcu_read_lock();
7463
7464 /* Most of the code in this while loop is lifted from
7465 * find_get_page. It's been modified to begin searching from a
7466 * page and return just the first page found in that range. If the
7467 * found idx is less than or equal to the end idx then we know that
7468 * a page exists. If no pages are found or if those pages are
7469 * outside of the range then we're fine (yay!) */
7470 while (page == NULL &&
7471 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7472 page = radix_tree_deref_slot(pagep);
7473 if (unlikely(!page))
7474 break;
7475
7476 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007477 if (radix_tree_deref_retry(page)) {
7478 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007479 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007480 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007481 /*
7482 * Otherwise, shmem/tmpfs must be storing a swap entry
7483 * here as an exceptional entry: so return it without
7484 * attempting to raise page count.
7485 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007486 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007487 break; /* TODO: Is this relevant for this use case? */
7488 }
7489
Filipe Manana91405152014-06-05 13:22:24 +01007490 if (!page_cache_get_speculative(page)) {
7491 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007492 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007493 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007494
7495 /*
7496 * Has the page moved?
7497 * This is part of the lockless pagecache protocol. See
7498 * include/linux/pagemap.h for details.
7499 */
7500 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007501 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007502 page = NULL;
7503 }
7504 }
7505
7506 if (page) {
7507 if (page->index <= end_idx)
7508 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007509 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007510 }
7511
7512 rcu_read_unlock();
7513 return found;
7514}
7515
Josef Bacikeb838e72012-07-31 16:28:48 -04007516static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7517 struct extent_state **cached_state, int writing)
7518{
7519 struct btrfs_ordered_extent *ordered;
7520 int ret = 0;
7521
7522 while (1) {
7523 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007524 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007525 /*
7526 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007527 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007528 * extents in this range.
7529 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007530 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007531 lockend - lockstart + 1);
7532
7533 /*
7534 * We need to make sure there are no buffered pages in this
7535 * range either, we could have raced between the invalidate in
7536 * generic_file_direct_write and locking the extent. The
7537 * invalidate needs to happen so that reads after a write do not
7538 * get stale data.
7539 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007540 if (!ordered &&
7541 (!writing ||
7542 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007543 break;
7544
7545 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007546 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007547
7548 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007549 /*
7550 * If we are doing a DIO read and the ordered extent we
7551 * found is for a buffered write, we can not wait for it
7552 * to complete and retry, because if we do so we can
7553 * deadlock with concurrent buffered writes on page
7554 * locks. This happens only if our DIO read covers more
7555 * than one extent map, if at this point has already
7556 * created an ordered extent for a previous extent map
7557 * and locked its range in the inode's io tree, and a
7558 * concurrent write against that previous extent map's
7559 * range and this range started (we unlock the ranges
7560 * in the io tree only when the bios complete and
7561 * buffered writes always lock pages before attempting
7562 * to lock range in the io tree).
7563 */
7564 if (writing ||
7565 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7566 btrfs_start_ordered_extent(inode, ordered, 1);
7567 else
7568 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007569 btrfs_put_ordered_extent(ordered);
7570 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007571 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007572 * We could trigger writeback for this range (and wait
7573 * for it to complete) and then invalidate the pages for
7574 * this range (through invalidate_inode_pages2_range()),
7575 * but that can lead us to a deadlock with a concurrent
7576 * call to readpages() (a buffered read or a defrag call
7577 * triggered a readahead) on a page lock due to an
7578 * ordered dio extent we created before but did not have
7579 * yet a corresponding bio submitted (whence it can not
7580 * complete), which makes readpages() wait for that
7581 * ordered extent to complete while holding a lock on
7582 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007584 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 }
7586
Filipe Mananaade77022016-02-18 14:28:55 +00007587 if (ret)
7588 break;
7589
Josef Bacikeb838e72012-07-31 16:28:48 -04007590 cond_resched();
7591 }
7592
7593 return ret;
7594}
7595
Liu Bo6f9994d2017-01-31 07:50:22 -08007596/* The callers of this must take lock_extent() */
7597static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7598 u64 orig_start, u64 block_start,
7599 u64 block_len, u64 orig_block_len,
7600 u64 ram_bytes, int compress_type,
7601 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007602{
7603 struct extent_map_tree *em_tree;
7604 struct extent_map *em;
7605 struct btrfs_root *root = BTRFS_I(inode)->root;
7606 int ret;
7607
Liu Bo6f9994d2017-01-31 07:50:22 -08007608 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7609 type == BTRFS_ORDERED_COMPRESSED ||
7610 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007611 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007612
Josef Bacik69ffb542012-09-11 15:40:07 -04007613 em_tree = &BTRFS_I(inode)->extent_tree;
7614 em = alloc_extent_map();
7615 if (!em)
7616 return ERR_PTR(-ENOMEM);
7617
7618 em->start = start;
7619 em->orig_start = orig_start;
7620 em->len = len;
7621 em->block_len = block_len;
7622 em->block_start = block_start;
7623 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007624 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007625 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007626 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007627 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007628 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007629 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007630 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007631 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7632 em->compress_type = compress_type;
7633 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007634
7635 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007636 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007637 em->start + em->len - 1, 0);
7638 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007639 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007640 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007641 /*
7642 * The caller has taken lock_extent(), who could race with us
7643 * to add em?
7644 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007645 } while (ret == -EEXIST);
7646
7647 if (ret) {
7648 free_extent_map(em);
7649 return ERR_PTR(ret);
7650 }
7651
Liu Bo6f9994d2017-01-31 07:50:22 -08007652 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007653 return em;
7654}
7655
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7657 struct buffer_head *bh_result, int create)
7658{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007659 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007660 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007661 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307662 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007663 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007665 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007666 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007667 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007668
Miao Xie172a5042013-02-21 02:48:22 -07007669 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007670 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007671 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007672 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007673
Josef Bacikc3298612012-08-03 16:49:19 -04007674 lockstart = start;
7675 lockend = start + len - 1;
7676
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007677 if (current->journal_info) {
7678 /*
7679 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007680 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007681 * confused.
7682 */
chandan50745b02015-08-28 21:10:13 +05307683 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007684 current->journal_info = NULL;
7685 }
7686
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 /*
7688 * If this errors out it's because we couldn't invalidate pagecache for
7689 * this range and we need to fallback to buffered.
7690 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007691 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7692 create)) {
7693 ret = -ENOTBLK;
7694 goto err;
7695 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007696
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007697 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007698 if (IS_ERR(em)) {
7699 ret = PTR_ERR(em);
7700 goto unlock_err;
7701 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007702
7703 /*
7704 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7705 * io. INLINE is special, and we could probably kludge it in here, but
7706 * it's still buffered so for safety lets just fall back to the generic
7707 * buffered path.
7708 *
7709 * For COMPRESSED we _have_ to read the entire extent in so we can
7710 * decompress it, so there will be buffering required no matter what we
7711 * do, so go ahead and fallback to buffered.
7712 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007713 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007714 * to buffered IO. Don't blame me, this is the price we pay for using
7715 * the generic code.
7716 */
7717 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7718 em->block_start == EXTENT_MAP_INLINE) {
7719 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007720 ret = -ENOTBLK;
7721 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722 }
7723
7724 /* Just a good old fashioned hole, return */
7725 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7726 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7727 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007728 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729 }
7730
7731 /*
7732 * We don't allocate a new extent in the following cases
7733 *
7734 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7735 * existing extent.
7736 * 2) The extent is marked as PREALLOC. We're good to go here and can
7737 * just use the extent.
7738 *
7739 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007740 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007741 len = min(len, em->len - (start - em->start));
7742 lockstart = start + len;
7743 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007744 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745
7746 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7747 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7748 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007750 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751
7752 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7753 type = BTRFS_ORDERED_PREALLOC;
7754 else
7755 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007756 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007757 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007758
Josef Bacik00361582013-08-14 14:02:47 -04007759 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007760 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007761 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007762 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007763
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007764 em2 = btrfs_create_dio_extent(inode, start, len,
7765 orig_start, block_start,
7766 len, orig_block_len,
7767 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007768 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007769 if (type == BTRFS_ORDERED_PREALLOC) {
7770 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007771 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007772 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007773 if (em2 && IS_ERR(em2)) {
7774 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007775 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007776 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007777 /*
7778 * For inode marked NODATACOW or extent marked PREALLOC,
7779 * use the existing or preallocated extent, so does not
7780 * need to adjust btrfs_space_info's bytes_may_use.
7781 */
7782 btrfs_free_reserved_data_space_noquota(inode,
7783 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007784 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007785 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007786 }
Josef Bacik00361582013-08-14 14:02:47 -04007787
Chris Mason46bfbb52010-05-26 11:04:10 -04007788 /*
7789 * this will cow the extent, reset the len in case we changed
7790 * it above
7791 */
7792 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007793 free_extent_map(em);
7794 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007795 if (IS_ERR(em)) {
7796 ret = PTR_ERR(em);
7797 goto unlock_err;
7798 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007799 len = min(len, em->len - (start - em->start));
7800unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007801 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7802 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007803 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804 bh_result->b_bdev = em->bdev;
7805 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007806 if (create) {
7807 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7808 set_buffer_new(bh_result);
7809
7810 /*
7811 * Need to update the i_size under the extent lock so buffered
7812 * readers will get the updated i_size when we unlock.
7813 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007814 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007815 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007816
chandan50745b02015-08-28 21:10:13 +05307817 WARN_ON(dio_data->reserve < len);
7818 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007819 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307820 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007821 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007822
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 /*
7824 * In the case of write we need to clear and unlock the entire range,
7825 * in the case of read we need to unlock only the end area that we
7826 * aren't using if there is any left over space.
7827 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007828 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007829 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7830 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007831 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007832 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007833 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007834 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007835
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836 free_extent_map(em);
7837
7838 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007839
7840unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007841 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007842 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007843err:
chandan50745b02015-08-28 21:10:13 +05307844 if (dio_data)
7845 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007846 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847}
7848
Omar Sandoval58efbc92017-08-22 23:45:59 -07007849static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7850 struct bio *bio,
7851 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007852{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007853 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007854 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007855
Mike Christie37226b22016-06-05 14:31:52 -05007856 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007857
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007858 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007859 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007860 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007861
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007862 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007863
Miao Xie8b110e32014-09-12 18:44:03 +08007864 return ret;
7865}
7866
7867static int btrfs_check_dio_repairable(struct inode *inode,
7868 struct bio *failed_bio,
7869 struct io_failure_record *failrec,
7870 int failed_mirror)
7871{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007873 int num_copies;
7874
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007875 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007876 if (num_copies == 1) {
7877 /*
7878 * we only have a single copy of the data, so don't bother with
7879 * all the retry and error correction code that follows. no
7880 * matter what the error is, it is very likely to persist.
7881 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007882 btrfs_debug(fs_info,
7883 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7884 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007885 return 0;
7886 }
7887
7888 failrec->failed_mirror = failed_mirror;
7889 failrec->this_mirror++;
7890 if (failrec->this_mirror == failed_mirror)
7891 failrec->this_mirror++;
7892
7893 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007894 btrfs_debug(fs_info,
7895 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7896 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007897 return 0;
7898 }
7899
7900 return 1;
7901}
7902
Omar Sandoval58efbc92017-08-22 23:45:59 -07007903static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7904 struct page *page, unsigned int pgoff,
7905 u64 start, u64 end, int failed_mirror,
7906 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007907{
7908 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007909 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7910 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007911 struct bio *bio;
7912 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007913 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007914 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007915 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007916 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007917 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007918
Mike Christie37226b22016-06-05 14:31:52 -05007919 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007920
7921 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7922 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007923 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007924
7925 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7926 failed_mirror);
7927 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007928 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007929 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007930 }
7931
Liu Bo17347ce2017-05-15 15:33:27 -07007932 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007933 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007934 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007935 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007936 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007937
7938 isector = start - btrfs_io_bio(failed_bio)->logical;
7939 isector >>= inode->i_sb->s_blocksize_bits;
7940 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307941 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007942 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007943
7944 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007945 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007946 read_mode, failrec->this_mirror, failrec->in_validation);
7947
Omar Sandoval58efbc92017-08-22 23:45:59 -07007948 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7949 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007950 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007951 bio_put(bio);
7952 }
7953
Omar Sandoval58efbc92017-08-22 23:45:59 -07007954 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007955}
7956
7957struct btrfs_retry_complete {
7958 struct completion done;
7959 struct inode *inode;
7960 u64 start;
7961 int uptodate;
7962};
7963
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007964static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007965{
7966 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007967 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007968 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007969 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007970 int i;
7971
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007972 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007973 goto end;
7974
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007976 io_tree = &BTRFS_I(inode)->io_tree;
7977 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007978 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979
Miao Xie8b110e32014-09-12 18:44:03 +08007980 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007981 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007982 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007983 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7984 io_tree, done->start, bvec->bv_page,
7985 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007986end:
7987 complete(&done->done);
7988 bio_put(bio);
7989}
7990
Omar Sandoval58efbc92017-08-22 23:45:59 -07007991static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7992 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007993{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007995 struct bio_vec bvec;
7996 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007997 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007998 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 unsigned int pgoff;
8000 u32 sectorsize;
8001 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008002 blk_status_t ret;
8003 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008004
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008006 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007
Miao Xiec1dc0892014-09-12 18:43:56 +08008008 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008009 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008010 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008011
Liu Bo17347ce2017-05-15 15:33:27 -07008012 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8013 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8014 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015
8016next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008017 done.uptodate = 0;
8018 done.start = start;
8019 init_completion(&done.done);
8020
Liu Bo17347ce2017-05-15 15:33:27 -07008021 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308022 pgoff, start, start + sectorsize - 1,
8023 io_bio->mirror_num,
8024 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008025 if (ret) {
8026 err = ret;
8027 goto next;
8028 }
Miao Xie8b110e32014-09-12 18:44:03 +08008029
David Sterba9c17f6c2017-07-19 19:26:45 +02008030 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008031
8032 if (!done.uptodate) {
8033 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308034 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008035 }
8036
Liu Bo629ebf42017-05-15 17:20:07 -07008037next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 start += sectorsize;
8039
Liu Bo97bf5a52017-04-07 13:11:10 -07008040 nr_sectors--;
8041 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008043 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 goto next_block_or_try_again;
8045 }
Miao Xie8b110e32014-09-12 18:44:03 +08008046 }
8047
Liu Bo629ebf42017-05-15 17:20:07 -07008048 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008049}
8050
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008051static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008052{
8053 struct btrfs_retry_complete *done = bio->bi_private;
8054 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008055 struct extent_io_tree *io_tree, *failure_tree;
8056 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008057 struct bio_vec *bvec;
8058 int uptodate;
8059 int ret;
8060 int i;
8061
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008062 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008063 goto end;
8064
8065 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308067 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008068 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308069
Josef Bacik7870d082017-05-05 11:57:15 -04008070 io_tree = &BTRFS_I(inode)->io_tree;
8071 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8072
David Sterbac09abff2017-07-13 18:10:07 +02008073 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008074 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008075 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8076 bvec->bv_offset, done->start,
8077 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008078 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008079 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8080 failure_tree, io_tree, done->start,
8081 bvec->bv_page,
8082 btrfs_ino(BTRFS_I(inode)),
8083 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008084 else
8085 uptodate = 0;
8086 }
8087
8088 done->uptodate = uptodate;
8089end:
8090 complete(&done->done);
8091 bio_put(bio);
8092}
8093
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008094static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8095 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008096{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008098 struct bio_vec bvec;
8099 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008100 struct btrfs_retry_complete done;
8101 u64 start;
8102 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103 u32 sectorsize;
8104 int nr_sectors;
8105 unsigned int pgoff;
8106 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008107 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008108 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008109 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008110
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308111 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008112 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308113
Omar Sandoval58efbc92017-08-22 23:45:59 -07008114 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008115 start = io_bio->logical;
8116 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008117 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008118
Liu Bo17347ce2017-05-15 15:33:27 -07008119 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8120 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121
Liu Bo17347ce2017-05-15 15:33:27 -07008122 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308123next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008124 if (uptodate) {
8125 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8126 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8127 bvec.bv_page, pgoff, start, sectorsize);
8128 if (likely(!ret))
8129 goto next;
8130 }
Miao Xie8b110e32014-09-12 18:44:03 +08008131try_again:
8132 done.uptodate = 0;
8133 done.start = start;
8134 init_completion(&done.done);
8135
Omar Sandoval58efbc92017-08-22 23:45:59 -07008136 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8137 pgoff, start, start + sectorsize - 1,
8138 io_bio->mirror_num, btrfs_retry_endio,
8139 &done);
8140 if (status) {
8141 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008142 goto next;
8143 }
8144
David Sterba9c17f6c2017-07-19 19:26:45 +02008145 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008146
8147 if (!done.uptodate) {
8148 /* We might have another mirror, so try again */
8149 goto try_again;
8150 }
8151next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152 offset += sectorsize;
8153 start += sectorsize;
8154
8155 ASSERT(nr_sectors);
8156
Liu Bo97bf5a52017-04-07 13:11:10 -07008157 nr_sectors--;
8158 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308159 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008160 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308161 goto next_block;
8162 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008163 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008164
8165 return err;
8166}
8167
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008168static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8169 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008170{
8171 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8172
8173 if (skip_csum) {
8174 if (unlikely(err))
8175 return __btrfs_correct_data_nocsum(inode, io_bio);
8176 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008177 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008178 } else {
8179 return __btrfs_subio_endio_read(inode, io_bio, err);
8180 }
8181}
8182
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008183static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008184{
8185 struct btrfs_dio_private *dip = bio->bi_private;
8186 struct inode *inode = dip->inode;
8187 struct bio *dio_bio;
8188 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008189 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008190
Liu Bo99c4e3b2017-09-15 15:06:51 -06008191 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008192 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008193
Josef Bacik4b46fce2010-05-23 11:00:55 -04008194 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008195 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008196 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008197
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008199
Liu Bo99c4e3b2017-09-15 15:06:51 -06008200 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008201 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008202
8203 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008204 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008205 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008206}
8207
Qu Wenruo524272602017-03-08 10:25:52 +08008208static void __endio_write_update_ordered(struct inode *inode,
8209 const u64 offset, const u64 bytes,
8210 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008211{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008212 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008213 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008214 struct btrfs_workqueue *wq;
8215 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008216 u64 ordered_offset = offset;
8217 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008218 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008219 int ret;
8220
Qu Wenruo524272602017-03-08 10:25:52 +08008221 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8222 wq = fs_info->endio_freespace_worker;
8223 func = btrfs_freespace_write_helper;
8224 } else {
8225 wq = fs_info->endio_write_workers;
8226 func = btrfs_endio_write_helper;
8227 }
8228
Chris Mason163cf092010-11-28 19:56:33 -05008229again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008230 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008231 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8232 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008233 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008234 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008235 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008236 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008237
Qu Wenruo524272602017-03-08 10:25:52 +08008238 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8239 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008240out_test:
8241 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008242 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8243 * in the range, we can exit.
8244 */
8245 if (ordered_offset == last_offset)
8246 return;
8247 /*
Chris Mason163cf092010-11-28 19:56:33 -05008248 * our bio might span multiple ordered extents. If we haven't
8249 * completed the accounting for the whole dio, go back and try again
8250 */
Filipe Manana14543772015-11-24 16:23:54 +00008251 if (ordered_offset < offset + bytes) {
8252 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008253 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008254 goto again;
8255 }
Filipe Manana14543772015-11-24 16:23:54 +00008256}
8257
8258static void btrfs_endio_direct_write(struct bio *bio)
8259{
8260 struct btrfs_dio_private *dip = bio->bi_private;
8261 struct bio *dio_bio = dip->dio_bio;
8262
Qu Wenruo524272602017-03-08 10:25:52 +08008263 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008264 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008265
Josef Bacik4b46fce2010-05-23 11:00:55 -04008266 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008267
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008268 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008269 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008270 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008271}
8272
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008273static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008274 struct bio *bio, int mirror_num,
8275 unsigned long bio_flags, u64 offset)
8276{
Josef Bacikc6100a42017-05-05 11:57:13 -04008277 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008278 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008279 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008280 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008281 return 0;
8282}
8283
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008284static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008285{
8286 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008287 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008288
Miao Xie8b110e32014-09-12 18:44:03 +08008289 if (err)
8290 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008291 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008292 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8293 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008294 (unsigned long long)bio->bi_iter.bi_sector,
8295 bio->bi_iter.bi_size, err);
8296
8297 if (dip->subio_endio)
8298 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008299
8300 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008301 dip->errors = 1;
8302
8303 /*
8304 * before atomic variable goto zero, we must make sure
8305 * dip->errors is perceived to be set.
8306 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008307 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008308 }
8309
8310 /* if there are more bios still pending for this dio, just exit */
8311 if (!atomic_dec_and_test(&dip->pending_bios))
8312 goto out;
8313
Chris Mason9be33952013-05-17 18:30:14 -04008314 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008315 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008316 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008317 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008318 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008319 }
8320out:
8321 bio_put(bio);
8322}
8323
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008324static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008325 struct btrfs_dio_private *dip,
8326 struct bio *bio,
8327 u64 file_offset)
8328{
8329 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8330 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008331 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008332
8333 /*
8334 * We load all the csum data we need when we submit
8335 * the first bio to reduce the csum tree search and
8336 * contention.
8337 */
8338 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008339 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008340 file_offset);
8341 if (ret)
8342 return ret;
8343 }
8344
8345 if (bio == dip->orig_bio)
8346 return 0;
8347
8348 file_offset -= dip->logical_offset;
8349 file_offset >>= inode->i_sb->s_blocksize_bits;
8350 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8351
8352 return 0;
8353}
8354
Omar Sandoval58efbc92017-08-22 23:45:59 -07008355static inline blk_status_t
8356__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008357 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008358{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008359 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008360 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008361 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008362 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008363
Liu Bo4c274bc2017-11-01 17:19:27 -06008364 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008365 if (async_submit)
8366 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8367
Josef Bacik5fd02042012-05-02 14:00:54 -04008368 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008369 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008370 if (ret)
8371 goto err;
8372 }
Miao Xiee65e1532010-11-22 03:04:43 +00008373
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008374 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008375 goto map;
8376
8377 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008378 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8379 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008380 __btrfs_submit_bio_start_direct_io,
8381 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008382 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008383 } else if (write) {
8384 /*
8385 * If we aren't doing async submit, calculate the csum of the
8386 * bio now.
8387 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008388 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008389 if (ret)
8390 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008391 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008392 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008393 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008394 if (ret)
8395 goto err;
8396 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008397map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008398 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008399err:
Miao Xiee65e1532010-11-22 03:04:43 +00008400 return ret;
8401}
8402
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008403static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008404{
8405 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008406 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008407 struct bio *bio;
8408 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008409 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008410 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008411 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008412 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008413 u64 submit_len;
8414 int clone_offset = 0;
8415 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308416 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008417 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008418
Kent Overstreet4f024f32013-10-11 15:44:27 -07008419 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008420 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008421 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8422 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008423 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008424 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008425
Liu Bo725130b2017-05-16 09:51:39 -07008426 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008427 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008428 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008429 goto submit;
8430 }
8431
David Woodhouse53b381b2013-01-29 18:40:14 -05008432 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008433 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008434 async_submit = 0;
8435 else
8436 async_submit = 1;
8437
Liu Bo725130b2017-05-16 09:51:39 -07008438 /* bio split */
8439 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008440 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008441 do {
Liu Bo725130b2017-05-16 09:51:39 -07008442 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008443
Liu Bo725130b2017-05-16 09:51:39 -07008444 /*
8445 * This will never fail as it's passing GPF_NOFS and
8446 * the allocation is backed by btrfs_bioset.
8447 */
Liu Boe4770942017-05-16 10:57:14 -07008448 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008449 clone_len);
8450 bio->bi_private = dip;
8451 bio->bi_end_io = btrfs_end_dio_bio;
8452 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008453
Liu Bo725130b2017-05-16 09:51:39 -07008454 ASSERT(submit_len >= clone_len);
8455 submit_len -= clone_len;
8456 if (submit_len == 0)
8457 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008458
Liu Bo725130b2017-05-16 09:51:39 -07008459 /*
8460 * Increase the count before we submit the bio so we know
8461 * the end IO handler won't happen before we increase the
8462 * count. Otherwise, the dip might get freed before we're
8463 * done setting it up.
8464 */
8465 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008466
Linus Torvalds66ba7722017-09-09 13:27:51 -07008467 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008468 async_submit);
8469 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008470 bio_put(bio);
8471 atomic_dec(&dip->pending_bios);
8472 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008473 }
Liu Bo725130b2017-05-16 09:51:39 -07008474
8475 clone_offset += clone_len;
8476 start_sector += clone_len >> 9;
8477 file_offset += clone_len;
8478
8479 map_length = submit_len;
8480 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8481 start_sector << 9, &map_length, NULL, 0);
8482 if (ret)
8483 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008484 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008485
Josef Bacik02f57c72011-04-06 14:25:44 -04008486submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008487 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008488 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008489 return 0;
8490
8491 bio_put(bio);
8492out_err:
8493 dip->errors = 1;
8494 /*
8495 * before atomic variable goto zero, we must
8496 * make sure dip->errors is perceived to be set.
8497 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008498 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008499 if (atomic_dec_and_test(&dip->pending_bios))
8500 bio_io_error(dip->orig_bio);
8501
8502 /* bio_end_io() will handle error, so we needn't return it */
8503 return 0;
8504}
8505
Mike Christie8a4c1e42016-06-05 14:31:50 -05008506static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8507 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008508{
Filipe Manana61de7182015-07-01 12:13:10 +01008509 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008510 struct bio *bio = NULL;
8511 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008512 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513 int ret = 0;
8514
David Sterba8b6c1d52017-06-02 17:48:13 +02008515 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008516
Miao Xiec1dc0892014-09-12 18:43:56 +08008517 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008518 if (!dip) {
8519 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008520 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008521 }
8522
8523 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008524 dip->inode = inode;
8525 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008526 dip->bytes = dio_bio->bi_iter.bi_size;
8527 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008528 bio->bi_private = dip;
8529 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008530 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008531 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008532 io_bio = btrfs_io_bio(bio);
8533 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008536 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008537 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008538 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 dip->subio_endio = btrfs_subio_endio_read;
8540 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541
Filipe Mananaf28a4922015-12-08 19:23:20 +00008542 /*
8543 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8544 * even if we fail to submit a bio, because in such case we do the
8545 * corresponding error handling below and it must not be done a second
8546 * time by btrfs_direct_IO().
8547 */
8548 if (write) {
8549 struct btrfs_dio_data *dio_data = current->journal_info;
8550
8551 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8552 dip->bytes;
8553 dio_data->unsubmitted_oe_range_start =
8554 dio_data->unsubmitted_oe_range_end;
8555 }
8556
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008557 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008558 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008559 return;
Chris Mason9be33952013-05-17 18:30:14 -04008560
Liu Bo3892ac92017-04-17 15:00:28 -07008561 if (io_bio->end_io)
8562 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008563
Josef Bacik4b46fce2010-05-23 11:00:55 -04008564free_ordered:
8565 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008566 * If we arrived here it means either we failed to submit the dip
8567 * or we either failed to clone the dio_bio or failed to allocate the
8568 * dip. If we cloned the dio_bio and allocated the dip, we can just
8569 * call bio_endio against our io_bio so that we get proper resource
8570 * cleanup if we fail to submit the dip, otherwise, we must do the
8571 * same as btrfs_endio_direct_[write|read] because we can't call these
8572 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008573 */
Liu Bo3892ac92017-04-17 15:00:28 -07008574 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008575 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008576 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008577 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008578 * and all the cleanup and final put for dio_bio (through
8579 * dio_end_io()).
8580 */
8581 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008582 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008583 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008584 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008585 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008586 file_offset,
8587 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008588 false);
Filipe Manana14543772015-11-24 16:23:54 +00008589 else
Filipe Manana61de7182015-07-01 12:13:10 +01008590 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8591 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008592
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008593 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008594 /*
8595 * Releases and cleans up our dio_bio, no need to bio_put()
8596 * nor bio_endio()/bio_io_error() against dio_bio.
8597 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008598 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008599 }
Liu Bo3892ac92017-04-17 15:00:28 -07008600 if (bio)
8601 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008602 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008603}
8604
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008605static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008606 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008607{
8608 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008609 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008610 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008611 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008612
8613 if (offset & blocksize_mask)
8614 goto out;
8615
Al Viro28060d52014-03-22 05:15:17 -04008616 if (iov_iter_alignment(iter) & blocksize_mask)
8617 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008618
Al Viro28060d52014-03-22 05:15:17 -04008619 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008620 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008621 return 0;
8622 /*
8623 * Check to make sure we don't have duplicate iov_base's in this
8624 * iovec, if so return EINVAL, otherwise we'll get csum errors
8625 * when reading back.
8626 */
8627 for (seg = 0; seg < iter->nr_segs; seg++) {
8628 for (i = seg + 1; i < iter->nr_segs; i++) {
8629 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008630 goto out;
8631 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008632 }
8633 retval = 0;
8634out:
8635 return retval;
8636}
Josef Bacikeb838e72012-07-31 16:28:48 -04008637
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008638static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008639{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008640 struct file *file = iocb->ki_filp;
8641 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008642 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308643 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008644 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008645 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008646 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008647 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008648 bool wakeup = true;
8649 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008650 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008651
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008652 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008653 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008654
Jens Axboefe0f07d2015-04-15 17:05:48 -06008655 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008656
Josef Bacik0e267c42013-07-02 10:38:02 -04008657 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008658 * The generic stuff only does filemap_write_and_wait_range, which
8659 * isn't enough if we've written compressed pages to this area, so
8660 * we need to flush the dirty pages again to make absolutely sure
8661 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008662 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008663 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008664 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8665 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008666 filemap_fdatawrite_range(inode->i_mapping, offset,
8667 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008668
Omar Sandoval6f673762015-03-16 04:33:52 -07008669 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008670 /*
8671 * If the write DIO is beyond the EOF, we need update
8672 * the isize, but it is protected by i_mutex. So we can
8673 * not unlock the i_mutex at this case.
8674 */
8675 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008676 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008677 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008678 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008679 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8680 ret = -EAGAIN;
8681 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008682 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008683 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8684 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008685 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008686 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008687
8688 /*
8689 * We need to know how many extents we reserved so that we can
8690 * do the accounting properly if we go over the number we
8691 * originally calculated. Abuse current->journal_info for this.
8692 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008693 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008694 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008695 dio_data.unsubmitted_oe_range_start = (u64)offset;
8696 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308697 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308698 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008699 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8700 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008701 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008702 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8703 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008704 }
8705
Omar Sandoval17f8c842015-03-16 04:33:50 -07008706 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008707 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008708 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008709 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008710 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308711 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008712 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008713 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308714 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008715 btrfs_delalloc_release_space(inode, data_reserved,
8716 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008717 /*
8718 * On error we might have left some ordered extents
8719 * without submitting corresponding bios for them, so
8720 * cleanup them up to avoid other tasks getting them
8721 * and waiting for them to complete forever.
8722 */
8723 if (dio_data.unsubmitted_oe_range_start <
8724 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008725 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008726 dio_data.unsubmitted_oe_range_start,
8727 dio_data.unsubmitted_oe_range_end -
8728 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008729 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008730 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008731 btrfs_delalloc_release_space(inode, data_reserved,
8732 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008733 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008734 }
Miao Xie38851cc2013-02-08 07:04:11 +00008735out:
Miao Xie2e60a512013-02-08 07:01:08 +00008736 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008737 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008738 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008739 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008740
Qu Wenruo364ecf32017-02-27 15:10:38 +08008741 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008742 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008743}
8744
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008745#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8746
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008747static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8748 __u64 start, __u64 len)
8749{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008750 int ret;
8751
8752 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8753 if (ret)
8754 return ret;
8755
David Sterba2135fb92017-06-23 04:09:57 +02008756 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008757}
8758
Chris Mason9ebefb182007-06-15 13:50:00 -04008759int btrfs_readpage(struct file *file, struct page *page)
8760{
Chris Masond1310b22008-01-24 16:13:08 -05008761 struct extent_io_tree *tree;
8762 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008763 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008764}
Chris Mason1832a6d2007-12-21 16:27:21 -05008765
Chris Mason39279cc2007-06-12 06:35:45 -04008766static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8767{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008768 struct inode *inode = page->mapping->host;
8769 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008770
8771 if (current->flags & PF_MEMALLOC) {
8772 redirty_page_for_writepage(wbc, page);
8773 unlock_page(page);
8774 return 0;
8775 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008776
8777 /*
8778 * If we are under memory pressure we will call this directly from the
8779 * VM, we need to make sure we have the inode referenced for the ordered
8780 * extent. If not just return like we didn't do anything.
8781 */
8782 if (!igrab(inode)) {
8783 redirty_page_for_writepage(wbc, page);
8784 return AOP_WRITEPAGE_ACTIVATE;
8785 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008786 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008787 btrfs_add_delayed_iput(inode);
8788 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008789}
Chris Mason39279cc2007-06-12 06:35:45 -04008790
Eric Sandeen48a3b632013-04-25 20:41:01 +00008791static int btrfs_writepages(struct address_space *mapping,
8792 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008793{
Chris Masond1310b22008-01-24 16:13:08 -05008794 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008795
Chris Masond1310b22008-01-24 16:13:08 -05008796 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008797 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008798}
8799
Chris Mason3ab2fb52007-11-08 10:59:22 -05008800static int
8801btrfs_readpages(struct file *file, struct address_space *mapping,
8802 struct list_head *pages, unsigned nr_pages)
8803{
Chris Masond1310b22008-01-24 16:13:08 -05008804 struct extent_io_tree *tree;
8805 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008806 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008807}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008808static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008809{
Chris Masond1310b22008-01-24 16:13:08 -05008810 struct extent_io_tree *tree;
8811 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008812 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008813
Chris Masond1310b22008-01-24 16:13:08 -05008814 tree = &BTRFS_I(page->mapping->host)->io_tree;
8815 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008816 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008817 if (ret == 1) {
8818 ClearPagePrivate(page);
8819 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008820 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008821 }
8822 return ret;
8823}
Chris Mason39279cc2007-06-12 06:35:45 -04008824
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8826{
Chris Mason98509cf2008-09-11 15:51:43 -04008827 if (PageWriteback(page) || PageDirty(page))
8828 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008829 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008830}
8831
Lukas Czernerd47992f2013-05-21 23:17:23 -04008832static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8833 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008834{
Josef Bacik5fd02042012-05-02 14:00:54 -04008835 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008836 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008838 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008839 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008840 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308841 u64 start;
8842 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008843 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008844
Chris Mason8b62b722009-09-02 16:53:46 -04008845 /*
8846 * we have the page locked, so new writeback can't start,
8847 * and the dirty bit won't be cleared while we are here.
8848 *
8849 * Wait for IO on this page so that we can safely clear
8850 * the PagePrivate2 bit and do ordered accounting
8851 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008852 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008853
Josef Bacik5fd02042012-05-02 14:00:54 -04008854 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008855 if (offset) {
8856 btrfs_releasepage(page, GFP_NOFS);
8857 return;
8858 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008859
8860 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008861 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308862again:
8863 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008864 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308865 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008866 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308867 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008868 /*
8869 * IO on this page will never be started, so we need
8870 * to account for any ordered extents now
8871 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008872 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308873 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008874 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008875 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008876 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008877 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008878 /*
8879 * whoever cleared the private bit is responsible
8880 * for the finish_ordered_io
8881 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008882 if (TestClearPagePrivate2(page)) {
8883 struct btrfs_ordered_inode_tree *tree;
8884 u64 new_len;
8885
8886 tree = &BTRFS_I(inode)->ordered_tree;
8887
8888 spin_lock_irq(&tree->lock);
8889 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308890 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008891 if (new_len < ordered->truncated_len)
8892 ordered->truncated_len = new_len;
8893 spin_unlock_irq(&tree->lock);
8894
8895 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308896 start,
8897 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008898 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008899 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008900 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008901 if (!inode_evicting) {
8902 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308903 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008904 &cached_state);
8905 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308906
8907 start = end + 1;
8908 if (start < page_end)
8909 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008910 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008911
Qu Wenruob9d0b382015-09-29 10:35:16 +08008912 /*
8913 * Qgroup reserved space handler
8914 * Page here will be either
8915 * 1) Already written to disk
8916 * In this case, its reserved space is released from data rsv map
8917 * and will be freed by delayed_ref handler finally.
8918 * So even we call qgroup_free_data(), it won't decrease reserved
8919 * space.
8920 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008921 * This means the reserved space should be freed here. However,
8922 * if a truncate invalidates the page (by clearing PageDirty)
8923 * and the page is accounted for while allocating extent
8924 * in btrfs_check_data_free_space() we let delayed_ref to
8925 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008926 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008927 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008928 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008929 if (!inode_evicting) {
8930 clear_extent_bit(tree, page_start, page_end,
8931 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008932 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8933 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008934 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008935
8936 __btrfs_releasepage(page, GFP_NOFS);
8937 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008938
Chris Mason4a096752008-07-21 10:29:44 -04008939 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008940 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008941 ClearPagePrivate(page);
8942 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008943 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008944 }
Chris Mason39279cc2007-06-12 06:35:45 -04008945}
8946
Chris Mason9ebefb182007-06-15 13:50:00 -04008947/*
8948 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8949 * called from a page fault handler when a page is first dirtied. Hence we must
8950 * be careful to check for EOF conditions here. We set the page up correctly
8951 * for a written page which means we get ENOSPC checking when writing into
8952 * holes and correct delalloc and unwritten extent mapping on filesystems that
8953 * support these features.
8954 *
8955 * We are not allowed to take the i_mutex here so we have to play games to
8956 * protect against truncate races as the page could now be beyond EOF. Because
8957 * vmtruncate() writes the inode size before removing pages, once we have the
8958 * page lock we can determine safely if the page is beyond EOF. If it is not
8959 * beyond EOF, then the page is guaranteed safe against truncation until we
8960 * unlock the page.
8961 */
Dave Jiang11bac802017-02-24 14:56:41 -08008962int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008963{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008964 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008965 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008966 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008967 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8968 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008969 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008970 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 char *kaddr;
8972 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008973 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008974 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008975 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308976 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008977 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308979 u64 end;
8980
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008981 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008982
Jan Karab2b5ef52012-06-12 16:20:45 +02008983 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008984 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008985 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008987
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308988 /*
8989 * Reserving delalloc space after obtaining the page lock can lead to
8990 * deadlock. For example, if a dirty page is locked by this function
8991 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8992 * dirty page write out, then the btrfs_writepage() function could
8993 * end up waiting indefinitely to get a lock on the page currently
8994 * being processed by btrfs_page_mkwrite() function.
8995 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008996 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308997 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008998 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008999 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009000 reserved = 1;
9001 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009002 if (ret) {
9003 if (ret == -ENOMEM)
9004 ret = VM_FAULT_OOM;
9005 else /* -ENOSPC, -EIO, etc */
9006 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009007 if (reserved)
9008 goto out;
9009 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009010 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009011
Nick Piggin56a76f82009-03-31 15:23:23 -07009012 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009014 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009016
Chris Mason9ebefb182007-06-15 13:50:00 -04009017 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009019 /* page got truncated out from underneath us */
9020 goto out_unlock;
9021 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009022 wait_on_page_writeback(page);
9023
David Sterbaff13db42015-12-03 14:30:40 +01009024 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025 set_page_extent_mapped(page);
9026
Chris Masoneb84ae02008-07-17 13:53:27 -04009027 /*
9028 * we can't set the delalloc bits if there are pending ordered
9029 * extents. Drop our locks and wait for them to finish
9030 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009031 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9032 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009033 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009034 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009035 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009036 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009037 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009038 btrfs_put_ordered_extent(ordered);
9039 goto again;
9040 }
9041
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009042 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009043 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009044 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009045 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309046 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009047 btrfs_delalloc_release_space(inode, data_reserved,
9048 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309049 }
9050 }
9051
Josef Bacikfbf19082009-10-01 17:10:23 -04009052 /*
Liu Bo54160342016-12-13 12:15:19 -08009053 * page_mkwrite gets called when the page is firstly dirtied after it's
9054 * faulted in, but write(2) could also dirty a page and set delalloc
9055 * bits, thus in this case for space account reason, we still need to
9056 * clear any delalloc bits within this page range since we have to
9057 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009058 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309059 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009060 EXTENT_DIRTY | EXTENT_DELALLOC |
9061 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009062 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009063
Filipe Mananae3b8a482017-11-04 00:16:59 +00009064 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009065 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009066 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009067 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009068 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009069 ret = VM_FAULT_SIGBUS;
9070 goto out_unlock;
9071 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009072 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009073
9074 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009075 if (page_start + PAGE_SIZE > size)
9076 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009077 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009078 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009079
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009080 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009081 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009082 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083 flush_dcache_page(page);
9084 kunmap(page);
9085 }
Chris Mason247e7432008-07-17 12:53:51 -04009086 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009087 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009088 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009089
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009090 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009091 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009092 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009093
David Sterbae43bbe52017-12-12 21:43:52 +01009094 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009095
9096out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009097 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009098 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009099 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009100 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009101 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009102 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009103 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009104out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009105 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009106 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9107 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009108out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009109 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009110 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009111 return ret;
9112}
9113
Josef Bacika41ad392011-01-31 15:30:16 -05009114static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009117 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009118 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009119 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009120 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009121 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009122 u64 mask = fs_info->sectorsize - 1;
9123 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009124
Josef Bacik0ef8b722013-10-25 16:13:35 -04009125 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9126 (u64)-1);
9127 if (ret)
9128 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009129
Josef Bacikfcb80c22011-05-03 10:40:22 -04009130 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009131 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009132 * 3 things going on here
9133 *
9134 * 1) We need to reserve space for our orphan item and the space to
9135 * delete our orphan item. Lord knows we don't want to have a dangling
9136 * orphan item because we didn't reserve space to remove it.
9137 *
9138 * 2) We need to reserve space to update our inode.
9139 *
9140 * 3) We need to have something to cache all the space that is going to
9141 * be free'd up by the truncate operation, but also have some slack
9142 * space reserved in case it uses space during the truncate (thank you
9143 * very much snapshotting).
9144 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009145 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009146 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009147 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009148 * doesn't end up using space reserved for updating the inode or
9149 * removing the orphan item. We also need to be able to stop the
9150 * transaction and start a new one, which means we need to be able to
9151 * update the inode several times, and we have no idea of knowing how
9152 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009153 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009154 * Then there is the orphan item, which does indeed need to be held on
9155 * to for the whole operation, and we need nobody to touch this reserved
9156 * space except the orphan code.
9157 *
9158 * So that leaves us with
9159 *
9160 * 1) root->orphan_block_rsv - for the orphan deletion.
9161 * 2) rsv - for the truncate reservation, which we will steal from the
9162 * transaction reservation.
9163 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9164 * updating the inode.
9165 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009166 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009167 if (!rsv)
9168 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009169 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009170 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009171
Josef Bacik907cbce2011-08-08 13:46:15 -04009172 /*
Josef Bacik07127182011-08-19 10:29:59 -04009173 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009174 * 1 for updating the inode.
9175 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009176 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009177 if (IS_ERR(trans)) {
9178 err = PTR_ERR(trans);
9179 goto out;
9180 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009181
Josef Bacik907cbce2011-08-08 13:46:15 -04009182 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009183 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009184 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009185 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009186
Chris Mason5a3f23d2009-03-31 13:27:11 -04009187 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009188 * So if we truncate and then write and fsync we normally would just
9189 * write the extents that changed, which is a problem if we need to
9190 * first truncate that entire inode. So set this flag so we write out
9191 * all of the extents in the inode to the sync log so we're completely
9192 * safe.
9193 */
9194 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009195 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009196
Yan, Zheng80825102009-11-12 09:35:36 +00009197 while (1) {
9198 ret = btrfs_truncate_inode_items(trans, root, inode,
9199 inode->i_size,
9200 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009201 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009202 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009203 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009204 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009205 }
Chris Mason39279cc2007-06-12 06:35:45 -04009206
Yan, Zheng80825102009-11-12 09:35:36 +00009207 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009208 if (ret) {
9209 err = ret;
9210 break;
9211 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009212
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009213 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009214 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009215
9216 trans = btrfs_start_transaction(root, 2);
9217 if (IS_ERR(trans)) {
9218 ret = err = PTR_ERR(trans);
9219 trans = NULL;
9220 break;
9221 }
9222
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009223 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009224 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009225 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009226 BUG_ON(ret); /* shouldn't happen */
9227 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009228 }
9229
Josef Bacikddfae632017-10-19 14:16:02 -04009230 /*
9231 * We can't call btrfs_truncate_block inside a trans handle as we could
9232 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9233 * we've truncated everything except the last little bit, and can do
9234 * btrfs_truncate_block and then update the disk_i_size.
9235 */
9236 if (ret == NEED_TRUNCATE_BLOCK) {
9237 btrfs_end_transaction(trans);
9238 btrfs_btree_balance_dirty(fs_info);
9239
9240 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9241 if (ret)
9242 goto out;
9243 trans = btrfs_start_transaction(root, 1);
9244 if (IS_ERR(trans)) {
9245 ret = PTR_ERR(trans);
9246 goto out;
9247 }
9248 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9249 }
9250
Yan, Zheng80825102009-11-12 09:35:36 +00009251 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009252 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009253 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009254 if (ret)
9255 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009256 }
9257
Chris Mason917c16b2011-11-08 14:49:59 -05009258 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009259 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009260 ret = btrfs_update_inode(trans, root, inode);
9261 if (ret && !err)
9262 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009263
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009264 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009265 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009266 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009267out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009268 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009269
Josef Bacik3893e332011-01-31 16:03:11 -05009270 if (ret && !err)
9271 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009272
Josef Bacik3893e332011-01-31 16:03:11 -05009273 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009274}
9275
Sven Wegener3b963622008-06-09 21:57:42 -04009276/*
Chris Masond352ac62008-09-29 15:18:18 -04009277 * create a new subvolume directory/inode (helper for the ioctl).
9278 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009279int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009280 struct btrfs_root *new_root,
9281 struct btrfs_root *parent_root,
9282 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009283{
Chris Mason39279cc2007-06-12 06:35:45 -04009284 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009285 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009286 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009287
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009288 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9289 new_dirid, new_dirid,
9290 S_IFDIR | (~current_umask() & S_IRWXUGO),
9291 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009292 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009293 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009294 inode->i_op = &btrfs_dir_inode_operations;
9295 inode->i_fop = &btrfs_dir_file_operations;
9296
Miklos Szeredibfe86842011-10-28 14:13:29 +02009297 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009298 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009299 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009300
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009301 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9302 if (err)
9303 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009304 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009305 new_root->root_key.objectid, err);
9306
Yan, Zheng76dda932009-09-21 16:00:26 -04009307 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009308
Yan, Zheng76dda932009-09-21 16:00:26 -04009309 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009310 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009311}
9312
Chris Mason39279cc2007-06-12 06:35:45 -04009313struct inode *btrfs_alloc_inode(struct super_block *sb)
9314{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009315 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009316 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009318
David Sterba712e36c2017-10-31 17:08:27 +01009319 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009320 if (!ei)
9321 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009322
9323 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009325 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009326 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009327 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009328 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009329 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009330 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009331 ei->disk_i_size = 0;
9332 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009333 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009334 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009335 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009336 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009337 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009338 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009339
Josef Bacik9e0baf62011-07-15 15:16:44 +00009340 spin_lock_init(&ei->lock);
9341 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009342 if (sb->s_magic != BTRFS_TEST_MAGIC)
9343 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9344 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009345 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009346 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009347 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009348
Miao Xie16cdcec2011-04-22 18:12:22 +08009349 ei->delayed_node = NULL;
9350
chandan r9cc97d62012-07-04 12:48:07 +05309351 ei->i_otime.tv_sec = 0;
9352 ei->i_otime.tv_nsec = 0;
9353
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009354 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009355 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009356 extent_io_tree_init(&ei->io_tree, inode);
9357 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009358 ei->io_tree.track_uptodate = 1;
9359 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009360 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009361 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009362 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009363 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009364 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009365 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009366 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009367 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009368
9369 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009370}
9371
Josef Bacikaaedb552013-10-11 14:44:09 -04009372#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9373void btrfs_test_destroy_inode(struct inode *inode)
9374{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009375 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009376 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9377}
9378#endif
9379
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009380static void btrfs_i_callback(struct rcu_head *head)
9381{
9382 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009383 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9384}
9385
Chris Mason39279cc2007-06-12 06:35:45 -04009386void btrfs_destroy_inode(struct inode *inode)
9387{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009388 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009389 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009390 struct btrfs_root *root = BTRFS_I(inode)->root;
9391
Al Virob3d9b7a2012-06-09 13:51:19 -04009392 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009393 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009394 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9395 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009396 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009397 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009398 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009399 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009400 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009401
Chris Mason5a3f23d2009-03-31 13:27:11 -04009402 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009403 * This can happen where we create an inode, but somebody else also
9404 * created the same inode and we need to destroy the one we already
9405 * created.
9406 */
9407 if (!root)
9408 goto free;
9409
Josef Bacik8a35d952012-05-23 14:26:42 -04009410 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9411 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009412 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009413 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009414 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009415 }
Josef Bacik7b128762008-07-24 12:17:14 -04009416
Chris Masond3977122009-01-05 21:25:51 -05009417 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009418 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9419 if (!ordered)
9420 break;
9421 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009422 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009423 "found ordered extent %llu %llu on inode cleanup",
9424 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009425 btrfs_remove_ordered_extent(inode, ordered);
9426 btrfs_put_ordered_extent(ordered);
9427 btrfs_put_ordered_extent(ordered);
9428 }
9429 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009430 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009431 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009432 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009433free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009434 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009435}
9436
Al Viro45321ac2010-06-07 13:43:19 -04009437int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009438{
9439 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009440
Naohiro Aota6379ef92013-06-06 09:56:34 +00009441 if (root == NULL)
9442 return 1;
9443
Liu Bofa6ac872013-02-20 14:10:23 +00009444 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009445 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009446 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009447 else
Al Viro45321ac2010-06-07 13:43:19 -04009448 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009449}
9450
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009451static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009452{
9453 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9454
9455 inode_init_once(&ei->vfs_inode);
9456}
9457
9458void btrfs_destroy_cachep(void)
9459{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009460 /*
9461 * Make sure all delayed rcu free inodes are flushed before we
9462 * destroy cache.
9463 */
9464 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009465 kmem_cache_destroy(btrfs_inode_cachep);
9466 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009467 kmem_cache_destroy(btrfs_path_cachep);
9468 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009469}
9470
Liu Bof5c29bd2017-11-02 17:21:50 -06009471int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009472{
David Sterba837e1972012-09-07 03:00:48 -06009473 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009474 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009475 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9476 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009477 if (!btrfs_inode_cachep)
9478 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009479
David Sterba837e1972012-09-07 03:00:48 -06009480 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009481 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009482 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009483 if (!btrfs_trans_handle_cachep)
9484 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009485
David Sterba837e1972012-09-07 03:00:48 -06009486 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009487 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009488 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009489 if (!btrfs_path_cachep)
9490 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009491
David Sterba837e1972012-09-07 03:00:48 -06009492 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009493 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009494 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009495 if (!btrfs_free_space_cachep)
9496 goto fail;
9497
Chris Mason39279cc2007-06-12 06:35:45 -04009498 return 0;
9499fail:
9500 btrfs_destroy_cachep();
9501 return -ENOMEM;
9502}
9503
David Howellsa528d352017-01-31 16:46:22 +00009504static int btrfs_getattr(const struct path *path, struct kstat *stat,
9505 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009506{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009507 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009508 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009509 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009510 u32 bi_flags = BTRFS_I(inode)->flags;
9511
9512 stat->result_mask |= STATX_BTIME;
9513 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9514 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9515 if (bi_flags & BTRFS_INODE_APPEND)
9516 stat->attributes |= STATX_ATTR_APPEND;
9517 if (bi_flags & BTRFS_INODE_COMPRESS)
9518 stat->attributes |= STATX_ATTR_COMPRESSED;
9519 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9520 stat->attributes |= STATX_ATTR_IMMUTABLE;
9521 if (bi_flags & BTRFS_INODE_NODUMP)
9522 stat->attributes |= STATX_ATTR_NODUMP;
9523
9524 stat->attributes_mask |= (STATX_ATTR_APPEND |
9525 STATX_ATTR_COMPRESSED |
9526 STATX_ATTR_IMMUTABLE |
9527 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009528
Chris Mason39279cc2007-06-12 06:35:45 -04009529 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009530 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009531
9532 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009533 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009534 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009535 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009536 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009537 return 0;
9538}
9539
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540static int btrfs_rename_exchange(struct inode *old_dir,
9541 struct dentry *old_dentry,
9542 struct inode *new_dir,
9543 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009544{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009545 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009546 struct btrfs_trans_handle *trans;
9547 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009548 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 struct inode *new_inode = new_dentry->d_inode;
9550 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009551 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009552 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009553 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9554 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555 u64 old_idx = 0;
9556 u64 new_idx = 0;
9557 u64 root_objectid;
9558 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009559 bool root_log_pinned = false;
9560 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561
9562 /* we only allow rename subvolume link between subvolumes */
9563 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9564 return -EXDEV;
9565
9566 /* close the race window with snapshot create/destroy ioctl */
9567 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009568 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009570 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571
9572 /*
9573 * We want to reserve the absolute worst case amount of items. So if
9574 * both inodes are subvols and we need to unlink them then that would
9575 * require 4 item modifications, but if they are both normal inodes it
9576 * would require 5 item modifications, so we'll assume their normal
9577 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9578 * should cover the worst case number of items we'll modify.
9579 */
9580 trans = btrfs_start_transaction(root, 12);
9581 if (IS_ERR(trans)) {
9582 ret = PTR_ERR(trans);
9583 goto out_notrans;
9584 }
9585
9586 /*
9587 * We need to find a free sequence number both in the source and
9588 * in the destination directory for the exchange.
9589 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009590 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591 if (ret)
9592 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009593 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594 if (ret)
9595 goto out_fail;
9596
9597 BTRFS_I(old_inode)->dir_index = 0ULL;
9598 BTRFS_I(new_inode)->dir_index = 0ULL;
9599
9600 /* Reference for the source. */
9601 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9602 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009603 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009605 btrfs_pin_log_trans(root);
9606 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 ret = btrfs_insert_inode_ref(trans, dest,
9608 new_dentry->d_name.name,
9609 new_dentry->d_name.len,
9610 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009611 btrfs_ino(BTRFS_I(new_dir)),
9612 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 if (ret)
9614 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 }
9616
9617 /* And now for the dest. */
9618 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9619 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009620 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009622 btrfs_pin_log_trans(dest);
9623 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 ret = btrfs_insert_inode_ref(trans, root,
9625 old_dentry->d_name.name,
9626 old_dentry->d_name.len,
9627 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009628 btrfs_ino(BTRFS_I(old_dir)),
9629 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 if (ret)
9631 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009632 }
9633
9634 /* Update inode version and ctime/mtime. */
9635 inode_inc_iversion(old_dir);
9636 inode_inc_iversion(new_dir);
9637 inode_inc_iversion(old_inode);
9638 inode_inc_iversion(new_inode);
9639 old_dir->i_ctime = old_dir->i_mtime = ctime;
9640 new_dir->i_ctime = new_dir->i_mtime = ctime;
9641 old_inode->i_ctime = ctime;
9642 new_inode->i_ctime = ctime;
9643
9644 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009645 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9646 BTRFS_I(old_inode), 1);
9647 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9648 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 }
9650
9651 /* src is a subvolume */
9652 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9653 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9654 ret = btrfs_unlink_subvol(trans, root, old_dir,
9655 root_objectid,
9656 old_dentry->d_name.name,
9657 old_dentry->d_name.len);
9658 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009659 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9660 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 old_dentry->d_name.name,
9662 old_dentry->d_name.len);
9663 if (!ret)
9664 ret = btrfs_update_inode(trans, root, old_inode);
9665 }
9666 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009667 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009668 goto out_fail;
9669 }
9670
9671 /* dest is a subvolume */
9672 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9673 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9674 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9675 root_objectid,
9676 new_dentry->d_name.name,
9677 new_dentry->d_name.len);
9678 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009679 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9680 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009681 new_dentry->d_name.name,
9682 new_dentry->d_name.len);
9683 if (!ret)
9684 ret = btrfs_update_inode(trans, dest, new_inode);
9685 }
9686 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009687 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009688 goto out_fail;
9689 }
9690
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009691 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 new_dentry->d_name.name,
9693 new_dentry->d_name.len, 0, old_idx);
9694 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009695 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 goto out_fail;
9697 }
9698
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009699 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 old_dentry->d_name.name,
9701 old_dentry->d_name.len, 0, new_idx);
9702 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009703 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 goto out_fail;
9705 }
9706
9707 if (old_inode->i_nlink == 1)
9708 BTRFS_I(old_inode)->dir_index = old_idx;
9709 if (new_inode->i_nlink == 1)
9710 BTRFS_I(new_inode)->dir_index = new_idx;
9711
Filipe Manana86e8aa02016-05-05 02:02:27 +01009712 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009714 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9715 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009717 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009719 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009721 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9722 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009723 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009724 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 }
9726out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009727 /*
9728 * If we have pinned a log and an error happened, we unpin tasks
9729 * trying to sync the log and force them to fallback to a transaction
9730 * commit if the log currently contains any of the inodes involved in
9731 * this rename operation (to ensure we do not persist a log with an
9732 * inconsistent state for any of these inodes or leading to any
9733 * inconsistencies when replayed). If the transaction was aborted, the
9734 * abortion reason is propagated to userspace when attempting to commit
9735 * the transaction. If the log does not contain any of these inodes, we
9736 * allow the tasks to sync it.
9737 */
9738 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009739 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9740 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9741 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009742 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009743 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009744 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009745
9746 if (root_log_pinned) {
9747 btrfs_end_log_trans(root);
9748 root_log_pinned = false;
9749 }
9750 if (dest_log_pinned) {
9751 btrfs_end_log_trans(dest);
9752 dest_log_pinned = false;
9753 }
9754 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009755 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756out_notrans:
9757 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009758 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009760 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009761
9762 return ret;
9763}
9764
9765static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9766 struct btrfs_root *root,
9767 struct inode *dir,
9768 struct dentry *dentry)
9769{
9770 int ret;
9771 struct inode *inode;
9772 u64 objectid;
9773 u64 index;
9774
9775 ret = btrfs_find_free_ino(root, &objectid);
9776 if (ret)
9777 return ret;
9778
9779 inode = btrfs_new_inode(trans, root, dir,
9780 dentry->d_name.name,
9781 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009782 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 objectid,
9784 S_IFCHR | WHITEOUT_MODE,
9785 &index);
9786
9787 if (IS_ERR(inode)) {
9788 ret = PTR_ERR(inode);
9789 return ret;
9790 }
9791
9792 inode->i_op = &btrfs_special_inode_operations;
9793 init_special_inode(inode, inode->i_mode,
9794 WHITEOUT_DEV);
9795
9796 ret = btrfs_init_inode_security(trans, inode, dir,
9797 &dentry->d_name);
9798 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009799 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009801 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9802 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009804 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805
9806 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009807out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009809 if (ret)
9810 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 iput(inode);
9812
Filipe Mananac9901612016-05-05 01:41:57 +01009813 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814}
9815
Chris Mason39279cc2007-06-12 06:35:45 -04009816static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817 struct inode *new_dir, struct dentry *new_dentry,
9818 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009819{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009820 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009821 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009822 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009823 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9824 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009825 struct inode *new_inode = d_inode(new_dentry);
9826 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009827 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009828 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009829 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009830 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009831 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009832
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009833 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009834 return -EPERM;
9835
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009836 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009837 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009838 return -EXDEV;
9839
Li Zefan33345d012011-04-20 10:31:50 +08009840 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009841 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009842 return -ENOTEMPTY;
9843
Chris Mason39279cc2007-06-12 06:35:45 -04009844 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009846 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009847
9848
9849 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009850 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009851 new_dentry->d_name.name,
9852 new_dentry->d_name.len);
9853
9854 if (ret) {
9855 if (ret == -EEXIST) {
9856 /* we shouldn't get
9857 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309858 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009859 return ret;
9860 }
9861 } else {
9862 /* maybe -EOVERFLOW */
9863 return ret;
9864 }
9865 }
9866 ret = 0;
9867
Chris Mason5a3f23d2009-03-31 13:27:11 -04009868 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009869 * we're using rename to replace one file with another. Start IO on it
9870 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009871 */
Chris Mason8d875f92014-08-12 10:47:42 -07009872 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009873 filemap_flush(old_inode->i_mapping);
9874
Yan, Zheng76dda932009-09-21 16:00:26 -04009875 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009876 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009877 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009878 /*
9879 * We want to reserve the absolute worst case amount of items. So if
9880 * both inodes are subvols and we need to unlink them then that would
9881 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009882 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009883 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9884 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009885 * If our rename has the whiteout flag, we need more 5 units for the
9886 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9887 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009888 */
Filipe Manana5062af32016-05-05 10:26:26 +01009889 trans_num_items = 11;
9890 if (flags & RENAME_WHITEOUT)
9891 trans_num_items += 5;
9892 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009893 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009894 ret = PTR_ERR(trans);
9895 goto out_notrans;
9896 }
Chris Mason5f39d392007-10-15 16:14:19 -04009897
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009898 if (dest != root)
9899 btrfs_record_root_in_trans(trans, dest);
9900
Nikolay Borisov877574e2017-02-20 13:50:33 +02009901 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009902 if (ret)
9903 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009904
Miao Xie67de1172013-12-26 13:07:06 +08009905 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009906 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009907 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009908 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009909 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009910 btrfs_pin_log_trans(root);
9911 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009912 ret = btrfs_insert_inode_ref(trans, dest,
9913 new_dentry->d_name.name,
9914 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009915 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009916 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009917 if (ret)
9918 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009919 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009920
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009921 inode_inc_iversion(old_dir);
9922 inode_inc_iversion(new_dir);
9923 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009924 old_dir->i_ctime = old_dir->i_mtime =
9925 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009926 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009927
Chris Mason12fcfd22009-03-24 10:24:20 -04009928 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009929 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9930 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009931
Li Zefan33345d012011-04-20 10:31:50 +08009932 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009933 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9934 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9935 old_dentry->d_name.name,
9936 old_dentry->d_name.len);
9937 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009938 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9939 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009940 old_dentry->d_name.name,
9941 old_dentry->d_name.len);
9942 if (!ret)
9943 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009944 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009945 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009946 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009947 goto out_fail;
9948 }
Chris Mason39279cc2007-06-12 06:35:45 -04009949
9950 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009951 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009952 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009953 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009954 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9955 root_objectid = BTRFS_I(new_inode)->location.objectid;
9956 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9957 root_objectid,
9958 new_dentry->d_name.name,
9959 new_dentry->d_name.len);
9960 BUG_ON(new_inode->i_nlink == 0);
9961 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009962 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9963 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009964 new_dentry->d_name.name,
9965 new_dentry->d_name.len);
9966 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009967 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009968 ret = btrfs_orphan_add(trans,
9969 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009970 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009971 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009972 goto out_fail;
9973 }
Chris Mason39279cc2007-06-12 06:35:45 -04009974 }
Josef Bacikaec74772008-07-24 12:12:38 -04009975
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009976 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009977 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009978 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009979 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009980 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009981 goto out_fail;
9982 }
Chris Mason39279cc2007-06-12 06:35:45 -04009983
Miao Xie67de1172013-12-26 13:07:06 +08009984 if (old_inode->i_nlink == 1)
9985 BTRFS_I(old_inode)->dir_index = index;
9986
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009987 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009988 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009989
David Sterbaf85b7372017-01-20 14:54:07 +01009990 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9991 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009992 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009993 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009994 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009995
9996 if (flags & RENAME_WHITEOUT) {
9997 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9998 old_dentry);
9999
10000 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010001 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010002 goto out_fail;
10003 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010004 }
Chris Mason39279cc2007-06-12 06:35:45 -040010005out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010006 /*
10007 * If we have pinned the log and an error happened, we unpin tasks
10008 * trying to sync the log and force them to fallback to a transaction
10009 * commit if the log currently contains any of the inodes involved in
10010 * this rename operation (to ensure we do not persist a log with an
10011 * inconsistent state for any of these inodes or leading to any
10012 * inconsistencies when replayed). If the transaction was aborted, the
10013 * abortion reason is propagated to userspace when attempting to commit
10014 * the transaction. If the log does not contain any of these inodes, we
10015 * allow the tasks to sync it.
10016 */
10017 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010018 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10019 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10020 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010021 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010022 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010023 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010024
10025 btrfs_end_log_trans(root);
10026 log_pinned = false;
10027 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010028 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010029out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010030 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010031 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010032
Chris Mason39279cc2007-06-12 06:35:45 -040010033 return ret;
10034}
10035
Miklos Szeredi80ace852014-07-23 15:15:32 +020010036static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10037 struct inode *new_dir, struct dentry *new_dentry,
10038 unsigned int flags)
10039{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010040 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010041 return -EINVAL;
10042
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010043 if (flags & RENAME_EXCHANGE)
10044 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10045 new_dentry);
10046
10047 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010048}
10049
Miao Xie8ccf6f192012-10-25 09:28:04 +000010050static void btrfs_run_delalloc_work(struct btrfs_work *work)
10051{
10052 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010053 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010054
10055 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10056 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010057 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010058 filemap_flush(inode->i_mapping);
10059 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10060 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010061 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062
10063 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010064 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010065 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010066 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010067 complete(&delalloc_work->completion);
10068}
10069
10070struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010071 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010072{
10073 struct btrfs_delalloc_work *work;
10074
David Sterba100d5702015-12-08 14:39:32 +010010075 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076 if (!work)
10077 return NULL;
10078
10079 init_completion(&work->completion);
10080 INIT_LIST_HEAD(&work->list);
10081 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010082 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010083 WARN_ON_ONCE(!inode);
10084 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10085 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010086
10087 return work;
10088}
10089
10090void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10091{
10092 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010093 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010094}
10095
Chris Masond352ac62008-09-29 15:18:18 -040010096/*
10097 * some fairly slow code that needs optimization. This walks the list
10098 * of all the inodes with pending delalloc and forces them to disk.
10099 */
Miao Xie6c255e62014-03-06 13:55:01 +080010100static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10101 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010102{
Chris Masonea8c2812008-08-04 23:17:27 -040010103 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010104 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010105 struct btrfs_delalloc_work *work, *next;
10106 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010107 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010108 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010109
Miao Xie8ccf6f192012-10-25 09:28:04 +000010110 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010111 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010112
Miao Xie573bfb72014-03-06 13:55:03 +080010113 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010114 spin_lock(&root->delalloc_lock);
10115 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010116 while (!list_empty(&splice)) {
10117 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010118 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010119
Miao Xieeb73c1b2013-05-15 07:48:22 +000010120 list_move_tail(&binode->delalloc_inodes,
10121 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010122 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010123 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010125 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010126 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010128
David Sterba651d4942015-11-27 19:24:16 +010010129 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010130 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010131 if (delay_iput)
10132 btrfs_add_delayed_iput(inode);
10133 else
10134 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010135 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010136 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010137 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010138 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010139 btrfs_queue_work(root->fs_info->flush_workers,
10140 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010141 ret++;
10142 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010143 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010144 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010145 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010146 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010147 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010148
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010149out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010150 list_for_each_entry_safe(work, next, &works, list) {
10151 list_del_init(&work->list);
10152 btrfs_wait_and_free_delalloc_work(work);
10153 }
10154
Miao Xieeb73c1b2013-05-15 07:48:22 +000010155 if (!list_empty_careful(&splice)) {
10156 spin_lock(&root->delalloc_lock);
10157 list_splice_tail(&splice, &root->delalloc_inodes);
10158 spin_unlock(&root->delalloc_lock);
10159 }
Miao Xie573bfb72014-03-06 13:55:03 +080010160 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161 return ret;
10162}
10163
10164int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010166 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010167 int ret;
10168
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010169 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 return -EROFS;
10171
Miao Xie6c255e62014-03-06 13:55:01 +080010172 ret = __start_delalloc_inodes(root, delay_iput, -1);
10173 if (ret > 0)
10174 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175 return ret;
10176}
10177
Miao Xie6c255e62014-03-06 13:55:01 +080010178int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10179 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010180{
10181 struct btrfs_root *root;
10182 struct list_head splice;
10183 int ret;
10184
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010185 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010186 return -EROFS;
10187
10188 INIT_LIST_HEAD(&splice);
10189
Miao Xie573bfb72014-03-06 13:55:03 +080010190 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010191 spin_lock(&fs_info->delalloc_root_lock);
10192 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010193 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010194 root = list_first_entry(&splice, struct btrfs_root,
10195 delalloc_root);
10196 root = btrfs_grab_fs_root(root);
10197 BUG_ON(!root);
10198 list_move_tail(&root->delalloc_root,
10199 &fs_info->delalloc_roots);
10200 spin_unlock(&fs_info->delalloc_root_lock);
10201
Miao Xie6c255e62014-03-06 13:55:01 +080010202 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010204 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 goto out;
10206
Miao Xie6c255e62014-03-06 13:55:01 +080010207 if (nr != -1) {
10208 nr -= ret;
10209 WARN_ON(nr < 0);
10210 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211 spin_lock(&fs_info->delalloc_root_lock);
10212 }
10213 spin_unlock(&fs_info->delalloc_root_lock);
10214
Miao Xie6c255e62014-03-06 13:55:01 +080010215 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010216out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010217 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 spin_lock(&fs_info->delalloc_root_lock);
10219 list_splice_tail(&splice, &fs_info->delalloc_roots);
10220 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010221 }
Miao Xie573bfb72014-03-06 13:55:03 +080010222 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010223 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010224}
10225
Chris Mason39279cc2007-06-12 06:35:45 -040010226static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10227 const char *symname)
10228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010229 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010230 struct btrfs_trans_handle *trans;
10231 struct btrfs_root *root = BTRFS_I(dir)->root;
10232 struct btrfs_path *path;
10233 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010234 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010235 int err;
10236 int drop_inode = 0;
10237 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010238 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010239 int name_len;
10240 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010241 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010242 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010243 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010244
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010245 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010246 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010247 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010248
Josef Bacik9ed74f22009-09-11 16:12:44 -040010249 /*
10250 * 2 items for inode item and ref
10251 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010252 * 1 item for updating parent inode item
10253 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010254 * 1 item for xattr if selinux is on
10255 */
Filipe Manana9269d122015-12-31 18:16:29 +000010256 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010257 if (IS_ERR(trans))
10258 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010259
Li Zefan581bb052011-04-20 10:06:11 +080010260 err = btrfs_find_free_ino(root, &objectid);
10261 if (err)
10262 goto out_unlock;
10263
Josef Bacikaec74772008-07-24 12:12:38 -040010264 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010265 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10266 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010267 if (IS_ERR(inode)) {
10268 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010269 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010270 }
Chris Mason39279cc2007-06-12 06:35:45 -040010271
Casey Schauflerad19db72011-12-15 10:09:07 -050010272 /*
10273 * If the active LSM wants to access the inode during
10274 * d_instantiate it needs these. Smack checks to see
10275 * if the filesystem supports xattrs by looking at the
10276 * ops vector.
10277 */
10278 inode->i_fop = &btrfs_file_operations;
10279 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010280 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010281 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10282
10283 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10284 if (err)
10285 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010286
Chris Mason39279cc2007-06-12 06:35:45 -040010287 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010288 if (!path) {
10289 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010290 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010291 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010292 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010293 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010294 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010295 datasize = btrfs_file_extent_calc_inline_size(name_len);
10296 err = btrfs_insert_empty_item(trans, root, path, &key,
10297 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010298 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010299 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010300 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010301 }
Chris Mason5f39d392007-10-15 16:14:19 -040010302 leaf = path->nodes[0];
10303 ei = btrfs_item_ptr(leaf, path->slots[0],
10304 struct btrfs_file_extent_item);
10305 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10306 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010307 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010308 btrfs_set_file_extent_encryption(leaf, ei, 0);
10309 btrfs_set_file_extent_compression(leaf, ei, 0);
10310 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10311 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10312
Chris Mason39279cc2007-06-12 06:35:45 -040010313 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010314 write_extent_buffer(leaf, symname, ptr, name_len);
10315 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010316 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010317
Chris Mason39279cc2007-06-12 06:35:45 -040010318 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010319 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010320 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010321 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010322 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010323 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010324 /*
10325 * Last step, add directory indexes for our symlink inode. This is the
10326 * last step to avoid extra cleanup of these indexes if an error happens
10327 * elsewhere above.
10328 */
10329 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010330 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10331 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010332 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010333 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010334 goto out_unlock_inode;
10335 }
10336
10337 unlock_new_inode(inode);
10338 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010339
10340out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010341 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010342 if (drop_inode) {
10343 inode_dec_link_count(inode);
10344 iput(inode);
10345 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010346 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010347 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010348
10349out_unlock_inode:
10350 drop_inode = 1;
10351 unlock_new_inode(inode);
10352 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010353}
Chris Mason16432982008-04-10 10:23:21 -040010354
Josef Bacik0af3d002010-06-21 14:48:16 -040010355static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10356 u64 start, u64 num_bytes, u64 min_size,
10357 loff_t actual_len, u64 *alloc_hint,
10358 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010359{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010360 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010361 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10362 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010363 struct btrfs_root *root = BTRFS_I(inode)->root;
10364 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010365 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010366 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010367 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010368 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010369 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010370 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010371 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010372
Josef Bacik0af3d002010-06-21 14:48:16 -040010373 if (trans)
10374 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010375 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010376 if (own_trans) {
10377 trans = btrfs_start_transaction(root, 3);
10378 if (IS_ERR(trans)) {
10379 ret = PTR_ERR(trans);
10380 break;
10381 }
Yan Zhengd899e052008-10-30 14:25:28 -040010382 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010383
Byongho Leeee221842015-12-15 01:42:10 +090010384 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010385 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010386 /*
10387 * If we are severely fragmented we could end up with really
10388 * small allocations, so if the allocator is returning small
10389 * chunks lets make its job easier by only searching for those
10390 * sized chunks.
10391 */
10392 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010393 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10394 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010396 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010397 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010398 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010399 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010400 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010401
Josef Bacik0b670dc2015-09-23 17:11:16 -040010402 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010403 ret = insert_reserved_file_extent(trans, inode,
10404 cur_offset, ins.objectid,
10405 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010406 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010407 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010408 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010409 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010410 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010411 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010412 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010413 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010414 break;
10415 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010416
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010417 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010418 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010419
Josef Bacik5dc562c2012-08-17 13:14:17 -040010420 em = alloc_extent_map();
10421 if (!em) {
10422 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10423 &BTRFS_I(inode)->runtime_flags);
10424 goto next;
10425 }
10426
10427 em->start = cur_offset;
10428 em->orig_start = cur_offset;
10429 em->len = ins.offset;
10430 em->block_start = ins.objectid;
10431 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010432 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010433 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010434 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010435 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10436 em->generation = trans->transid;
10437
10438 while (1) {
10439 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010440 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010441 write_unlock(&em_tree->lock);
10442 if (ret != -EEXIST)
10443 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010444 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010445 cur_offset + ins.offset - 1,
10446 0);
10447 }
10448 free_extent_map(em);
10449next:
Yan Zhengd899e052008-10-30 14:25:28 -040010450 num_bytes -= ins.offset;
10451 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010452 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010453
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010454 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010455 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010456 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010457 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010458 (actual_len > inode->i_size) &&
10459 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010460 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010461 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010462 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010463 i_size = cur_offset;
10464 i_size_write(inode, i_size);
10465 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010466 }
10467
Yan Zhengd899e052008-10-30 14:25:28 -040010468 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010469
10470 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010471 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010472 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010473 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010474 break;
10475 }
Yan Zhengd899e052008-10-30 14:25:28 -040010476
Josef Bacik0af3d002010-06-21 14:48:16 -040010477 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010478 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010479 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010480 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010481 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010482 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010483 return ret;
10484}
10485
Josef Bacik0af3d002010-06-21 14:48:16 -040010486int btrfs_prealloc_file_range(struct inode *inode, int mode,
10487 u64 start, u64 num_bytes, u64 min_size,
10488 loff_t actual_len, u64 *alloc_hint)
10489{
10490 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10491 min_size, actual_len, alloc_hint,
10492 NULL);
10493}
10494
10495int btrfs_prealloc_file_range_trans(struct inode *inode,
10496 struct btrfs_trans_handle *trans, int mode,
10497 u64 start, u64 num_bytes, u64 min_size,
10498 loff_t actual_len, u64 *alloc_hint)
10499{
10500 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10501 min_size, actual_len, alloc_hint, trans);
10502}
10503
Chris Masone6dcd2d2008-07-17 12:53:50 -040010504static int btrfs_set_page_dirty(struct page *page)
10505{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010506 return __set_page_dirty_nobuffers(page);
10507}
10508
Al Viro10556cb2011-06-20 19:28:19 -040010509static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010510{
Li Zefanb83cc962010-12-20 16:04:08 +080010511 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010512 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010513
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010514 if (mask & MAY_WRITE &&
10515 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10516 if (btrfs_root_readonly(root))
10517 return -EROFS;
10518 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10519 return -EACCES;
10520 }
Al Viro2830ba72011-06-20 19:16:29 -040010521 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010522}
Chris Mason39279cc2007-06-12 06:35:45 -040010523
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010524static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10525{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010526 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010527 struct btrfs_trans_handle *trans;
10528 struct btrfs_root *root = BTRFS_I(dir)->root;
10529 struct inode *inode = NULL;
10530 u64 objectid;
10531 u64 index;
10532 int ret = 0;
10533
10534 /*
10535 * 5 units required for adding orphan entry
10536 */
10537 trans = btrfs_start_transaction(root, 5);
10538 if (IS_ERR(trans))
10539 return PTR_ERR(trans);
10540
10541 ret = btrfs_find_free_ino(root, &objectid);
10542 if (ret)
10543 goto out;
10544
10545 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010546 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010547 if (IS_ERR(inode)) {
10548 ret = PTR_ERR(inode);
10549 inode = NULL;
10550 goto out;
10551 }
10552
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010553 inode->i_fop = &btrfs_file_operations;
10554 inode->i_op = &btrfs_file_inode_operations;
10555
10556 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010557 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10558
Chris Masonb0d5d102014-09-08 13:08:51 -070010559 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10560 if (ret)
10561 goto out_inode;
10562
10563 ret = btrfs_update_inode(trans, root, inode);
10564 if (ret)
10565 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010566 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010567 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010568 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010569
Filipe Manana5762b5c2014-08-01 00:10:32 +010010570 /*
10571 * We set number of links to 0 in btrfs_new_inode(), and here we set
10572 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10573 * through:
10574 *
10575 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10576 */
10577 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010578 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010579 d_tmpfile(dentry, inode);
10580 mark_inode_dirty(inode);
10581
10582out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010583 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010584 if (ret)
10585 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010586 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010587 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010588
10589out_inode:
10590 unlock_new_inode(inode);
10591 goto out;
10592
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010593}
10594
David Sterba20a7db82017-02-17 16:24:29 +010010595__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010596static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010597{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010598 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010599}
10600
Josef Bacikc6100a42017-05-05 11:57:13 -040010601static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10602{
10603 struct inode *inode = private_data;
10604 return btrfs_sb(inode->i_sb);
10605}
10606
10607static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10608 u64 start, u64 end)
10609{
10610 struct inode *inode = private_data;
10611 u64 isize;
10612
10613 isize = i_size_read(inode);
10614 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10615 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10616 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10617 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10618 }
10619}
10620
10621void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10622{
10623 struct inode *inode = private_data;
10624 unsigned long index = start >> PAGE_SHIFT;
10625 unsigned long end_index = end >> PAGE_SHIFT;
10626 struct page *page;
10627
10628 while (index <= end_index) {
10629 page = find_get_page(inode->i_mapping, index);
10630 ASSERT(page); /* Pages should be in the extent_io_tree */
10631 set_page_writeback(page);
10632 put_page(page);
10633 index++;
10634 }
10635}
10636
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010637static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010638 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010639 .lookup = btrfs_lookup,
10640 .create = btrfs_create,
10641 .unlink = btrfs_unlink,
10642 .link = btrfs_link,
10643 .mkdir = btrfs_mkdir,
10644 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010645 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010646 .symlink = btrfs_symlink,
10647 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010648 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010649 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010650 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010651 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010652 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010653 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010654 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010655};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010656static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010657 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010658 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010659 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010660};
Yan, Zheng76dda932009-09-21 16:00:26 -040010661
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010662static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010663 .llseek = generic_file_llseek,
10664 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010665 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010666 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010667 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010668#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010669 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010670#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010671 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010672 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010673};
10674
David Sterba20e55062015-11-19 11:42:28 +010010675static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010676 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010677 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010678 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010679 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010680 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010681 .tree_fs_info = iotree_fs_info,
10682 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010683
10684 /* optional callbacks */
10685 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010686 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010687 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010688 .set_bit_hook = btrfs_set_bit_hook,
10689 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010690 .merge_extent_hook = btrfs_merge_extent_hook,
10691 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010692 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010693};
10694
Chris Mason35054392009-01-21 13:11:13 -050010695/*
10696 * btrfs doesn't support the bmap operation because swapfiles
10697 * use bmap to make a mapping of extents in the file. They assume
10698 * these extents won't change over the life of the file and they
10699 * use the bmap result to do IO directly to the drive.
10700 *
10701 * the btrfs bmap call would return logical addresses that aren't
10702 * suitable for IO and they also will change frequently as COW
10703 * operations happen. So, swapfile + btrfs == corruption.
10704 *
10705 * For now we're avoiding this by dropping bmap.
10706 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010707static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010708 .readpage = btrfs_readpage,
10709 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010710 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010711 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010712 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010713 .invalidatepage = btrfs_invalidatepage,
10714 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010715 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010716 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010717};
10718
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010719static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010720 .readpage = btrfs_readpage,
10721 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010722 .invalidatepage = btrfs_invalidatepage,
10723 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010724};
10725
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010726static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010727 .getattr = btrfs_getattr,
10728 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010729 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010730 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010731 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010732 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010733 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010734 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010735};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010736static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010737 .getattr = btrfs_getattr,
10738 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010739 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010740 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010741 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010742 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010743 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010744};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010745static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010746 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010747 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010748 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010749 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010750 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010751 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010752};
Yan, Zheng76dda932009-09-21 16:00:26 -040010753
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010754const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010755 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010756 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010757};