blob: e90d7c597ef3a651a15458b8b3108a02065c6d3e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050046#include <linux/iversion.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000062#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080063#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080064#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040065
66struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080067 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040068 struct btrfs_root *root;
69};
70
Filipe Mananaf28a4922015-12-08 19:23:20 +000071struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000072 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080075 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000076};
77
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070078static const struct inode_operations btrfs_dir_inode_operations;
79static const struct inode_operations btrfs_symlink_inode_operations;
80static const struct inode_operations btrfs_dir_ro_inode_operations;
81static const struct inode_operations btrfs_special_inode_operations;
82static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070083static const struct address_space_operations btrfs_aops;
84static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070085static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010086static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040087
88static struct kmem_cache *btrfs_inode_cachep;
89struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Qu Wenruo524272602017-03-08 10:25:52 +0800118static void __endio_write_update_ordered(struct inode *inode,
119 const u64 offset, const u64 bytes,
120 const bool uptodate);
121
122/*
123 * Cleanup all submitted ordered extents in specified range to handle errors
124 * from the fill_dellaloc() callback.
125 *
126 * NOTE: caller must ensure that when an error happens, it can not call
127 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
128 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
129 * to be released, which we want to happen only when finishing the ordered
130 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
131 * fill_delalloc() callback already does proper cleanup for the first page of
132 * the range, that is, it invokes the callback writepage_end_io_hook() for the
133 * range of the first page.
134 */
135static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
136 const u64 offset,
137 const u64 bytes)
138{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900139 unsigned long index = offset >> PAGE_SHIFT;
140 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
141 struct page *page;
142
143 while (index <= end_index) {
144 page = find_get_page(inode->i_mapping, index);
145 index++;
146 if (!page)
147 continue;
148 ClearPagePrivate2(page);
149 put_page(page);
150 }
Qu Wenruo524272602017-03-08 10:25:52 +0800151 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
152 bytes - PAGE_SIZE, false);
153}
154
Eric Sandeen48a3b632013-04-25 20:41:01 +0000155static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400156
Josef Bacik6a3891c2015-03-16 17:38:52 -0400157#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
158void btrfs_test_inode_set_ops(struct inode *inode)
159{
160 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
161}
162#endif
163
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000164static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500165 struct inode *inode, struct inode *dir,
166 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500167{
168 int err;
169
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000170 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500171 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500172 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500173 return err;
174}
175
Chris Masond352ac62008-09-29 15:18:18 -0400176/*
Chris Masonc8b97812008-10-29 14:49:59 -0400177 * this does all the hard work for inserting an inline extent into
178 * the btree. The caller should have done a btrfs_drop_extents so that
179 * no overlapping inline items exist in the btree
180 */
Chris Mason40f76582014-05-21 13:35:51 -0700181static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000182 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 struct btrfs_root *root, struct inode *inode,
184 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400186 struct page **compressed_pages)
187{
Chris Masonc8b97812008-10-29 14:49:59 -0400188 struct extent_buffer *leaf;
189 struct page *page = NULL;
190 char *kaddr;
191 unsigned long ptr;
192 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400193 int ret;
194 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400195 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400196
Li Zefanfe3f5662011-03-28 08:30:38 +0000197 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400198 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400199
Chris Masonc8b97812008-10-29 14:49:59 -0400200 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201
202 if (!extent_inserted) {
203 struct btrfs_key key;
204 size_t datasize;
205
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200206 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000207 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200208 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000209
210 datasize = btrfs_file_extent_calc_inline_size(cur_size);
211 path->leave_spinning = 1;
212 ret = btrfs_insert_empty_item(trans, root, path, &key,
213 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200214 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000215 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 }
217 leaf = path->nodes[0];
218 ei = btrfs_item_ptr(leaf, path->slots[0],
219 struct btrfs_file_extent_item);
220 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
221 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
222 btrfs_set_file_extent_encryption(leaf, ei, 0);
223 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
224 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
225 ptr = btrfs_file_extent_inline_start(ei);
226
Li Zefan261507a02010-12-17 14:21:50 +0800227 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400228 struct page *cpage;
229 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500230 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400231 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500232 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400234
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400238
239 i++;
240 ptr += cur_size;
241 compressed_size -= cur_size;
242 }
243 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800244 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 } else {
246 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300247 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400248 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800249 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300250 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400251 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800252 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300253 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400254 }
255 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000256 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Yan, Zhengc2167752009-11-12 09:34:21 +0000258 /*
259 * we're an inline extent, so nobody can
260 * extend the file past i_size without locking
261 * a page we already have locked.
262 *
263 * We must do any isize and inode updates
264 * before we unlock the pages. Otherwise we
265 * could end up racing with unlink.
266 */
Chris Masonc8b97812008-10-29 14:49:59 -0400267 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000269
Chris Masonc8b97812008-10-29 14:49:59 -0400270fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200271 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400272}
273
274
275/*
276 * conditionally insert an inline extent into the file. This
277 * does the checks required to make sure the data is small enough
278 * to fit as an inline extent.
279 */
Josef Bacik00361582013-08-14 14:02:47 -0400280static noinline int cow_file_range_inline(struct btrfs_root *root,
281 struct inode *inode, u64 start,
282 u64 end, size_t compressed_size,
283 int compress_type,
284 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400287 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 isize = i_size_read(inode);
289 u64 actual_end = min(end + 1, isize);
290 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400291 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400292 u64 data_len = inline_len;
293 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000294 struct btrfs_path *path;
295 int extent_inserted = 0;
296 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400297
298 if (compressed_size)
299 data_len = compressed_size;
300
301 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 actual_end > fs_info->sectorsize ||
303 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400304 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400305 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400306 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400307 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400308 return 1;
309 }
310
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 path = btrfs_alloc_path();
312 if (!path)
313 return -ENOMEM;
314
Josef Bacik00361582013-08-14 14:02:47 -0400315 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000316 if (IS_ERR(trans)) {
317 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400318 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000319 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400320 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400321
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000322 if (compressed_size && compressed_pages)
323 extent_item_size = btrfs_file_extent_calc_inline_size(
324 compressed_size);
325 else
326 extent_item_size = btrfs_file_extent_calc_inline_size(
327 inline_len);
328
329 ret = __btrfs_drop_extents(trans, root, inode, path,
330 start, aligned_end, NULL,
331 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400332 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400333 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400334 goto out;
335 }
Chris Masonc8b97812008-10-29 14:49:59 -0400336
337 if (isize > actual_end)
338 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000339 ret = insert_inline_extent(trans, path, extent_inserted,
340 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400341 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000342 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400343 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400344 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400345 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400346 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400347 ret = 1;
348 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100349 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400350
Josef Bacikbdc20e62013-02-28 13:23:38 -0500351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200352 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400353out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800354 /*
355 * Don't forget to free the reserved space, as for inlined extent
356 * it won't count as data extent, free them directly here.
357 * And at reserve time, it's always aligned to page size, so
358 * just free one page here.
359 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800360 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000361 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400362 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400363 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400364}
365
Chris Mason771ed682008-11-06 22:02:51 -0500366struct async_extent {
367 u64 start;
368 u64 ram_size;
369 u64 compressed_size;
370 struct page **pages;
371 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800372 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct list_head list;
374};
375
376struct async_cow {
377 struct inode *inode;
378 struct btrfs_root *root;
379 struct page *locked_page;
380 u64 start;
381 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600382 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500383 struct list_head extents;
384 struct btrfs_work work;
385};
386
387static noinline int add_async_extent(struct async_cow *cow,
388 u64 start, u64 ram_size,
389 u64 compressed_size,
390 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800391 unsigned long nr_pages,
392 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500393{
394 struct async_extent *async_extent;
395
396 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100397 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500398 async_extent->start = start;
399 async_extent->ram_size = ram_size;
400 async_extent->compressed_size = compressed_size;
401 async_extent->pages = pages;
402 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800403 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500404 list_add_tail(&async_extent->list, &cow->extents);
405 return 0;
406}
407
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300408static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800409{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800411
412 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400413 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800414 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200415 /* defrag ioctl */
416 if (BTRFS_I(inode)->defrag_compress)
417 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800418 /* bad compression ratios */
419 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
420 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400421 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800422 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200423 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300424 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800425 return 0;
426}
427
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200428static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800429 u64 start, u64 end, u64 num_bytes, u64 small_write)
430{
431 /* If this is a small write inside eof, kick off a defrag */
432 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200433 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800434 btrfs_add_inode_defrag(NULL, inode);
435}
436
Chris Masonc8b97812008-10-29 14:49:59 -0400437/*
Chris Mason771ed682008-11-06 22:02:51 -0500438 * we create compressed extents in two phases. The first
439 * phase compresses a range of pages that have already been
440 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400441 *
Chris Mason771ed682008-11-06 22:02:51 -0500442 * This is done inside an ordered work queue, and the compression
443 * is spread across many cpus. The actual IO submission is step
444 * two, and the ordered work queue takes care of making sure that
445 * happens in the same order things were put onto the queue by
446 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400447 *
Chris Mason771ed682008-11-06 22:02:51 -0500448 * If this code finds it can't get good compression, it puts an
449 * entry onto the work queue to write the uncompressed bytes. This
450 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300451 * are written in the same order that the flusher thread sent them
452 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400453 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100454static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500455 struct page *locked_page,
456 u64 start, u64 end,
457 struct async_cow *async_cow,
458 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400461 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400463 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500464 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400465 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400466 struct page **pages = NULL;
467 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400468 unsigned long total_compressed = 0;
469 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400470 int i;
471 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400473 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400474
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200475 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
476 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400477
Chris Mason42dc7ba2008-12-15 11:44:56 -0500478 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400479again:
480 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300481 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100482 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
483 nr_pages = min_t(unsigned long, nr_pages,
484 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400485
Chris Masonf03d9301f2009-02-04 09:31:06 -0500486 /*
487 * we don't want to send crud past the end of i_size through
488 * compression, that's just a waste of CPU time. So, if the
489 * end of the file is before the start of our current
490 * requested range of bytes, we bail out to the uncompressed
491 * cleanup code that can deal with all of this.
492 *
493 * It isn't really the fastest way to fix things, but this is a
494 * very uncommon corner.
495 */
496 if (actual_end <= start)
497 goto cleanup_and_bail_uncompressed;
498
Chris Masonc8b97812008-10-29 14:49:59 -0400499 total_compressed = actual_end - start;
500
Shilong Wang4bcbb332014-10-07 18:44:35 -0400501 /*
502 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400503 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400504 */
505 if (total_compressed <= blocksize &&
506 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
507 goto cleanup_and_bail_uncompressed;
508
David Sterba069eac72017-02-14 19:36:54 +0100509 total_compressed = min_t(unsigned long, total_compressed,
510 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400511 total_in = 0;
512 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400513
Chris Mason771ed682008-11-06 22:02:51 -0500514 /*
515 * we do compression for mount -o compress and when the
516 * inode has not been flagged as nocompress. This flag can
517 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300519 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400520 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100521 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800522 if (!pages) {
523 /* just bail out to the uncompressed code */
524 goto cont;
525 }
Chris Mason179e29e2007-11-01 11:28:41 -0400526
David Sterbaeec63c62017-07-17 19:41:31 +0200527 if (BTRFS_I(inode)->defrag_compress)
528 compress_type = BTRFS_I(inode)->defrag_compress;
529 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200530 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800531
Chris Mason4adaa612013-03-26 13:07:00 -0400532 /*
533 * we need to call clear_page_dirty_for_io on each
534 * page in the range. Otherwise applications with the file
535 * mmap'd can wander in and change the page contents while
536 * we are compressing them.
537 *
538 * If the compression fails for any reason, we set the pages
539 * dirty again later on.
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;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400965 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500966 struct btrfs_key ins;
967 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000968 unsigned clear_bits;
969 unsigned long page_ops;
970 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500971 int ret = 0;
972
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200973 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500975 ret = -EINVAL;
976 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400977 }
Chris Mason771ed682008-11-06 22:02:51 -0500978
Qu Wenruofda28322013-02-26 08:10:22 +0000979 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500980 num_bytes = max(blocksize, num_bytes);
Chris Mason771ed682008-11-06 22:02:51 -0500981
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200982 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400983
Chris Mason771ed682008-11-06 22:02:51 -0500984 if (start == 0) {
985 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800986 ret = cow_file_range_inline(root, inode, start, end, 0,
987 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500988 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400989 /*
990 * We use DO_ACCOUNTING here because we need the
991 * delalloc_release_metadata to be run _after_ we drop
992 * our outstanding extent for clearing delalloc for this
993 * range.
994 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800995 extent_clear_unlock_delalloc(inode, start, end,
996 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400998 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
999 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001000 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1001 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001002 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001003 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001004 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001005 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001008 }
1009 }
Chris Masonc8b97812008-10-29 14:49:59 -04001010
Anand Jain3752d222018-02-15 12:29:38 +08001011 BUG_ON(num_bytes > btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Josef Bacik4b46fce2010-05-23 11:00:55 -04001013 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001014 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1015 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001016
Anand Jain3752d222018-02-15 12:29:38 +08001017 while (num_bytes > 0) {
1018 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001019 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001020 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001021 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001022 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001023 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001024 cur_alloc_size = ins.offset;
1025 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001026
Chris Mason771ed682008-11-06 22:02:51 -05001027 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001028 em = create_io_em(inode, start, ins.offset, /* len */
1029 start, /* orig_start */
1030 ins.objectid, /* block_start */
1031 ins.offset, /* block_len */
1032 ins.offset, /* orig_block_len */
1033 ram_size, /* ram_bytes */
1034 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001035 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001037 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001039
Chris Masone6dcd2d2008-07-17 12:53:50 -04001040 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001041 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001042 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001043 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001044
Yan Zheng17d217f2008-12-12 10:03:38 -05001045 if (root->root_key.objectid ==
1046 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1047 ret = btrfs_reloc_clone_csums(inode, start,
1048 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001049 /*
1050 * Only drop cache here, and process as normal.
1051 *
1052 * We must not allow extent_clear_unlock_delalloc()
1053 * at out_unlock label to free meta of this ordered
1054 * extent, as its meta should be freed by
1055 * btrfs_finish_ordered_io().
1056 *
1057 * So we must continue until @start is increased to
1058 * skip current ordered extent.
1059 */
Josef Bacik00361582013-08-14 14:02:47 -04001060 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001061 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1062 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001063 }
1064
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001065 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001066
Chris Masonc8b97812008-10-29 14:49:59 -04001067 /* we're not doing compressed IO, don't unlock the first
1068 * page (which the caller expects to stay locked), don't
1069 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001070 *
1071 * Do set the Private2 bit so we know this page was properly
1072 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001073 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001074 page_ops = unlock ? PAGE_UNLOCK : 0;
1075 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001076
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001078 start + ram_size - 1,
1079 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001080 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001081 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001082 if (num_bytes < cur_alloc_size)
1083 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001084 else
Anand Jain3752d222018-02-15 12:29:38 +08001085 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001086 alloc_hint = ins.objectid + ins.offset;
1087 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001088 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001089
1090 /*
1091 * btrfs_reloc_clone_csums() error, since start is increased
1092 * extent_clear_unlock_delalloc() at out_unlock label won't
1093 * free metadata of current ordered extent, we're OK to exit.
1094 */
1095 if (ret)
1096 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001097 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001098out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001099 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001100
Filipe Mananad9f85962014-08-25 10:43:00 +01001101out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001102 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001103out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001104 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001105 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001107 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1108 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001109 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1110 PAGE_END_WRITEBACK;
1111 /*
1112 * If we reserved an extent for our delalloc range (or a subrange) and
1113 * failed to create the respective ordered extent, then it means that
1114 * when we reserved the extent we decremented the extent's size from
1115 * the data space_info's bytes_may_use counter and incremented the
1116 * space_info's bytes_reserved counter by the same amount. We must make
1117 * sure extent_clear_unlock_delalloc() does not try to decrement again
1118 * the data space_info's bytes_may_use counter, therefore we do not pass
1119 * it the flag EXTENT_CLEAR_DATA_RESV.
1120 */
1121 if (extent_reserved) {
1122 extent_clear_unlock_delalloc(inode, start,
1123 start + cur_alloc_size,
1124 start + cur_alloc_size,
1125 locked_page,
1126 clear_bits,
1127 page_ops);
1128 start += cur_alloc_size;
1129 if (start >= end)
1130 goto out;
1131 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001132 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1133 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001134 clear_bits | EXTENT_CLEAR_DATA_RESV,
1135 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001136 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001137}
Chris Masonc8b97812008-10-29 14:49:59 -04001138
Chris Mason771ed682008-11-06 22:02:51 -05001139/*
1140 * work queue call back to started compression on a file and pages
1141 */
1142static noinline void async_cow_start(struct btrfs_work *work)
1143{
1144 struct async_cow *async_cow;
1145 int num_added = 0;
1146 async_cow = container_of(work, struct async_cow, work);
1147
1148 compress_file_range(async_cow->inode, async_cow->locked_page,
1149 async_cow->start, async_cow->end, async_cow,
1150 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001151 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001152 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001153 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001154 }
Chris Mason771ed682008-11-06 22:02:51 -05001155}
1156
1157/*
1158 * work queue call back to submit previously compressed pages
1159 */
1160static noinline void async_cow_submit(struct btrfs_work *work)
1161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001163 struct async_cow *async_cow;
1164 struct btrfs_root *root;
1165 unsigned long nr_pages;
1166
1167 async_cow = container_of(work, struct async_cow, work);
1168
1169 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001170 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001171 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1172 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001173
David Sterbaee863952015-02-16 19:41:40 +01001174 /*
1175 * atomic_sub_return implies a barrier for waitqueue_active
1176 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001177 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001178 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001179 waitqueue_active(&fs_info->async_submit_wait))
1180 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001181
Chris Masond3977122009-01-05 21:25:51 -05001182 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001183 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001184}
Chris Masonc8b97812008-10-29 14:49:59 -04001185
Chris Mason771ed682008-11-06 22:02:51 -05001186static noinline void async_cow_free(struct btrfs_work *work)
1187{
1188 struct async_cow *async_cow;
1189 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001190 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001191 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001192 kfree(async_cow);
1193}
1194
1195static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1196 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001197 unsigned long *nr_written,
1198 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001200 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001201 struct async_cow *async_cow;
1202 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 unsigned long nr_pages;
1204 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001205
Chris Masona3429ab2009-10-08 12:30:20 -04001206 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001207 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001208 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001209 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001210 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001211 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001212 async_cow->root = root;
1213 async_cow->locked_page = locked_page;
1214 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001215 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001216
Wang Shilongf79707b2014-07-17 11:44:09 +08001217 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001218 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001219 cur_end = end;
1220 else
Byongho Leeee221842015-12-15 01:42:10 +09001221 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001222
1223 async_cow->end = cur_end;
1224 INIT_LIST_HEAD(&async_cow->extents);
1225
Liu Bo9e0af232014-08-15 23:36:53 +08001226 btrfs_init_work(&async_cow->work,
1227 btrfs_delalloc_helper,
1228 async_cow_start, async_cow_submit,
1229 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001230
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 nr_pages = (cur_end - start + PAGE_SIZE) >>
1232 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001233 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001235 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001236
Chris Mason771ed682008-11-06 22:02:51 -05001237 *nr_written += nr_pages;
1238 start = cur_end + 1;
1239 }
1240 *page_started = 1;
1241 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001242}
1243
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001244static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001245 u64 bytenr, u64 num_bytes)
1246{
1247 int ret;
1248 struct btrfs_ordered_sum *sums;
1249 LIST_HEAD(list);
1250
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001251 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001252 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 if (ret == 0 && list_empty(&list))
1254 return 0;
1255
1256 while (!list_empty(&list)) {
1257 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1258 list_del(&sums->list);
1259 kfree(sums);
1260 }
1261 return 1;
1262}
1263
Chris Masond352ac62008-09-29 15:18:18 -04001264/*
1265 * when nowcow writeback call back. This checks for snapshots or COW copies
1266 * of the extents that exist in the file, and COWs the file as required.
1267 *
1268 * If no cow copies or snapshots exist, we write directly to the existing
1269 * blocks on disk
1270 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001271static noinline int run_delalloc_nocow(struct inode *inode,
1272 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001273 u64 start, u64 end, int *page_started, int force,
1274 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001277 struct btrfs_root *root = BTRFS_I(inode)->root;
1278 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001279 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001281 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001282 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 u64 cow_start;
1284 u64 cur_offset;
1285 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 u64 disk_bytenr;
1288 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001289 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001290 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001292 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001293 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int nocow;
1295 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001296 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001297 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001298
1299 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001300 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001301 extent_clear_unlock_delalloc(inode, start, end, end,
1302 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001303 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001304 EXTENT_DO_ACCOUNTING |
1305 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 PAGE_CLEAR_DIRTY |
1307 PAGE_SET_WRITEBACK |
1308 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001309 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001310 }
Li Zefan82d59022011-04-20 10:33:24 +08001311
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001312 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001313
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 cow_start = (u64)-1;
1315 cur_offset = start;
1316 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001317 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001319 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001320 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1322 leaf = path->nodes[0];
1323 btrfs_item_key_to_cpu(leaf, &found_key,
1324 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001325 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 found_key.type == BTRFS_EXTENT_DATA_KEY)
1327 path->slots[0]--;
1328 }
1329 check_prev = 0;
1330next_slot:
1331 leaf = path->nodes[0];
1332 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1333 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001334 if (ret < 0) {
1335 if (cow_start != (u64)-1)
1336 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001337 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001338 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (ret > 0)
1340 break;
1341 leaf = path->nodes[0];
1342 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 0;
1345 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001346 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Filipe Manana1d512cb2015-11-09 00:33:58 +00001349 if (found_key.objectid > ino)
1350 break;
1351 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1352 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1353 path->slots[0]++;
1354 goto next_slot;
1355 }
1356 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 found_key.offset > end)
1358 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (found_key.offset > cur_offset) {
1361 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001362 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 goto out_check;
1364 }
Chris Masonc31f8832008-01-08 15:46:31 -05001365
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 fi = btrfs_item_ptr(leaf, path->slots[0],
1367 struct btrfs_file_extent_item);
1368 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
Josef Bacikcc95bef2013-04-04 14:31:27 -04001370 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1372 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 extent_end = found_key.offset +
1376 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001377 disk_num_bytes =
1378 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (extent_end <= start) {
1380 path->slots[0]++;
1381 goto next_slot;
1382 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 if (disk_bytenr == 0)
1384 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (btrfs_file_extent_compression(leaf, fi) ||
1386 btrfs_file_extent_encryption(leaf, fi) ||
1387 btrfs_file_extent_other_encoding(leaf, fi))
1388 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001391 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001393 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 found_key.offset -
1395 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001396 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 disk_bytenr += cur_offset - found_key.offset;
1399 num_bytes = min(end + 1, extent_end) - cur_offset;
1400 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 * if there are pending snapshots for this root,
1402 * we fall into common COW way.
1403 */
1404 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001405 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001406 if (!err)
1407 goto out_check;
1408 }
1409 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 * force cow if csum exists in the range.
1411 * this ensure that csum for a given extent are
1412 * either valid or do not exist.
1413 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001414 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 num_bytes)) {
1416 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001418 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 }
1420 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1421 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001422 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001423 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 nocow = 1;
1426 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1427 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001428 btrfs_file_extent_inline_len(leaf,
1429 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001430 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 } else {
1433 BUG_ON(1);
1434 }
1435out_check:
1436 if (extent_end <= start) {
1437 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001438 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001439 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001440 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001441 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 goto next_slot;
1443 }
1444 if (!nocow) {
1445 if (cow_start == (u64)-1)
1446 cow_start = cur_offset;
1447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
1450 path->slots[0]++;
1451 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001452 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001453
David Sterbab3b4aa72011-04-21 01:20:15 +02001454 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001456 ret = cow_file_range(inode, locked_page,
1457 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001458 end, page_started, nr_written, 1,
1459 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001460 if (ret) {
1461 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001462 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001463 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001465 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001469 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001470
Yan Zhengd899e052008-10-30 14:25:28 -04001471 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001472 u64 orig_start = found_key.offset - extent_offset;
1473
1474 em = create_io_em(inode, cur_offset, num_bytes,
1475 orig_start,
1476 disk_bytenr, /* block_start */
1477 num_bytes, /* block_len */
1478 disk_num_bytes, /* orig_block_len */
1479 ram_bytes, BTRFS_COMPRESS_NONE,
1480 BTRFS_ORDERED_PREALLOC);
1481 if (IS_ERR(em)) {
1482 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001483 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001484 if (nocow)
1485 btrfs_dec_nocow_writers(fs_info,
1486 disk_bytenr);
1487 ret = PTR_ERR(em);
1488 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001489 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001490 free_extent_map(em);
1491 }
1492
1493 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001494 type = BTRFS_ORDERED_PREALLOC;
1495 } else {
1496 type = BTRFS_ORDERED_NOCOW;
1497 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001498
1499 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001500 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001501 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001503 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001504
Yan, Zhengefa56462010-05-16 10:49:59 -04001505 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001506 BTRFS_DATA_RELOC_TREE_OBJECTID)
1507 /*
1508 * Error handled later, as we must prevent
1509 * extent_clear_unlock_delalloc() in error handler
1510 * from freeing metadata of created ordered extent.
1511 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001512 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1513 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001514
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001516 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001517 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001518 EXTENT_DELALLOC |
1519 EXTENT_CLEAR_DATA_RESV,
1520 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1521
Wang Shilonge9894fd2014-03-27 11:12:25 +08001522 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001523 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001525
1526 /*
1527 * btrfs_reloc_clone_csums() error, now we're OK to call error
1528 * handler, as metadata for created ordered extent will only
1529 * be freed by btrfs_finish_ordered_io().
1530 */
1531 if (ret)
1532 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 if (cur_offset > end)
1534 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001535 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001536 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001537
Josef Bacik17ca04a2012-05-31 15:58:55 -04001538 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001540 cur_offset = end;
1541 }
1542
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001544 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1545 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001546 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001547 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 }
1549
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001551 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001552 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001554 EXTENT_DELALLOC | EXTENT_DEFRAG |
1555 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1556 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001557 PAGE_SET_WRITEBACK |
1558 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001559 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001560 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001561}
1562
Wang Shilong47059d92014-07-03 18:22:07 +08001563static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1564{
1565
1566 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1567 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1568 return 0;
1569
1570 /*
1571 * @defrag_bytes is a hint value, no spinlock held here,
1572 * if is not zero, it means the file is defragging.
1573 * Force cow if given extent needs to be defragged.
1574 */
1575 if (BTRFS_I(inode)->defrag_bytes &&
1576 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1577 EXTENT_DEFRAG, 0, NULL))
1578 return 1;
1579
1580 return 0;
1581}
1582
Chris Masond352ac62008-09-29 15:18:18 -04001583/*
1584 * extent_io.c call back to do delayed allocation processing
1585 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001586static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001587 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001588 unsigned long *nr_written,
1589 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001590{
Josef Bacikc6100a42017-05-05 11:57:13 -04001591 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001592 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001593 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001594 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001595
Wang Shilong47059d92014-07-03 18:22:07 +08001596 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001597 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001598 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001599 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001600 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001601 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001602 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001603 ret = cow_file_range(inode, locked_page, start, end, end,
1604 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001605 } else {
1606 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1607 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001608 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001609 page_started, nr_written,
1610 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001611 }
Qu Wenruo524272602017-03-08 10:25:52 +08001612 if (ret)
1613 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001614 return ret;
1615}
1616
Josef Bacikc6100a42017-05-05 11:57:13 -04001617static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001618 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001619{
Josef Bacikc6100a42017-05-05 11:57:13 -04001620 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 size;
1622
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001625 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 size = orig->end - orig->start + 1;
1628 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001629 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 new_size;
1631
1632 /*
Josef Bacikba117212015-03-13 15:01:24 -04001633 * See the explanation in btrfs_merge_extent_hook, the same
1634 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 */
1636 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001637 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001638 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001639 num_extents += count_max_extents(new_size);
1640 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 return;
1642 }
1643
Josef Bacik9e0baf62011-07-15 15:16:44 +00001644 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001645 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001646 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647}
1648
1649/*
1650 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1651 * extents so we can keep track of new extents that are just merged onto old
1652 * extents, such as when we are doing sequential writes, so we can properly
1653 * account for the metadata space we'll need.
1654 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001655static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001656 struct extent_state *new,
1657 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658{
Josef Bacikc6100a42017-05-05 11:57:13 -04001659 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001661 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663 /* not delalloc, ignore it */
1664 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001665 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001666
Josef Bacik8461a3d2015-03-13 15:12:08 -04001667 if (new->start > other->start)
1668 new_size = new->end - other->start + 1;
1669 else
1670 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671
1672 /* we're not bigger than the max, unreserve the space and go */
1673 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1674 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001675 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 spin_unlock(&BTRFS_I(inode)->lock);
1677 return;
1678 }
1679
1680 /*
Josef Bacikba117212015-03-13 15:01:24 -04001681 * We have to add up either side to figure out how many extents were
1682 * accounted for before we merged into one big extent. If the number of
1683 * extents we accounted for is <= the amount we need for the new range
1684 * then we can return, otherwise drop. Think of it like this
1685 *
1686 * [ 4k][MAX_SIZE]
1687 *
1688 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1689 * need 2 outstanding extents, on one side we have 1 and the other side
1690 * we have 1 so they are == and we can return. But in this case
1691 *
1692 * [MAX_SIZE+4k][MAX_SIZE+4k]
1693 *
1694 * Each range on their own accounts for 2 extents, but merged together
1695 * they are only 3 extents worth of accounting, so we need to drop in
1696 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001697 */
Josef Bacikba117212015-03-13 15:01:24 -04001698 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001699 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001700 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001701 num_extents += count_max_extents(old_size);
1702 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001703 return;
1704
Josef Bacik9e0baf62011-07-15 15:16:44 +00001705 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001706 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001707 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001708}
1709
Miao Xieeb73c1b2013-05-15 07:48:22 +00001710static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1711 struct inode *inode)
1712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1714
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 spin_lock(&root->delalloc_lock);
1716 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1717 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1718 &root->delalloc_inodes);
1719 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1720 &BTRFS_I(inode)->runtime_flags);
1721 root->nr_delalloc_inodes++;
1722 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 BUG_ON(!list_empty(&root->delalloc_root));
1725 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 &fs_info->delalloc_roots);
1727 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 }
1729 }
1730 spin_unlock(&root->delalloc_lock);
1731}
1732
1733static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001734 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 if (!list_empty(&inode->delalloc_inodes)) {
1740 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001742 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001743 root->nr_delalloc_inodes--;
1744 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 BUG_ON(list_empty(&root->delalloc_root));
1747 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 }
1750 }
1751 spin_unlock(&root->delalloc_lock);
1752}
1753
Chris Masond352ac62008-09-29 15:18:18 -04001754/*
1755 * extent_io.c set_bit_hook, used to track delayed allocation
1756 * bytes in this file, and to maintain the list of inodes that
1757 * have pending delalloc work to be done.
1758 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001759static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001760 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001761{
Josef Bacikc6100a42017-05-05 11:57:13 -04001762 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001763
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1765
Wang Shilong47059d92014-07-03 18:22:07 +08001766 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1767 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001768 /*
1769 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001770 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001771 * bit, which is only set or cleared with irqs on
1772 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001773 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001774 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001776 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001777 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778
Josef Bacik8b62f872017-10-19 14:15:55 -04001779 spin_lock(&BTRFS_I(inode)->lock);
1780 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1781 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001782
Josef Bacik6a3891c2015-03-16 17:38:52 -04001783 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001785 return;
1786
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001787 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1788 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001790 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001791 if (*bits & EXTENT_DEFRAG)
1792 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001793 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001794 &BTRFS_I(inode)->runtime_flags))
1795 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001796 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001797 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001798
1799 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1800 (*bits & EXTENT_DELALLOC_NEW)) {
1801 spin_lock(&BTRFS_I(inode)->lock);
1802 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1803 state->start;
1804 spin_unlock(&BTRFS_I(inode)->lock);
1805 }
Chris Mason291d6732008-01-29 15:55:23 -05001806}
1807
Chris Masond352ac62008-09-29 15:18:18 -04001808/*
1809 * extent_io.c clear_bit_hook, see set_bit_hook for why
1810 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001811static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001812 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001813 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001814{
Josef Bacikc6100a42017-05-05 11:57:13 -04001815 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001817 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001818 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001819
Filipe Manana4a4b9642017-07-27 19:52:55 +01001820 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1821 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001822 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001823 spin_unlock(&inode->lock);
1824 }
Wang Shilong47059d92014-07-03 18:22:07 +08001825
Chris Mason75eff682008-12-15 15:54:40 -05001826 /*
1827 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001828 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001829 * bit, which is only set or cleared with irqs on
1830 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001831 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001832 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001833 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001834
Josef Bacik8b62f872017-10-19 14:15:55 -04001835 spin_lock(&inode->lock);
1836 btrfs_mod_outstanding_extents(inode, -num_extents);
1837 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838
Josef Bacikb6d08f02013-09-27 14:57:43 -04001839 /*
1840 * We don't reserve metadata space for space cache inodes so we
1841 * don't need to call dellalloc_release_metadata if there is an
1842 * error.
1843 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001844 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001846 btrfs_delalloc_release_metadata(inode, len);
1847
Josef Bacik6a3891c2015-03-16 17:38:52 -04001848 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001850 return;
1851
Filipe Mananaa315e682017-03-06 23:04:20 +00001852 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1853 do_list && !(state->state & EXTENT_NORESERVE) &&
1854 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 btrfs_free_reserved_data_space_noquota(
1856 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001857 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001859 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1860 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001861 spin_lock(&inode->lock);
1862 inode->delalloc_bytes -= len;
1863 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001864 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001865 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001868 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001869
1870 if ((state->state & EXTENT_DELALLOC_NEW) &&
1871 (*bits & EXTENT_DELALLOC_NEW)) {
1872 spin_lock(&inode->lock);
1873 ASSERT(inode->new_delalloc_bytes >= len);
1874 inode->new_delalloc_bytes -= len;
1875 spin_unlock(&inode->lock);
1876 }
Chris Mason291d6732008-01-29 15:55:23 -05001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
1880 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1881 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001882 *
1883 * return 1 if page cannot be merged to bio
1884 * return 0 if page can be merged to bio
1885 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001886 */
Mike Christie81a75f672016-06-05 14:31:54 -05001887int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001888 size_t size, struct bio *bio,
1889 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 struct inode *inode = page->mapping->host;
1892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001893 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001894 u64 length = 0;
1895 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001896 int ret;
1897
Chris Mason771ed682008-11-06 22:02:51 -05001898 if (bio_flags & EXTENT_BIO_COMPRESSED)
1899 return 0;
1900
Kent Overstreet4f024f32013-10-11 15:44:27 -07001901 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001902 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001903 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1904 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001905 if (ret < 0)
1906 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001907 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001908 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001909 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
1913 * in order to insert checksums into the metadata in large chunks,
1914 * we wait until bio submission time. All the pages in the bio are
1915 * checksummed and sums are attached onto the ordered extent record.
1916 *
1917 * At IO completion time the cums attached on the ordered extent record
1918 * are inserted into the btree
1919 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001920static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001921 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001922 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001923{
Josef Bacikc6100a42017-05-05 11:57:13 -04001924 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001925 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001926
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001929 return 0;
1930}
Chris Masone0156402008-04-16 11:15:20 -04001931
Chris Mason4a69a412008-11-06 22:03:00 -05001932/*
1933 * in order to insert checksums into the metadata in large chunks,
1934 * we wait until bio submission time. All the pages in the bio are
1935 * checksummed and sums are attached onto the ordered extent record.
1936 *
1937 * At IO completion time the cums attached on the ordered extent record
1938 * are inserted into the btree
1939 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001940static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001941 int mirror_num, unsigned long bio_flags,
1942 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001943{
Josef Bacikc6100a42017-05-05 11:57:13 -04001944 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001948 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001949 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001951 bio_endio(bio);
1952 }
Stefan Behrens61891922012-11-05 18:51:52 +01001953 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001954}
1955
Chris Masond352ac62008-09-29 15:18:18 -04001956/*
Chris Masoncad321a2008-12-17 14:51:42 -05001957 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001958 * on write, or reading the csums from the tree before a read.
1959 *
1960 * Rules about async/sync submit,
1961 * a) read: sync submit
1962 *
1963 * b) write without checksum: sync submit
1964 *
1965 * c) write with checksum:
1966 * c-1) if bio is issued by fsync: sync submit
1967 * (sync_writers != 0)
1968 *
1969 * c-2) if root is reloc root: sync submit
1970 * (only in case of buffered IO)
1971 *
1972 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001973 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001974static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001975 int mirror_num, unsigned long bio_flags,
1976 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001977{
Josef Bacikc6100a42017-05-05 11:57:13 -04001978 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001980 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301981 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001982 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001983 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001984 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001985
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001986 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001987
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001988 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301989 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001990
Mike Christie37226b22016-06-05 14:31:52 -05001991 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001993 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001994 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001995
Chris Masond20f7042008-12-08 16:58:54 -05001996 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001997 ret = btrfs_submit_compressed_read(inode, bio,
1998 mirror_num,
1999 bio_flags);
2000 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002001 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002002 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002003 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002004 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002005 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002006 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002007 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002008 /* csum items have already been cloned */
2009 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2010 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002011 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002012 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2013 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 __btrfs_submit_bio_start,
2015 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002016 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002017 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002018 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002019 if (ret)
2020 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002021 }
2022
Chris Mason0b86a832008-03-24 15:01:56 -04002023mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002024 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002025
2026out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002027 if (ret) {
2028 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002029 bio_endio(bio);
2030 }
Stefan Behrens61891922012-11-05 18:51:52 +01002031 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002032}
Chris Mason6885f302008-02-20 16:11:05 -05002033
Chris Masond352ac62008-09-29 15:18:18 -04002034/*
2035 * given a list of ordered sums record them in the inode. This happens
2036 * at IO completion time based on sums calculated at bio submission time.
2037 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002038static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002039 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002040{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002041 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002042 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002043
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002044 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002045 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002046 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002047 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002048 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002049 if (ret)
2050 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002051 }
2052 return 0;
2053}
2054
Josef Bacik2ac55d42010-02-03 19:33:23 +00002055int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002056 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002057 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002058{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002059 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002060 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002061 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002062}
2063
Chris Masond352ac62008-09-29 15:18:18 -04002064/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002065struct btrfs_writepage_fixup {
2066 struct page *page;
2067 struct btrfs_work work;
2068};
2069
Christoph Hellwigb2950862008-12-02 09:54:17 -05002070static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002071{
2072 struct btrfs_writepage_fixup *fixup;
2073 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002074 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002075 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002076 struct page *page;
2077 struct inode *inode;
2078 u64 page_start;
2079 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002080 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002081
2082 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2083 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002084again:
Chris Mason247e7432008-07-17 12:53:51 -04002085 lock_page(page);
2086 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2087 ClearPageChecked(page);
2088 goto out_page;
2089 }
2090
2091 inode = page->mapping->host;
2092 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002093 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002094
David Sterbaff13db42015-12-03 14:30:40 +01002095 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002096 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002097
2098 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002099 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002100 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002101
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002102 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002103 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002104 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002105 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002106 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002107 unlock_page(page);
2108 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002109 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002110 goto again;
2111 }
Chris Mason247e7432008-07-17 12:53:51 -04002112
Qu Wenruo364ecf32017-02-27 15:10:38 +08002113 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002114 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002115 if (ret) {
2116 mapping_set_error(page->mapping, ret);
2117 end_extent_writepage(page, ret, page_start, page_end);
2118 ClearPageChecked(page);
2119 goto out;
2120 }
2121
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002122 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2123 &cached_state, 0);
2124 if (ret) {
2125 mapping_set_error(page->mapping, ret);
2126 end_extent_writepage(page, ret, page_start, page_end);
2127 ClearPageChecked(page);
2128 goto out;
2129 }
2130
Chris Mason247e7432008-07-17 12:53:51 -04002131 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002132 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002133 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002134out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002135 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002136 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002137out_page:
2138 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002139 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002140 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002141 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002142}
2143
2144/*
2145 * There are a few paths in the higher layers of the kernel that directly
2146 * set the page dirty bit without asking the filesystem if it is a
2147 * good idea. This causes problems because we want to make sure COW
2148 * properly happens and the data=ordered rules are followed.
2149 *
Chris Masonc8b97812008-10-29 14:49:59 -04002150 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002151 * hasn't been properly setup for IO. We kick off an async process
2152 * to fix it up. The async helper will wait for ordered extents, set
2153 * the delalloc bit and make it safe to write the page.
2154 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002155static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002156{
2157 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002158 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002159 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002160
Chris Mason8b62b722009-09-02 16:53:46 -04002161 /* this page is properly in the ordered list */
2162 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002163 return 0;
2164
2165 if (PageChecked(page))
2166 return -EAGAIN;
2167
2168 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2169 if (!fixup)
2170 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002171
Chris Mason247e7432008-07-17 12:53:51 -04002172 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002173 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002174 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2175 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002176 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002177 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002178 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002179}
2180
Yan Zhengd899e052008-10-30 14:25:28 -04002181static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2182 struct inode *inode, u64 file_pos,
2183 u64 disk_bytenr, u64 disk_num_bytes,
2184 u64 num_bytes, u64 ram_bytes,
2185 u8 compression, u8 encryption,
2186 u16 other_encoding, int extent_type)
2187{
2188 struct btrfs_root *root = BTRFS_I(inode)->root;
2189 struct btrfs_file_extent_item *fi;
2190 struct btrfs_path *path;
2191 struct extent_buffer *leaf;
2192 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002193 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002194 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002195 int ret;
2196
2197 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002198 if (!path)
2199 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002200
Chris Masona1ed8352009-09-11 12:27:37 -04002201 /*
2202 * we may be replacing one extent in the tree with another.
2203 * The new extent is pinned in the extent map, and we don't want
2204 * to drop it from the cache until it is completely in the btree.
2205 *
2206 * So, tell btrfs_drop_extents to leave this extent in the cache.
2207 * the caller is expected to unpin it and allow it to be merged
2208 * with the others.
2209 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002210 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2211 file_pos + num_bytes, NULL, 0,
2212 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002213 if (ret)
2214 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002215
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002216 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002217 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002218 ins.offset = file_pos;
2219 ins.type = BTRFS_EXTENT_DATA_KEY;
2220
2221 path->leave_spinning = 1;
2222 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2223 sizeof(*fi));
2224 if (ret)
2225 goto out;
2226 }
Yan Zhengd899e052008-10-30 14:25:28 -04002227 leaf = path->nodes[0];
2228 fi = btrfs_item_ptr(leaf, path->slots[0],
2229 struct btrfs_file_extent_item);
2230 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2231 btrfs_set_file_extent_type(leaf, fi, extent_type);
2232 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2233 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2234 btrfs_set_file_extent_offset(leaf, fi, 0);
2235 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2236 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2237 btrfs_set_file_extent_compression(leaf, fi, compression);
2238 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2239 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002240
Yan Zhengd899e052008-10-30 14:25:28 -04002241 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002242 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002243
2244 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002245
2246 ins.objectid = disk_bytenr;
2247 ins.offset = disk_num_bytes;
2248 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002249
Qu Wenruo297d7502015-09-08 17:08:37 +08002250 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002251 * Release the reserved range from inode dirty range map, as it is
2252 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002253 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002254 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2255 if (ret < 0)
2256 goto out;
2257 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002258 ret = btrfs_alloc_reserved_file_extent(trans, root,
2259 btrfs_ino(BTRFS_I(inode)),
2260 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261out:
Yan Zhengd899e052008-10-30 14:25:28 -04002262 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002263
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002265}
2266
Liu Bo38c227d2013-01-29 03:18:40 +00002267/* snapshot-aware defrag */
2268struct sa_defrag_extent_backref {
2269 struct rb_node node;
2270 struct old_sa_defrag_extent *old;
2271 u64 root_id;
2272 u64 inum;
2273 u64 file_pos;
2274 u64 extent_offset;
2275 u64 num_bytes;
2276 u64 generation;
2277};
2278
2279struct old_sa_defrag_extent {
2280 struct list_head list;
2281 struct new_sa_defrag_extent *new;
2282
2283 u64 extent_offset;
2284 u64 bytenr;
2285 u64 offset;
2286 u64 len;
2287 int count;
2288};
2289
2290struct new_sa_defrag_extent {
2291 struct rb_root root;
2292 struct list_head head;
2293 struct btrfs_path *path;
2294 struct inode *inode;
2295 u64 file_pos;
2296 u64 len;
2297 u64 bytenr;
2298 u64 disk_len;
2299 u8 compress_type;
2300};
2301
2302static int backref_comp(struct sa_defrag_extent_backref *b1,
2303 struct sa_defrag_extent_backref *b2)
2304{
2305 if (b1->root_id < b2->root_id)
2306 return -1;
2307 else if (b1->root_id > b2->root_id)
2308 return 1;
2309
2310 if (b1->inum < b2->inum)
2311 return -1;
2312 else if (b1->inum > b2->inum)
2313 return 1;
2314
2315 if (b1->file_pos < b2->file_pos)
2316 return -1;
2317 else if (b1->file_pos > b2->file_pos)
2318 return 1;
2319
2320 /*
2321 * [------------------------------] ===> (a range of space)
2322 * |<--->| |<---->| =============> (fs/file tree A)
2323 * |<---------------------------->| ===> (fs/file tree B)
2324 *
2325 * A range of space can refer to two file extents in one tree while
2326 * refer to only one file extent in another tree.
2327 *
2328 * So we may process a disk offset more than one time(two extents in A)
2329 * and locate at the same extent(one extent in B), then insert two same
2330 * backrefs(both refer to the extent in B).
2331 */
2332 return 0;
2333}
2334
2335static void backref_insert(struct rb_root *root,
2336 struct sa_defrag_extent_backref *backref)
2337{
2338 struct rb_node **p = &root->rb_node;
2339 struct rb_node *parent = NULL;
2340 struct sa_defrag_extent_backref *entry;
2341 int ret;
2342
2343 while (*p) {
2344 parent = *p;
2345 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2346
2347 ret = backref_comp(backref, entry);
2348 if (ret < 0)
2349 p = &(*p)->rb_left;
2350 else
2351 p = &(*p)->rb_right;
2352 }
2353
2354 rb_link_node(&backref->node, parent, p);
2355 rb_insert_color(&backref->node, root);
2356}
2357
2358/*
2359 * Note the backref might has changed, and in this case we just return 0.
2360 */
2361static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2362 void *ctx)
2363{
2364 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002365 struct old_sa_defrag_extent *old = ctx;
2366 struct new_sa_defrag_extent *new = old->new;
2367 struct btrfs_path *path = new->path;
2368 struct btrfs_key key;
2369 struct btrfs_root *root;
2370 struct sa_defrag_extent_backref *backref;
2371 struct extent_buffer *leaf;
2372 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002373 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002374 int slot;
2375 int ret;
2376 u64 extent_offset;
2377 u64 num_bytes;
2378
2379 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002380 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002381 return 0;
2382
2383 key.objectid = root_id;
2384 key.type = BTRFS_ROOT_ITEM_KEY;
2385 key.offset = (u64)-1;
2386
Liu Bo38c227d2013-01-29 03:18:40 +00002387 root = btrfs_read_fs_root_no_name(fs_info, &key);
2388 if (IS_ERR(root)) {
2389 if (PTR_ERR(root) == -ENOENT)
2390 return 0;
2391 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002392 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002393 inum, offset, root_id);
2394 return PTR_ERR(root);
2395 }
2396
2397 key.objectid = inum;
2398 key.type = BTRFS_EXTENT_DATA_KEY;
2399 if (offset > (u64)-1 << 32)
2400 key.offset = 0;
2401 else
2402 key.offset = offset;
2403
2404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302405 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002406 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002407 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002408
2409 while (1) {
2410 cond_resched();
2411
2412 leaf = path->nodes[0];
2413 slot = path->slots[0];
2414
2415 if (slot >= btrfs_header_nritems(leaf)) {
2416 ret = btrfs_next_leaf(root, path);
2417 if (ret < 0) {
2418 goto out;
2419 } else if (ret > 0) {
2420 ret = 0;
2421 goto out;
2422 }
2423 continue;
2424 }
2425
2426 path->slots[0]++;
2427
2428 btrfs_item_key_to_cpu(leaf, &key, slot);
2429
2430 if (key.objectid > inum)
2431 goto out;
2432
2433 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2434 continue;
2435
2436 extent = btrfs_item_ptr(leaf, slot,
2437 struct btrfs_file_extent_item);
2438
2439 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2440 continue;
2441
Liu Boe68afa42013-07-01 22:13:26 +08002442 /*
2443 * 'offset' refers to the exact key.offset,
2444 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2445 * (key.offset - extent_offset).
2446 */
2447 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002448 continue;
2449
Liu Boe68afa42013-07-01 22:13:26 +08002450 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002451 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002452
Liu Bo38c227d2013-01-29 03:18:40 +00002453 if (extent_offset >= old->extent_offset + old->offset +
2454 old->len || extent_offset + num_bytes <=
2455 old->extent_offset + old->offset)
2456 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002457 break;
2458 }
2459
2460 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2461 if (!backref) {
2462 ret = -ENOENT;
2463 goto out;
2464 }
2465
2466 backref->root_id = root_id;
2467 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002468 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002469 backref->num_bytes = num_bytes;
2470 backref->extent_offset = extent_offset;
2471 backref->generation = btrfs_file_extent_generation(leaf, extent);
2472 backref->old = old;
2473 backref_insert(&new->root, backref);
2474 old->count++;
2475out:
2476 btrfs_release_path(path);
2477 WARN_ON(ret);
2478 return ret;
2479}
2480
2481static noinline bool record_extent_backrefs(struct btrfs_path *path,
2482 struct new_sa_defrag_extent *new)
2483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002484 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002485 struct old_sa_defrag_extent *old, *tmp;
2486 int ret;
2487
2488 new->path = path;
2489
2490 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002491 ret = iterate_inodes_from_logical(old->bytenr +
2492 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002493 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002494 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002495 if (ret < 0 && ret != -ENOENT)
2496 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002497
2498 /* no backref to be processed for this extent */
2499 if (!old->count) {
2500 list_del(&old->list);
2501 kfree(old);
2502 }
2503 }
2504
2505 if (list_empty(&new->head))
2506 return false;
2507
2508 return true;
2509}
2510
2511static int relink_is_mergable(struct extent_buffer *leaf,
2512 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002513 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002514{
Liu Bo116e0022013-08-02 16:30:40 +08002515 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002516 return 0;
2517
2518 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2519 return 0;
2520
Liu Bo116e0022013-08-02 16:30:40 +08002521 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2522 return 0;
2523
2524 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002525 btrfs_file_extent_other_encoding(leaf, fi))
2526 return 0;
2527
2528 return 1;
2529}
2530
2531/*
2532 * Note the backref might has changed, and in this case we just return 0.
2533 */
2534static noinline int relink_extent_backref(struct btrfs_path *path,
2535 struct sa_defrag_extent_backref *prev,
2536 struct sa_defrag_extent_backref *backref)
2537{
2538 struct btrfs_file_extent_item *extent;
2539 struct btrfs_file_extent_item *item;
2540 struct btrfs_ordered_extent *ordered;
2541 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002542 struct btrfs_root *root;
2543 struct btrfs_key key;
2544 struct extent_buffer *leaf;
2545 struct old_sa_defrag_extent *old = backref->old;
2546 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002547 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002548 struct inode *inode;
2549 struct extent_state *cached = NULL;
2550 int ret = 0;
2551 u64 start;
2552 u64 len;
2553 u64 lock_start;
2554 u64 lock_end;
2555 bool merge = false;
2556 int index;
2557
2558 if (prev && prev->root_id == backref->root_id &&
2559 prev->inum == backref->inum &&
2560 prev->file_pos + prev->num_bytes == backref->file_pos)
2561 merge = true;
2562
2563 /* step 1: get root */
2564 key.objectid = backref->root_id;
2565 key.type = BTRFS_ROOT_ITEM_KEY;
2566 key.offset = (u64)-1;
2567
Liu Bo38c227d2013-01-29 03:18:40 +00002568 index = srcu_read_lock(&fs_info->subvol_srcu);
2569
2570 root = btrfs_read_fs_root_no_name(fs_info, &key);
2571 if (IS_ERR(root)) {
2572 srcu_read_unlock(&fs_info->subvol_srcu, index);
2573 if (PTR_ERR(root) == -ENOENT)
2574 return 0;
2575 return PTR_ERR(root);
2576 }
Liu Bo38c227d2013-01-29 03:18:40 +00002577
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002578 if (btrfs_root_readonly(root)) {
2579 srcu_read_unlock(&fs_info->subvol_srcu, index);
2580 return 0;
2581 }
2582
Liu Bo38c227d2013-01-29 03:18:40 +00002583 /* step 2: get inode */
2584 key.objectid = backref->inum;
2585 key.type = BTRFS_INODE_ITEM_KEY;
2586 key.offset = 0;
2587
2588 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2589 if (IS_ERR(inode)) {
2590 srcu_read_unlock(&fs_info->subvol_srcu, index);
2591 return 0;
2592 }
2593
2594 srcu_read_unlock(&fs_info->subvol_srcu, index);
2595
2596 /* step 3: relink backref */
2597 lock_start = backref->file_pos;
2598 lock_end = backref->file_pos + backref->num_bytes - 1;
2599 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002600 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002601
2602 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2603 if (ordered) {
2604 btrfs_put_ordered_extent(ordered);
2605 goto out_unlock;
2606 }
2607
2608 trans = btrfs_join_transaction(root);
2609 if (IS_ERR(trans)) {
2610 ret = PTR_ERR(trans);
2611 goto out_unlock;
2612 }
2613
2614 key.objectid = backref->inum;
2615 key.type = BTRFS_EXTENT_DATA_KEY;
2616 key.offset = backref->file_pos;
2617
2618 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2619 if (ret < 0) {
2620 goto out_free_path;
2621 } else if (ret > 0) {
2622 ret = 0;
2623 goto out_free_path;
2624 }
2625
2626 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2627 struct btrfs_file_extent_item);
2628
2629 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2630 backref->generation)
2631 goto out_free_path;
2632
2633 btrfs_release_path(path);
2634
2635 start = backref->file_pos;
2636 if (backref->extent_offset < old->extent_offset + old->offset)
2637 start += old->extent_offset + old->offset -
2638 backref->extent_offset;
2639
2640 len = min(backref->extent_offset + backref->num_bytes,
2641 old->extent_offset + old->offset + old->len);
2642 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2643
2644 ret = btrfs_drop_extents(trans, root, inode, start,
2645 start + len, 1);
2646 if (ret)
2647 goto out_free_path;
2648again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002649 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002650 key.type = BTRFS_EXTENT_DATA_KEY;
2651 key.offset = start;
2652
Liu Boa09a0a72013-03-11 09:20:58 +00002653 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002654 if (merge) {
2655 struct btrfs_file_extent_item *fi;
2656 u64 extent_len;
2657 struct btrfs_key found_key;
2658
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002659 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002660 if (ret < 0)
2661 goto out_free_path;
2662
2663 path->slots[0]--;
2664 leaf = path->nodes[0];
2665 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2666
2667 fi = btrfs_item_ptr(leaf, path->slots[0],
2668 struct btrfs_file_extent_item);
2669 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2670
Liu Bo116e0022013-08-02 16:30:40 +08002671 if (extent_len + found_key.offset == start &&
2672 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002673 btrfs_set_file_extent_num_bytes(leaf, fi,
2674 extent_len + len);
2675 btrfs_mark_buffer_dirty(leaf);
2676 inode_add_bytes(inode, len);
2677
2678 ret = 1;
2679 goto out_free_path;
2680 } else {
2681 merge = false;
2682 btrfs_release_path(path);
2683 goto again;
2684 }
2685 }
2686
2687 ret = btrfs_insert_empty_item(trans, root, path, &key,
2688 sizeof(*extent));
2689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002691 goto out_free_path;
2692 }
2693
2694 leaf = path->nodes[0];
2695 item = btrfs_item_ptr(leaf, path->slots[0],
2696 struct btrfs_file_extent_item);
2697 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2698 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2699 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2700 btrfs_set_file_extent_num_bytes(leaf, item, len);
2701 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2702 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2703 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2704 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2705 btrfs_set_file_extent_encryption(leaf, item, 0);
2706 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2707
2708 btrfs_mark_buffer_dirty(leaf);
2709 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002710 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002711
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002712 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002713 new->disk_len, 0,
2714 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002715 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002716 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002717 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002718 goto out_free_path;
2719 }
2720
2721 ret = 1;
2722out_free_path:
2723 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002724 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002725 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002726out_unlock:
2727 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002728 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002729 iput(inode);
2730 return ret;
2731}
2732
Liu Bo6f519562013-10-29 10:45:05 +08002733static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2734{
2735 struct old_sa_defrag_extent *old, *tmp;
2736
2737 if (!new)
2738 return;
2739
2740 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002741 kfree(old);
2742 }
2743 kfree(new);
2744}
2745
Liu Bo38c227d2013-01-29 03:18:40 +00002746static void relink_file_extents(struct new_sa_defrag_extent *new)
2747{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002748 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002749 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002750 struct sa_defrag_extent_backref *backref;
2751 struct sa_defrag_extent_backref *prev = NULL;
2752 struct inode *inode;
2753 struct btrfs_root *root;
2754 struct rb_node *node;
2755 int ret;
2756
2757 inode = new->inode;
2758 root = BTRFS_I(inode)->root;
2759
2760 path = btrfs_alloc_path();
2761 if (!path)
2762 return;
2763
2764 if (!record_extent_backrefs(path, new)) {
2765 btrfs_free_path(path);
2766 goto out;
2767 }
2768 btrfs_release_path(path);
2769
2770 while (1) {
2771 node = rb_first(&new->root);
2772 if (!node)
2773 break;
2774 rb_erase(node, &new->root);
2775
2776 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2777
2778 ret = relink_extent_backref(path, prev, backref);
2779 WARN_ON(ret < 0);
2780
2781 kfree(prev);
2782
2783 if (ret == 1)
2784 prev = backref;
2785 else
2786 prev = NULL;
2787 cond_resched();
2788 }
2789 kfree(prev);
2790
2791 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002792out:
Liu Bo6f519562013-10-29 10:45:05 +08002793 free_sa_defrag_extent(new);
2794
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002795 atomic_dec(&fs_info->defrag_running);
2796 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002797}
2798
2799static struct new_sa_defrag_extent *
2800record_old_file_extents(struct inode *inode,
2801 struct btrfs_ordered_extent *ordered)
2802{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002804 struct btrfs_root *root = BTRFS_I(inode)->root;
2805 struct btrfs_path *path;
2806 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002807 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002808 struct new_sa_defrag_extent *new;
2809 int ret;
2810
2811 new = kmalloc(sizeof(*new), GFP_NOFS);
2812 if (!new)
2813 return NULL;
2814
2815 new->inode = inode;
2816 new->file_pos = ordered->file_offset;
2817 new->len = ordered->len;
2818 new->bytenr = ordered->start;
2819 new->disk_len = ordered->disk_len;
2820 new->compress_type = ordered->compress_type;
2821 new->root = RB_ROOT;
2822 INIT_LIST_HEAD(&new->head);
2823
2824 path = btrfs_alloc_path();
2825 if (!path)
2826 goto out_kfree;
2827
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002828 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002829 key.type = BTRFS_EXTENT_DATA_KEY;
2830 key.offset = new->file_pos;
2831
2832 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2833 if (ret < 0)
2834 goto out_free_path;
2835 if (ret > 0 && path->slots[0] > 0)
2836 path->slots[0]--;
2837
2838 /* find out all the old extents for the file range */
2839 while (1) {
2840 struct btrfs_file_extent_item *extent;
2841 struct extent_buffer *l;
2842 int slot;
2843 u64 num_bytes;
2844 u64 offset;
2845 u64 end;
2846 u64 disk_bytenr;
2847 u64 extent_offset;
2848
2849 l = path->nodes[0];
2850 slot = path->slots[0];
2851
2852 if (slot >= btrfs_header_nritems(l)) {
2853 ret = btrfs_next_leaf(root, path);
2854 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002855 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002856 else if (ret > 0)
2857 break;
2858 continue;
2859 }
2860
2861 btrfs_item_key_to_cpu(l, &key, slot);
2862
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002863 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002864 break;
2865 if (key.type != BTRFS_EXTENT_DATA_KEY)
2866 break;
2867 if (key.offset >= new->file_pos + new->len)
2868 break;
2869
2870 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2871
2872 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2873 if (key.offset + num_bytes < new->file_pos)
2874 goto next;
2875
2876 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2877 if (!disk_bytenr)
2878 goto next;
2879
2880 extent_offset = btrfs_file_extent_offset(l, extent);
2881
2882 old = kmalloc(sizeof(*old), GFP_NOFS);
2883 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002884 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002885
2886 offset = max(new->file_pos, key.offset);
2887 end = min(new->file_pos + new->len, key.offset + num_bytes);
2888
2889 old->bytenr = disk_bytenr;
2890 old->extent_offset = extent_offset;
2891 old->offset = offset - key.offset;
2892 old->len = end - offset;
2893 old->new = new;
2894 old->count = 0;
2895 list_add_tail(&old->list, &new->head);
2896next:
2897 path->slots[0]++;
2898 cond_resched();
2899 }
2900
2901 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002902 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002903
2904 return new;
2905
Liu Bo38c227d2013-01-29 03:18:40 +00002906out_free_path:
2907 btrfs_free_path(path);
2908out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002909 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002910 return NULL;
2911}
2912
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002913static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002914 u64 start, u64 len)
2915{
2916 struct btrfs_block_group_cache *cache;
2917
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002918 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002919 ASSERT(cache);
2920
2921 spin_lock(&cache->lock);
2922 cache->delalloc_bytes -= len;
2923 spin_unlock(&cache->lock);
2924
2925 btrfs_put_block_group(cache);
2926}
2927
Chris Masond352ac62008-09-29 15:18:18 -04002928/* as ordered data IO finishes, this gets called so we can finish
2929 * an ordered extent if the range of bytes in the file it covers are
2930 * fully written.
2931 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002932static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002933{
Josef Bacik5fd02042012-05-02 14:00:54 -04002934 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002935 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002936 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002937 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002938 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002939 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002940 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002941 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002942 int ret = 0;
2943 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002944 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002945 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002946 bool range_locked = false;
2947 bool clear_new_delalloc_bytes = false;
2948
2949 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2950 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2951 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2952 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002954 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002955
Josef Bacik5fd02042012-05-02 14:00:54 -04002956 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2957 ret = -EIO;
2958 goto out;
2959 }
2960
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002961 btrfs_free_io_failure_record(BTRFS_I(inode),
2962 ordered_extent->file_offset,
2963 ordered_extent->file_offset +
2964 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002965
Josef Bacik77cef2e2013-08-29 13:57:21 -04002966 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2967 truncated = true;
2968 logical_len = ordered_extent->truncated_len;
2969 /* Truncated the entire extent, don't bother adding */
2970 if (!logical_len)
2971 goto out;
2972 }
2973
Yan, Zhengc2167752009-11-12 09:34:21 +00002974 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002976
2977 /*
2978 * For mwrite(mmap + memset to write) case, we still reserve
2979 * space for NOCOW range.
2980 * As NOCOW won't cause a new delayed ref, just free the space
2981 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002982 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002983 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002984 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2985 if (nolock)
2986 trans = btrfs_join_transaction_nolock(root);
2987 else
2988 trans = btrfs_join_transaction(root);
2989 if (IS_ERR(trans)) {
2990 ret = PTR_ERR(trans);
2991 trans = NULL;
2992 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002993 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002994 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002995 ret = btrfs_update_inode_fallback(trans, root, inode);
2996 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002997 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002998 goto out;
2999 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003000
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003001 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003002 lock_extent_bits(io_tree, ordered_extent->file_offset,
3003 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003004 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003005
Liu Bo38c227d2013-01-29 03:18:40 +00003006 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3007 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003008 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003009 if (ret) {
3010 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003011 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003012 /* the inode is shared */
3013 new = record_old_file_extents(inode, ordered_extent);
3014
3015 clear_extent_bit(io_tree, ordered_extent->file_offset,
3016 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003017 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003018 }
3019
Josef Bacik0cb59c92010-07-02 12:14:14 -04003020 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003021 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003022 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003023 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 if (IS_ERR(trans)) {
3025 ret = PTR_ERR(trans);
3026 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003027 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003028 }
Chris Masona79b7d42014-05-22 16:18:52 -07003029
Josef Bacik69fe2d72017-10-19 14:15:57 -04003030 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003031
Chris Masonc8b97812008-10-29 14:49:59 -04003032 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003033 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003034 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003035 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003036 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3037 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003038 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003039 ordered_extent->file_offset,
3040 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003041 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003042 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003043 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003044 ret = insert_reserved_file_extent(trans, inode,
3045 ordered_extent->file_offset,
3046 ordered_extent->start,
3047 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003048 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003049 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003050 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003051 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003052 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003053 ordered_extent->start,
3054 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003055 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003056 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3057 ordered_extent->file_offset, ordered_extent->len,
3058 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003060 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003061 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003063
Nikolay Borisovac01f262018-01-08 10:59:43 +02003064 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3065 if (ret) {
3066 btrfs_abort_transaction(trans, ret);
3067 goto out;
3068 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003069
Josef Bacik6c760c02012-11-09 10:53:21 -05003070 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3071 ret = btrfs_update_inode_fallback(trans, root, inode);
3072 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003073 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003074 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003075 }
3076 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003077out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003078 if (range_locked || clear_new_delalloc_bytes) {
3079 unsigned int clear_bits = 0;
3080
3081 if (range_locked)
3082 clear_bits |= EXTENT_LOCKED;
3083 if (clear_new_delalloc_bytes)
3084 clear_bits |= EXTENT_DELALLOC_NEW;
3085 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3086 ordered_extent->file_offset,
3087 ordered_extent->file_offset +
3088 ordered_extent->len - 1,
3089 clear_bits,
3090 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003091 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003092 }
3093
Miao Xiea698d0752012-09-20 01:51:59 -06003094 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003095 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003096
Josef Bacik77cef2e2013-08-29 13:57:21 -04003097 if (ret || truncated) {
3098 u64 start, end;
3099
3100 if (truncated)
3101 start = ordered_extent->file_offset + logical_len;
3102 else
3103 start = ordered_extent->file_offset;
3104 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003105 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003106
3107 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003108 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003109
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003110 /*
3111 * If the ordered extent had an IOERR or something else went
3112 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003113 * back to the allocator. We only free the extent in the
3114 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003115 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003116 if ((ret || !logical_len) &&
3117 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003118 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003119 btrfs_free_reserved_extent(fs_info,
3120 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003121 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003122 }
3123
3124
Josef Bacik5fd02042012-05-02 14:00:54 -04003125 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003126 * This needs to be done to make sure anybody waiting knows we are done
3127 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003128 */
3129 btrfs_remove_ordered_extent(inode, ordered_extent);
3130
Liu Bo38c227d2013-01-29 03:18:40 +00003131 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003132 if (new) {
3133 if (ret) {
3134 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003135 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003136 } else {
3137 relink_file_extents(new);
3138 }
3139 }
Liu Bo38c227d2013-01-29 03:18:40 +00003140
Chris Masone6dcd2d2008-07-17 12:53:50 -04003141 /* once for us */
3142 btrfs_put_ordered_extent(ordered_extent);
3143 /* once for the tree */
3144 btrfs_put_ordered_extent(ordered_extent);
3145
Josef Bacik5fd02042012-05-02 14:00:54 -04003146 return ret;
3147}
3148
3149static void finish_ordered_fn(struct btrfs_work *work)
3150{
3151 struct btrfs_ordered_extent *ordered_extent;
3152 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3153 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003154}
3155
David Sterbac3988d62017-02-17 15:18:32 +01003156static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003157 struct extent_state *state, int uptodate)
3158{
Josef Bacik5fd02042012-05-02 14:00:54 -04003159 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003161 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003162 struct btrfs_workqueue *wq;
3163 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003164
liubo1abe9b82011-03-24 11:18:59 +00003165 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3166
Chris Mason8b62b722009-09-02 16:53:46 -04003167 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003168 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3169 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003170 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003171
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003172 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003173 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003174 func = btrfs_freespace_write_helper;
3175 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003177 func = btrfs_endio_write_helper;
3178 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003179
Liu Bo9e0af232014-08-15 23:36:53 +08003180 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3181 NULL);
3182 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003183}
3184
Miao Xiedc380ae2014-09-12 18:43:55 +08003185static int __readpage_endio_check(struct inode *inode,
3186 struct btrfs_io_bio *io_bio,
3187 int icsum, struct page *page,
3188 int pgoff, u64 start, size_t len)
3189{
3190 char *kaddr;
3191 u32 csum_expected;
3192 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003193
3194 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3195
3196 kaddr = kmap_atomic(page);
3197 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003198 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003199 if (csum != csum_expected)
3200 goto zeroit;
3201
3202 kunmap_atomic(kaddr);
3203 return 0;
3204zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003205 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003206 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003207 memset(kaddr + pgoff, 1, len);
3208 flush_dcache_page(page);
3209 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003210 return -EIO;
3211}
3212
Chris Masond352ac62008-09-29 15:18:18 -04003213/*
Chris Masond352ac62008-09-29 15:18:18 -04003214 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003215 * if there's a match, we allow the bio to finish. If not, the code in
3216 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003217 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003218static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3219 u64 phy_offset, struct page *page,
3220 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003221{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003222 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003223 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003224 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003225 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003226
Chris Masond20f7042008-12-08 16:58:54 -05003227 if (PageChecked(page)) {
3228 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003229 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003230 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003231
3232 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003233 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003234
Yan Zheng17d217f2008-12-12 10:03:38 -05003235 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003236 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003237 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003238 return 0;
3239 }
3240
Miao Xiefacc8a222013-07-25 19:22:34 +08003241 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003242 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3243 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003244}
Chris Masonb888db22007-08-27 16:49:44 -04003245
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003246void btrfs_add_delayed_iput(struct inode *inode)
3247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003249 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003250
3251 if (atomic_add_unless(&inode->i_count, -1, 1))
3252 return;
3253
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003255 if (binode->delayed_iput_count == 0) {
3256 ASSERT(list_empty(&binode->delayed_iput));
3257 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3258 } else {
3259 binode->delayed_iput_count++;
3260 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003261 spin_unlock(&fs_info->delayed_iput_lock);
3262}
3263
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003264void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003265{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003266
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003267 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003268 while (!list_empty(&fs_info->delayed_iputs)) {
3269 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003270
David Sterba8089fe62015-11-19 14:15:51 +01003271 inode = list_first_entry(&fs_info->delayed_iputs,
3272 struct btrfs_inode, delayed_iput);
3273 if (inode->delayed_iput_count) {
3274 inode->delayed_iput_count--;
3275 list_move_tail(&inode->delayed_iput,
3276 &fs_info->delayed_iputs);
3277 } else {
3278 list_del_init(&inode->delayed_iput);
3279 }
3280 spin_unlock(&fs_info->delayed_iput_lock);
3281 iput(&inode->vfs_inode);
3282 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283 }
David Sterba8089fe62015-11-19 14:15:51 +01003284 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003285}
3286
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003288 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 * files in the subvolume, it removes orphan item and frees block_rsv
3290 * structure.
3291 */
3292void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3293 struct btrfs_root *root)
3294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003295 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003296 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 int ret;
3298
Josef Bacik8a35d952012-05-23 14:26:42 -04003299 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3301 return;
3302
Josef Bacik90290e12011-12-02 15:44:12 -05003303 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003304 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003305 spin_unlock(&root->orphan_lock);
3306 return;
3307 }
3308
3309 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3310 spin_unlock(&root->orphan_lock);
3311 return;
3312 }
3313
3314 block_rsv = root->orphan_block_rsv;
3315 root->orphan_block_rsv = NULL;
3316 spin_unlock(&root->orphan_lock);
3317
Miao Xie27cdeb72014-04-02 19:51:05 +08003318 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003320 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003322 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003323 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003324 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003325 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3326 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327 }
3328
Josef Bacik90290e12011-12-02 15:44:12 -05003329 if (block_rsv) {
3330 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003331 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003332 }
3333}
3334
3335/*
Josef Bacik7b128762008-07-24 12:17:14 -04003336 * This creates an orphan entry for the given inode in case something goes
3337 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 *
3339 * NOTE: caller of this function should reserve 5 units of metadata for
3340 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003341 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003342int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3343 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003344{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003345 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3346 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003347 struct btrfs_block_rsv *block_rsv = NULL;
3348 int reserve = 0;
3349 int insert = 0;
3350 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003351
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003353 block_rsv = btrfs_alloc_block_rsv(fs_info,
3354 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003355 if (!block_rsv)
3356 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003357 }
3358
Yan, Zhengd68fc572010-05-16 10:49:58 -04003359 spin_lock(&root->orphan_lock);
3360 if (!root->orphan_block_rsv) {
3361 root->orphan_block_rsv = block_rsv;
3362 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003363 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003364 block_rsv = NULL;
3365 }
Josef Bacik7b128762008-07-24 12:17:14 -04003366
Josef Bacik8a35d952012-05-23 14:26:42 -04003367 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003368 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003369#if 0
3370 /*
3371 * For proper ENOSPC handling, we should do orphan
3372 * cleanup when mounting. But this introduces backward
3373 * compatibility issue.
3374 */
3375 if (!xchg(&root->orphan_item_inserted, 1))
3376 insert = 2;
3377 else
3378 insert = 1;
3379#endif
3380 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003381 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003382 }
Josef Bacik7b128762008-07-24 12:17:14 -04003383
Josef Bacik72ac3c02012-05-23 14:13:11 -04003384 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003385 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003386 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003387 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003388
Yan, Zhengd68fc572010-05-16 10:49:58 -04003389 /* grab metadata reservation from transaction handle */
3390 if (reserve) {
3391 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003392 ASSERT(!ret);
3393 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003394 /*
3395 * dec doesn't need spin_lock as ->orphan_block_rsv
3396 * would be released only if ->orphan_inodes is
3397 * zero.
3398 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003399 atomic_dec(&root->orphan_inodes);
3400 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003401 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003402 if (insert)
3403 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003404 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003405 return ret;
3406 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003407 }
3408
3409 /* insert an orphan item to track this unlinked/truncated file */
3410 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003411 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003412 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003413 if (reserve) {
3414 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003415 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003416 btrfs_orphan_release_metadata(inode);
3417 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003418 /*
3419 * btrfs_orphan_commit_root may race with us and set
3420 * ->orphan_block_rsv to zero, in order to avoid that,
3421 * decrease ->orphan_inodes after everything is done.
3422 */
3423 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003424 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003425 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003426 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003427 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003428 return ret;
3429 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003430 }
3431 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003432 }
3433
3434 /* insert an orphan item to track subvolume contains orphan files */
3435 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003436 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003437 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003438 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003439 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003440 return ret;
3441 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003442 }
3443 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003444}
3445
3446/*
3447 * We have done the truncate/delete so we can go ahead and remove the orphan
3448 * item for this particular inode.
3449 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003450static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003451 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003452{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003453 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003455 int ret = 0;
3456
Josef Bacik8a35d952012-05-23 14:26:42 -04003457 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003458 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003459 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003460
Liu Bo1a932ef2018-01-25 11:02:54 -07003461 if (delete_item && trans)
3462 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3463
Josef Bacik72ac3c02012-05-23 14:13:11 -04003464 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003465 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003466 btrfs_orphan_release_metadata(inode);
3467
Liu Bo1a932ef2018-01-25 11:02:54 -07003468 /*
3469 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3470 * to zero, in order to avoid that, decrease ->orphan_inodes after
3471 * everything is done.
3472 */
3473 if (delete_item)
3474 atomic_dec(&root->orphan_inodes);
3475
Josef Bacik4ef31a42013-08-13 14:10:08 -04003476 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003477}
3478
3479/*
3480 * this cleans up any orphans that may be left on the list from the last use
3481 * of this root.
3482 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003484{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003485 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003486 struct btrfs_path *path;
3487 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003488 struct btrfs_key key, found_key;
3489 struct btrfs_trans_handle *trans;
3490 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003491 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003492 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3493
Yan, Zhengd68fc572010-05-16 10:49:58 -04003494 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003496
3497 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003498 if (!path) {
3499 ret = -ENOMEM;
3500 goto out;
3501 }
David Sterbae4058b52015-11-27 16:31:35 +01003502 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003503
3504 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003505 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003506 key.offset = (u64)-1;
3507
Josef Bacik7b128762008-07-24 12:17:14 -04003508 while (1) {
3509 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003510 if (ret < 0)
3511 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003512
3513 /*
3514 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003515 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003516 * find the key and see if we have stuff that matches
3517 */
3518 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003519 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003520 if (path->slots[0] == 0)
3521 break;
3522 path->slots[0]--;
3523 }
3524
3525 /* pull out the item */
3526 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003527 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3528
3529 /* make sure the item matches what we want */
3530 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3531 break;
David Sterba962a2982014-06-04 18:41:45 +02003532 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003533 break;
3534
3535 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003536 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003537
3538 /*
3539 * this is where we are basically btrfs_lookup, without the
3540 * crossing root thing. we store the inode number in the
3541 * offset of the orphan item.
3542 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003543
3544 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003545 btrfs_err(fs_info,
3546 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003547 ret = -EINVAL;
3548 goto out;
3549 }
3550
3551 last_objectid = found_key.offset;
3552
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003553 found_key.objectid = found_key.offset;
3554 found_key.type = BTRFS_INODE_ITEM_KEY;
3555 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303557 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003558 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003559 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003560
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003561 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003562 struct btrfs_root *dead_root;
3563 struct btrfs_fs_info *fs_info = root->fs_info;
3564 int is_dead_root = 0;
3565
3566 /*
3567 * this is an orphan in the tree root. Currently these
3568 * could come from 2 sources:
3569 * a) a snapshot deletion in progress
3570 * b) a free space cache inode
3571 * We need to distinguish those two, as the snapshot
3572 * orphan must not get deleted.
3573 * find_dead_roots already ran before us, so if this
3574 * is a snapshot deletion, we should find the root
3575 * in the dead_roots list
3576 */
3577 spin_lock(&fs_info->trans_lock);
3578 list_for_each_entry(dead_root, &fs_info->dead_roots,
3579 root_list) {
3580 if (dead_root->root_key.objectid ==
3581 found_key.objectid) {
3582 is_dead_root = 1;
3583 break;
3584 }
3585 }
3586 spin_unlock(&fs_info->trans_lock);
3587 if (is_dead_root) {
3588 /* prevent this orphan from being found again */
3589 key.offset = found_key.objectid - 1;
3590 continue;
3591 }
3592 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003593 /*
3594 * Inode is already gone but the orphan item is still there,
3595 * kill the orphan item.
3596 */
Filipe Manana67710892016-06-06 11:51:25 +01003597 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003598 trans = btrfs_start_transaction(root, 1);
3599 if (IS_ERR(trans)) {
3600 ret = PTR_ERR(trans);
3601 goto out;
3602 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003603 btrfs_debug(fs_info, "auto deleting %Lu",
3604 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003605 ret = btrfs_del_orphan_item(trans, root,
3606 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003607 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003608 if (ret)
3609 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003610 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003611 }
Josef Bacik7b128762008-07-24 12:17:14 -04003612
Josef Bacik7b128762008-07-24 12:17:14 -04003613 /*
3614 * add this inode to the orphan list so btrfs_orphan_del does
3615 * the proper thing when we hit it
3616 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003617 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3618 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003619 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003620
Josef Bacik7b128762008-07-24 12:17:14 -04003621 /* if we have links, this was a truncate, lets do that */
3622 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303623 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003624 iput(inode);
3625 continue;
3626 }
Josef Bacik7b128762008-07-24 12:17:14 -04003627 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003628
3629 /* 1 for the orphan item deletion. */
3630 trans = btrfs_start_transaction(root, 1);
3631 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003632 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003633 ret = PTR_ERR(trans);
3634 goto out;
3635 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003636 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003637 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003638 if (ret) {
3639 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003640 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003641 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003642
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003643 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003644 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003645 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003646 } else {
3647 nr_unlink++;
3648 }
3649
3650 /* this will do delete_inode and everything for us */
3651 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003652 if (ret)
3653 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003654 }
Miao Xie3254c872011-11-10 20:45:05 -05003655 /* release the path since we're done with it */
3656 btrfs_release_path(path);
3657
Yan, Zhengd68fc572010-05-16 10:49:58 -04003658 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3659
3660 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003661 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003662 (u64)-1);
3663
Miao Xie27cdeb72014-04-02 19:51:05 +08003664 if (root->orphan_block_rsv ||
3665 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003666 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003667 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003668 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003669 }
Josef Bacik7b128762008-07-24 12:17:14 -04003670
3671 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003672 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003673 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003674 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003675
3676out:
3677 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003678 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003679 btrfs_free_path(path);
3680 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003681}
3682
Chris Masond352ac62008-09-29 15:18:18 -04003683/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003684 * very simple check to peek ahead in the leaf looking for xattrs. If we
3685 * don't find any xattrs, we know there can't be any acls.
3686 *
3687 * slot is the slot the inode is in, objectid is the objectid of the inode
3688 */
3689static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003690 int slot, u64 objectid,
3691 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003692{
3693 u32 nritems = btrfs_header_nritems(leaf);
3694 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003695 static u64 xattr_access = 0;
3696 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003697 int scanned = 0;
3698
Josef Bacikf23b5a52013-06-19 10:16:26 -04003699 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003700 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3701 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3702 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3703 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003704 }
3705
Chris Mason46a53cc2009-04-27 11:47:50 -04003706 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003707 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003708 while (slot < nritems) {
3709 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3710
3711 /* we found a different objectid, there must not be acls */
3712 if (found_key.objectid != objectid)
3713 return 0;
3714
3715 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003716 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003717 if (*first_xattr_slot == -1)
3718 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003719 if (found_key.offset == xattr_access ||
3720 found_key.offset == xattr_default)
3721 return 1;
3722 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003723
3724 /*
3725 * we found a key greater than an xattr key, there can't
3726 * be any acls later on
3727 */
3728 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3729 return 0;
3730
3731 slot++;
3732 scanned++;
3733
3734 /*
3735 * it goes inode, inode backrefs, xattrs, extents,
3736 * so if there are a ton of hard links to an inode there can
3737 * be a lot of backrefs. Don't waste time searching too hard,
3738 * this is just an optimization
3739 */
3740 if (scanned >= 8)
3741 break;
3742 }
3743 /* we hit the end of the leaf before we found an xattr or
3744 * something larger than an xattr. We have to assume the inode
3745 * has acls
3746 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003747 if (*first_xattr_slot == -1)
3748 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003749 return 1;
3750}
3751
3752/*
Chris Masond352ac62008-09-29 15:18:18 -04003753 * read an inode from the btree into the in-memory inode
3754 */
Filipe Manana67710892016-06-06 11:51:25 +01003755static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003758 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003759 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 struct btrfs_inode_item *inode_item;
3761 struct btrfs_root *root = BTRFS_I(inode)->root;
3762 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003763 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003764 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003765 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003766 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003767 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003768 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003769
3770 ret = btrfs_fill_inode(inode, &rdev);
3771 if (!ret)
3772 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003773
3774 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003775 if (!path) {
3776 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003777 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003778 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003779
Chris Mason39279cc2007-06-12 06:35:45 -04003780 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003781
Chris Mason39279cc2007-06-12 06:35:45 -04003782 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003783 if (ret) {
3784 if (ret > 0)
3785 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003786 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003787 }
Chris Mason39279cc2007-06-12 06:35:45 -04003788
Chris Mason5f39d392007-10-15 16:14:19 -04003789 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003790
3791 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003792 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003793
Chris Mason5f39d392007-10-15 16:14:19 -04003794 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3795 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003796 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003797 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003798 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3799 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003800 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003801
David Sterbaa937b972014-12-12 17:39:12 +01003802 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3803 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003804
David Sterbaa937b972014-12-12 17:39:12 +01003805 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3806 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003807
David Sterbaa937b972014-12-12 17:39:12 +01003808 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3809 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003810
chandan r9cc97d62012-07-04 12:48:07 +05303811 BTRFS_I(inode)->i_otime.tv_sec =
3812 btrfs_timespec_sec(leaf, &inode_item->otime);
3813 BTRFS_I(inode)->i_otime.tv_nsec =
3814 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003815
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003816 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003817 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003818 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3819
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003820 inode_set_iversion_queried(inode,
3821 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003822 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003823 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003824 rdev = btrfs_inode_rdev(leaf, inode_item);
3825
Josef Bacikaec74772008-07-24 12:12:38 -04003826 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003827 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003828
3829cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003830 /*
3831 * If we were modified in the current generation and evicted from memory
3832 * and then re-read we need to do a full sync since we don't have any
3833 * idea about which extents were modified before we were evicted from
3834 * cache.
3835 *
3836 * This is required for both inode re-read from disk and delayed inode
3837 * in delayed_nodes_tree.
3838 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003839 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003840 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3841 &BTRFS_I(inode)->runtime_flags);
3842
Filipe Mananabde6c242015-07-24 00:00:19 +01003843 /*
3844 * We don't persist the id of the transaction where an unlink operation
3845 * against the inode was last made. So here we assume the inode might
3846 * have been evicted, and therefore the exact value of last_unlink_trans
3847 * lost, and set it to last_trans to avoid metadata inconsistencies
3848 * between the inode and its parent if the inode is fsync'ed and the log
3849 * replayed. For example, in the scenario:
3850 *
3851 * touch mydir/foo
3852 * ln mydir/foo mydir/bar
3853 * sync
3854 * unlink mydir/bar
3855 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3856 * xfs_io -c fsync mydir/foo
3857 * <power failure>
3858 * mount fs, triggers fsync log replay
3859 *
3860 * We must make sure that when we fsync our inode foo we also log its
3861 * parent inode, otherwise after log replay the parent still has the
3862 * dentry with the "bar" name but our inode foo has a link count of 1
3863 * and doesn't have an inode ref with the name "bar" anymore.
3864 *
3865 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003866 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003867 * transaction commits on fsync if our inode is a directory, or if our
3868 * inode is not a directory, logging its parent unnecessarily.
3869 */
3870 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3871
Miao Xie67de1172013-12-26 13:07:06 +08003872 path->slots[0]++;
3873 if (inode->i_nlink != 1 ||
3874 path->slots[0] >= btrfs_header_nritems(leaf))
3875 goto cache_acl;
3876
3877 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003878 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003879 goto cache_acl;
3880
3881 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3882 if (location.type == BTRFS_INODE_REF_KEY) {
3883 struct btrfs_inode_ref *ref;
3884
3885 ref = (struct btrfs_inode_ref *)ptr;
3886 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3887 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3888 struct btrfs_inode_extref *extref;
3889
3890 extref = (struct btrfs_inode_extref *)ptr;
3891 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3892 extref);
3893 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003894cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003895 /*
3896 * try to precache a NULL acl entry for files that don't have
3897 * any xattrs or acls
3898 */
Li Zefan33345d012011-04-20 10:31:50 +08003899 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003900 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003901 if (first_xattr_slot != -1) {
3902 path->slots[0] = first_xattr_slot;
3903 ret = btrfs_load_inode_props(inode, path);
3904 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003905 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003906 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003907 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003908 root->root_key.objectid, ret);
3909 }
3910 btrfs_free_path(path);
3911
Al Viro72c04902009-06-24 16:58:48 -04003912 if (!maybe_acls)
3913 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003914
Chris Mason39279cc2007-06-12 06:35:45 -04003915 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003916 case S_IFREG:
3917 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003918 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 inode->i_fop = &btrfs_file_operations;
3920 inode->i_op = &btrfs_file_inode_operations;
3921 break;
3922 case S_IFDIR:
3923 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003924 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003925 break;
3926 case S_IFLNK:
3927 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003928 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003929 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3930 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003931 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003932 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003933 init_special_inode(inode, inode->i_mode, rdev);
3934 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003935 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003936
3937 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003938 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003939
3940make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003941 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003942 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003943 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003944}
3945
Chris Masond352ac62008-09-29 15:18:18 -04003946/*
3947 * given a leaf and an inode, copy the inode fields into the leaf
3948 */
Chris Masone02119d2008-09-05 16:13:11 -04003949static void fill_inode_item(struct btrfs_trans_handle *trans,
3950 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003951 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003952 struct inode *inode)
3953{
Liu Bo51fab692012-12-27 09:01:21 +00003954 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003955
Liu Bo51fab692012-12-27 09:01:21 +00003956 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003957
Liu Bo51fab692012-12-27 09:01:21 +00003958 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3959 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3960 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3961 &token);
3962 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3963 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003964
David Sterbaa937b972014-12-12 17:39:12 +01003965 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003966 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003967 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003968 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003969
David Sterbaa937b972014-12-12 17:39:12 +01003970 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003971 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003972 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003973 inode->i_mtime.tv_nsec, &token);
3974
David Sterbaa937b972014-12-12 17:39:12 +01003975 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003976 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003977 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003978 inode->i_ctime.tv_nsec, &token);
3979
chandan r9cc97d62012-07-04 12:48:07 +05303980 btrfs_set_token_timespec_sec(leaf, &item->otime,
3981 BTRFS_I(inode)->i_otime.tv_sec, &token);
3982 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3983 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3984
Liu Bo51fab692012-12-27 09:01:21 +00003985 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3986 &token);
3987 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3988 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003989 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3990 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003991 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3992 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3993 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3994 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003995}
3996
Chris Masond352ac62008-09-29 15:18:18 -04003997/*
3998 * copy everything in the in-memory inode into the btree.
3999 */
Chris Mason21151332011-11-10 20:39:08 -05004000static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004001 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004002{
4003 struct btrfs_inode_item *inode_item;
4004 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004005 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004006 int ret;
4007
4008 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004009 if (!path)
4010 return -ENOMEM;
4011
Chris Masonb9473432009-03-13 11:00:37 -04004012 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004013 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4014 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004015 if (ret) {
4016 if (ret > 0)
4017 ret = -ENOENT;
4018 goto failed;
4019 }
4020
Chris Mason5f39d392007-10-15 16:14:19 -04004021 leaf = path->nodes[0];
4022 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004023 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004024
Chris Masone02119d2008-09-05 16:13:11 -04004025 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004026 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004027 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004028 ret = 0;
4029failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004030 btrfs_free_path(path);
4031 return ret;
4032}
4033
Chris Masond352ac62008-09-29 15:18:18 -04004034/*
Chris Mason21151332011-11-10 20:39:08 -05004035 * copy everything in the in-memory inode into the btree.
4036 */
4037noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4038 struct btrfs_root *root, struct inode *inode)
4039{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004040 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004041 int ret;
4042
4043 /*
4044 * If the inode is a free space inode, we can deadlock during commit
4045 * if we put it into the delayed code.
4046 *
4047 * The data relocation inode should also be directly updated
4048 * without delay
4049 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004050 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004051 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004052 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004053 btrfs_update_root_times(trans, root);
4054
Chris Mason21151332011-11-10 20:39:08 -05004055 ret = btrfs_delayed_update_inode(trans, root, inode);
4056 if (!ret)
4057 btrfs_set_inode_last_trans(trans, inode);
4058 return ret;
4059 }
4060
4061 return btrfs_update_inode_item(trans, root, inode);
4062}
4063
Josef Bacikbe6aef62012-10-22 15:43:12 -04004064noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4065 struct btrfs_root *root,
4066 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004067{
4068 int ret;
4069
4070 ret = btrfs_update_inode(trans, root, inode);
4071 if (ret == -ENOSPC)
4072 return btrfs_update_inode_item(trans, root, inode);
4073 return ret;
4074}
4075
4076/*
Chris Masond352ac62008-09-29 15:18:18 -04004077 * unlink helper that gets used here in inode.c and in the tree logging
4078 * recovery code. It remove a link in a directory with a given name, and
4079 * also drops the back refs in the inode to the directory
4080 */
Al Viro92986792011-03-04 17:14:37 +00004081static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4082 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004083 struct btrfs_inode *dir,
4084 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004085 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004086{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004087 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004088 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004089 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004090 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004092 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004093 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004094 u64 ino = btrfs_ino(inode);
4095 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004096
4097 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004098 if (!path) {
4099 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004100 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004101 }
4102
Chris Masonb9473432009-03-13 11:00:37 -04004103 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004104 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004105 name, name_len, -1);
4106 if (IS_ERR(di)) {
4107 ret = PTR_ERR(di);
4108 goto err;
4109 }
4110 if (!di) {
4111 ret = -ENOENT;
4112 goto err;
4113 }
Chris Mason5f39d392007-10-15 16:14:19 -04004114 leaf = path->nodes[0];
4115 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004116 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004117 if (ret)
4118 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004119 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004120
Miao Xie67de1172013-12-26 13:07:06 +08004121 /*
4122 * If we don't have dir index, we have to get it by looking up
4123 * the inode ref, since we get the inode ref, remove it directly,
4124 * it is unnecessary to do delayed deletion.
4125 *
4126 * But if we have dir index, needn't search inode ref to get it.
4127 * Since the inode ref is close to the inode item, it is better
4128 * that we delay to delete it, and just do this deletion when
4129 * we update the inode item.
4130 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004131 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004132 ret = btrfs_delayed_delete_inode_ref(inode);
4133 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004134 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004135 goto skip_backref;
4136 }
4137 }
4138
Li Zefan33345d012011-04-20 10:31:50 +08004139 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4140 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004141 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004142 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004143 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004144 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004145 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004146 goto err;
4147 }
Miao Xie67de1172013-12-26 13:07:06 +08004148skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004149 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004151 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004152 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004153 }
Chris Mason39279cc2007-06-12 06:35:45 -04004154
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004155 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4156 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004157 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004158 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159 goto err;
4160 }
Chris Masone02119d2008-09-05 16:13:11 -04004161
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004162 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4163 index);
Chris Mason6418c962010-10-30 07:34:24 -04004164 if (ret == -ENOENT)
4165 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004166 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004167 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004168err:
4169 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004170 if (ret)
4171 goto out;
4172
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004173 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004174 inode_inc_iversion(&inode->vfs_inode);
4175 inode_inc_iversion(&dir->vfs_inode);
4176 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4177 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4178 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004179out:
Chris Mason39279cc2007-06-12 06:35:45 -04004180 return ret;
4181}
4182
Al Viro92986792011-03-04 17:14:37 +00004183int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4184 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004185 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004186 const char *name, int name_len)
4187{
4188 int ret;
4189 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4190 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004191 drop_nlink(&inode->vfs_inode);
4192 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004193 }
4194 return ret;
4195}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004196
4197/*
4198 * helper to start transaction for unlink and rmdir.
4199 *
Josef Bacikd52be812013-05-29 14:54:47 -04004200 * unlink and rmdir are special in btrfs, they do not always free space, so
4201 * if we cannot make our reservations the normal way try and see if there is
4202 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4203 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204 */
Josef Bacikd52be812013-05-29 14:54:47 -04004205static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004207 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208
Josef Bacike70bea52011-10-11 14:18:24 -04004209 /*
4210 * 1 for the possible orphan item
4211 * 1 for the dir item
4212 * 1 for the dir index
4213 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004214 * 1 for the inode
4215 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004216 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004217}
4218
Chris Mason39279cc2007-06-12 06:35:45 -04004219static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4220{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004221 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004222 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004223 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004224 int ret;
4225
Josef Bacikd52be812013-05-29 14:54:47 -04004226 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004227 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004228 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004229
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004230 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4231 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004232
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004233 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4234 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4235 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004236 if (ret)
4237 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004238
Yan, Zhenga22285a2010-05-16 10:48:46 -04004239 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004240 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004241 if (ret)
4242 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004243 }
Josef Bacik7b128762008-07-24 12:17:14 -04004244
Tsutomu Itohb5324022011-07-19 07:27:20 +00004245out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004246 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004247 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004248 return ret;
4249}
4250
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4252 struct btrfs_root *root,
4253 struct inode *dir, u64 objectid,
4254 const char *name, int name_len)
4255{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004256 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004257 struct btrfs_path *path;
4258 struct extent_buffer *leaf;
4259 struct btrfs_dir_item *di;
4260 struct btrfs_key key;
4261 u64 index;
4262 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004263 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004264
4265 path = btrfs_alloc_path();
4266 if (!path)
4267 return -ENOMEM;
4268
Li Zefan33345d012011-04-20 10:31:50 +08004269 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004270 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271 if (IS_ERR_OR_NULL(di)) {
4272 if (!di)
4273 ret = -ENOENT;
4274 else
4275 ret = PTR_ERR(di);
4276 goto out;
4277 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004278
4279 leaf = path->nodes[0];
4280 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4281 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4282 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004283 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004284 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004285 goto out;
4286 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004287 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004289 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4290 root->root_key.objectid, dir_ino,
4291 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004292 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004293 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004294 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004295 goto out;
4296 }
Li Zefan33345d012011-04-20 10:31:50 +08004297 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004298 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (IS_ERR_OR_NULL(di)) {
4300 if (!di)
4301 ret = -ENOENT;
4302 else
4303 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004304 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004305 goto out;
4306 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004307
4308 leaf = path->nodes[0];
4309 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004310 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004311 index = key.offset;
4312 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004313 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004314
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004315 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004316 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004317 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004318 goto out;
4319 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004320
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004321 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004322 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004323 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004324 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004325 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004326 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004327out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004328 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004330}
4331
Chris Mason39279cc2007-06-12 06:35:45 -04004332static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4333{
David Howells2b0143b2015-03-17 22:25:59 +00004334 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004335 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004336 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004338 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004339
David Sterbab3ae2442012-09-13 16:04:34 -06004340 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004341 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004342 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004343 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004344
Josef Bacikd52be812013-05-29 14:54:47 -04004345 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004346 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004347 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004348
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004349 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004350 err = btrfs_unlink_subvol(trans, root, dir,
4351 BTRFS_I(inode)->location.objectid,
4352 dentry->d_name.name,
4353 dentry->d_name.len);
4354 goto out;
4355 }
4356
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004357 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004358 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004359 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004360
Filipe Manana44f714d2016-06-06 16:11:13 +01004361 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4362
Chris Mason39279cc2007-06-12 06:35:45 -04004363 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004364 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4365 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4366 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004367 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004368 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004369 /*
4370 * Propagate the last_unlink_trans value of the deleted dir to
4371 * its parent directory. This is to prevent an unrecoverable
4372 * log tree in the case we do something like this:
4373 * 1) create dir foo
4374 * 2) create snapshot under dir foo
4375 * 3) delete the snapshot
4376 * 4) rmdir foo
4377 * 5) mkdir foo
4378 * 6) fsync foo or some file inside foo
4379 */
4380 if (last_unlink_trans >= trans->transid)
4381 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4382 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004383out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004384 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004385 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004386
Chris Mason39279cc2007-06-12 06:35:45 -04004387 return err;
4388}
4389
Chris Mason28f75a02015-02-04 06:59:29 -08004390static int truncate_space_check(struct btrfs_trans_handle *trans,
4391 struct btrfs_root *root,
4392 u64 bytes_deleted)
4393{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004394 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004395 int ret;
4396
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004397 /*
4398 * This is only used to apply pressure to the enospc system, we don't
4399 * intend to use this reservation at all.
4400 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004401 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004402 bytes_deleted *= fs_info->nodesize;
4403 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004404 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004405 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004406 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004407 trans->transid,
4408 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004409 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004410 }
Chris Mason28f75a02015-02-04 06:59:29 -08004411 return ret;
4412
4413}
4414
Josef Bacikddfae632017-10-19 14:16:02 -04004415/*
4416 * Return this if we need to call truncate_block for the last bit of the
4417 * truncate.
4418 */
4419#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004420
Chris Mason323ac952008-10-01 19:05:46 -04004421/*
Chris Mason39279cc2007-06-12 06:35:45 -04004422 * this can truncate away extent items, csum items and directory items.
4423 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004424 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004425 *
4426 * csum items that cross the new i_size are truncated to the new size
4427 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004428 *
4429 * min_type is the minimum key type to truncate down to. If set to 0, this
4430 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004431 */
Yan, Zheng80825102009-11-12 09:35:36 +00004432int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4433 struct btrfs_root *root,
4434 struct inode *inode,
4435 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004438 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004439 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004440 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004441 struct btrfs_key key;
4442 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004443 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004444 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004445 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004446 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004447 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004448 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004449 int found_extent;
4450 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004451 int pending_del_nr = 0;
4452 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004453 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004454 int ret;
4455 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004456 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004457 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004458 bool be_nice = false;
4459 bool should_throttle = false;
4460 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004461
4462 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004463
Chris Mason28ed1342014-12-17 09:41:04 -08004464 /*
4465 * for non-free space inodes and ref cows, we want to back off from
4466 * time to time
4467 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004468 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004469 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004470 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004471
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004472 path = btrfs_alloc_path();
4473 if (!path)
4474 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004475 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004476
Josef Bacik5dc562c2012-08-17 13:14:17 -04004477 /*
4478 * We want to drop from the next block forward in case this new size is
4479 * not block aligned since we will be keeping the last block of the
4480 * extent just the way it is.
4481 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004482 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004483 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004484 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004485 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004486 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004487
Miao Xie16cdcec2011-04-22 18:12:22 +08004488 /*
4489 * This function is also used to drop the items in the log tree before
4490 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4491 * it is used to drop the loged items. So we shouldn't kill the delayed
4492 * items.
4493 */
4494 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004495 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004496
Li Zefan33345d012011-04-20 10:31:50 +08004497 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004498 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004499 key.type = (u8)-1;
4500
Chris Mason85e21ba2008-01-29 15:11:36 -05004501search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004502 /*
4503 * with a 16K leaf size and 128MB extents, you can actually queue
4504 * up a huge file in a single leaf. Most of the time that
4505 * bytes_deleted is > 0, it will be huge by the time we get here
4506 */
Byongho Leeee221842015-12-15 01:42:10 +09004507 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004508 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004509 err = -EAGAIN;
4510 goto error;
4511 }
4512 }
4513
4514
Chris Masonb9473432009-03-13 11:00:37 -04004515 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004516 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004517 if (ret < 0) {
4518 err = ret;
4519 goto out;
4520 }
Chris Masond3977122009-01-05 21:25:51 -05004521
Chris Mason85e21ba2008-01-29 15:11:36 -05004522 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004523 /* there are no items in the tree for us to truncate, we're
4524 * done
4525 */
Yan, Zheng80825102009-11-12 09:35:36 +00004526 if (path->slots[0] == 0)
4527 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004528 path->slots[0]--;
4529 }
4530
Chris Masond3977122009-01-05 21:25:51 -05004531 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004532 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004533 leaf = path->nodes[0];
4534 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004535 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004536
Li Zefan33345d012011-04-20 10:31:50 +08004537 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004538 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004539
Chris Mason85e21ba2008-01-29 15:11:36 -05004540 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004541 break;
4542
Chris Mason5f39d392007-10-15 16:14:19 -04004543 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004544 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004545 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004546 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004547 extent_type = btrfs_file_extent_type(leaf, fi);
4548 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004549 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004550 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004551
4552 trace_btrfs_truncate_show_fi_regular(
4553 BTRFS_I(inode), leaf, fi,
4554 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004555 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004556 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004557 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004558
4559 trace_btrfs_truncate_show_fi_inline(
4560 BTRFS_I(inode), leaf, fi, path->slots[0],
4561 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004562 }
Yan008630c2007-11-07 13:31:09 -05004563 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004564 }
Yan, Zheng80825102009-11-12 09:35:36 +00004565 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004566 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004567 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004568 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004569 break;
4570 if (found_key.offset >= new_size)
4571 del_item = 1;
4572 else
4573 del_item = 0;
4574 }
Chris Mason39279cc2007-06-12 06:35:45 -04004575 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004576 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004577 if (found_type != BTRFS_EXTENT_DATA_KEY)
4578 goto delete;
4579
4580 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004581 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004582 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004583 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004584 u64 orig_num_bytes =
4585 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004586 extent_num_bytes = ALIGN(new_size -
4587 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004588 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004589 btrfs_set_file_extent_num_bytes(leaf, fi,
4590 extent_num_bytes);
4591 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004592 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004593 if (test_bit(BTRFS_ROOT_REF_COWS,
4594 &root->state) &&
4595 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004596 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004597 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004598 } else {
Chris Masondb945352007-10-15 16:15:53 -04004599 extent_num_bytes =
4600 btrfs_file_extent_disk_num_bytes(leaf,
4601 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004602 extent_offset = found_key.offset -
4603 btrfs_file_extent_offset(leaf, fi);
4604
Chris Mason39279cc2007-06-12 06:35:45 -04004605 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004606 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004607 if (extent_start != 0) {
4608 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004609 if (test_bit(BTRFS_ROOT_REF_COWS,
4610 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004611 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004612 }
4613 }
Chris Mason90692182008-02-08 13:49:28 -05004614 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004615 /*
4616 * we can't truncate inline items that have had
4617 * special encodings
4618 */
4619 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004620 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004621 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4622 btrfs_file_extent_compression(leaf, fi) == 0) {
4623 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004624
Josef Bacikddfae632017-10-19 14:16:02 -04004625 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4626 size = btrfs_file_extent_calc_inline_size(size);
4627 btrfs_truncate_item(root->fs_info, path, size, 1);
4628 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004629 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004630 * We have to bail so the last_size is set to
4631 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004632 */
Josef Bacikddfae632017-10-19 14:16:02 -04004633 err = NEED_TRUNCATE_BLOCK;
4634 break;
Chris Mason90692182008-02-08 13:49:28 -05004635 }
Josef Bacikddfae632017-10-19 14:16:02 -04004636
4637 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4638 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004639 }
Chris Mason179e29e2007-11-01 11:28:41 -04004640delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004641 if (del_item)
4642 last_size = found_key.offset;
4643 else
4644 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004645 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004646 if (!pending_del_nr) {
4647 /* no pending yet, add ourselves */
4648 pending_del_slot = path->slots[0];
4649 pending_del_nr = 1;
4650 } else if (pending_del_nr &&
4651 path->slots[0] + 1 == pending_del_slot) {
4652 /* hop on the pending chunk */
4653 pending_del_nr++;
4654 pending_del_slot = path->slots[0];
4655 } else {
Chris Masond3977122009-01-05 21:25:51 -05004656 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004657 }
Chris Mason39279cc2007-06-12 06:35:45 -04004658 } else {
4659 break;
4660 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004661 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004662
Miao Xie27cdeb72014-04-02 19:51:05 +08004663 if (found_extent &&
4664 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004665 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004666 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004667 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004668 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004669 extent_num_bytes, 0,
4670 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004671 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004672 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004673 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4674 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004675 trans->delayed_ref_updates * 2,
4676 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004677 if (be_nice) {
4678 if (truncate_space_check(trans, root,
4679 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004680 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004681 }
4682 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004683 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004684 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004685 }
Chris Mason39279cc2007-06-12 06:35:45 -04004686 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004687
Yan, Zheng80825102009-11-12 09:35:36 +00004688 if (found_type == BTRFS_INODE_ITEM_KEY)
4689 break;
4690
4691 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004692 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004693 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004694 if (pending_del_nr) {
4695 ret = btrfs_del_items(trans, root, path,
4696 pending_del_slot,
4697 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004698 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004699 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004700 goto error;
4701 }
Yan, Zheng80825102009-11-12 09:35:36 +00004702 pending_del_nr = 0;
4703 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004704 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004705 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004706 unsigned long updates = trans->delayed_ref_updates;
4707 if (updates) {
4708 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004709 ret = btrfs_run_delayed_refs(trans,
4710 fs_info,
4711 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004712 if (ret && !err)
4713 err = ret;
4714 }
4715 }
Chris Mason28f75a02015-02-04 06:59:29 -08004716 /*
4717 * if we failed to refill our space rsv, bail out
4718 * and let the transaction restart
4719 */
4720 if (should_end) {
4721 err = -EAGAIN;
4722 goto error;
4723 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004724 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004725 } else {
4726 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004727 }
Chris Mason39279cc2007-06-12 06:35:45 -04004728 }
Yan, Zheng80825102009-11-12 09:35:36 +00004729out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004730 if (pending_del_nr) {
4731 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4732 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004733 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004734 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004735 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004736error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004737 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4738 ASSERT(last_size >= new_size);
4739 if (!err && last_size > new_size)
4740 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004741 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004742 }
Chris Mason28ed1342014-12-17 09:41:04 -08004743
Chris Mason39279cc2007-06-12 06:35:45 -04004744 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004745
Byongho Leeee221842015-12-15 01:42:10 +09004746 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004747 unsigned long updates = trans->delayed_ref_updates;
4748 if (updates) {
4749 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004750 ret = btrfs_run_delayed_refs(trans, fs_info,
4751 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004752 if (ret && !err)
4753 err = ret;
4754 }
4755 }
Yan, Zheng80825102009-11-12 09:35:36 +00004756 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004757}
4758
Chris Masona52d9a82007-08-27 16:49:44 -04004759/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004761 * @inode - inode that we're zeroing
4762 * @from - the offset to start zeroing
4763 * @len - the length to zero, 0 to zero the entire range respective to the
4764 * offset
4765 * @front - zero up to the offset instead of from the offset on
4766 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004768 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004769 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304770int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004771 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004774 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4776 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004777 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004778 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004779 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004781 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004783 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004784 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004785 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 u64 block_start;
4787 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004788
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004789 if (IS_ALIGNED(offset, blocksize) &&
4790 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004791 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792
Josef Bacik8b62f872017-10-19 14:15:55 -04004793 block_start = round_down(from, blocksize);
4794 block_end = block_start + blocksize - 1;
4795
Qu Wenruo364ecf32017-02-27 15:10:38 +08004796 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004797 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004798 if (ret)
4799 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004800
Chris Mason211c17f2008-05-15 09:13:45 -04004801again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004802 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004803 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004804 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004805 block_start, blocksize);
4806 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004807 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004808 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004809 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004810
Chris Masona52d9a82007-08-27 16:49:44 -04004811 if (!PageUptodate(page)) {
4812 ret = btrfs_readpage(NULL, page);
4813 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004814 if (page->mapping != mapping) {
4815 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004816 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004817 goto again;
4818 }
Chris Masona52d9a82007-08-27 16:49:44 -04004819 if (!PageUptodate(page)) {
4820 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004821 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004822 }
4823 }
Chris Mason211c17f2008-05-15 09:13:45 -04004824 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004827 set_page_extent_mapped(page);
4828
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004830 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004832 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004833 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004834 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004835 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004836 btrfs_put_ordered_extent(ordered);
4837 goto again;
4838 }
4839
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004841 EXTENT_DIRTY | EXTENT_DELALLOC |
4842 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004843 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004844
Filipe Mananae3b8a482017-11-04 00:16:59 +00004845 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004846 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004847 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304848 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004849 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004850 goto out_unlock;
4851 }
4852
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304853 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004854 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004856 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004857 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304858 memset(kaddr + (block_start - page_offset(page)),
4859 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004860 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304861 memset(kaddr + (block_start - page_offset(page)) + offset,
4862 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004863 flush_dcache_page(page);
4864 kunmap(page);
4865 }
Chris Mason247e7432008-07-17 12:53:51 -04004866 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004867 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004868 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004869
Chris Mason89642222008-07-24 09:41:53 -04004870out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004871 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004872 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304873 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004874 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004875 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004876 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004877out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004878 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004879 return ret;
4880}
4881
Josef Bacik16e75492013-10-22 12:18:51 -04004882static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4883 u64 offset, u64 len)
4884{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004886 struct btrfs_trans_handle *trans;
4887 int ret;
4888
4889 /*
4890 * Still need to make sure the inode looks like it's been updated so
4891 * that any holes get logged if we fsync.
4892 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004893 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4894 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004895 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4896 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4897 return 0;
4898 }
4899
4900 /*
4901 * 1 - for the one we're dropping
4902 * 1 - for the one we're adding
4903 * 1 - for updating the inode.
4904 */
4905 trans = btrfs_start_transaction(root, 3);
4906 if (IS_ERR(trans))
4907 return PTR_ERR(trans);
4908
4909 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4910 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004911 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004912 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004913 return ret;
4914 }
4915
David Sterbaf85b7372017-01-20 14:54:07 +01004916 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4917 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004918 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004919 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004920 else
4921 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004922 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004923 return ret;
4924}
4925
Josef Bacik695a0d02011-03-04 15:46:53 -05004926/*
4927 * This function puts in dummy file extents for the area we're creating a hole
4928 * for. So if we are truncating this file to a larger size we need to insert
4929 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4930 * the range between oldsize and size
4931 */
Josef Bacika41ad392011-01-31 15:30:16 -05004932int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004935 struct btrfs_root *root = BTRFS_I(inode)->root;
4936 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004937 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004938 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004939 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004940 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4941 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004942 u64 last_byte;
4943 u64 cur_offset;
4944 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004945 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004946
Josef Bacika71754f2013-06-17 17:14:39 -04004947 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304948 * If our size started in the middle of a block we need to zero out the
4949 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004950 * expose stale data.
4951 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304952 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004953 if (err)
4954 return err;
4955
Yan Zheng9036c102008-10-30 14:19:41 -04004956 if (size <= hole_start)
4957 return 0;
4958
Yan Zheng9036c102008-10-30 14:19:41 -04004959 while (1) {
4960 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004961
David Sterbaff13db42015-12-03 14:30:40 +01004962 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004963 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004964 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004965 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004966 if (!ordered)
4967 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004968 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004969 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004970 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004971 btrfs_put_ordered_extent(ordered);
4972 }
4973
Yan Zheng9036c102008-10-30 14:19:41 -04004974 cur_offset = hole_start;
4975 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004976 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004977 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004978 if (IS_ERR(em)) {
4979 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004980 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004981 break;
4982 }
Yan Zheng9036c102008-10-30 14:19:41 -04004983 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004984 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004985 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004987 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004988
Josef Bacik16e75492013-10-22 12:18:51 -04004989 err = maybe_insert_hole(root, inode, cur_offset,
4990 hole_size);
4991 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004992 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004993 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004994 cur_offset + hole_size - 1, 0);
4995 hole_em = alloc_extent_map();
4996 if (!hole_em) {
4997 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4998 &BTRFS_I(inode)->runtime_flags);
4999 goto next;
5000 }
5001 hole_em->start = cur_offset;
5002 hole_em->len = hole_size;
5003 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005004
Josef Bacik5dc562c2012-08-17 13:14:17 -04005005 hole_em->block_start = EXTENT_MAP_HOLE;
5006 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005007 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005008 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005009 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005010 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005011 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005012
5013 while (1) {
5014 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005015 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005016 write_unlock(&em_tree->lock);
5017 if (err != -EEXIST)
5018 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005019 btrfs_drop_extent_cache(BTRFS_I(inode),
5020 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005021 cur_offset +
5022 hole_size - 1, 0);
5023 }
5024 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005025 }
Josef Bacik16e75492013-10-22 12:18:51 -04005026next:
Yan Zheng9036c102008-10-30 14:19:41 -04005027 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005028 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005029 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005030 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005031 break;
5032 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005033 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005034 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005035 return err;
5036}
5037
Eric Sandeen3972f262013-01-12 02:57:22 +00005038static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005039{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005040 struct btrfs_root *root = BTRFS_I(inode)->root;
5041 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005042 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005043 loff_t newsize = attr->ia_size;
5044 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005045 int ret;
5046
Eric Sandeen3972f262013-01-12 02:57:22 +00005047 /*
5048 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5049 * special case where we need to update the times despite not having
5050 * these flags set. For all other operations the VFS set these flags
5051 * explicitly if it wants a timestamp update.
5052 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005053 if (newsize != oldsize) {
5054 inode_inc_iversion(inode);
5055 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5056 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005057 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005058 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005059
Josef Bacika41ad392011-01-31 15:30:16 -05005060 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005061 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005062 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005063 * This is to ensure the snapshot captures a fully consistent
5064 * state of this file - if the snapshot captures this expanding
5065 * truncation, it must capture all writes that happened before
5066 * this truncation.
5067 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005068 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005069 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005070 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005071 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005072 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 }
Yan, Zheng80825102009-11-12 09:35:36 +00005074
Miao Xief4a2f4c2011-12-14 20:12:01 -05005075 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005076 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005077 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005078 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005079 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005080
5081 i_size_write(inode, newsize);
5082 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305083 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005084 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005085 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005086 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005087 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005088
Josef Bacika41ad392011-01-31 15:30:16 -05005089 /*
5090 * We're truncating a file that used to have good data down to
5091 * zero. Make sure it gets into the ordered flush list so that
5092 * any new writes get down to disk quickly.
5093 */
5094 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005095 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5096 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005097
Josef Bacikf3fe8202013-01-07 17:03:21 -05005098 /*
5099 * 1 for the orphan item we're going to add
5100 * 1 for the orphan item deletion.
5101 */
5102 trans = btrfs_start_transaction(root, 2);
5103 if (IS_ERR(trans))
5104 return PTR_ERR(trans);
5105
5106 /*
5107 * We need to do this in case we fail at _any_ point during the
5108 * actual truncate. Once we do the truncate_setsize we could
5109 * invalidate pages which forces any outstanding ordered io to
5110 * be instantly completed which will give us extents that need
5111 * to be truncated. If we fail to get an orphan inode down we
5112 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005113 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005114 * will be consistent.
5115 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005116 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005117 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005118 if (ret)
5119 return ret;
5120
Josef Bacika41ad392011-01-31 15:30:16 -05005121 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5122 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005123
5124 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005125 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005126 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005127 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005128
Josef Bacika41ad392011-01-31 15:30:16 -05005129 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005130 if (ret && inode->i_nlink) {
5131 int err;
5132
Liu Bo19fd2df2016-12-01 13:01:02 -08005133 /* To get a stable disk_i_size */
5134 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5135 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005136 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005137 return err;
5138 }
5139
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005140 /*
5141 * failed to truncate, disk_i_size is only adjusted down
5142 * as we remove extents, so it should represent the true
5143 * size of the inode, so reset the in memory size and
5144 * delete our orphan entry.
5145 */
5146 trans = btrfs_join_transaction(root);
5147 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005148 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005149 return ret;
5150 }
5151 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005152 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005153 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005154 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005155 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005156 }
Yan, Zheng80825102009-11-12 09:35:36 +00005157 }
5158
Josef Bacika41ad392011-01-31 15:30:16 -05005159 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005160}
5161
Chris Mason39279cc2007-06-12 06:35:45 -04005162static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5163{
David Howells2b0143b2015-03-17 22:25:59 +00005164 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005165 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005166 int err;
5167
Li Zefanb83cc962010-12-20 16:04:08 +08005168 if (btrfs_root_readonly(root))
5169 return -EROFS;
5170
Jan Kara31051c82016-05-26 16:55:18 +02005171 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005172 if (err)
5173 return err;
5174
Chris Mason5a3f23d2009-03-31 13:27:11 -04005175 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005176 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005177 if (err)
5178 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005179 }
Yan Zheng9036c102008-10-30 14:19:41 -04005180
Christoph Hellwig10257742010-06-04 11:30:02 +02005181 if (attr->ia_valid) {
5182 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005183 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005184 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005185
Josef Bacik22c44fe2011-11-30 10:45:38 -05005186 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005187 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005188 }
5189
Chris Mason39279cc2007-06-12 06:35:45 -04005190 return err;
5191}
Chris Mason61295eb2008-01-14 16:24:38 -05005192
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005193/*
5194 * While truncating the inode pages during eviction, we get the VFS calling
5195 * btrfs_invalidatepage() against each page of the inode. This is slow because
5196 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5197 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5198 * extent_state structures over and over, wasting lots of time.
5199 *
5200 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5201 * those expensive operations on a per page basis and do only the ordered io
5202 * finishing, while we release here the extent_map and extent_state structures,
5203 * without the excessive merging and splitting.
5204 */
5205static void evict_inode_truncate_pages(struct inode *inode)
5206{
5207 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5208 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5209 struct rb_node *node;
5210
5211 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005212 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005213
5214 write_lock(&map_tree->lock);
5215 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5216 struct extent_map *em;
5217
5218 node = rb_first(&map_tree->map);
5219 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005220 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5221 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 remove_extent_mapping(map_tree, em);
5223 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005224 if (need_resched()) {
5225 write_unlock(&map_tree->lock);
5226 cond_resched();
5227 write_lock(&map_tree->lock);
5228 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005229 }
5230 write_unlock(&map_tree->lock);
5231
Filipe Manana6ca07092015-05-26 00:55:42 +01005232 /*
5233 * Keep looping until we have no more ranges in the io tree.
5234 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005235 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5236 * still in progress (unlocked the pages in the bio but did not yet
5237 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005238 * ranges can still be locked and eviction started because before
5239 * submitting those bios, which are executed by a separate task (work
5240 * queue kthread), inode references (inode->i_count) were not taken
5241 * (which would be dropped in the end io callback of each bio).
5242 * Therefore here we effectively end up waiting for those bios and
5243 * anyone else holding locked ranges without having bumped the inode's
5244 * reference count - if we don't do it, when they access the inode's
5245 * io_tree to unlock a range it may be too late, leading to an
5246 * use-after-free issue.
5247 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248 spin_lock(&io_tree->lock);
5249 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5250 struct extent_state *state;
5251 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005252 u64 start;
5253 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005254
5255 node = rb_first(&io_tree->state);
5256 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005257 start = state->start;
5258 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005259 spin_unlock(&io_tree->lock);
5260
David Sterbaff13db42015-12-03 14:30:40 +01005261 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005262
5263 /*
5264 * If still has DELALLOC flag, the extent didn't reach disk,
5265 * and its reserved space won't be freed by delayed_ref.
5266 * So we need to free its reserved space here.
5267 * (Refer to comment in btrfs_invalidatepage, case 2)
5268 *
5269 * Note, end is the bytenr of last byte, so we need + 1 here.
5270 */
5271 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005272 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005273
Filipe Manana6ca07092015-05-26 00:55:42 +01005274 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 EXTENT_LOCKED | EXTENT_DIRTY |
5276 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005277 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278
Filipe Manana7064dd52014-08-08 02:47:05 +01005279 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005280 spin_lock(&io_tree->lock);
5281 }
5282 spin_unlock(&io_tree->lock);
5283}
5284
Al Virobd555972010-06-07 11:35:40 -04005285void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005287 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005288 struct btrfs_trans_handle *trans;
5289 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005290 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005291 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005292 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005293 int ret;
5294
liubo1abe9b82011-03-24 11:18:59 +00005295 trace_btrfs_inode_evict(inode);
5296
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005297 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005298 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005299 return;
5300 }
5301
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005302 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005303
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005304 evict_inode_truncate_pages(inode);
5305
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005306 if (inode->i_nlink &&
5307 ((btrfs_root_refs(&root->root_item) != 0 &&
5308 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005309 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005310 goto no_delete;
5311
Chris Mason39279cc2007-06-12 06:35:45 -04005312 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005313 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005314 goto no_delete;
5315 }
Al Virobd555972010-06-07 11:35:40 -04005316 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005317 if (!special_file(inode->i_mode))
5318 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005319
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005320 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005321
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005322 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005323 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005324 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005325 goto no_delete;
5326 }
5327
Yan, Zheng76dda932009-09-21 16:00:26 -04005328 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005329 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5330 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005331 goto no_delete;
5332 }
5333
Nikolay Borisovaa790212017-01-10 20:35:40 +02005334 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005335 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005336 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005337 goto no_delete;
5338 }
5339
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005340 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005341 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005342 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005343 goto no_delete;
5344 }
Josef Bacik4a338542011-08-29 11:01:31 -04005345 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005346 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005348
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005349 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005350
Josef Bacik4289a662011-08-05 13:22:24 -04005351 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005352 * This is a bit simpler than btrfs_truncate since we've already
5353 * reserved our space for our orphan item in the unlink, so we just
5354 * need to reserve some slack space in case we add bytes and update
5355 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005356 */
Yan, Zheng80825102009-11-12 09:35:36 +00005357 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005358 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5359 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005360
Josef Bacik726c35f2011-09-26 15:46:06 -04005361 /*
5362 * Try and steal from the global reserve since we will
5363 * likely not use this space anyway, we want to try as
5364 * hard as possible to get this to work.
5365 */
5366 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005367 steal_from_global++;
5368 else
5369 steal_from_global = 0;
5370 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005371
Josef Bacik3bce8762015-02-24 12:35:51 -08005372 /*
5373 * steal_from_global == 0: we reserved stuff, hooray!
5374 * steal_from_global == 1: we didn't reserve stuff, boo!
5375 * steal_from_global == 2: we've committed, still not a lot of
5376 * room but maybe we'll have room in the global reserve this
5377 * time.
5378 * steal_from_global == 3: abandon all hope!
5379 */
5380 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 btrfs_warn(fs_info,
5382 "Could not get space for a delete, will truncate on mount %d",
5383 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005384 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005386 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005387 }
5388
Miao Xie0e8c36a2012-12-19 06:59:51 +00005389 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005390 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005391 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005392 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005393 goto no_delete;
5394 }
5395
Josef Bacik3bce8762015-02-24 12:35:51 -08005396 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005397 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005398 * sure there is room to do it, if not we need to commit and try
5399 * again.
5400 */
5401 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005402 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005403 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005404 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005405 else
5406 ret = -ENOSPC;
5407 }
5408
5409 /*
5410 * Couldn't steal from the global reserve, we have too much
5411 * pending stuff built up, commit the transaction and try it
5412 * again.
5413 */
5414 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005415 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005416 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005417 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005418 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005419 goto no_delete;
5420 }
5421 continue;
5422 } else {
5423 steal_from_global = 0;
5424 }
5425
Josef Bacik4289a662011-08-05 13:22:24 -04005426 trans->block_rsv = rsv;
5427
Yan, Zhengd68fc572010-05-16 10:49:58 -04005428 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005429 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005430 break;
5431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005432 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005433 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005434 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005435 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005436 }
5437
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005438 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005439
Josef Bacik4ef31a42013-08-13 14:10:08 -04005440 /*
5441 * Errors here aren't a big deal, it just means we leave orphan items
5442 * in the tree. They will be cleaned up on the next mount.
5443 */
Yan, Zheng80825102009-11-12 09:35:36 +00005444 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005445 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005446 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005447 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005448 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005449 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005451 trans->block_rsv = &fs_info->trans_block_rsv;
5452 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005453 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005454 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005455
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005456 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005457 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005458no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005459 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005460 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005461}
5462
5463/*
5464 * this returns the key found in the dir entry in the location pointer.
5465 * If no dir entries were found, location->objectid is 0.
5466 */
5467static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5468 struct btrfs_key *location)
5469{
5470 const char *name = dentry->d_name.name;
5471 int namelen = dentry->d_name.len;
5472 struct btrfs_dir_item *di;
5473 struct btrfs_path *path;
5474 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005475 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005476
5477 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005478 if (!path)
5479 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005480
David Sterbaf85b7372017-01-20 14:54:07 +01005481 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5482 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005483 if (IS_ERR(di))
5484 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005485
David Sterbac7040052011-04-19 18:00:01 +02005486 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005487 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005488
Chris Mason5f39d392007-10-15 16:14:19 -04005489 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005490 if (location->type != BTRFS_INODE_ITEM_KEY &&
5491 location->type != BTRFS_ROOT_ITEM_KEY) {
5492 btrfs_warn(root->fs_info,
5493"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5494 __func__, name, btrfs_ino(BTRFS_I(dir)),
5495 location->objectid, location->type, location->offset);
5496 goto out_err;
5497 }
Chris Mason39279cc2007-06-12 06:35:45 -04005498out:
Chris Mason39279cc2007-06-12 06:35:45 -04005499 btrfs_free_path(path);
5500 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005501out_err:
5502 location->objectid = 0;
5503 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005504}
5505
5506/*
5507 * when we hit a tree root in a directory, the btrfs part of the inode
5508 * needs to be changed to reflect the root directory of the tree root. This
5509 * is kind of like crossing a mount point.
5510 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005511static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512 struct inode *dir,
5513 struct dentry *dentry,
5514 struct btrfs_key *location,
5515 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005516{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517 struct btrfs_path *path;
5518 struct btrfs_root *new_root;
5519 struct btrfs_root_ref *ref;
5520 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005521 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 int ret;
5523 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005524
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 path = btrfs_alloc_path();
5526 if (!path) {
5527 err = -ENOMEM;
5528 goto out;
5529 }
Chris Mason39279cc2007-06-12 06:35:45 -04005530
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005532 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5533 key.type = BTRFS_ROOT_REF_KEY;
5534 key.offset = location->objectid;
5535
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005536 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005537 if (ret) {
5538 if (ret < 0)
5539 err = ret;
5540 goto out;
5541 }
Chris Mason39279cc2007-06-12 06:35:45 -04005542
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543 leaf = path->nodes[0];
5544 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005545 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5547 goto out;
5548
5549 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5550 (unsigned long)(ref + 1),
5551 dentry->d_name.len);
5552 if (ret)
5553 goto out;
5554
David Sterbab3b4aa72011-04-21 01:20:15 +02005555 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005556
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005557 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005558 if (IS_ERR(new_root)) {
5559 err = PTR_ERR(new_root);
5560 goto out;
5561 }
5562
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005563 *sub_root = new_root;
5564 location->objectid = btrfs_root_dirid(&new_root->root_item);
5565 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005566 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005567 err = 0;
5568out:
5569 btrfs_free_path(path);
5570 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005571}
5572
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573static void inode_tree_add(struct inode *inode)
5574{
5575 struct btrfs_root *root = BTRFS_I(inode)->root;
5576 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 struct rb_node **p;
5578 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005579 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005580 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581
Al Viro1d3382cb2010-10-23 15:19:20 -04005582 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005583 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005584 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005586 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 while (*p) {
5588 parent = *p;
5589 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5590
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005591 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005592 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005593 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005594 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 else {
5596 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005597 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005598 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005599 RB_CLEAR_NODE(parent);
5600 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005601 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 }
5603 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005604 rb_link_node(new, parent, p);
5605 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 spin_unlock(&root->inode_lock);
5607}
5608
5609static void inode_tree_del(struct inode *inode)
5610{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005611 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005613 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005615 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005619 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005621 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005622
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005623 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005624 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005625 spin_lock(&root->inode_lock);
5626 empty = RB_EMPTY_ROOT(&root->inode_tree);
5627 spin_unlock(&root->inode_lock);
5628 if (empty)
5629 btrfs_add_dead_root(root);
5630 }
5631}
5632
Jeff Mahoney143bede2012-03-01 14:56:26 +01005633void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005634{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005635 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005636 struct rb_node *node;
5637 struct rb_node *prev;
5638 struct btrfs_inode *entry;
5639 struct inode *inode;
5640 u64 objectid = 0;
5641
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005642 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005643 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005644
5645 spin_lock(&root->inode_lock);
5646again:
5647 node = root->inode_tree.rb_node;
5648 prev = NULL;
5649 while (node) {
5650 prev = node;
5651 entry = rb_entry(node, struct btrfs_inode, rb_node);
5652
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005653 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005654 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005655 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 node = node->rb_right;
5657 else
5658 break;
5659 }
5660 if (!node) {
5661 while (prev) {
5662 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005663 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005664 node = prev;
5665 break;
5666 }
5667 prev = rb_next(prev);
5668 }
5669 }
5670 while (node) {
5671 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005672 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005673 inode = igrab(&entry->vfs_inode);
5674 if (inode) {
5675 spin_unlock(&root->inode_lock);
5676 if (atomic_read(&inode->i_count) > 1)
5677 d_prune_aliases(inode);
5678 /*
Al Viro45321ac2010-06-07 13:43:19 -04005679 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005680 * the inode cache when its usage count
5681 * hits zero.
5682 */
5683 iput(inode);
5684 cond_resched();
5685 spin_lock(&root->inode_lock);
5686 goto again;
5687 }
5688
5689 if (cond_resched_lock(&root->inode_lock))
5690 goto again;
5691
5692 node = rb_next(node);
5693 }
5694 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005695}
5696
Chris Masone02119d2008-09-05 16:13:11 -04005697static int btrfs_init_locked_inode(struct inode *inode, void *p)
5698{
5699 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005700 inode->i_ino = args->location->objectid;
5701 memcpy(&BTRFS_I(inode)->location, args->location,
5702 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005703 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005704 return 0;
5705}
5706
5707static int btrfs_find_actor(struct inode *inode, void *opaque)
5708{
5709 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005710 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005711 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005712}
5713
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005714static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005715 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005716 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005717{
5718 struct inode *inode;
5719 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005720 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005721
Chris Mason90d3e592014-01-09 17:28:00 -08005722 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005723 args.root = root;
5724
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005725 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005726 btrfs_init_locked_inode,
5727 (void *)&args);
5728 return inode;
5729}
5730
Balaji Rao1a54ef82008-07-21 02:01:04 +05305731/* Get an inode object given its location and corresponding root.
5732 * Returns in *is_new if the inode was read from disk
5733 */
5734struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005735 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305736{
5737 struct inode *inode;
5738
Chris Mason90d3e592014-01-09 17:28:00 -08005739 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305740 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005741 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305742
5743 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005744 int ret;
5745
5746 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005747 if (!is_bad_inode(inode)) {
5748 inode_tree_add(inode);
5749 unlock_new_inode(inode);
5750 if (new)
5751 *new = 1;
5752 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005753 unlock_new_inode(inode);
5754 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005755 ASSERT(ret < 0);
5756 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005757 }
5758 }
5759
Balaji Rao1a54ef82008-07-21 02:01:04 +05305760 return inode;
5761}
5762
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763static struct inode *new_simple_dir(struct super_block *s,
5764 struct btrfs_key *key,
5765 struct btrfs_root *root)
5766{
5767 struct inode *inode = new_inode(s);
5768
5769 if (!inode)
5770 return ERR_PTR(-ENOMEM);
5771
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005772 BTRFS_I(inode)->root = root;
5773 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005774 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005775
5776 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005777 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005778 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005779 inode->i_fop = &simple_dir_operations;
5780 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005781 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305782 inode->i_atime = inode->i_mtime;
5783 inode->i_ctime = inode->i_mtime;
5784 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005785
5786 return inode;
5787}
5788
Chris Mason3de45862008-11-17 21:02:50 -05005789struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005790{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005791 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005792 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005794 struct btrfs_root *sub_root = root;
5795 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005796 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005797 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005798
5799 if (dentry->d_name.len > BTRFS_NAME_LEN)
5800 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005801
Jeff Layton39e3c952012-11-28 11:30:53 -05005802 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 if (ret < 0)
5804 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005805
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005806 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005807 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005808
5809 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005810 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005812 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005813
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005814 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005815 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005816 &location, &sub_root);
5817 if (ret < 0) {
5818 if (ret != -ENOENT)
5819 inode = ERR_PTR(ret);
5820 else
5821 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5822 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005823 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005824 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005825 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005826
Julia Lawall34d19ba2011-01-24 19:55:19 +00005827 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005828 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005829 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005830 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005831 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005832 if (ret) {
5833 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005834 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005835 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005836 }
5837
Chris Mason3de45862008-11-17 21:02:50 -05005838 return inode;
5839}
5840
Nick Pigginfe15ce42011-01-07 17:49:23 +11005841static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005842{
5843 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005844 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005845
Li Zefan848cce02012-02-21 17:04:28 +08005846 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005847 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005848
Li Zefan848cce02012-02-21 17:04:28 +08005849 if (inode) {
5850 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005851 if (btrfs_root_refs(&root->root_item) == 0)
5852 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005853
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005854 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005855 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005856 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005857 return 0;
5858}
5859
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005860static void btrfs_dentry_release(struct dentry *dentry)
5861{
Daeseok Youn944a4512014-04-14 15:37:02 +09005862 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005863}
5864
Chris Mason3de45862008-11-17 21:02:50 -05005865static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005866 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005867{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005868 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005869
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005870 inode = btrfs_lookup_dentry(dir, dentry);
5871 if (IS_ERR(inode)) {
5872 if (PTR_ERR(inode) == -ENOENT)
5873 inode = NULL;
5874 else
5875 return ERR_CAST(inode);
5876 }
5877
Al Viro41d28bc2014-10-12 22:24:21 -04005878 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005879}
5880
Miao Xie16cdcec2011-04-22 18:12:22 +08005881unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005882 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5883};
5884
Josef Bacik23b5ec72017-07-24 15:14:25 -04005885/*
5886 * All this infrastructure exists because dir_emit can fault, and we are holding
5887 * the tree lock when doing readdir. For now just allocate a buffer and copy
5888 * our information into that, and then dir_emit from the buffer. This is
5889 * similar to what NFS does, only we don't keep the buffer around in pagecache
5890 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5891 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5892 * tree lock.
5893 */
5894static int btrfs_opendir(struct inode *inode, struct file *file)
5895{
5896 struct btrfs_file_private *private;
5897
5898 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5899 if (!private)
5900 return -ENOMEM;
5901 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5902 if (!private->filldir_buf) {
5903 kfree(private);
5904 return -ENOMEM;
5905 }
5906 file->private_data = private;
5907 return 0;
5908}
5909
5910struct dir_entry {
5911 u64 ino;
5912 u64 offset;
5913 unsigned type;
5914 int name_len;
5915};
5916
5917static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5918{
5919 while (entries--) {
5920 struct dir_entry *entry = addr;
5921 char *name = (char *)(entry + 1);
5922
5923 ctx->pos = entry->offset;
5924 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5925 entry->type))
5926 return 1;
5927 addr += sizeof(struct dir_entry) + entry->name_len;
5928 ctx->pos++;
5929 }
5930 return 0;
5931}
5932
Al Viro9cdda8d2013-05-22 16:48:09 -04005933static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005934{
Al Viro9cdda8d2013-05-22 16:48:09 -04005935 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005936 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005937 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005938 struct btrfs_dir_item *di;
5939 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005940 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005941 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005942 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005943 struct list_head ins_list;
5944 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005945 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005946 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005947 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005948 char *name_ptr;
5949 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005950 int entries = 0;
5951 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005952 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005953 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005954
Al Viro9cdda8d2013-05-22 16:48:09 -04005955 if (!dir_emit_dots(file, ctx))
5956 return 0;
5957
David Woodhouse49593bf2008-08-17 17:08:36 +01005958 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005959 if (!path)
5960 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005961
Josef Bacik23b5ec72017-07-24 15:14:25 -04005962 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005963 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005964
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005965 INIT_LIST_HEAD(&ins_list);
5966 INIT_LIST_HEAD(&del_list);
5967 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005968
Josef Bacik23b5ec72017-07-24 15:14:25 -04005969again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005970 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005971 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005972 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005973
Chris Mason39279cc2007-06-12 06:35:45 -04005974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5975 if (ret < 0)
5976 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005977
5978 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005979 struct dir_entry *entry;
5980
Chris Mason5f39d392007-10-15 16:14:19 -04005981 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005982 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005983 if (slot >= btrfs_header_nritems(leaf)) {
5984 ret = btrfs_next_leaf(root, path);
5985 if (ret < 0)
5986 goto err;
5987 else if (ret > 0)
5988 break;
5989 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005990 }
Chris Mason3de45862008-11-17 21:02:50 -05005991
Chris Mason5f39d392007-10-15 16:14:19 -04005992 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5993
5994 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005995 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005996 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005997 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005998 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005999 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006000 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006001 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006002 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006003 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006004 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6005 PAGE_SIZE) {
6006 btrfs_release_path(path);
6007 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6008 if (ret)
6009 goto nopos;
6010 addr = private->filldir_buf;
6011 entries = 0;
6012 total_len = 0;
6013 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006014 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006015
6016 entry = addr;
6017 entry->name_len = name_len;
6018 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006019 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6020 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006021 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006022 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006023 entry->ino = location.objectid;
6024 entry->offset = found_key.offset;
6025 entries++;
6026 addr += sizeof(struct dir_entry) + name_len;
6027 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006028next:
6029 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006030 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006031 btrfs_release_path(path);
6032
6033 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6034 if (ret)
6035 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006036
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006037 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006038 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006039 goto nopos;
6040
Zach Browndb62efb2013-07-11 16:19:42 -07006041 /*
6042 * Stop new entries from being returned after we return the last
6043 * entry.
6044 *
6045 * New directory entries are assigned a strictly increasing
6046 * offset. This means that new entries created during readdir
6047 * are *guaranteed* to be seen in the future by that readdir.
6048 * This has broken buggy programs which operate on names as
6049 * they're returned by readdir. Until we re-use freed offsets
6050 * we have this hack to stop new entries from being returned
6051 * under the assumption that they'll never reach this huge
6052 * offset.
6053 *
6054 * This is being careful not to overflow 32bit loff_t unless the
6055 * last entry requires it because doing so has broken 32bit apps
6056 * in the past.
6057 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006058 if (ctx->pos >= INT_MAX)
6059 ctx->pos = LLONG_MAX;
6060 else
6061 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006062nopos:
6063 ret = 0;
6064err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006065 if (put)
6066 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006067 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006068 return ret;
6069}
6070
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006071int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006072{
6073 struct btrfs_root *root = BTRFS_I(inode)->root;
6074 struct btrfs_trans_handle *trans;
6075 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006076 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006077
Josef Bacik72ac3c02012-05-23 14:13:11 -04006078 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006079 return 0;
6080
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006081 if (btrfs_fs_closing(root->fs_info) &&
6082 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006083 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006084
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006085 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006086 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006087 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006088 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006089 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006090 if (IS_ERR(trans))
6091 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006092 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006093 }
6094 return ret;
6095}
6096
6097/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006098 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006099 * inode changes. But, it is most likely to find the inode in cache.
6100 * FIXME, needs more benchmarking...there are no reasons other than performance
6101 * to keep or drop this code.
6102 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006103static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006104{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006105 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006106 struct btrfs_root *root = BTRFS_I(inode)->root;
6107 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006108 int ret;
6109
Josef Bacik72ac3c02012-05-23 14:13:11 -04006110 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006111 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006112
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006113 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006114 if (IS_ERR(trans))
6115 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006116
6117 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006118 if (ret && ret == -ENOSPC) {
6119 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006120 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006121 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006122 if (IS_ERR(trans))
6123 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006124
Chris Mason94b60442010-05-26 11:02:00 -04006125 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006126 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006127 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006128 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006129 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006130
6131 return ret;
6132}
6133
6134/*
6135 * This is a copy of file_update_time. We need this so we can return error on
6136 * ENOSPC for updating the inode in the case of file write and mmap writes.
6137 */
Josef Bacike41f9412012-03-26 09:46:47 -04006138static int btrfs_update_time(struct inode *inode, struct timespec *now,
6139 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006140{
Alexander Block2bc5565282012-06-15 09:49:33 +02006141 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006142 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006143
6144 if (btrfs_root_readonly(root))
6145 return -EROFS;
6146
Josef Bacike41f9412012-03-26 09:46:47 -04006147 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006148 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006149 if (flags & S_CTIME)
6150 inode->i_ctime = *now;
6151 if (flags & S_MTIME)
6152 inode->i_mtime = *now;
6153 if (flags & S_ATIME)
6154 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006155 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006156}
6157
Chris Masond352ac62008-09-29 15:18:18 -04006158/*
6159 * find the highest existing sequence number in a directory
6160 * and then set the in-memory index_cnt variable to reflect
6161 * free sequence numbers
6162 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006163static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006164{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006165 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006166 struct btrfs_key key, found_key;
6167 struct btrfs_path *path;
6168 struct extent_buffer *leaf;
6169 int ret;
6170
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006171 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006172 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006173 key.offset = (u64)-1;
6174
6175 path = btrfs_alloc_path();
6176 if (!path)
6177 return -ENOMEM;
6178
6179 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6180 if (ret < 0)
6181 goto out;
6182 /* FIXME: we should be able to handle this */
6183 if (ret == 0)
6184 goto out;
6185 ret = 0;
6186
6187 /*
6188 * MAGIC NUMBER EXPLANATION:
6189 * since we search a directory based on f_pos we have to start at 2
6190 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6191 * else has to start at 2
6192 */
6193 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006194 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006195 goto out;
6196 }
6197
6198 path->slots[0]--;
6199
6200 leaf = path->nodes[0];
6201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6202
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006203 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006204 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006205 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006206 goto out;
6207 }
6208
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006209 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006210out:
6211 btrfs_free_path(path);
6212 return ret;
6213}
6214
Chris Masond352ac62008-09-29 15:18:18 -04006215/*
6216 * helper to find a free sequence number in a given directory. This current
6217 * code is very simple, later versions will do smarter things in the btree
6218 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006219int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006220{
6221 int ret = 0;
6222
Nikolay Borisov877574e2017-02-20 13:50:33 +02006223 if (dir->index_cnt == (u64)-1) {
6224 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006225 if (ret) {
6226 ret = btrfs_set_inode_index_count(dir);
6227 if (ret)
6228 return ret;
6229 }
Josef Bacikaec74772008-07-24 12:12:38 -04006230 }
6231
Nikolay Borisov877574e2017-02-20 13:50:33 +02006232 *index = dir->index_cnt;
6233 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006234
6235 return ret;
6236}
6237
Chris Masonb0d5d102014-09-08 13:08:51 -07006238static int btrfs_insert_inode_locked(struct inode *inode)
6239{
6240 struct btrfs_iget_args args;
6241 args.location = &BTRFS_I(inode)->location;
6242 args.root = BTRFS_I(inode)->root;
6243
6244 return insert_inode_locked4(inode,
6245 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6246 btrfs_find_actor, &args);
6247}
6248
Anand Jain19aee8d2017-07-18 17:37:05 +08006249/*
6250 * Inherit flags from the parent inode.
6251 *
6252 * Currently only the compression flags and the cow flags are inherited.
6253 */
6254static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6255{
6256 unsigned int flags;
6257
6258 if (!dir)
6259 return;
6260
6261 flags = BTRFS_I(dir)->flags;
6262
6263 if (flags & BTRFS_INODE_NOCOMPRESS) {
6264 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6265 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6266 } else if (flags & BTRFS_INODE_COMPRESS) {
6267 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6268 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6269 }
6270
6271 if (flags & BTRFS_INODE_NODATACOW) {
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6273 if (S_ISREG(inode->i_mode))
6274 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6275 }
6276
6277 btrfs_update_iflags(inode);
6278}
6279
Chris Mason39279cc2007-06-12 06:35:45 -04006280static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6281 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006282 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006283 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006284 u64 ref_objectid, u64 objectid,
6285 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006287 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006289 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006290 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006291 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006292 struct btrfs_inode_ref *ref;
6293 struct btrfs_key key[2];
6294 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006295 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006296 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006297 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006298
Chris Mason5f39d392007-10-15 16:14:19 -04006299 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006300 if (!path)
6301 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006303 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006304 if (!inode) {
6305 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006306 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006307 }
Chris Mason39279cc2007-06-12 06:35:45 -04006308
Li Zefan581bb052011-04-20 10:06:11 +08006309 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006310 * O_TMPFILE, set link count to 0, so that after this point,
6311 * we fill in an inode item with the correct link count.
6312 */
6313 if (!name)
6314 set_nlink(inode, 0);
6315
6316 /*
Li Zefan581bb052011-04-20 10:06:11 +08006317 * we have to initialize this early, so we can reclaim the inode
6318 * number if we fail afterwards in this function.
6319 */
6320 inode->i_ino = objectid;
6321
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006322 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006323 trace_btrfs_inode_request(dir);
6324
Nikolay Borisov877574e2017-02-20 13:50:33 +02006325 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006326 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006327 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006328 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006329 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006330 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006331 } else if (dir) {
6332 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006333 }
6334 /*
6335 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006336 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006337 * number
6338 */
6339 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006340 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006341 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006342 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006343 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006344
Josef Bacik5dc562c2012-08-17 13:14:17 -04006345 /*
6346 * We could have gotten an inode number from somebody who was fsynced
6347 * and then removed in this same transaction, so let's just set full
6348 * sync since it will be a full sync anyway and this will blow away the
6349 * old info in the log.
6350 */
6351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6352
Chris Mason9c583092008-01-29 15:15:18 -05006353 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006354 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006355 key[0].offset = 0;
6356
Chris Mason9c583092008-01-29 15:15:18 -05006357 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006358
6359 if (name) {
6360 /*
6361 * Start new inodes with an inode_ref. This is slightly more
6362 * efficient for small numbers of hard links since they will
6363 * be packed into one item. Extended refs will kick in if we
6364 * add more hard links than can fit in the ref item.
6365 */
6366 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006367 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006368 key[1].offset = ref_objectid;
6369
6370 sizes[1] = name_len + sizeof(*ref);
6371 }
Chris Mason9c583092008-01-29 15:15:18 -05006372
Chris Masonb0d5d102014-09-08 13:08:51 -07006373 location = &BTRFS_I(inode)->location;
6374 location->objectid = objectid;
6375 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006376 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006377
6378 ret = btrfs_insert_inode_locked(inode);
6379 if (ret < 0)
6380 goto fail;
6381
Chris Masonb9473432009-03-13 11:00:37 -04006382 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006384 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006386
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006387 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006388 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306389
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006390 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306391 inode->i_atime = inode->i_mtime;
6392 inode->i_ctime = inode->i_mtime;
6393 BTRFS_I(inode)->i_otime = inode->i_mtime;
6394
Chris Mason5f39d392007-10-15 16:14:19 -04006395 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6396 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006397 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006398 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006399 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006400
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006401 if (name) {
6402 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6403 struct btrfs_inode_ref);
6404 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6405 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6406 ptr = (unsigned long)(ref + 1);
6407 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6408 }
Chris Mason9c583092008-01-29 15:15:18 -05006409
Chris Mason5f39d392007-10-15 16:14:19 -04006410 btrfs_mark_buffer_dirty(path->nodes[0]);
6411 btrfs_free_path(path);
6412
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006413 btrfs_inherit_iflags(inode, dir);
6414
Al Viro569254b2011-07-24 17:08:40 -04006415 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006416 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006417 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006418 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006419 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6420 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006421 }
6422
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006423 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006424
6425 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006426 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006427
Alexander Block8ea05e32012-07-25 17:35:53 +02006428 btrfs_update_root_times(trans, root);
6429
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006430 ret = btrfs_inode_inherit_props(trans, inode, dir);
6431 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006433 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006434 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006435
Chris Mason39279cc2007-06-12 06:35:45 -04006436 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006437
6438fail_unlock:
6439 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006440fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006441 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006442 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006443 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006444 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006445 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006446}
6447
6448static inline u8 btrfs_inode_type(struct inode *inode)
6449{
6450 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6451}
6452
Chris Masond352ac62008-09-29 15:18:18 -04006453/*
6454 * utility function to add 'inode' into 'parent_inode' with
6455 * a give name and a given sequence number.
6456 * if 'add_backref' is true, also insert a backref from the
6457 * inode to the parent directory.
6458 */
Chris Masone02119d2008-09-05 16:13:11 -04006459int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006460 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006461 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006462{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006464 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006465 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006466 struct btrfs_root *root = parent_inode->root;
6467 u64 ino = btrfs_ino(inode);
6468 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006469
Li Zefan33345d012011-04-20 10:31:50 +08006470 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006471 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006472 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006473 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006474 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006475 key.offset = 0;
6476 }
Chris Mason39279cc2007-06-12 06:35:45 -04006477
Li Zefan33345d012011-04-20 10:31:50 +08006478 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006479 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6480 root->root_key.objectid, parent_ino,
6481 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006482 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006483 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6484 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006485 }
6486
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006487 /* Nothing to clean up yet */
6488 if (ret)
6489 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006490
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006491 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6492 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006493 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006494 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006495 goto fail_dir_item;
6496 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006497 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006499 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006501 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006502 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006503 inode_inc_iversion(&parent_inode->vfs_inode);
6504 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6505 current_time(&parent_inode->vfs_inode);
6506 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006507 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006508 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006509 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006510
6511fail_dir_item:
6512 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6513 u64 local_index;
6514 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006515 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6516 root->root_key.objectid, parent_ino,
6517 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006518
6519 } else if (add_backref) {
6520 u64 local_index;
6521 int err;
6522
6523 err = btrfs_del_inode_ref(trans, root, name, name_len,
6524 ino, parent_ino, &local_index);
6525 }
6526 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006527}
6528
6529static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006530 struct btrfs_inode *dir, struct dentry *dentry,
6531 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006532{
Josef Bacika1b075d2010-11-19 20:36:11 +00006533 int err = btrfs_add_link(trans, dir, inode,
6534 dentry->d_name.name, dentry->d_name.len,
6535 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006536 if (err > 0)
6537 err = -EEXIST;
6538 return err;
6539}
6540
Josef Bacik618e21d2007-07-11 10:18:17 -04006541static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006542 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006543{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006544 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006545 struct btrfs_trans_handle *trans;
6546 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006547 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006548 int err;
6549 int drop_inode = 0;
6550 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006551 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006552
Josef Bacik9ed74f22009-09-11 16:12:44 -04006553 /*
6554 * 2 for inode item and ref
6555 * 2 for dir items
6556 * 1 for xattr if selinux is on
6557 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006558 trans = btrfs_start_transaction(root, 5);
6559 if (IS_ERR(trans))
6560 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006561
Li Zefan581bb052011-04-20 10:06:11 +08006562 err = btrfs_find_free_ino(root, &objectid);
6563 if (err)
6564 goto out_unlock;
6565
Josef Bacikaec74772008-07-24 12:12:38 -04006566 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006567 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6568 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006569 if (IS_ERR(inode)) {
6570 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006571 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006572 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006573
Casey Schauflerad19db72011-12-15 10:09:07 -05006574 /*
6575 * If the active LSM wants to access the inode during
6576 * d_instantiate it needs these. Smack checks to see
6577 * if the filesystem supports xattrs by looking at the
6578 * ops vector.
6579 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006580 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006581 init_special_inode(inode, inode->i_mode, rdev);
6582
6583 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006585 goto out_unlock_inode;
6586
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006587 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6588 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006589 if (err) {
6590 goto out_unlock_inode;
6591 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006592 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006593 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006594 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006595 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006596
Josef Bacik618e21d2007-07-11 10:18:17 -04006597out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006598 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006599 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006600 if (drop_inode) {
6601 inode_dec_link_count(inode);
6602 iput(inode);
6603 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006604 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006605
6606out_unlock_inode:
6607 drop_inode = 1;
6608 unlock_new_inode(inode);
6609 goto out_unlock;
6610
Josef Bacik618e21d2007-07-11 10:18:17 -04006611}
6612
Chris Mason39279cc2007-06-12 06:35:45 -04006613static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006614 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006615{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006616 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006617 struct btrfs_trans_handle *trans;
6618 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006619 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006620 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006621 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006622 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006623 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006624
Josef Bacik9ed74f22009-09-11 16:12:44 -04006625 /*
6626 * 2 for inode item and ref
6627 * 2 for dir items
6628 * 1 for xattr if selinux is on
6629 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006630 trans = btrfs_start_transaction(root, 5);
6631 if (IS_ERR(trans))
6632 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006633
Li Zefan581bb052011-04-20 10:06:11 +08006634 err = btrfs_find_free_ino(root, &objectid);
6635 if (err)
6636 goto out_unlock;
6637
Josef Bacikaec74772008-07-24 12:12:38 -04006638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006639 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6640 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006641 if (IS_ERR(inode)) {
6642 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006644 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006645 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006646 /*
6647 * If the active LSM wants to access the inode during
6648 * d_instantiate it needs these. Smack checks to see
6649 * if the filesystem supports xattrs by looking at the
6650 * ops vector.
6651 */
6652 inode->i_fop = &btrfs_file_operations;
6653 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006654 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006655
6656 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6657 if (err)
6658 goto out_unlock_inode;
6659
6660 err = btrfs_update_inode(trans, root, inode);
6661 if (err)
6662 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006663
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006664 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6665 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006666 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006667 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006668
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006669 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006670 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006671 d_instantiate(dentry, inode);
6672
Chris Mason39279cc2007-06-12 06:35:45 -04006673out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006674 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006675 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006676 inode_dec_link_count(inode);
6677 iput(inode);
6678 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006679 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006681
6682out_unlock_inode:
6683 unlock_new_inode(inode);
6684 goto out_unlock;
6685
Chris Mason39279cc2007-06-12 06:35:45 -04006686}
6687
6688static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6689 struct dentry *dentry)
6690{
Filipe Manana271dba42016-01-05 16:24:05 +00006691 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006692 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006693 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006695 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006696 int err;
6697 int drop_inode = 0;
6698
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006699 /* do not allow sys_link's with other subvols of the same device */
6700 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006701 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006702
Mark Fashehf1863732012-08-08 11:32:27 -07006703 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006704 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006705
Nikolay Borisov877574e2017-02-20 13:50:33 +02006706 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006707 if (err)
6708 goto fail;
6709
Yan, Zhenga22285a2010-05-16 10:48:46 -04006710 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006711 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006713 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006715 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 if (IS_ERR(trans)) {
6717 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006718 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006719 goto fail;
6720 }
Chris Mason5f39d392007-10-15 16:14:19 -04006721
Miao Xie67de1172013-12-26 13:07:06 +08006722 /* There are several dir indexes for this inode, clear the cache. */
6723 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006724 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006725 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006726 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006727 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006728 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006729
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006730 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6731 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006732
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006734 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006736 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006737 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006738 if (err)
6739 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006740 if (inode->i_nlink == 1) {
6741 /*
6742 * If new hard link count is 1, it's a file created
6743 * with open(2) O_TMPFILE flag.
6744 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006745 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006746 if (err)
6747 goto fail;
6748 }
Al Viro08c422c2011-12-23 07:58:13 -05006749 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006750 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006751 }
Chris Mason39279cc2007-06-12 06:35:45 -04006752
Chris Mason1832a6d2007-12-21 16:27:21 -05006753fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006754 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006755 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006756 if (drop_inode) {
6757 inode_dec_link_count(inode);
6758 iput(inode);
6759 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006760 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006761 return err;
6762}
6763
Al Viro18bb1db2011-07-26 01:41:39 -04006764static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006765{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006766 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006767 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006768 struct btrfs_trans_handle *trans;
6769 struct btrfs_root *root = BTRFS_I(dir)->root;
6770 int err = 0;
6771 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006772 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006773 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006774
Josef Bacik9ed74f22009-09-11 16:12:44 -04006775 /*
6776 * 2 items for inode and ref
6777 * 2 items for dir items
6778 * 1 for xattr if selinux is on
6779 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006780 trans = btrfs_start_transaction(root, 5);
6781 if (IS_ERR(trans))
6782 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006783
Li Zefan581bb052011-04-20 10:06:11 +08006784 err = btrfs_find_free_ino(root, &objectid);
6785 if (err)
6786 goto out_fail;
6787
Josef Bacikaec74772008-07-24 12:12:38 -04006788 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006789 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6790 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006791 if (IS_ERR(inode)) {
6792 err = PTR_ERR(inode);
6793 goto out_fail;
6794 }
Chris Mason5f39d392007-10-15 16:14:19 -04006795
Chris Mason39279cc2007-06-12 06:35:45 -04006796 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006797 /* these must be set before we unlock the inode */
6798 inode->i_op = &btrfs_dir_inode_operations;
6799 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006800
Eric Paris2a7dba32011-02-01 11:05:39 -05006801 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006802 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006803 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006804
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006805 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006806 err = btrfs_update_inode(trans, root, inode);
6807 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006808 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006809
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006810 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6811 dentry->d_name.name,
6812 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006813 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006814 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006815
Chris Mason39279cc2007-06-12 06:35:45 -04006816 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006817 /*
6818 * mkdir is special. We're unlocking after we call d_instantiate
6819 * to avoid a race with nfsd calling d_instantiate.
6820 */
6821 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006822 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006823
6824out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006825 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006826 if (drop_on_err) {
6827 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006828 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006829 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006830 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006831 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006832
6833out_fail_inode:
6834 unlock_new_inode(inode);
6835 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006836}
6837
Chris Masonc8b97812008-10-29 14:49:59 -04006838static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006839 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006840 size_t pg_offset, u64 extent_offset,
6841 struct btrfs_file_extent_item *item)
6842{
6843 int ret;
6844 struct extent_buffer *leaf = path->nodes[0];
6845 char *tmp;
6846 size_t max_size;
6847 unsigned long inline_size;
6848 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006849 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006850
6851 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006852 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006853 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6854 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006855 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006856 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006857 if (!tmp)
6858 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006859 ptr = btrfs_file_extent_inline_start(item);
6860
6861 read_extent_buffer(leaf, tmp, ptr, inline_size);
6862
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006863 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006864 ret = btrfs_decompress(compress_type, tmp, page,
6865 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006866
6867 /*
6868 * decompression code contains a memset to fill in any space between the end
6869 * of the uncompressed data and the end of max_size in case the decompressed
6870 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6871 * the end of an inline extent and the beginning of the next block, so we
6872 * cover that region here.
6873 */
6874
6875 if (max_size + pg_offset < PAGE_SIZE) {
6876 char *map = kmap(page);
6877 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6878 kunmap(page);
6879 }
Chris Masonc8b97812008-10-29 14:49:59 -04006880 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006881 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006882}
6883
Chris Masond352ac62008-09-29 15:18:18 -04006884/*
6885 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006886 * the ugly parts come from merging extents from the disk with the in-ram
6887 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006888 * where the in-ram extents might be locked pending data=ordered completion.
6889 *
6890 * This also copies inline extents directly into the page.
6891 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006892struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6893 struct page *page,
6894 size_t pg_offset, u64 start, u64 len,
6895 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006896{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006898 int ret;
6899 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006900 u64 extent_start = 0;
6901 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006902 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006903 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006904 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006905 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006906 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006907 struct extent_buffer *leaf;
6908 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006910 struct extent_map_tree *em_tree = &inode->extent_tree;
6911 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006912 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006913
Chris Mason890871b2009-09-02 16:24:52 -04006914 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006915 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006916 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006917 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006918 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006919
Chris Masona52d9a82007-08-27 16:49:44 -04006920 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006921 if (em->start > start || em->start + em->len <= start)
6922 free_extent_map(em);
6923 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006924 free_extent_map(em);
6925 else
6926 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 }
David Sterba172ddd62011-04-21 00:48:27 +02006928 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006929 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006930 err = -ENOMEM;
6931 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006932 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006933 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006934 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006935 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006936 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006937 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006938
6939 if (!path) {
6940 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006941 if (!path) {
6942 err = -ENOMEM;
6943 goto out;
6944 }
6945 /*
6946 * Chances are we'll be called again, so go ahead and do
6947 * readahead
6948 */
David Sterbae4058b52015-11-27 16:31:35 +01006949 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006950 }
6951
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006952 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006953 if (ret < 0) {
6954 err = ret;
6955 goto out;
6956 }
6957
6958 if (ret != 0) {
6959 if (path->slots[0] == 0)
6960 goto not_found;
6961 path->slots[0]--;
6962 }
6963
Chris Mason5f39d392007-10-15 16:14:19 -04006964 leaf = path->nodes[0];
6965 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006966 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006967 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006969 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006970 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006971 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006972 /*
6973 * If we backup past the first extent we want to move forward
6974 * and see if there is an extent in front of us, otherwise we'll
6975 * say there is a hole for our whole search range which can
6976 * cause problems.
6977 */
6978 extent_end = start;
6979 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006980 }
6981
Chris Mason5f39d392007-10-15 16:14:19 -04006982 found_type = btrfs_file_extent_type(leaf, item);
6983 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006984 if (found_type == BTRFS_FILE_EXTENT_REG ||
6985 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006986 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006987 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006988
6989 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6990 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006991 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6992 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006993 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006994 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006995 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006996
6997 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6998 path->slots[0],
6999 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007000 }
Josef Bacik25a50342013-10-14 12:08:38 -04007001next:
Yan Zheng9036c102008-10-30 14:19:41 -04007002 if (start >= extent_end) {
7003 path->slots[0]++;
7004 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7005 ret = btrfs_next_leaf(root, path);
7006 if (ret < 0) {
7007 err = ret;
7008 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007009 }
Yan Zheng9036c102008-10-30 14:19:41 -04007010 if (ret > 0)
7011 goto not_found;
7012 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007013 }
Yan Zheng9036c102008-10-30 14:19:41 -04007014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7015 if (found_key.objectid != objectid ||
7016 found_key.type != BTRFS_EXTENT_DATA_KEY)
7017 goto not_found;
7018 if (start + len <= found_key.offset)
7019 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007020 if (start > found_key.offset)
7021 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007022 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007023 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007024 em->len = found_key.offset - start;
7025 goto not_found_em;
7026 }
7027
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007028 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007029 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007030
Yan Zhengd899e052008-10-30 14:25:28 -04007031 if (found_type == BTRFS_FILE_EXTENT_REG ||
7032 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007033 goto insert;
7034 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007035 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007036 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007037 size_t size;
7038 size_t extent_offset;
7039 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007040
Filipe Manana7ffbb592014-06-09 03:48:05 +01007041 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007042 goto out;
Yan689f9342007-10-29 11:41:07 -04007043
Chris Mason514ac8a2014-01-03 21:07:00 -08007044 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007045 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007046 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7047 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007048 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007049 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007050 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007051 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007052 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007053 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007054 if (btrfs_file_extent_compression(leaf, item) !=
7055 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007056 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007057 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007058 if (ret) {
7059 err = ret;
7060 goto out;
7061 }
Chris Masonc8b97812008-10-29 14:49:59 -04007062 } else {
7063 map = kmap(page);
7064 read_extent_buffer(leaf, map + pg_offset, ptr,
7065 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007066 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007067 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007068 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007069 copy_size);
7070 }
Chris Masonc8b97812008-10-29 14:49:59 -04007071 kunmap(page);
7072 }
Chris Mason179e29e2007-11-01 11:28:41 -04007073 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007074 }
Chris Masond1310b22008-01-24 16:13:08 -05007075 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007076 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007077 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007078 }
7079not_found:
7080 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007081 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007082 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007083not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007084 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007085insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007086 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007087 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007088 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007089 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7090 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007091 err = -EIO;
7092 goto out;
7093 }
Chris Masond1310b22008-01-24 16:13:08 -05007094
7095 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007096 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007097 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007098 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007099out:
liubo1abe9b82011-03-24 11:18:59 +00007100
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007101 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007102
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007103 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007104 if (err) {
7105 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007106 return ERR_PTR(err);
7107 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007108 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007109 return em;
7110}
7111
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007112struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7113 struct page *page,
7114 size_t pg_offset, u64 start, u64 len,
7115 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007116{
7117 struct extent_map *em;
7118 struct extent_map *hole_em = NULL;
7119 u64 range_start = start;
7120 u64 end;
7121 u64 found;
7122 u64 found_end;
7123 int err = 0;
7124
7125 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7126 if (IS_ERR(em))
7127 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007128 /*
7129 * If our em maps to:
7130 * - a hole or
7131 * - a pre-alloc extent,
7132 * there might actually be delalloc bytes behind it.
7133 */
7134 if (em->block_start != EXTENT_MAP_HOLE &&
7135 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7136 return em;
7137 else
7138 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007139
7140 /* check to see if we've wrapped (len == -1 or similar) */
7141 end = start + len;
7142 if (end < start)
7143 end = (u64)-1;
7144 else
7145 end -= 1;
7146
7147 em = NULL;
7148
7149 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007150 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007151 end, len, EXTENT_DELALLOC, 1);
7152 found_end = range_start + found;
7153 if (found_end < range_start)
7154 found_end = (u64)-1;
7155
7156 /*
7157 * we didn't find anything useful, return
7158 * the original results from get_extent()
7159 */
7160 if (range_start > end || found_end <= start) {
7161 em = hole_em;
7162 hole_em = NULL;
7163 goto out;
7164 }
7165
7166 /* adjust the range_start to make sure it doesn't
7167 * go backwards from the start they passed in
7168 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307169 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007170 found = found_end - range_start;
7171
7172 if (found > 0) {
7173 u64 hole_start = start;
7174 u64 hole_len = len;
7175
David Sterba172ddd62011-04-21 00:48:27 +02007176 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007177 if (!em) {
7178 err = -ENOMEM;
7179 goto out;
7180 }
7181 /*
7182 * when btrfs_get_extent can't find anything it
7183 * returns one huge hole
7184 *
7185 * make sure what it found really fits our range, and
7186 * adjust to make sure it is based on the start from
7187 * the caller
7188 */
7189 if (hole_em) {
7190 u64 calc_end = extent_map_end(hole_em);
7191
7192 if (calc_end <= start || (hole_em->start > end)) {
7193 free_extent_map(hole_em);
7194 hole_em = NULL;
7195 } else {
7196 hole_start = max(hole_em->start, start);
7197 hole_len = calc_end - hole_start;
7198 }
7199 }
7200 em->bdev = NULL;
7201 if (hole_em && range_start > hole_start) {
7202 /* our hole starts before our delalloc, so we
7203 * have to return just the parts of the hole
7204 * that go until the delalloc starts
7205 */
7206 em->len = min(hole_len,
7207 range_start - hole_start);
7208 em->start = hole_start;
7209 em->orig_start = hole_start;
7210 /*
7211 * don't adjust block start at all,
7212 * it is fixed at EXTENT_MAP_HOLE
7213 */
7214 em->block_start = hole_em->block_start;
7215 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007216 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7217 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007218 } else {
7219 em->start = range_start;
7220 em->len = found;
7221 em->orig_start = range_start;
7222 em->block_start = EXTENT_MAP_DELALLOC;
7223 em->block_len = found;
7224 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007225 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007226 return hole_em;
7227 }
7228out:
7229
7230 free_extent_map(hole_em);
7231 if (err) {
7232 free_extent_map(em);
7233 return ERR_PTR(err);
7234 }
7235 return em;
7236}
7237
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007238static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7239 const u64 start,
7240 const u64 len,
7241 const u64 orig_start,
7242 const u64 block_start,
7243 const u64 block_len,
7244 const u64 orig_block_len,
7245 const u64 ram_bytes,
7246 const int type)
7247{
7248 struct extent_map *em = NULL;
7249 int ret;
7250
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007251 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007252 em = create_io_em(inode, start, len, orig_start,
7253 block_start, block_len, orig_block_len,
7254 ram_bytes,
7255 BTRFS_COMPRESS_NONE, /* compress_type */
7256 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007257 if (IS_ERR(em))
7258 goto out;
7259 }
7260 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7261 len, block_len, type);
7262 if (ret) {
7263 if (em) {
7264 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007265 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007266 start + len - 1, 0);
7267 }
7268 em = ERR_PTR(ret);
7269 }
7270 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007271
7272 return em;
7273}
7274
Josef Bacik4b46fce2010-05-23 11:00:55 -04007275static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7276 u64 start, u64 len)
7277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007280 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 struct btrfs_key ins;
7282 u64 alloc_hint;
7283 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007284
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007286 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007287 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007288 if (ret)
7289 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007291 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7292 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007293 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007294 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007296 btrfs_free_reserved_extent(fs_info, ins.objectid,
7297 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007298
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299 return em;
7300}
7301
Chris Mason46bfbb52010-05-26 11:04:10 -04007302/*
7303 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7304 * block must be cow'd
7305 */
Josef Bacik00361582013-08-14 14:02:47 -04007306noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007307 u64 *orig_start, u64 *orig_block_len,
7308 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007309{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007311 struct btrfs_path *path;
7312 int ret;
7313 struct extent_buffer *leaf;
7314 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007315 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007316 struct btrfs_file_extent_item *fi;
7317 struct btrfs_key key;
7318 u64 disk_bytenr;
7319 u64 backref_offset;
7320 u64 extent_end;
7321 u64 num_bytes;
7322 int slot;
7323 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007324 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007325
Chris Mason46bfbb52010-05-26 11:04:10 -04007326 path = btrfs_alloc_path();
7327 if (!path)
7328 return -ENOMEM;
7329
David Sterbaf85b7372017-01-20 14:54:07 +01007330 ret = btrfs_lookup_file_extent(NULL, root, path,
7331 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 if (ret < 0)
7333 goto out;
7334
7335 slot = path->slots[0];
7336 if (ret == 1) {
7337 if (slot == 0) {
7338 /* can't find the item, must cow */
7339 ret = 0;
7340 goto out;
7341 }
7342 slot--;
7343 }
7344 ret = 0;
7345 leaf = path->nodes[0];
7346 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007347 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 key.type != BTRFS_EXTENT_DATA_KEY) {
7349 /* not our file or wrong item type, must cow */
7350 goto out;
7351 }
7352
7353 if (key.offset > offset) {
7354 /* Wrong offset, must cow */
7355 goto out;
7356 }
7357
7358 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7359 found_type = btrfs_file_extent_type(leaf, fi);
7360 if (found_type != BTRFS_FILE_EXTENT_REG &&
7361 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7362 /* not a regular extent, must cow */
7363 goto out;
7364 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007365
7366 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7367 goto out;
7368
Miao Xiee77751a2013-12-27 21:11:50 +08007369 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7370 if (extent_end <= offset)
7371 goto out;
7372
Chris Mason46bfbb52010-05-26 11:04:10 -04007373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007374 if (disk_bytenr == 0)
7375 goto out;
7376
7377 if (btrfs_file_extent_compression(leaf, fi) ||
7378 btrfs_file_extent_encryption(leaf, fi) ||
7379 btrfs_file_extent_other_encoding(leaf, fi))
7380 goto out;
7381
Chris Mason46bfbb52010-05-26 11:04:10 -04007382 backref_offset = btrfs_file_extent_offset(leaf, fi);
7383
Josef Bacik7ee9e442013-06-21 16:37:03 -04007384 if (orig_start) {
7385 *orig_start = key.offset - backref_offset;
7386 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7387 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7388 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007390 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007391 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007392
7393 num_bytes = min(offset + *len, extent_end) - offset;
7394 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7395 u64 range_end;
7396
Jeff Mahoneyda170662016-06-15 09:22:56 -04007397 range_end = round_up(offset + num_bytes,
7398 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007399 ret = test_range_bit(io_tree, offset, range_end,
7400 EXTENT_DELALLOC, 0, NULL);
7401 if (ret) {
7402 ret = -EAGAIN;
7403 goto out;
7404 }
7405 }
7406
Josef Bacik1bda19e2013-10-18 12:10:36 -04007407 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007408
7409 /*
7410 * look for other files referencing this extent, if we
7411 * find any we must cow
7412 */
Josef Bacik00361582013-08-14 14:02:47 -04007413
Liu Boe4c3b2d2017-01-30 12:25:28 -08007414 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007415 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007416 if (ret) {
7417 ret = 0;
7418 goto out;
7419 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007420
7421 /*
7422 * adjust disk_bytenr and num_bytes to cover just the bytes
7423 * in this extent we are about to write. If there
7424 * are any csums in that range we have to cow in order
7425 * to keep the csums correct
7426 */
7427 disk_bytenr += backref_offset;
7428 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007429 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7430 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007431 /*
7432 * all of the above have passed, it is safe to overwrite this extent
7433 * without cow
7434 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007435 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007436 ret = 1;
7437out:
7438 btrfs_free_path(path);
7439 return ret;
7440}
7441
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007442bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7443{
7444 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007445 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007446 void **pagep = NULL;
7447 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007448 unsigned long start_idx;
7449 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007450
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007451 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007452
7453 /*
7454 * end is the last byte in the last page. end == start is legal
7455 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007456 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007457
7458 rcu_read_lock();
7459
7460 /* Most of the code in this while loop is lifted from
7461 * find_get_page. It's been modified to begin searching from a
7462 * page and return just the first page found in that range. If the
7463 * found idx is less than or equal to the end idx then we know that
7464 * a page exists. If no pages are found or if those pages are
7465 * outside of the range then we're fine (yay!) */
7466 while (page == NULL &&
7467 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7468 page = radix_tree_deref_slot(pagep);
7469 if (unlikely(!page))
7470 break;
7471
7472 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007473 if (radix_tree_deref_retry(page)) {
7474 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007475 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007476 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007477 /*
7478 * Otherwise, shmem/tmpfs must be storing a swap entry
7479 * here as an exceptional entry: so return it without
7480 * attempting to raise page count.
7481 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007482 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007483 break; /* TODO: Is this relevant for this use case? */
7484 }
7485
Filipe Manana91405152014-06-05 13:22:24 +01007486 if (!page_cache_get_speculative(page)) {
7487 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007488 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007489 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007490
7491 /*
7492 * Has the page moved?
7493 * This is part of the lockless pagecache protocol. See
7494 * include/linux/pagemap.h for details.
7495 */
7496 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007497 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007498 page = NULL;
7499 }
7500 }
7501
7502 if (page) {
7503 if (page->index <= end_idx)
7504 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007505 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007506 }
7507
7508 rcu_read_unlock();
7509 return found;
7510}
7511
Josef Bacikeb838e72012-07-31 16:28:48 -04007512static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7513 struct extent_state **cached_state, int writing)
7514{
7515 struct btrfs_ordered_extent *ordered;
7516 int ret = 0;
7517
7518 while (1) {
7519 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007520 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007521 /*
7522 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007523 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007524 * extents in this range.
7525 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007526 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007527 lockend - lockstart + 1);
7528
7529 /*
7530 * We need to make sure there are no buffered pages in this
7531 * range either, we could have raced between the invalidate in
7532 * generic_file_direct_write and locking the extent. The
7533 * invalidate needs to happen so that reads after a write do not
7534 * get stale data.
7535 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007536 if (!ordered &&
7537 (!writing ||
7538 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 break;
7540
7541 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007542 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007543
7544 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007545 /*
7546 * If we are doing a DIO read and the ordered extent we
7547 * found is for a buffered write, we can not wait for it
7548 * to complete and retry, because if we do so we can
7549 * deadlock with concurrent buffered writes on page
7550 * locks. This happens only if our DIO read covers more
7551 * than one extent map, if at this point has already
7552 * created an ordered extent for a previous extent map
7553 * and locked its range in the inode's io tree, and a
7554 * concurrent write against that previous extent map's
7555 * range and this range started (we unlock the ranges
7556 * in the io tree only when the bios complete and
7557 * buffered writes always lock pages before attempting
7558 * to lock range in the io tree).
7559 */
7560 if (writing ||
7561 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7562 btrfs_start_ordered_extent(inode, ordered, 1);
7563 else
7564 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 btrfs_put_ordered_extent(ordered);
7566 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007568 * We could trigger writeback for this range (and wait
7569 * for it to complete) and then invalidate the pages for
7570 * this range (through invalidate_inode_pages2_range()),
7571 * but that can lead us to a deadlock with a concurrent
7572 * call to readpages() (a buffered read or a defrag call
7573 * triggered a readahead) on a page lock due to an
7574 * ordered dio extent we created before but did not have
7575 * yet a corresponding bio submitted (whence it can not
7576 * complete), which makes readpages() wait for that
7577 * ordered extent to complete while holding a lock on
7578 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007579 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007580 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007581 }
7582
Filipe Mananaade77022016-02-18 14:28:55 +00007583 if (ret)
7584 break;
7585
Josef Bacikeb838e72012-07-31 16:28:48 -04007586 cond_resched();
7587 }
7588
7589 return ret;
7590}
7591
Liu Bo6f9994d2017-01-31 07:50:22 -08007592/* The callers of this must take lock_extent() */
7593static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7594 u64 orig_start, u64 block_start,
7595 u64 block_len, u64 orig_block_len,
7596 u64 ram_bytes, int compress_type,
7597 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007598{
7599 struct extent_map_tree *em_tree;
7600 struct extent_map *em;
7601 struct btrfs_root *root = BTRFS_I(inode)->root;
7602 int ret;
7603
Liu Bo6f9994d2017-01-31 07:50:22 -08007604 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7605 type == BTRFS_ORDERED_COMPRESSED ||
7606 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007607 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007608
Josef Bacik69ffb542012-09-11 15:40:07 -04007609 em_tree = &BTRFS_I(inode)->extent_tree;
7610 em = alloc_extent_map();
7611 if (!em)
7612 return ERR_PTR(-ENOMEM);
7613
7614 em->start = start;
7615 em->orig_start = orig_start;
7616 em->len = len;
7617 em->block_len = block_len;
7618 em->block_start = block_start;
7619 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007620 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007621 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007622 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007623 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007624 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007625 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007626 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007627 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7628 em->compress_type = compress_type;
7629 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007630
7631 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007632 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007633 em->start + em->len - 1, 0);
7634 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007635 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007636 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007637 /*
7638 * The caller has taken lock_extent(), who could race with us
7639 * to add em?
7640 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007641 } while (ret == -EEXIST);
7642
7643 if (ret) {
7644 free_extent_map(em);
7645 return ERR_PTR(ret);
7646 }
7647
Liu Bo6f9994d2017-01-31 07:50:22 -08007648 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007649 return em;
7650}
7651
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7653 struct buffer_head *bh_result, int create)
7654{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307658 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007659 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007660 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007661 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007662 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007663 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664
Miao Xie172a5042013-02-21 02:48:22 -07007665 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007666 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007667 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007668 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007669
Josef Bacikc3298612012-08-03 16:49:19 -04007670 lockstart = start;
7671 lockend = start + len - 1;
7672
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007673 if (current->journal_info) {
7674 /*
7675 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007676 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007677 * confused.
7678 */
chandan50745b02015-08-28 21:10:13 +05307679 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007680 current->journal_info = NULL;
7681 }
7682
Josef Bacikeb838e72012-07-31 16:28:48 -04007683 /*
7684 * If this errors out it's because we couldn't invalidate pagecache for
7685 * this range and we need to fallback to buffered.
7686 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007687 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7688 create)) {
7689 ret = -ENOTBLK;
7690 goto err;
7691 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007692
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007693 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007694 if (IS_ERR(em)) {
7695 ret = PTR_ERR(em);
7696 goto unlock_err;
7697 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007698
7699 /*
7700 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7701 * io. INLINE is special, and we could probably kludge it in here, but
7702 * it's still buffered so for safety lets just fall back to the generic
7703 * buffered path.
7704 *
7705 * For COMPRESSED we _have_ to read the entire extent in so we can
7706 * decompress it, so there will be buffering required no matter what we
7707 * do, so go ahead and fallback to buffered.
7708 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007709 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710 * to buffered IO. Don't blame me, this is the price we pay for using
7711 * the generic code.
7712 */
7713 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7714 em->block_start == EXTENT_MAP_INLINE) {
7715 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007716 ret = -ENOTBLK;
7717 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718 }
7719
7720 /* Just a good old fashioned hole, return */
7721 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7722 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7723 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007724 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725 }
7726
7727 /*
7728 * We don't allocate a new extent in the following cases
7729 *
7730 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7731 * existing extent.
7732 * 2) The extent is marked as PREALLOC. We're good to go here and can
7733 * just use the extent.
7734 *
7735 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007736 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007737 len = min(len, em->len - (start - em->start));
7738 lockstart = start + len;
7739 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007740 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741
7742 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7743 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7744 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007746 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747
7748 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7749 type = BTRFS_ORDERED_PREALLOC;
7750 else
7751 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007752 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007754
Josef Bacik00361582013-08-14 14:02:47 -04007755 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007756 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007757 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007758 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007759
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007760 em2 = btrfs_create_dio_extent(inode, start, len,
7761 orig_start, block_start,
7762 len, orig_block_len,
7763 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007764 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007765 if (type == BTRFS_ORDERED_PREALLOC) {
7766 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007767 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007768 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007769 if (em2 && IS_ERR(em2)) {
7770 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007772 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007773 /*
7774 * For inode marked NODATACOW or extent marked PREALLOC,
7775 * use the existing or preallocated extent, so does not
7776 * need to adjust btrfs_space_info's bytes_may_use.
7777 */
7778 btrfs_free_reserved_data_space_noquota(inode,
7779 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007780 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007782 }
Josef Bacik00361582013-08-14 14:02:47 -04007783
Chris Mason46bfbb52010-05-26 11:04:10 -04007784 /*
7785 * this will cow the extent, reset the len in case we changed
7786 * it above
7787 */
7788 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007789 free_extent_map(em);
7790 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007791 if (IS_ERR(em)) {
7792 ret = PTR_ERR(em);
7793 goto unlock_err;
7794 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007795 len = min(len, em->len - (start - em->start));
7796unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007797 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7798 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007799 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800 bh_result->b_bdev = em->bdev;
7801 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007802 if (create) {
7803 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7804 set_buffer_new(bh_result);
7805
7806 /*
7807 * Need to update the i_size under the extent lock so buffered
7808 * readers will get the updated i_size when we unlock.
7809 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007810 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007811 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007812
chandan50745b02015-08-28 21:10:13 +05307813 WARN_ON(dio_data->reserve < len);
7814 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007815 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307816 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007817 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007818
Josef Bacikeb838e72012-07-31 16:28:48 -04007819 /*
7820 * In the case of write we need to clear and unlock the entire range,
7821 * in the case of read we need to unlock only the end area that we
7822 * aren't using if there is any left over space.
7823 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007824 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007825 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7826 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007827 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007828 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007829 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007830 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007831
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832 free_extent_map(em);
7833
7834 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007835
7836unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007837 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007838 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007839err:
chandan50745b02015-08-28 21:10:13 +05307840 if (dio_data)
7841 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007842 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843}
7844
Omar Sandoval58efbc92017-08-22 23:45:59 -07007845static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7846 struct bio *bio,
7847 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007848{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007849 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007850 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007851
Mike Christie37226b22016-06-05 14:31:52 -05007852 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007853
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007854 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007855 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007856 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007857
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007858 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007859
Miao Xie8b110e32014-09-12 18:44:03 +08007860 return ret;
7861}
7862
7863static int btrfs_check_dio_repairable(struct inode *inode,
7864 struct bio *failed_bio,
7865 struct io_failure_record *failrec,
7866 int failed_mirror)
7867{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007869 int num_copies;
7870
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007871 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007872 if (num_copies == 1) {
7873 /*
7874 * we only have a single copy of the data, so don't bother with
7875 * all the retry and error correction code that follows. no
7876 * matter what the error is, it is very likely to persist.
7877 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007878 btrfs_debug(fs_info,
7879 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7880 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007881 return 0;
7882 }
7883
7884 failrec->failed_mirror = failed_mirror;
7885 failrec->this_mirror++;
7886 if (failrec->this_mirror == failed_mirror)
7887 failrec->this_mirror++;
7888
7889 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007890 btrfs_debug(fs_info,
7891 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7892 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007893 return 0;
7894 }
7895
7896 return 1;
7897}
7898
Omar Sandoval58efbc92017-08-22 23:45:59 -07007899static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7900 struct page *page, unsigned int pgoff,
7901 u64 start, u64 end, int failed_mirror,
7902 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007903{
7904 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007905 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7906 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007907 struct bio *bio;
7908 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007909 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007910 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007911 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007912 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007913 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007914
Mike Christie37226b22016-06-05 14:31:52 -05007915 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007916
7917 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7918 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007919 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007920
7921 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7922 failed_mirror);
7923 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007924 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007925 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007926 }
7927
Liu Bo17347ce2017-05-15 15:33:27 -07007928 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007929 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007930 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007931 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007932 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007933
7934 isector = start - btrfs_io_bio(failed_bio)->logical;
7935 isector >>= inode->i_sb->s_blocksize_bits;
7936 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007938 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007939
7940 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007941 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007942 read_mode, failrec->this_mirror, failrec->in_validation);
7943
Omar Sandoval58efbc92017-08-22 23:45:59 -07007944 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7945 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007946 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007947 bio_put(bio);
7948 }
7949
Omar Sandoval58efbc92017-08-22 23:45:59 -07007950 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007951}
7952
7953struct btrfs_retry_complete {
7954 struct completion done;
7955 struct inode *inode;
7956 u64 start;
7957 int uptodate;
7958};
7959
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007960static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007961{
7962 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007963 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007964 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007965 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007966 int i;
7967
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007968 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007969 goto end;
7970
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307971 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007972 io_tree = &BTRFS_I(inode)->io_tree;
7973 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007974 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975
Miao Xie8b110e32014-09-12 18:44:03 +08007976 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007977 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007978 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007979 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7980 io_tree, done->start, bvec->bv_page,
7981 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007982end:
7983 complete(&done->done);
7984 bio_put(bio);
7985}
7986
Omar Sandoval58efbc92017-08-22 23:45:59 -07007987static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7988 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007989{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307990 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007991 struct bio_vec bvec;
7992 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007993 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007994 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 unsigned int pgoff;
7996 u32 sectorsize;
7997 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007998 blk_status_t ret;
7999 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008000
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008002 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003
Miao Xiec1dc0892014-09-12 18:43:56 +08008004 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008005 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008006 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008007
Liu Bo17347ce2017-05-15 15:33:27 -07008008 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8009 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8010 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308011
8012next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008013 done.uptodate = 0;
8014 done.start = start;
8015 init_completion(&done.done);
8016
Liu Bo17347ce2017-05-15 15:33:27 -07008017 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 pgoff, start, start + sectorsize - 1,
8019 io_bio->mirror_num,
8020 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008021 if (ret) {
8022 err = ret;
8023 goto next;
8024 }
Miao Xie8b110e32014-09-12 18:44:03 +08008025
David Sterba9c17f6c2017-07-19 19:26:45 +02008026 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008027
8028 if (!done.uptodate) {
8029 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008031 }
8032
Liu Bo629ebf42017-05-15 17:20:07 -07008033next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308034 start += sectorsize;
8035
Liu Bo97bf5a52017-04-07 13:11:10 -07008036 nr_sectors--;
8037 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008039 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308040 goto next_block_or_try_again;
8041 }
Miao Xie8b110e32014-09-12 18:44:03 +08008042 }
8043
Liu Bo629ebf42017-05-15 17:20:07 -07008044 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008045}
8046
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008047static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008048{
8049 struct btrfs_retry_complete *done = bio->bi_private;
8050 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008051 struct extent_io_tree *io_tree, *failure_tree;
8052 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008053 struct bio_vec *bvec;
8054 int uptodate;
8055 int ret;
8056 int i;
8057
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008058 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008059 goto end;
8060
8061 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008064 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065
Josef Bacik7870d082017-05-05 11:57:15 -04008066 io_tree = &BTRFS_I(inode)->io_tree;
8067 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8068
David Sterbac09abff2017-07-13 18:10:07 +02008069 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008070 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008071 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8072 bvec->bv_offset, done->start,
8073 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008074 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008075 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8076 failure_tree, io_tree, done->start,
8077 bvec->bv_page,
8078 btrfs_ino(BTRFS_I(inode)),
8079 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008080 else
8081 uptodate = 0;
8082 }
8083
8084 done->uptodate = uptodate;
8085end:
8086 complete(&done->done);
8087 bio_put(bio);
8088}
8089
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008090static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8091 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008092{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008094 struct bio_vec bvec;
8095 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008096 struct btrfs_retry_complete done;
8097 u64 start;
8098 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099 u32 sectorsize;
8100 int nr_sectors;
8101 unsigned int pgoff;
8102 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008103 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008104 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008105 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008106
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308107 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008108 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308109
Omar Sandoval58efbc92017-08-22 23:45:59 -07008110 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008111 start = io_bio->logical;
8112 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008113 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008114
Liu Bo17347ce2017-05-15 15:33:27 -07008115 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8116 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308117
Liu Bo17347ce2017-05-15 15:33:27 -07008118 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308119next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008120 if (uptodate) {
8121 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8122 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8123 bvec.bv_page, pgoff, start, sectorsize);
8124 if (likely(!ret))
8125 goto next;
8126 }
Miao Xie8b110e32014-09-12 18:44:03 +08008127try_again:
8128 done.uptodate = 0;
8129 done.start = start;
8130 init_completion(&done.done);
8131
Omar Sandoval58efbc92017-08-22 23:45:59 -07008132 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8133 pgoff, start, start + sectorsize - 1,
8134 io_bio->mirror_num, btrfs_retry_endio,
8135 &done);
8136 if (status) {
8137 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008138 goto next;
8139 }
8140
David Sterba9c17f6c2017-07-19 19:26:45 +02008141 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008142
8143 if (!done.uptodate) {
8144 /* We might have another mirror, so try again */
8145 goto try_again;
8146 }
8147next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308148 offset += sectorsize;
8149 start += sectorsize;
8150
8151 ASSERT(nr_sectors);
8152
Liu Bo97bf5a52017-04-07 13:11:10 -07008153 nr_sectors--;
8154 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308155 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008156 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308157 goto next_block;
8158 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008159 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008160
8161 return err;
8162}
8163
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008164static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8165 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008166{
8167 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8168
8169 if (skip_csum) {
8170 if (unlikely(err))
8171 return __btrfs_correct_data_nocsum(inode, io_bio);
8172 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008173 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008174 } else {
8175 return __btrfs_subio_endio_read(inode, io_bio, err);
8176 }
8177}
8178
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008179static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008180{
8181 struct btrfs_dio_private *dip = bio->bi_private;
8182 struct inode *inode = dip->inode;
8183 struct bio *dio_bio;
8184 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008185 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008186
Liu Bo99c4e3b2017-09-15 15:06:51 -06008187 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008188 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008189
Josef Bacik4b46fce2010-05-23 11:00:55 -04008190 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008191 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008192 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008193
Josef Bacik4b46fce2010-05-23 11:00:55 -04008194 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008195
Liu Bo99c4e3b2017-09-15 15:06:51 -06008196 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008197 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008198
8199 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008200 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008201 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008202}
8203
Qu Wenruo524272602017-03-08 10:25:52 +08008204static void __endio_write_update_ordered(struct inode *inode,
8205 const u64 offset, const u64 bytes,
8206 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008208 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008209 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008210 struct btrfs_workqueue *wq;
8211 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008212 u64 ordered_offset = offset;
8213 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008214 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008215 int ret;
8216
Qu Wenruo524272602017-03-08 10:25:52 +08008217 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8218 wq = fs_info->endio_freespace_worker;
8219 func = btrfs_freespace_write_helper;
8220 } else {
8221 wq = fs_info->endio_write_workers;
8222 func = btrfs_endio_write_helper;
8223 }
8224
Chris Mason163cf092010-11-28 19:56:33 -05008225again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008226 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008227 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8228 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008229 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008230 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008231 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008232 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233
Qu Wenruo524272602017-03-08 10:25:52 +08008234 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8235 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008236out_test:
8237 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008238 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8239 * in the range, we can exit.
8240 */
8241 if (ordered_offset == last_offset)
8242 return;
8243 /*
Chris Mason163cf092010-11-28 19:56:33 -05008244 * our bio might span multiple ordered extents. If we haven't
8245 * completed the accounting for the whole dio, go back and try again
8246 */
Filipe Manana14543772015-11-24 16:23:54 +00008247 if (ordered_offset < offset + bytes) {
8248 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008249 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008250 goto again;
8251 }
Filipe Manana14543772015-11-24 16:23:54 +00008252}
8253
8254static void btrfs_endio_direct_write(struct bio *bio)
8255{
8256 struct btrfs_dio_private *dip = bio->bi_private;
8257 struct bio *dio_bio = dip->dio_bio;
8258
Qu Wenruo524272602017-03-08 10:25:52 +08008259 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008260 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008261
Josef Bacik4b46fce2010-05-23 11:00:55 -04008262 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008263
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008264 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008265 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008266 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008267}
8268
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008269static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008270 struct bio *bio, int mirror_num,
8271 unsigned long bio_flags, u64 offset)
8272{
Josef Bacikc6100a42017-05-05 11:57:13 -04008273 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008274 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008275 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008276 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008277 return 0;
8278}
8279
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008280static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008281{
8282 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008283 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008284
Miao Xie8b110e32014-09-12 18:44:03 +08008285 if (err)
8286 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008287 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008288 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8289 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008290 (unsigned long long)bio->bi_iter.bi_sector,
8291 bio->bi_iter.bi_size, err);
8292
8293 if (dip->subio_endio)
8294 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008295
8296 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008297 dip->errors = 1;
8298
8299 /*
8300 * before atomic variable goto zero, we must make sure
8301 * dip->errors is perceived to be set.
8302 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008303 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008304 }
8305
8306 /* if there are more bios still pending for this dio, just exit */
8307 if (!atomic_dec_and_test(&dip->pending_bios))
8308 goto out;
8309
Chris Mason9be33952013-05-17 18:30:14 -04008310 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008311 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008312 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008313 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008314 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008315 }
8316out:
8317 bio_put(bio);
8318}
8319
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008320static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008321 struct btrfs_dio_private *dip,
8322 struct bio *bio,
8323 u64 file_offset)
8324{
8325 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8326 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008327 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008328
8329 /*
8330 * We load all the csum data we need when we submit
8331 * the first bio to reduce the csum tree search and
8332 * contention.
8333 */
8334 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008335 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008336 file_offset);
8337 if (ret)
8338 return ret;
8339 }
8340
8341 if (bio == dip->orig_bio)
8342 return 0;
8343
8344 file_offset -= dip->logical_offset;
8345 file_offset >>= inode->i_sb->s_blocksize_bits;
8346 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8347
8348 return 0;
8349}
8350
Omar Sandoval58efbc92017-08-22 23:45:59 -07008351static inline blk_status_t
8352__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008353 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008356 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008357 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008358 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008359
Liu Bo4c274bc2017-11-01 17:19:27 -06008360 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008361 if (async_submit)
8362 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8363
Josef Bacik5fd02042012-05-02 14:00:54 -04008364 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008365 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008366 if (ret)
8367 goto err;
8368 }
Miao Xiee65e1532010-11-22 03:04:43 +00008369
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008370 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008371 goto map;
8372
8373 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008374 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8375 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008376 __btrfs_submit_bio_start_direct_io,
8377 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008378 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008379 } else if (write) {
8380 /*
8381 * If we aren't doing async submit, calculate the csum of the
8382 * bio now.
8383 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008384 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008385 if (ret)
8386 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008387 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008388 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008389 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008390 if (ret)
8391 goto err;
8392 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008393map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008394 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008395err:
Miao Xiee65e1532010-11-22 03:04:43 +00008396 return ret;
8397}
8398
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008399static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008400{
8401 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008402 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008403 struct bio *bio;
8404 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008405 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008406 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008407 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008408 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008409 u64 submit_len;
8410 int clone_offset = 0;
8411 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308412 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008413 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008414
Kent Overstreet4f024f32013-10-11 15:44:27 -07008415 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008416 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008417 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8418 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008419 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008420 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008421
Liu Bo725130b2017-05-16 09:51:39 -07008422 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008423 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008424 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008425 goto submit;
8426 }
8427
David Woodhouse53b381b2013-01-29 18:40:14 -05008428 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008429 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008430 async_submit = 0;
8431 else
8432 async_submit = 1;
8433
Liu Bo725130b2017-05-16 09:51:39 -07008434 /* bio split */
8435 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008436 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008437 do {
Liu Bo725130b2017-05-16 09:51:39 -07008438 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008439
Liu Bo725130b2017-05-16 09:51:39 -07008440 /*
8441 * This will never fail as it's passing GPF_NOFS and
8442 * the allocation is backed by btrfs_bioset.
8443 */
Liu Boe4770942017-05-16 10:57:14 -07008444 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008445 clone_len);
8446 bio->bi_private = dip;
8447 bio->bi_end_io = btrfs_end_dio_bio;
8448 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008449
Liu Bo725130b2017-05-16 09:51:39 -07008450 ASSERT(submit_len >= clone_len);
8451 submit_len -= clone_len;
8452 if (submit_len == 0)
8453 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008454
Liu Bo725130b2017-05-16 09:51:39 -07008455 /*
8456 * Increase the count before we submit the bio so we know
8457 * the end IO handler won't happen before we increase the
8458 * count. Otherwise, the dip might get freed before we're
8459 * done setting it up.
8460 */
8461 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008462
Linus Torvalds66ba7722017-09-09 13:27:51 -07008463 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008464 async_submit);
8465 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008466 bio_put(bio);
8467 atomic_dec(&dip->pending_bios);
8468 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008469 }
Liu Bo725130b2017-05-16 09:51:39 -07008470
8471 clone_offset += clone_len;
8472 start_sector += clone_len >> 9;
8473 file_offset += clone_len;
8474
8475 map_length = submit_len;
8476 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8477 start_sector << 9, &map_length, NULL, 0);
8478 if (ret)
8479 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008480 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008481
Josef Bacik02f57c72011-04-06 14:25:44 -04008482submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008483 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008484 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008485 return 0;
8486
8487 bio_put(bio);
8488out_err:
8489 dip->errors = 1;
8490 /*
8491 * before atomic variable goto zero, we must
8492 * make sure dip->errors is perceived to be set.
8493 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008494 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008495 if (atomic_dec_and_test(&dip->pending_bios))
8496 bio_io_error(dip->orig_bio);
8497
8498 /* bio_end_io() will handle error, so we needn't return it */
8499 return 0;
8500}
8501
Mike Christie8a4c1e42016-06-05 14:31:50 -05008502static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8503 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008504{
Filipe Manana61de7182015-07-01 12:13:10 +01008505 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008506 struct bio *bio = NULL;
8507 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008508 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509 int ret = 0;
8510
David Sterba8b6c1d52017-06-02 17:48:13 +02008511 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008512
Miao Xiec1dc0892014-09-12 18:43:56 +08008513 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008514 if (!dip) {
8515 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008516 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008517 }
8518
8519 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008520 dip->inode = inode;
8521 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008522 dip->bytes = dio_bio->bi_iter.bi_size;
8523 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008524 bio->bi_private = dip;
8525 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008526 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008527 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008528 io_bio = btrfs_io_bio(bio);
8529 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008530
Miao Xiec1dc0892014-09-12 18:43:56 +08008531 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008532 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008533 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008534 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 dip->subio_endio = btrfs_subio_endio_read;
8536 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008537
Filipe Mananaf28a4922015-12-08 19:23:20 +00008538 /*
8539 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8540 * even if we fail to submit a bio, because in such case we do the
8541 * corresponding error handling below and it must not be done a second
8542 * time by btrfs_direct_IO().
8543 */
8544 if (write) {
8545 struct btrfs_dio_data *dio_data = current->journal_info;
8546
8547 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8548 dip->bytes;
8549 dio_data->unsubmitted_oe_range_start =
8550 dio_data->unsubmitted_oe_range_end;
8551 }
8552
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008553 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008554 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008555 return;
Chris Mason9be33952013-05-17 18:30:14 -04008556
Liu Bo3892ac92017-04-17 15:00:28 -07008557 if (io_bio->end_io)
8558 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008559
Josef Bacik4b46fce2010-05-23 11:00:55 -04008560free_ordered:
8561 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008562 * If we arrived here it means either we failed to submit the dip
8563 * or we either failed to clone the dio_bio or failed to allocate the
8564 * dip. If we cloned the dio_bio and allocated the dip, we can just
8565 * call bio_endio against our io_bio so that we get proper resource
8566 * cleanup if we fail to submit the dip, otherwise, we must do the
8567 * same as btrfs_endio_direct_[write|read] because we can't call these
8568 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008569 */
Liu Bo3892ac92017-04-17 15:00:28 -07008570 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008571 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008572 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008573 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008574 * and all the cleanup and final put for dio_bio (through
8575 * dio_end_io()).
8576 */
8577 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008578 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008579 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008580 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008581 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008582 file_offset,
8583 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008584 false);
Filipe Manana14543772015-11-24 16:23:54 +00008585 else
Filipe Manana61de7182015-07-01 12:13:10 +01008586 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8587 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008588
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008589 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008590 /*
8591 * Releases and cleans up our dio_bio, no need to bio_put()
8592 * nor bio_endio()/bio_io_error() against dio_bio.
8593 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008594 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595 }
Liu Bo3892ac92017-04-17 15:00:28 -07008596 if (bio)
8597 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008598 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008599}
8600
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008601static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008602 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008603{
8604 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008605 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008606 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008607 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008608
8609 if (offset & blocksize_mask)
8610 goto out;
8611
Al Viro28060d52014-03-22 05:15:17 -04008612 if (iov_iter_alignment(iter) & blocksize_mask)
8613 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008614
Al Viro28060d52014-03-22 05:15:17 -04008615 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008616 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008617 return 0;
8618 /*
8619 * Check to make sure we don't have duplicate iov_base's in this
8620 * iovec, if so return EINVAL, otherwise we'll get csum errors
8621 * when reading back.
8622 */
8623 for (seg = 0; seg < iter->nr_segs; seg++) {
8624 for (i = seg + 1; i < iter->nr_segs; i++) {
8625 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008626 goto out;
8627 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008628 }
8629 retval = 0;
8630out:
8631 return retval;
8632}
Josef Bacikeb838e72012-07-31 16:28:48 -04008633
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008634static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008635{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008636 struct file *file = iocb->ki_filp;
8637 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008638 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308639 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008640 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008641 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008642 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008643 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008644 bool wakeup = true;
8645 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008646 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008647
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008648 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008649 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008650
Jens Axboefe0f07d2015-04-15 17:05:48 -06008651 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008652
Josef Bacik0e267c42013-07-02 10:38:02 -04008653 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008654 * The generic stuff only does filemap_write_and_wait_range, which
8655 * isn't enough if we've written compressed pages to this area, so
8656 * we need to flush the dirty pages again to make absolutely sure
8657 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008658 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008659 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008660 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8661 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008662 filemap_fdatawrite_range(inode->i_mapping, offset,
8663 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008664
Omar Sandoval6f673762015-03-16 04:33:52 -07008665 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008666 /*
8667 * If the write DIO is beyond the EOF, we need update
8668 * the isize, but it is protected by i_mutex. So we can
8669 * not unlock the i_mutex at this case.
8670 */
8671 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008672 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008673 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008674 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008675 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8676 ret = -EAGAIN;
8677 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008678 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008679 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8680 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008681 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008682 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008683
8684 /*
8685 * We need to know how many extents we reserved so that we can
8686 * do the accounting properly if we go over the number we
8687 * originally calculated. Abuse current->journal_info for this.
8688 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008689 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008690 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008691 dio_data.unsubmitted_oe_range_start = (u64)offset;
8692 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308693 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308694 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008695 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8696 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008697 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008698 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8699 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008700 }
8701
Omar Sandoval17f8c842015-03-16 04:33:50 -07008702 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008703 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008704 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008705 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008706 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308707 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008708 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008709 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308710 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008711 btrfs_delalloc_release_space(inode, data_reserved,
8712 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008713 /*
8714 * On error we might have left some ordered extents
8715 * without submitting corresponding bios for them, so
8716 * cleanup them up to avoid other tasks getting them
8717 * and waiting for them to complete forever.
8718 */
8719 if (dio_data.unsubmitted_oe_range_start <
8720 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008721 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008722 dio_data.unsubmitted_oe_range_start,
8723 dio_data.unsubmitted_oe_range_end -
8724 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008725 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008726 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008727 btrfs_delalloc_release_space(inode, data_reserved,
8728 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008729 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008730 }
Miao Xie38851cc2013-02-08 07:04:11 +00008731out:
Miao Xie2e60a512013-02-08 07:01:08 +00008732 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008733 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008734 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008735 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008736
Qu Wenruo364ecf32017-02-27 15:10:38 +08008737 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008738 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008739}
8740
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008741#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8742
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008743static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8744 __u64 start, __u64 len)
8745{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008746 int ret;
8747
8748 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8749 if (ret)
8750 return ret;
8751
David Sterba2135fb92017-06-23 04:09:57 +02008752 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008753}
8754
Chris Mason9ebefb182007-06-15 13:50:00 -04008755int btrfs_readpage(struct file *file, struct page *page)
8756{
Chris Masond1310b22008-01-24 16:13:08 -05008757 struct extent_io_tree *tree;
8758 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008759 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008760}
Chris Mason1832a6d2007-12-21 16:27:21 -05008761
Chris Mason39279cc2007-06-12 06:35:45 -04008762static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8763{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008764 struct inode *inode = page->mapping->host;
8765 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008766
8767 if (current->flags & PF_MEMALLOC) {
8768 redirty_page_for_writepage(wbc, page);
8769 unlock_page(page);
8770 return 0;
8771 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008772
8773 /*
8774 * If we are under memory pressure we will call this directly from the
8775 * VM, we need to make sure we have the inode referenced for the ordered
8776 * extent. If not just return like we didn't do anything.
8777 */
8778 if (!igrab(inode)) {
8779 redirty_page_for_writepage(wbc, page);
8780 return AOP_WRITEPAGE_ACTIVATE;
8781 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008782 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008783 btrfs_add_delayed_iput(inode);
8784 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008785}
Chris Mason39279cc2007-06-12 06:35:45 -04008786
Eric Sandeen48a3b632013-04-25 20:41:01 +00008787static int btrfs_writepages(struct address_space *mapping,
8788 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008789{
Chris Masond1310b22008-01-24 16:13:08 -05008790 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008791
Chris Masond1310b22008-01-24 16:13:08 -05008792 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008793 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008794}
8795
Chris Mason3ab2fb52007-11-08 10:59:22 -05008796static int
8797btrfs_readpages(struct file *file, struct address_space *mapping,
8798 struct list_head *pages, unsigned nr_pages)
8799{
Chris Masond1310b22008-01-24 16:13:08 -05008800 struct extent_io_tree *tree;
8801 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008802 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008803}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008804static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008805{
Chris Masond1310b22008-01-24 16:13:08 -05008806 struct extent_io_tree *tree;
8807 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008808 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008809
Chris Masond1310b22008-01-24 16:13:08 -05008810 tree = &BTRFS_I(page->mapping->host)->io_tree;
8811 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008812 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008813 if (ret == 1) {
8814 ClearPagePrivate(page);
8815 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008816 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008817 }
8818 return ret;
8819}
Chris Mason39279cc2007-06-12 06:35:45 -04008820
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8822{
Chris Mason98509cf2008-09-11 15:51:43 -04008823 if (PageWriteback(page) || PageDirty(page))
8824 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008825 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008826}
8827
Lukas Czernerd47992f2013-05-21 23:17:23 -04008828static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8829 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008830{
Josef Bacik5fd02042012-05-02 14:00:54 -04008831 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008832 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008834 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008836 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308837 u64 start;
8838 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008839 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008840
Chris Mason8b62b722009-09-02 16:53:46 -04008841 /*
8842 * we have the page locked, so new writeback can't start,
8843 * and the dirty bit won't be cleared while we are here.
8844 *
8845 * Wait for IO on this page so that we can safely clear
8846 * the PagePrivate2 bit and do ordered accounting
8847 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008848 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008849
Josef Bacik5fd02042012-05-02 14:00:54 -04008850 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008851 if (offset) {
8852 btrfs_releasepage(page, GFP_NOFS);
8853 return;
8854 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008855
8856 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008857 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308858again:
8859 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008860 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308861 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308863 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008864 /*
8865 * IO on this page will never be started, so we need
8866 * to account for any ordered extents now
8867 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008868 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308869 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008870 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008871 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008872 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008873 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008874 /*
8875 * whoever cleared the private bit is responsible
8876 * for the finish_ordered_io
8877 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008878 if (TestClearPagePrivate2(page)) {
8879 struct btrfs_ordered_inode_tree *tree;
8880 u64 new_len;
8881
8882 tree = &BTRFS_I(inode)->ordered_tree;
8883
8884 spin_lock_irq(&tree->lock);
8885 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308886 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008887 if (new_len < ordered->truncated_len)
8888 ordered->truncated_len = new_len;
8889 spin_unlock_irq(&tree->lock);
8890
8891 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308892 start,
8893 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008894 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008895 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008896 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008897 if (!inode_evicting) {
8898 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308899 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008900 &cached_state);
8901 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308902
8903 start = end + 1;
8904 if (start < page_end)
8905 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008907
Qu Wenruob9d0b382015-09-29 10:35:16 +08008908 /*
8909 * Qgroup reserved space handler
8910 * Page here will be either
8911 * 1) Already written to disk
8912 * In this case, its reserved space is released from data rsv map
8913 * and will be freed by delayed_ref handler finally.
8914 * So even we call qgroup_free_data(), it won't decrease reserved
8915 * space.
8916 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008917 * This means the reserved space should be freed here. However,
8918 * if a truncate invalidates the page (by clearing PageDirty)
8919 * and the page is accounted for while allocating extent
8920 * in btrfs_check_data_free_space() we let delayed_ref to
8921 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008922 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008923 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008924 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008925 if (!inode_evicting) {
8926 clear_extent_bit(tree, page_start, page_end,
8927 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008928 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8929 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008930 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008931
8932 __btrfs_releasepage(page, GFP_NOFS);
8933 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934
Chris Mason4a096752008-07-21 10:29:44 -04008935 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008936 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008937 ClearPagePrivate(page);
8938 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008939 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008940 }
Chris Mason39279cc2007-06-12 06:35:45 -04008941}
8942
Chris Mason9ebefb182007-06-15 13:50:00 -04008943/*
8944 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8945 * called from a page fault handler when a page is first dirtied. Hence we must
8946 * be careful to check for EOF conditions here. We set the page up correctly
8947 * for a written page which means we get ENOSPC checking when writing into
8948 * holes and correct delalloc and unwritten extent mapping on filesystems that
8949 * support these features.
8950 *
8951 * We are not allowed to take the i_mutex here so we have to play games to
8952 * protect against truncate races as the page could now be beyond EOF. Because
8953 * vmtruncate() writes the inode size before removing pages, once we have the
8954 * page lock we can determine safely if the page is beyond EOF. If it is not
8955 * beyond EOF, then the page is guaranteed safe against truncation until we
8956 * unlock the page.
8957 */
Dave Jiang11bac802017-02-24 14:56:41 -08008958int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008959{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008960 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008961 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008963 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8964 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008965 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008966 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008967 char *kaddr;
8968 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008969 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008970 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008971 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308972 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008973 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308975 u64 end;
8976
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008977 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008978
Jan Karab2b5ef52012-06-12 16:20:45 +02008979 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008980 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008981 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008983
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308984 /*
8985 * Reserving delalloc space after obtaining the page lock can lead to
8986 * deadlock. For example, if a dirty page is locked by this function
8987 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8988 * dirty page write out, then the btrfs_writepage() function could
8989 * end up waiting indefinitely to get a lock on the page currently
8990 * being processed by btrfs_page_mkwrite() function.
8991 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008992 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308993 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008994 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008995 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008996 reserved = 1;
8997 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008998 if (ret) {
8999 if (ret == -ENOMEM)
9000 ret = VM_FAULT_OOM;
9001 else /* -ENOSPC, -EIO, etc */
9002 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009003 if (reserved)
9004 goto out;
9005 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009006 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009007
Nick Piggin56a76f82009-03-31 15:23:23 -07009008 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009010 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009011 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009012
Chris Mason9ebefb182007-06-15 13:50:00 -04009013 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 /* page got truncated out from underneath us */
9016 goto out_unlock;
9017 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 wait_on_page_writeback(page);
9019
David Sterbaff13db42015-12-03 14:30:40 +01009020 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009021 set_page_extent_mapped(page);
9022
Chris Masoneb84ae02008-07-17 13:53:27 -04009023 /*
9024 * we can't set the delalloc bits if there are pending ordered
9025 * extents. Drop our locks and wait for them to finish
9026 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009027 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9028 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009029 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009030 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009031 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009033 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009034 btrfs_put_ordered_extent(ordered);
9035 goto again;
9036 }
9037
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009038 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009039 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009040 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009041 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309042 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009043 btrfs_delalloc_release_space(inode, data_reserved,
9044 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309045 }
9046 }
9047
Josef Bacikfbf19082009-10-01 17:10:23 -04009048 /*
Liu Bo54160342016-12-13 12:15:19 -08009049 * page_mkwrite gets called when the page is firstly dirtied after it's
9050 * faulted in, but write(2) could also dirty a page and set delalloc
9051 * bits, thus in this case for space account reason, we still need to
9052 * clear any delalloc bits within this page range since we have to
9053 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009054 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309055 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009056 EXTENT_DIRTY | EXTENT_DELALLOC |
9057 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009058 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009059
Filipe Mananae3b8a482017-11-04 00:16:59 +00009060 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009061 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009062 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009063 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009064 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009065 ret = VM_FAULT_SIGBUS;
9066 goto out_unlock;
9067 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009069
9070 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009071 if (page_start + PAGE_SIZE > size)
9072 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009073 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009074 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009075
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009076 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009077 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009078 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009079 flush_dcache_page(page);
9080 kunmap(page);
9081 }
Chris Mason247e7432008-07-17 12:53:51 -04009082 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009084 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009085
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009086 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009087 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009088 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009089
David Sterbae43bbe52017-12-12 21:43:52 +01009090 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009091
9092out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009093 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009094 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009095 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009096 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009097 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009098 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009099 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009100out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009101 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009102 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9103 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009104out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009105 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009106 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009107 return ret;
9108}
9109
Josef Bacika41ad392011-01-31 15:30:16 -05009110static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009111{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009112 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009113 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009114 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009115 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009116 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009117 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009118 u64 mask = fs_info->sectorsize - 1;
9119 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009120
Josef Bacik0ef8b722013-10-25 16:13:35 -04009121 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9122 (u64)-1);
9123 if (ret)
9124 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009125
Josef Bacikfcb80c22011-05-03 10:40:22 -04009126 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009127 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009128 * 3 things going on here
9129 *
9130 * 1) We need to reserve space for our orphan item and the space to
9131 * delete our orphan item. Lord knows we don't want to have a dangling
9132 * orphan item because we didn't reserve space to remove it.
9133 *
9134 * 2) We need to reserve space to update our inode.
9135 *
9136 * 3) We need to have something to cache all the space that is going to
9137 * be free'd up by the truncate operation, but also have some slack
9138 * space reserved in case it uses space during the truncate (thank you
9139 * very much snapshotting).
9140 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009141 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009143 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009144 * doesn't end up using space reserved for updating the inode or
9145 * removing the orphan item. We also need to be able to stop the
9146 * transaction and start a new one, which means we need to be able to
9147 * update the inode several times, and we have no idea of knowing how
9148 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009149 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009150 * Then there is the orphan item, which does indeed need to be held on
9151 * to for the whole operation, and we need nobody to touch this reserved
9152 * space except the orphan code.
9153 *
9154 * So that leaves us with
9155 *
9156 * 1) root->orphan_block_rsv - for the orphan deletion.
9157 * 2) rsv - for the truncate reservation, which we will steal from the
9158 * transaction reservation.
9159 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9160 * updating the inode.
9161 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009162 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009163 if (!rsv)
9164 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009165 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009166 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009167
Josef Bacik907cbce2011-08-08 13:46:15 -04009168 /*
Josef Bacik07127182011-08-19 10:29:59 -04009169 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009170 * 1 for updating the inode.
9171 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009172 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009173 if (IS_ERR(trans)) {
9174 err = PTR_ERR(trans);
9175 goto out;
9176 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009177
Josef Bacik907cbce2011-08-08 13:46:15 -04009178 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009179 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009180 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009181 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009182
Chris Mason5a3f23d2009-03-31 13:27:11 -04009183 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009184 * So if we truncate and then write and fsync we normally would just
9185 * write the extents that changed, which is a problem if we need to
9186 * first truncate that entire inode. So set this flag so we write out
9187 * all of the extents in the inode to the sync log so we're completely
9188 * safe.
9189 */
9190 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009191 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009192
Yan, Zheng80825102009-11-12 09:35:36 +00009193 while (1) {
9194 ret = btrfs_truncate_inode_items(trans, root, inode,
9195 inode->i_size,
9196 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009197 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009198 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009199 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009200 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009201 }
Chris Mason39279cc2007-06-12 06:35:45 -04009202
Yan, Zheng80825102009-11-12 09:35:36 +00009203 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009204 if (ret) {
9205 err = ret;
9206 break;
9207 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009208
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009209 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009210 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009211
9212 trans = btrfs_start_transaction(root, 2);
9213 if (IS_ERR(trans)) {
9214 ret = err = PTR_ERR(trans);
9215 trans = NULL;
9216 break;
9217 }
9218
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009219 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009220 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009221 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009222 BUG_ON(ret); /* shouldn't happen */
9223 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009224 }
9225
Josef Bacikddfae632017-10-19 14:16:02 -04009226 /*
9227 * We can't call btrfs_truncate_block inside a trans handle as we could
9228 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9229 * we've truncated everything except the last little bit, and can do
9230 * btrfs_truncate_block and then update the disk_i_size.
9231 */
9232 if (ret == NEED_TRUNCATE_BLOCK) {
9233 btrfs_end_transaction(trans);
9234 btrfs_btree_balance_dirty(fs_info);
9235
9236 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9237 if (ret)
9238 goto out;
9239 trans = btrfs_start_transaction(root, 1);
9240 if (IS_ERR(trans)) {
9241 ret = PTR_ERR(trans);
9242 goto out;
9243 }
9244 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9245 }
9246
Yan, Zheng80825102009-11-12 09:35:36 +00009247 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009248 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009249 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009250 if (ret)
9251 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009252 }
9253
Chris Mason917c16b2011-11-08 14:49:59 -05009254 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009255 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009256 ret = btrfs_update_inode(trans, root, inode);
9257 if (ret && !err)
9258 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009259
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009260 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009261 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009262 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009263out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009264 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009265
Josef Bacik3893e332011-01-31 16:03:11 -05009266 if (ret && !err)
9267 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009268
Josef Bacik3893e332011-01-31 16:03:11 -05009269 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009270}
9271
Sven Wegener3b963622008-06-09 21:57:42 -04009272/*
Chris Masond352ac62008-09-29 15:18:18 -04009273 * create a new subvolume directory/inode (helper for the ioctl).
9274 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009275int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009276 struct btrfs_root *new_root,
9277 struct btrfs_root *parent_root,
9278 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009279{
Chris Mason39279cc2007-06-12 06:35:45 -04009280 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009281 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009282 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009283
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009284 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9285 new_dirid, new_dirid,
9286 S_IFDIR | (~current_umask() & S_IRWXUGO),
9287 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009288 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009289 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009290 inode->i_op = &btrfs_dir_inode_operations;
9291 inode->i_fop = &btrfs_dir_file_operations;
9292
Miklos Szeredibfe86842011-10-28 14:13:29 +02009293 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009294 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009295 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009296
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009297 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9298 if (err)
9299 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009300 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009301 new_root->root_key.objectid, err);
9302
Yan, Zheng76dda932009-09-21 16:00:26 -04009303 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009304
Yan, Zheng76dda932009-09-21 16:00:26 -04009305 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009306 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009307}
9308
Chris Mason39279cc2007-06-12 06:35:45 -04009309struct inode *btrfs_alloc_inode(struct super_block *sb)
9310{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009311 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009312 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009314
David Sterba712e36c2017-10-31 17:08:27 +01009315 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009316 if (!ei)
9317 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009318
9319 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009321 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009322 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009323 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009325 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009326 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009327 ei->disk_i_size = 0;
9328 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009329 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009331 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009332 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009333 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009334 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009335
Josef Bacik9e0baf62011-07-15 15:16:44 +00009336 spin_lock_init(&ei->lock);
9337 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009338 if (sb->s_magic != BTRFS_TEST_MAGIC)
9339 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9340 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009341 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009342 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009343 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009344
Miao Xie16cdcec2011-04-22 18:12:22 +08009345 ei->delayed_node = NULL;
9346
chandan r9cc97d62012-07-04 12:48:07 +05309347 ei->i_otime.tv_sec = 0;
9348 ei->i_otime.tv_nsec = 0;
9349
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009350 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009351 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009352 extent_io_tree_init(&ei->io_tree, inode);
9353 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009354 ei->io_tree.track_uptodate = 1;
9355 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009356 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009357 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009358 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009359 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009360 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009361 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009362 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009363 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009364
9365 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009366}
9367
Josef Bacikaaedb552013-10-11 14:44:09 -04009368#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9369void btrfs_test_destroy_inode(struct inode *inode)
9370{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009371 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009372 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9373}
9374#endif
9375
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009376static void btrfs_i_callback(struct rcu_head *head)
9377{
9378 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009379 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9380}
9381
Chris Mason39279cc2007-06-12 06:35:45 -04009382void btrfs_destroy_inode(struct inode *inode)
9383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009385 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009386 struct btrfs_root *root = BTRFS_I(inode)->root;
9387
Al Virob3d9b7a2012-06-09 13:51:19 -04009388 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009389 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009390 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9391 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009392 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009393 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009394 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009395 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009396 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009397
Chris Mason5a3f23d2009-03-31 13:27:11 -04009398 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009399 * This can happen where we create an inode, but somebody else also
9400 * created the same inode and we need to destroy the one we already
9401 * created.
9402 */
9403 if (!root)
9404 goto free;
9405
Josef Bacik8a35d952012-05-23 14:26:42 -04009406 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9407 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009408 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009409 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009410 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009411 }
Josef Bacik7b128762008-07-24 12:17:14 -04009412
Chris Masond3977122009-01-05 21:25:51 -05009413 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009414 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9415 if (!ordered)
9416 break;
9417 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009418 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009419 "found ordered extent %llu %llu on inode cleanup",
9420 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009421 btrfs_remove_ordered_extent(inode, ordered);
9422 btrfs_put_ordered_extent(ordered);
9423 btrfs_put_ordered_extent(ordered);
9424 }
9425 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009426 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009427 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009428 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009429free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009430 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009431}
9432
Al Viro45321ac2010-06-07 13:43:19 -04009433int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009434{
9435 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009436
Naohiro Aota6379ef92013-06-06 09:56:34 +00009437 if (root == NULL)
9438 return 1;
9439
Liu Bofa6ac872013-02-20 14:10:23 +00009440 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009441 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009442 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009443 else
Al Viro45321ac2010-06-07 13:43:19 -04009444 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009445}
9446
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009447static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009448{
9449 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9450
9451 inode_init_once(&ei->vfs_inode);
9452}
9453
9454void btrfs_destroy_cachep(void)
9455{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009456 /*
9457 * Make sure all delayed rcu free inodes are flushed before we
9458 * destroy cache.
9459 */
9460 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009461 kmem_cache_destroy(btrfs_inode_cachep);
9462 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009463 kmem_cache_destroy(btrfs_path_cachep);
9464 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009465}
9466
Liu Bof5c29bd2017-11-02 17:21:50 -06009467int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009468{
David Sterba837e1972012-09-07 03:00:48 -06009469 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009470 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009471 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9472 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009473 if (!btrfs_inode_cachep)
9474 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009475
David Sterba837e1972012-09-07 03:00:48 -06009476 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009477 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009478 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009479 if (!btrfs_trans_handle_cachep)
9480 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009481
David Sterba837e1972012-09-07 03:00:48 -06009482 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009483 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009484 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009485 if (!btrfs_path_cachep)
9486 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009487
David Sterba837e1972012-09-07 03:00:48 -06009488 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009489 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009490 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009491 if (!btrfs_free_space_cachep)
9492 goto fail;
9493
Chris Mason39279cc2007-06-12 06:35:45 -04009494 return 0;
9495fail:
9496 btrfs_destroy_cachep();
9497 return -ENOMEM;
9498}
9499
David Howellsa528d352017-01-31 16:46:22 +00009500static int btrfs_getattr(const struct path *path, struct kstat *stat,
9501 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009502{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009503 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009504 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009505 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009506 u32 bi_flags = BTRFS_I(inode)->flags;
9507
9508 stat->result_mask |= STATX_BTIME;
9509 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9510 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9511 if (bi_flags & BTRFS_INODE_APPEND)
9512 stat->attributes |= STATX_ATTR_APPEND;
9513 if (bi_flags & BTRFS_INODE_COMPRESS)
9514 stat->attributes |= STATX_ATTR_COMPRESSED;
9515 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9516 stat->attributes |= STATX_ATTR_IMMUTABLE;
9517 if (bi_flags & BTRFS_INODE_NODUMP)
9518 stat->attributes |= STATX_ATTR_NODUMP;
9519
9520 stat->attributes_mask |= (STATX_ATTR_APPEND |
9521 STATX_ATTR_COMPRESSED |
9522 STATX_ATTR_IMMUTABLE |
9523 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009524
Chris Mason39279cc2007-06-12 06:35:45 -04009525 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009526 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009527
9528 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009529 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009530 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009531 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009532 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009533 return 0;
9534}
9535
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009536static int btrfs_rename_exchange(struct inode *old_dir,
9537 struct dentry *old_dentry,
9538 struct inode *new_dir,
9539 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009540{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009541 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009542 struct btrfs_trans_handle *trans;
9543 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009544 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 struct inode *new_inode = new_dentry->d_inode;
9546 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009547 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009548 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009549 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9550 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 u64 old_idx = 0;
9552 u64 new_idx = 0;
9553 u64 root_objectid;
9554 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009555 bool root_log_pinned = false;
9556 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557
9558 /* we only allow rename subvolume link between subvolumes */
9559 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9560 return -EXDEV;
9561
9562 /* close the race window with snapshot create/destroy ioctl */
9563 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009564 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009565 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009566 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567
9568 /*
9569 * We want to reserve the absolute worst case amount of items. So if
9570 * both inodes are subvols and we need to unlink them then that would
9571 * require 4 item modifications, but if they are both normal inodes it
9572 * would require 5 item modifications, so we'll assume their normal
9573 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9574 * should cover the worst case number of items we'll modify.
9575 */
9576 trans = btrfs_start_transaction(root, 12);
9577 if (IS_ERR(trans)) {
9578 ret = PTR_ERR(trans);
9579 goto out_notrans;
9580 }
9581
9582 /*
9583 * We need to find a free sequence number both in the source and
9584 * in the destination directory for the exchange.
9585 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009586 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587 if (ret)
9588 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009589 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 if (ret)
9591 goto out_fail;
9592
9593 BTRFS_I(old_inode)->dir_index = 0ULL;
9594 BTRFS_I(new_inode)->dir_index = 0ULL;
9595
9596 /* Reference for the source. */
9597 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9598 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009599 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009601 btrfs_pin_log_trans(root);
9602 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603 ret = btrfs_insert_inode_ref(trans, dest,
9604 new_dentry->d_name.name,
9605 new_dentry->d_name.len,
9606 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009607 btrfs_ino(BTRFS_I(new_dir)),
9608 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 if (ret)
9610 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 }
9612
9613 /* And now for the dest. */
9614 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9615 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009616 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009618 btrfs_pin_log_trans(dest);
9619 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 ret = btrfs_insert_inode_ref(trans, root,
9621 old_dentry->d_name.name,
9622 old_dentry->d_name.len,
9623 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009624 btrfs_ino(BTRFS_I(old_dir)),
9625 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009626 if (ret)
9627 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 }
9629
9630 /* Update inode version and ctime/mtime. */
9631 inode_inc_iversion(old_dir);
9632 inode_inc_iversion(new_dir);
9633 inode_inc_iversion(old_inode);
9634 inode_inc_iversion(new_inode);
9635 old_dir->i_ctime = old_dir->i_mtime = ctime;
9636 new_dir->i_ctime = new_dir->i_mtime = ctime;
9637 old_inode->i_ctime = ctime;
9638 new_inode->i_ctime = ctime;
9639
9640 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009641 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9642 BTRFS_I(old_inode), 1);
9643 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9644 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009645 }
9646
9647 /* src is a subvolume */
9648 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9649 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9650 ret = btrfs_unlink_subvol(trans, root, old_dir,
9651 root_objectid,
9652 old_dentry->d_name.name,
9653 old_dentry->d_name.len);
9654 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009655 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9656 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 old_dentry->d_name.name,
9658 old_dentry->d_name.len);
9659 if (!ret)
9660 ret = btrfs_update_inode(trans, root, old_inode);
9661 }
9662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009663 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 goto out_fail;
9665 }
9666
9667 /* dest is a subvolume */
9668 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9669 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9670 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9671 root_objectid,
9672 new_dentry->d_name.name,
9673 new_dentry->d_name.len);
9674 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009675 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9676 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009677 new_dentry->d_name.name,
9678 new_dentry->d_name.len);
9679 if (!ret)
9680 ret = btrfs_update_inode(trans, dest, new_inode);
9681 }
9682 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009683 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 goto out_fail;
9685 }
9686
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009687 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009688 new_dentry->d_name.name,
9689 new_dentry->d_name.len, 0, old_idx);
9690 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009691 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 goto out_fail;
9693 }
9694
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009695 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 old_dentry->d_name.name,
9697 old_dentry->d_name.len, 0, new_idx);
9698 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009699 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 goto out_fail;
9701 }
9702
9703 if (old_inode->i_nlink == 1)
9704 BTRFS_I(old_inode)->dir_index = old_idx;
9705 if (new_inode->i_nlink == 1)
9706 BTRFS_I(new_inode)->dir_index = new_idx;
9707
Filipe Manana86e8aa02016-05-05 02:02:27 +01009708 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009710 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9711 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009713 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009715 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009717 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9718 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009720 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009721 }
9722out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009723 /*
9724 * If we have pinned a log and an error happened, we unpin tasks
9725 * trying to sync the log and force them to fallback to a transaction
9726 * commit if the log currently contains any of the inodes involved in
9727 * this rename operation (to ensure we do not persist a log with an
9728 * inconsistent state for any of these inodes or leading to any
9729 * inconsistencies when replayed). If the transaction was aborted, the
9730 * abortion reason is propagated to userspace when attempting to commit
9731 * the transaction. If the log does not contain any of these inodes, we
9732 * allow the tasks to sync it.
9733 */
9734 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009735 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9736 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9737 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009738 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009739 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009740 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009741
9742 if (root_log_pinned) {
9743 btrfs_end_log_trans(root);
9744 root_log_pinned = false;
9745 }
9746 if (dest_log_pinned) {
9747 btrfs_end_log_trans(dest);
9748 dest_log_pinned = false;
9749 }
9750 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009751 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752out_notrans:
9753 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009754 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009755 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009756 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757
9758 return ret;
9759}
9760
9761static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9762 struct btrfs_root *root,
9763 struct inode *dir,
9764 struct dentry *dentry)
9765{
9766 int ret;
9767 struct inode *inode;
9768 u64 objectid;
9769 u64 index;
9770
9771 ret = btrfs_find_free_ino(root, &objectid);
9772 if (ret)
9773 return ret;
9774
9775 inode = btrfs_new_inode(trans, root, dir,
9776 dentry->d_name.name,
9777 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009778 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009779 objectid,
9780 S_IFCHR | WHITEOUT_MODE,
9781 &index);
9782
9783 if (IS_ERR(inode)) {
9784 ret = PTR_ERR(inode);
9785 return ret;
9786 }
9787
9788 inode->i_op = &btrfs_special_inode_operations;
9789 init_special_inode(inode, inode->i_mode,
9790 WHITEOUT_DEV);
9791
9792 ret = btrfs_init_inode_security(trans, inode, dir,
9793 &dentry->d_name);
9794 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009795 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009796
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009797 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9798 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009800 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801
9802 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009803out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009804 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009805 if (ret)
9806 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 iput(inode);
9808
Filipe Mananac9901612016-05-05 01:41:57 +01009809 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009810}
9811
Chris Mason39279cc2007-06-12 06:35:45 -04009812static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009813 struct inode *new_dir, struct dentry *new_dentry,
9814 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009815{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009816 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009817 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009818 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009819 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9820 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009821 struct inode *new_inode = d_inode(new_dentry);
9822 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009823 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009824 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009825 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009826 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009827 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009828
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009829 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009830 return -EPERM;
9831
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009832 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009833 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009834 return -EXDEV;
9835
Li Zefan33345d012011-04-20 10:31:50 +08009836 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009837 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 return -ENOTEMPTY;
9839
Chris Mason39279cc2007-06-12 06:35:45 -04009840 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009841 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009842 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009843
9844
9845 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009846 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009847 new_dentry->d_name.name,
9848 new_dentry->d_name.len);
9849
9850 if (ret) {
9851 if (ret == -EEXIST) {
9852 /* we shouldn't get
9853 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309854 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009855 return ret;
9856 }
9857 } else {
9858 /* maybe -EOVERFLOW */
9859 return ret;
9860 }
9861 }
9862 ret = 0;
9863
Chris Mason5a3f23d2009-03-31 13:27:11 -04009864 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009865 * we're using rename to replace one file with another. Start IO on it
9866 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009867 */
Chris Mason8d875f92014-08-12 10:47:42 -07009868 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009869 filemap_flush(old_inode->i_mapping);
9870
Yan, Zheng76dda932009-09-21 16:00:26 -04009871 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009872 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009873 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009874 /*
9875 * We want to reserve the absolute worst case amount of items. So if
9876 * both inodes are subvols and we need to unlink them then that would
9877 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009878 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009879 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9880 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009881 * If our rename has the whiteout flag, we need more 5 units for the
9882 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9883 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009884 */
Filipe Manana5062af32016-05-05 10:26:26 +01009885 trans_num_items = 11;
9886 if (flags & RENAME_WHITEOUT)
9887 trans_num_items += 5;
9888 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009889 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890 ret = PTR_ERR(trans);
9891 goto out_notrans;
9892 }
Chris Mason5f39d392007-10-15 16:14:19 -04009893
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009894 if (dest != root)
9895 btrfs_record_root_in_trans(trans, dest);
9896
Nikolay Borisov877574e2017-02-20 13:50:33 +02009897 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009898 if (ret)
9899 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009900
Miao Xie67de1172013-12-26 13:07:06 +08009901 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009902 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009903 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009904 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009905 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009906 btrfs_pin_log_trans(root);
9907 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009908 ret = btrfs_insert_inode_ref(trans, dest,
9909 new_dentry->d_name.name,
9910 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009911 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009912 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009913 if (ret)
9914 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009915 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009916
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009917 inode_inc_iversion(old_dir);
9918 inode_inc_iversion(new_dir);
9919 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009920 old_dir->i_ctime = old_dir->i_mtime =
9921 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009922 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009923
Chris Mason12fcfd22009-03-24 10:24:20 -04009924 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009925 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9926 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009927
Li Zefan33345d012011-04-20 10:31:50 +08009928 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9930 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9931 old_dentry->d_name.name,
9932 old_dentry->d_name.len);
9933 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009934 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9935 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009936 old_dentry->d_name.name,
9937 old_dentry->d_name.len);
9938 if (!ret)
9939 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009940 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009941 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009942 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009943 goto out_fail;
9944 }
Chris Mason39279cc2007-06-12 06:35:45 -04009945
9946 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009947 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009948 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009949 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009950 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9951 root_objectid = BTRFS_I(new_inode)->location.objectid;
9952 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9953 root_objectid,
9954 new_dentry->d_name.name,
9955 new_dentry->d_name.len);
9956 BUG_ON(new_inode->i_nlink == 0);
9957 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009958 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9959 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009960 new_dentry->d_name.name,
9961 new_dentry->d_name.len);
9962 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009963 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009964 ret = btrfs_orphan_add(trans,
9965 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009966 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009967 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009968 goto out_fail;
9969 }
Chris Mason39279cc2007-06-12 06:35:45 -04009970 }
Josef Bacikaec74772008-07-24 12:12:38 -04009971
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009972 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009973 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009974 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009975 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009976 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009977 goto out_fail;
9978 }
Chris Mason39279cc2007-06-12 06:35:45 -04009979
Miao Xie67de1172013-12-26 13:07:06 +08009980 if (old_inode->i_nlink == 1)
9981 BTRFS_I(old_inode)->dir_index = index;
9982
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009983 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009984 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009985
David Sterbaf85b7372017-01-20 14:54:07 +01009986 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9987 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009988 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009989 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009990 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009991
9992 if (flags & RENAME_WHITEOUT) {
9993 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9994 old_dentry);
9995
9996 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009997 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009998 goto out_fail;
9999 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010000 }
Chris Mason39279cc2007-06-12 06:35:45 -040010001out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010002 /*
10003 * If we have pinned the log and an error happened, we unpin tasks
10004 * trying to sync the log and force them to fallback to a transaction
10005 * commit if the log currently contains any of the inodes involved in
10006 * this rename operation (to ensure we do not persist a log with an
10007 * inconsistent state for any of these inodes or leading to any
10008 * inconsistencies when replayed). If the transaction was aborted, the
10009 * abortion reason is propagated to userspace when attempting to commit
10010 * the transaction. If the log does not contain any of these inodes, we
10011 * allow the tasks to sync it.
10012 */
10013 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010014 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10015 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10016 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010017 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010018 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010019 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010020
10021 btrfs_end_log_trans(root);
10022 log_pinned = false;
10023 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010024 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010025out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010026 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010027 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010028
Chris Mason39279cc2007-06-12 06:35:45 -040010029 return ret;
10030}
10031
Miklos Szeredi80ace852014-07-23 15:15:32 +020010032static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10033 struct inode *new_dir, struct dentry *new_dentry,
10034 unsigned int flags)
10035{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010036 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010037 return -EINVAL;
10038
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010039 if (flags & RENAME_EXCHANGE)
10040 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10041 new_dentry);
10042
10043 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010044}
10045
Miao Xie8ccf6f192012-10-25 09:28:04 +000010046static void btrfs_run_delalloc_work(struct btrfs_work *work)
10047{
10048 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010049 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010050
10051 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10052 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010053 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010054 filemap_flush(inode->i_mapping);
10055 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10056 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010057 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010058
10059 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010060 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010061 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010062 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010063 complete(&delalloc_work->completion);
10064}
10065
10066struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010067 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010068{
10069 struct btrfs_delalloc_work *work;
10070
David Sterba100d5702015-12-08 14:39:32 +010010071 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010072 if (!work)
10073 return NULL;
10074
10075 init_completion(&work->completion);
10076 INIT_LIST_HEAD(&work->list);
10077 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010078 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010079 WARN_ON_ONCE(!inode);
10080 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10081 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010082
10083 return work;
10084}
10085
10086void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10087{
10088 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010089 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010090}
10091
Chris Masond352ac62008-09-29 15:18:18 -040010092/*
10093 * some fairly slow code that needs optimization. This walks the list
10094 * of all the inodes with pending delalloc and forces them to disk.
10095 */
Miao Xie6c255e62014-03-06 13:55:01 +080010096static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10097 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010098{
Chris Masonea8c2812008-08-04 23:17:27 -040010099 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010100 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010101 struct btrfs_delalloc_work *work, *next;
10102 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010103 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010104 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010105
Miao Xie8ccf6f192012-10-25 09:28:04 +000010106 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010107 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010108
Miao Xie573bfb72014-03-06 13:55:03 +080010109 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 spin_lock(&root->delalloc_lock);
10111 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010112 while (!list_empty(&splice)) {
10113 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010114 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010115
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 list_move_tail(&binode->delalloc_inodes,
10117 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010118 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010119 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010120 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010121 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010122 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010124
David Sterba651d4942015-11-27 19:24:16 +010010125 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010126 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010127 if (delay_iput)
10128 btrfs_add_delayed_iput(inode);
10129 else
10130 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010131 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010132 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010133 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010134 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010135 btrfs_queue_work(root->fs_info->flush_workers,
10136 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010137 ret++;
10138 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010139 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010140 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010141 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010142 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010144
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010145out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010146 list_for_each_entry_safe(work, next, &works, list) {
10147 list_del_init(&work->list);
10148 btrfs_wait_and_free_delalloc_work(work);
10149 }
10150
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 if (!list_empty_careful(&splice)) {
10152 spin_lock(&root->delalloc_lock);
10153 list_splice_tail(&splice, &root->delalloc_inodes);
10154 spin_unlock(&root->delalloc_lock);
10155 }
Miao Xie573bfb72014-03-06 13:55:03 +080010156 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010157 return ret;
10158}
10159
10160int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010162 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010163 int ret;
10164
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010165 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010166 return -EROFS;
10167
Miao Xie6c255e62014-03-06 13:55:01 +080010168 ret = __start_delalloc_inodes(root, delay_iput, -1);
10169 if (ret > 0)
10170 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010171 return ret;
10172}
10173
Miao Xie6c255e62014-03-06 13:55:01 +080010174int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10175 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010176{
10177 struct btrfs_root *root;
10178 struct list_head splice;
10179 int ret;
10180
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010181 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010182 return -EROFS;
10183
10184 INIT_LIST_HEAD(&splice);
10185
Miao Xie573bfb72014-03-06 13:55:03 +080010186 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010187 spin_lock(&fs_info->delalloc_root_lock);
10188 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010189 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010190 root = list_first_entry(&splice, struct btrfs_root,
10191 delalloc_root);
10192 root = btrfs_grab_fs_root(root);
10193 BUG_ON(!root);
10194 list_move_tail(&root->delalloc_root,
10195 &fs_info->delalloc_roots);
10196 spin_unlock(&fs_info->delalloc_root_lock);
10197
Miao Xie6c255e62014-03-06 13:55:01 +080010198 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010199 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010200 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010201 goto out;
10202
Miao Xie6c255e62014-03-06 13:55:01 +080010203 if (nr != -1) {
10204 nr -= ret;
10205 WARN_ON(nr < 0);
10206 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 spin_lock(&fs_info->delalloc_root_lock);
10208 }
10209 spin_unlock(&fs_info->delalloc_root_lock);
10210
Miao Xie6c255e62014-03-06 13:55:01 +080010211 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010212out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010213 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010214 spin_lock(&fs_info->delalloc_root_lock);
10215 list_splice_tail(&splice, &fs_info->delalloc_roots);
10216 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010217 }
Miao Xie573bfb72014-03-06 13:55:03 +080010218 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010219 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010220}
10221
Chris Mason39279cc2007-06-12 06:35:45 -040010222static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10223 const char *symname)
10224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010225 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010226 struct btrfs_trans_handle *trans;
10227 struct btrfs_root *root = BTRFS_I(dir)->root;
10228 struct btrfs_path *path;
10229 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010230 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010231 int err;
10232 int drop_inode = 0;
10233 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010234 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010235 int name_len;
10236 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010237 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010238 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010239 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010240
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010241 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010242 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010243 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010244
Josef Bacik9ed74f22009-09-11 16:12:44 -040010245 /*
10246 * 2 items for inode item and ref
10247 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010248 * 1 item for updating parent inode item
10249 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010250 * 1 item for xattr if selinux is on
10251 */
Filipe Manana9269d122015-12-31 18:16:29 +000010252 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010253 if (IS_ERR(trans))
10254 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010255
Li Zefan581bb052011-04-20 10:06:11 +080010256 err = btrfs_find_free_ino(root, &objectid);
10257 if (err)
10258 goto out_unlock;
10259
Josef Bacikaec74772008-07-24 12:12:38 -040010260 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010261 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10262 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010263 if (IS_ERR(inode)) {
10264 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010265 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010266 }
Chris Mason39279cc2007-06-12 06:35:45 -040010267
Casey Schauflerad19db72011-12-15 10:09:07 -050010268 /*
10269 * If the active LSM wants to access the inode during
10270 * d_instantiate it needs these. Smack checks to see
10271 * if the filesystem supports xattrs by looking at the
10272 * ops vector.
10273 */
10274 inode->i_fop = &btrfs_file_operations;
10275 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010276 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010277 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10278
10279 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10280 if (err)
10281 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010282
Chris Mason39279cc2007-06-12 06:35:45 -040010283 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010284 if (!path) {
10285 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010286 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010287 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010288 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010289 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010290 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010291 datasize = btrfs_file_extent_calc_inline_size(name_len);
10292 err = btrfs_insert_empty_item(trans, root, path, &key,
10293 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010294 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010295 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010296 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010297 }
Chris Mason5f39d392007-10-15 16:14:19 -040010298 leaf = path->nodes[0];
10299 ei = btrfs_item_ptr(leaf, path->slots[0],
10300 struct btrfs_file_extent_item);
10301 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10302 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010303 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010304 btrfs_set_file_extent_encryption(leaf, ei, 0);
10305 btrfs_set_file_extent_compression(leaf, ei, 0);
10306 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10307 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10308
Chris Mason39279cc2007-06-12 06:35:45 -040010309 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010310 write_extent_buffer(leaf, symname, ptr, name_len);
10311 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010312 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010313
Chris Mason39279cc2007-06-12 06:35:45 -040010314 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010315 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010316 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010317 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010318 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010319 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010320 /*
10321 * Last step, add directory indexes for our symlink inode. This is the
10322 * last step to avoid extra cleanup of these indexes if an error happens
10323 * elsewhere above.
10324 */
10325 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010326 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10327 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010328 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010329 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010330 goto out_unlock_inode;
10331 }
10332
10333 unlock_new_inode(inode);
10334 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010335
10336out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010337 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010338 if (drop_inode) {
10339 inode_dec_link_count(inode);
10340 iput(inode);
10341 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010342 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010343 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010344
10345out_unlock_inode:
10346 drop_inode = 1;
10347 unlock_new_inode(inode);
10348 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010349}
Chris Mason16432982008-04-10 10:23:21 -040010350
Josef Bacik0af3d002010-06-21 14:48:16 -040010351static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10352 u64 start, u64 num_bytes, u64 min_size,
10353 loff_t actual_len, u64 *alloc_hint,
10354 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010355{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010356 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010357 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10358 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010359 struct btrfs_root *root = BTRFS_I(inode)->root;
10360 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010361 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010362 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010363 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010364 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010365 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010366 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010367 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010368
Josef Bacik0af3d002010-06-21 14:48:16 -040010369 if (trans)
10370 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010371 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010372 if (own_trans) {
10373 trans = btrfs_start_transaction(root, 3);
10374 if (IS_ERR(trans)) {
10375 ret = PTR_ERR(trans);
10376 break;
10377 }
Yan Zhengd899e052008-10-30 14:25:28 -040010378 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010379
Byongho Leeee221842015-12-15 01:42:10 +090010380 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010381 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010382 /*
10383 * If we are severely fragmented we could end up with really
10384 * small allocations, so if the allocator is returning small
10385 * chunks lets make its job easier by only searching for those
10386 * sized chunks.
10387 */
10388 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010389 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10390 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010391 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010392 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010393 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010394 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010396 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010397
Josef Bacik0b670dc2015-09-23 17:11:16 -040010398 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010399 ret = insert_reserved_file_extent(trans, inode,
10400 cur_offset, ins.objectid,
10401 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010402 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010403 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010404 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010405 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010406 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010407 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010408 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010409 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010410 break;
10411 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010412
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010413 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010414 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010415
Josef Bacik5dc562c2012-08-17 13:14:17 -040010416 em = alloc_extent_map();
10417 if (!em) {
10418 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10419 &BTRFS_I(inode)->runtime_flags);
10420 goto next;
10421 }
10422
10423 em->start = cur_offset;
10424 em->orig_start = cur_offset;
10425 em->len = ins.offset;
10426 em->block_start = ins.objectid;
10427 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010428 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010429 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010430 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010431 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10432 em->generation = trans->transid;
10433
10434 while (1) {
10435 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010436 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010437 write_unlock(&em_tree->lock);
10438 if (ret != -EEXIST)
10439 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010440 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010441 cur_offset + ins.offset - 1,
10442 0);
10443 }
10444 free_extent_map(em);
10445next:
Yan Zhengd899e052008-10-30 14:25:28 -040010446 num_bytes -= ins.offset;
10447 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010448 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010449
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010450 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010451 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010452 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010453 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010454 (actual_len > inode->i_size) &&
10455 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010456 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010457 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010458 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010459 i_size = cur_offset;
10460 i_size_write(inode, i_size);
10461 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010462 }
10463
Yan Zhengd899e052008-10-30 14:25:28 -040010464 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010465
10466 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010467 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010468 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010469 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010470 break;
10471 }
Yan Zhengd899e052008-10-30 14:25:28 -040010472
Josef Bacik0af3d002010-06-21 14:48:16 -040010473 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010474 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010475 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010476 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010477 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010478 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010479 return ret;
10480}
10481
Josef Bacik0af3d002010-06-21 14:48:16 -040010482int btrfs_prealloc_file_range(struct inode *inode, int mode,
10483 u64 start, u64 num_bytes, u64 min_size,
10484 loff_t actual_len, u64 *alloc_hint)
10485{
10486 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10487 min_size, actual_len, alloc_hint,
10488 NULL);
10489}
10490
10491int btrfs_prealloc_file_range_trans(struct inode *inode,
10492 struct btrfs_trans_handle *trans, int mode,
10493 u64 start, u64 num_bytes, u64 min_size,
10494 loff_t actual_len, u64 *alloc_hint)
10495{
10496 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10497 min_size, actual_len, alloc_hint, trans);
10498}
10499
Chris Masone6dcd2d2008-07-17 12:53:50 -040010500static int btrfs_set_page_dirty(struct page *page)
10501{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010502 return __set_page_dirty_nobuffers(page);
10503}
10504
Al Viro10556cb2011-06-20 19:28:19 -040010505static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010506{
Li Zefanb83cc962010-12-20 16:04:08 +080010507 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010508 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010509
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010510 if (mask & MAY_WRITE &&
10511 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10512 if (btrfs_root_readonly(root))
10513 return -EROFS;
10514 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10515 return -EACCES;
10516 }
Al Viro2830ba72011-06-20 19:16:29 -040010517 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010518}
Chris Mason39279cc2007-06-12 06:35:45 -040010519
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010520static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10521{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010522 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010523 struct btrfs_trans_handle *trans;
10524 struct btrfs_root *root = BTRFS_I(dir)->root;
10525 struct inode *inode = NULL;
10526 u64 objectid;
10527 u64 index;
10528 int ret = 0;
10529
10530 /*
10531 * 5 units required for adding orphan entry
10532 */
10533 trans = btrfs_start_transaction(root, 5);
10534 if (IS_ERR(trans))
10535 return PTR_ERR(trans);
10536
10537 ret = btrfs_find_free_ino(root, &objectid);
10538 if (ret)
10539 goto out;
10540
10541 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010542 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010543 if (IS_ERR(inode)) {
10544 ret = PTR_ERR(inode);
10545 inode = NULL;
10546 goto out;
10547 }
10548
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010549 inode->i_fop = &btrfs_file_operations;
10550 inode->i_op = &btrfs_file_inode_operations;
10551
10552 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010553 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10554
Chris Masonb0d5d102014-09-08 13:08:51 -070010555 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10556 if (ret)
10557 goto out_inode;
10558
10559 ret = btrfs_update_inode(trans, root, inode);
10560 if (ret)
10561 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010562 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010563 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010564 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010565
Filipe Manana5762b5c2014-08-01 00:10:32 +010010566 /*
10567 * We set number of links to 0 in btrfs_new_inode(), and here we set
10568 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10569 * through:
10570 *
10571 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10572 */
10573 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010574 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010575 d_tmpfile(dentry, inode);
10576 mark_inode_dirty(inode);
10577
10578out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010579 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010580 if (ret)
10581 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010582 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010583 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010584
10585out_inode:
10586 unlock_new_inode(inode);
10587 goto out;
10588
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010589}
10590
David Sterba20a7db82017-02-17 16:24:29 +010010591__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010592static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010593{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010594 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010595}
10596
Josef Bacikc6100a42017-05-05 11:57:13 -040010597static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10598{
10599 struct inode *inode = private_data;
10600 return btrfs_sb(inode->i_sb);
10601}
10602
10603static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10604 u64 start, u64 end)
10605{
10606 struct inode *inode = private_data;
10607 u64 isize;
10608
10609 isize = i_size_read(inode);
10610 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10611 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10612 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10613 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10614 }
10615}
10616
10617void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10618{
10619 struct inode *inode = private_data;
10620 unsigned long index = start >> PAGE_SHIFT;
10621 unsigned long end_index = end >> PAGE_SHIFT;
10622 struct page *page;
10623
10624 while (index <= end_index) {
10625 page = find_get_page(inode->i_mapping, index);
10626 ASSERT(page); /* Pages should be in the extent_io_tree */
10627 set_page_writeback(page);
10628 put_page(page);
10629 index++;
10630 }
10631}
10632
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010633static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010634 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010635 .lookup = btrfs_lookup,
10636 .create = btrfs_create,
10637 .unlink = btrfs_unlink,
10638 .link = btrfs_link,
10639 .mkdir = btrfs_mkdir,
10640 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010641 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010642 .symlink = btrfs_symlink,
10643 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010644 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010645 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010646 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010647 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010648 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010649 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010650 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010651};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010652static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010653 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010654 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010655 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010656};
Yan, Zheng76dda932009-09-21 16:00:26 -040010657
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010658static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010659 .llseek = generic_file_llseek,
10660 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010661 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010662 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010663 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010664#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010665 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010666#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010667 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010668 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010669};
10670
David Sterba20e55062015-11-19 11:42:28 +010010671static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010672 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010673 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010674 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010675 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010676 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010677 .tree_fs_info = iotree_fs_info,
10678 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010679
10680 /* optional callbacks */
10681 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010682 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010683 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010684 .set_bit_hook = btrfs_set_bit_hook,
10685 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010686 .merge_extent_hook = btrfs_merge_extent_hook,
10687 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010688 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010689};
10690
Chris Mason35054392009-01-21 13:11:13 -050010691/*
10692 * btrfs doesn't support the bmap operation because swapfiles
10693 * use bmap to make a mapping of extents in the file. They assume
10694 * these extents won't change over the life of the file and they
10695 * use the bmap result to do IO directly to the drive.
10696 *
10697 * the btrfs bmap call would return logical addresses that aren't
10698 * suitable for IO and they also will change frequently as COW
10699 * operations happen. So, swapfile + btrfs == corruption.
10700 *
10701 * For now we're avoiding this by dropping bmap.
10702 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010703static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010704 .readpage = btrfs_readpage,
10705 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010706 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010707 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010708 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010709 .invalidatepage = btrfs_invalidatepage,
10710 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010711 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010712 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010713};
10714
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010715static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010716 .readpage = btrfs_readpage,
10717 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010718 .invalidatepage = btrfs_invalidatepage,
10719 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010720};
10721
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010722static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010723 .getattr = btrfs_getattr,
10724 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010725 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010726 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010727 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010728 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010729 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010730 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010731};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010732static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010733 .getattr = btrfs_getattr,
10734 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010735 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010736 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010737 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010738 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010739 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010740};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010741static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010742 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010743 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010744 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010745 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010746 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010747 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010748};
Yan, Zheng76dda932009-09-21 16:00:26 -040010749
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010750const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010751 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010752 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010753};