blob: 28491a82e6456fdf3b1ba1c350bcd74240f06976 [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);
Anand Jain566b1762018-02-15 18:07:59 +0800981 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500982
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200983 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (start == 0) {
986 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800987 ret = cow_file_range_inline(root, inode, start, end, 0,
988 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500989 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400990 /*
991 * We use DO_ACCOUNTING here because we need the
992 * delalloc_release_metadata to be run _after_ we drop
993 * our outstanding extent for clearing delalloc for this
994 * range.
995 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800996 extent_clear_unlock_delalloc(inode, start, end,
997 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400999 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1000 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1002 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001003 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001004 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001005 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001006 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001009 }
1010 }
Chris Masonc8b97812008-10-29 14:49:59 -04001011
Josef Bacik4b46fce2010-05-23 11:00:55 -04001012 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001013 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1014 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001015
Anand Jain3752d222018-02-15 12:29:38 +08001016 while (num_bytes > 0) {
1017 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001018 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001019 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001020 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001021 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001022 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001023 cur_alloc_size = ins.offset;
1024 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001025
Chris Mason771ed682008-11-06 22:02:51 -05001026 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001027 em = create_io_em(inode, start, ins.offset, /* len */
1028 start, /* orig_start */
1029 ins.objectid, /* block_start */
1030 ins.offset, /* block_len */
1031 ins.offset, /* orig_block_len */
1032 ram_size, /* ram_bytes */
1033 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001034 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001035 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001036 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001037 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038
Chris Masone6dcd2d2008-07-17 12:53:50 -04001039 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001040 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001041 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001042 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001043
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 if (root->root_key.objectid ==
1045 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1046 ret = btrfs_reloc_clone_csums(inode, start,
1047 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001048 /*
1049 * Only drop cache here, and process as normal.
1050 *
1051 * We must not allow extent_clear_unlock_delalloc()
1052 * at out_unlock label to free meta of this ordered
1053 * extent, as its meta should be freed by
1054 * btrfs_finish_ordered_io().
1055 *
1056 * So we must continue until @start is increased to
1057 * skip current ordered extent.
1058 */
Josef Bacik00361582013-08-14 14:02:47 -04001059 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001060 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1061 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001062 }
1063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001064 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001065
Chris Masonc8b97812008-10-29 14:49:59 -04001066 /* we're not doing compressed IO, don't unlock the first
1067 * page (which the caller expects to stay locked), don't
1068 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001069 *
1070 * Do set the Private2 bit so we know this page was properly
1071 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001072 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001073 page_ops = unlock ? PAGE_UNLOCK : 0;
1074 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001075
Josef Bacikc2790a22013-07-29 11:20:47 -04001076 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001077 start + ram_size - 1,
1078 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001080 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001081 if (num_bytes < cur_alloc_size)
1082 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001083 else
Anand Jain3752d222018-02-15 12:29:38 +08001084 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001087 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001088
1089 /*
1090 * btrfs_reloc_clone_csums() error, since start is increased
1091 * extent_clear_unlock_delalloc() at out_unlock label won't
1092 * free metadata of current ordered extent, we're OK to exit.
1093 */
1094 if (ret)
1095 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001096 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001097out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001098 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001099
Filipe Mananad9f85962014-08-25 10:43:00 +01001100out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001101 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001102out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001104 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001106 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1107 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001108 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1109 PAGE_END_WRITEBACK;
1110 /*
1111 * If we reserved an extent for our delalloc range (or a subrange) and
1112 * failed to create the respective ordered extent, then it means that
1113 * when we reserved the extent we decremented the extent's size from
1114 * the data space_info's bytes_may_use counter and incremented the
1115 * space_info's bytes_reserved counter by the same amount. We must make
1116 * sure extent_clear_unlock_delalloc() does not try to decrement again
1117 * the data space_info's bytes_may_use counter, therefore we do not pass
1118 * it the flag EXTENT_CLEAR_DATA_RESV.
1119 */
1120 if (extent_reserved) {
1121 extent_clear_unlock_delalloc(inode, start,
1122 start + cur_alloc_size,
1123 start + cur_alloc_size,
1124 locked_page,
1125 clear_bits,
1126 page_ops);
1127 start += cur_alloc_size;
1128 if (start >= end)
1129 goto out;
1130 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001131 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1132 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001133 clear_bits | EXTENT_CLEAR_DATA_RESV,
1134 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001136}
Chris Masonc8b97812008-10-29 14:49:59 -04001137
Chris Mason771ed682008-11-06 22:02:51 -05001138/*
1139 * work queue call back to started compression on a file and pages
1140 */
1141static noinline void async_cow_start(struct btrfs_work *work)
1142{
1143 struct async_cow *async_cow;
1144 int num_added = 0;
1145 async_cow = container_of(work, struct async_cow, work);
1146
1147 compress_file_range(async_cow->inode, async_cow->locked_page,
1148 async_cow->start, async_cow->end, async_cow,
1149 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001150 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001151 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001152 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001153 }
Chris Mason771ed682008-11-06 22:02:51 -05001154}
1155
1156/*
1157 * work queue call back to submit previously compressed pages
1158 */
1159static noinline void async_cow_submit(struct btrfs_work *work)
1160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001162 struct async_cow *async_cow;
1163 struct btrfs_root *root;
1164 unsigned long nr_pages;
1165
1166 async_cow = container_of(work, struct async_cow, work);
1167
1168 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001169 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1171 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001172
David Sterbaee863952015-02-16 19:41:40 +01001173 /*
1174 * atomic_sub_return implies a barrier for waitqueue_active
1175 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001177 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 waitqueue_active(&fs_info->async_submit_wait))
1179 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001180
Chris Masond3977122009-01-05 21:25:51 -05001181 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001182 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001183}
Chris Masonc8b97812008-10-29 14:49:59 -04001184
Chris Mason771ed682008-11-06 22:02:51 -05001185static noinline void async_cow_free(struct btrfs_work *work)
1186{
1187 struct async_cow *async_cow;
1188 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001189 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001190 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001191 kfree(async_cow);
1192}
1193
1194static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1195 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001196 unsigned long *nr_written,
1197 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001200 struct async_cow *async_cow;
1201 struct btrfs_root *root = BTRFS_I(inode)->root;
1202 unsigned long nr_pages;
1203 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001204
Chris Masona3429ab2009-10-08 12:30:20 -04001205 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001206 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001207 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001208 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001209 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001210 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001211 async_cow->root = root;
1212 async_cow->locked_page = locked_page;
1213 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001214 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001215
Wang Shilongf79707b2014-07-17 11:44:09 +08001216 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001218 cur_end = end;
1219 else
Byongho Leeee221842015-12-15 01:42:10 +09001220 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001221
1222 async_cow->end = cur_end;
1223 INIT_LIST_HEAD(&async_cow->extents);
1224
Liu Bo9e0af232014-08-15 23:36:53 +08001225 btrfs_init_work(&async_cow->work,
1226 btrfs_delalloc_helper,
1227 async_cow_start, async_cow_submit,
1228 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001230 nr_pages = (cur_end - start + PAGE_SIZE) >>
1231 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001232 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001234 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001235
Chris Mason771ed682008-11-06 22:02:51 -05001236 *nr_written += nr_pages;
1237 start = cur_end + 1;
1238 }
1239 *page_started = 1;
1240 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001241}
1242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001243static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001244 u64 bytenr, u64 num_bytes)
1245{
1246 int ret;
1247 struct btrfs_ordered_sum *sums;
1248 LIST_HEAD(list);
1249
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001250 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001251 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001252 if (ret == 0 && list_empty(&list))
1253 return 0;
1254
1255 while (!list_empty(&list)) {
1256 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1257 list_del(&sums->list);
1258 kfree(sums);
1259 }
1260 return 1;
1261}
1262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
1264 * when nowcow writeback call back. This checks for snapshots or COW copies
1265 * of the extents that exist in the file, and COWs the file as required.
1266 *
1267 * If no cow copies or snapshots exist, we write directly to the existing
1268 * blocks on disk
1269 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001270static noinline int run_delalloc_nocow(struct inode *inode,
1271 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001272 u64 start, u64 end, int *page_started, int force,
1273 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001276 struct btrfs_root *root = BTRFS_I(inode)->root;
1277 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001281 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 u64 cow_start;
1283 u64 cur_offset;
1284 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 disk_bytenr;
1287 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001288 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001292 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 int nocow;
1294 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001295 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001296 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
1298 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001299 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001300 extent_clear_unlock_delalloc(inode, start, end, end,
1301 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001302 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001303 EXTENT_DO_ACCOUNTING |
1304 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001305 PAGE_CLEAR_DIRTY |
1306 PAGE_SET_WRITEBACK |
1307 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001308 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001309 }
Li Zefan82d59022011-04-20 10:33:24 +08001310
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001311 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001312
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
1314 cur_offset = start;
1315 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001316 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001318 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1321 leaf = path->nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &found_key,
1323 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001324 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.type == BTRFS_EXTENT_DATA_KEY)
1326 path->slots[0]--;
1327 }
1328 check_prev = 0;
1329next_slot:
1330 leaf = path->nodes[0];
1331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1332 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001333 if (ret < 0) {
1334 if (cow_start != (u64)-1)
1335 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001336 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001337 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 if (ret > 0)
1339 break;
1340 leaf = path->nodes[0];
1341 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 nocow = 0;
1344 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
Filipe Manana1d512cb2015-11-09 00:33:58 +00001348 if (found_key.objectid > ino)
1349 break;
1350 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1351 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1352 path->slots[0]++;
1353 goto next_slot;
1354 }
1355 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 found_key.offset > end)
1357 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (found_key.offset > cur_offset) {
1360 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001361 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 goto out_check;
1363 }
Chris Masonc31f8832008-01-08 15:46:31 -05001364
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 fi = btrfs_item_ptr(leaf, path->slots[0],
1366 struct btrfs_file_extent_item);
1367 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001368
Josef Bacikcc95bef2013-04-04 14:31:27 -04001369 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1371 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001373 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 extent_end = found_key.offset +
1375 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001376 disk_num_bytes =
1377 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (extent_end <= start) {
1379 path->slots[0]++;
1380 goto next_slot;
1381 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 if (disk_bytenr == 0)
1383 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 if (btrfs_file_extent_compression(leaf, fi) ||
1385 btrfs_file_extent_encryption(leaf, fi) ||
1386 btrfs_file_extent_other_encoding(leaf, fi))
1387 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1389 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001390 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001391 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001392 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 found_key.offset -
1394 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001395 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 disk_bytenr += cur_offset - found_key.offset;
1398 num_bytes = min(end + 1, extent_end) - cur_offset;
1399 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001400 * if there are pending snapshots for this root,
1401 * we fall into common COW way.
1402 */
1403 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001404 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 if (!err)
1406 goto out_check;
1407 }
1408 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001409 * force cow if csum exists in the range.
1410 * this ensure that csum for a given extent are
1411 * either valid or do not exist.
1412 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001413 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001414 num_bytes)) {
1415 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001416 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001417 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001418 }
1419 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1420 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001421 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001422 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001423 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 nocow = 1;
1425 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1426 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001427 btrfs_file_extent_inline_len(leaf,
1428 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001429 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001430 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 } else {
1432 BUG_ON(1);
1433 }
1434out_check:
1435 if (extent_end <= start) {
1436 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001437 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001438 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001439 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001440 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 goto next_slot;
1442 }
1443 if (!nocow) {
1444 if (cow_start == (u64)-1)
1445 cow_start = cur_offset;
1446 cur_offset = extent_end;
1447 if (cur_offset > end)
1448 break;
1449 path->slots[0]++;
1450 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001451 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001452
David Sterbab3b4aa72011-04-21 01:20:15 +02001453 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001455 ret = cow_file_range(inode, locked_page,
1456 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001457 end, page_started, nr_written, 1,
1458 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001459 if (ret) {
1460 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001461 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001462 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001463 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001464 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001465 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001466 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001467 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001468 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001469
Yan Zhengd899e052008-10-30 14:25:28 -04001470 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001471 u64 orig_start = found_key.offset - extent_offset;
1472
1473 em = create_io_em(inode, cur_offset, num_bytes,
1474 orig_start,
1475 disk_bytenr, /* block_start */
1476 num_bytes, /* block_len */
1477 disk_num_bytes, /* orig_block_len */
1478 ram_bytes, BTRFS_COMPRESS_NONE,
1479 BTRFS_ORDERED_PREALLOC);
1480 if (IS_ERR(em)) {
1481 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001482 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001483 if (nocow)
1484 btrfs_dec_nocow_writers(fs_info,
1485 disk_bytenr);
1486 ret = PTR_ERR(em);
1487 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001488 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001489 free_extent_map(em);
1490 }
1491
1492 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001493 type = BTRFS_ORDERED_PREALLOC;
1494 } else {
1495 type = BTRFS_ORDERED_NOCOW;
1496 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001497
1498 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001499 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001500 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001501 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001502 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001503
Yan, Zhengefa56462010-05-16 10:49:59 -04001504 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001505 BTRFS_DATA_RELOC_TREE_OBJECTID)
1506 /*
1507 * Error handled later, as we must prevent
1508 * extent_clear_unlock_delalloc() in error handler
1509 * from freeing metadata of created ordered extent.
1510 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001511 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1512 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001513
Josef Bacikc2790a22013-07-29 11:20:47 -04001514 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001515 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001516 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001517 EXTENT_DELALLOC |
1518 EXTENT_CLEAR_DATA_RESV,
1519 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1520
Wang Shilonge9894fd2014-03-27 11:12:25 +08001521 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001522 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001523 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001524
1525 /*
1526 * btrfs_reloc_clone_csums() error, now we're OK to call error
1527 * handler, as metadata for created ordered extent will only
1528 * be freed by btrfs_finish_ordered_io().
1529 */
1530 if (ret)
1531 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001532 if (cur_offset > end)
1533 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001534 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001535 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001536
Josef Bacik17ca04a2012-05-31 15:58:55 -04001537 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001538 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001539 cur_offset = end;
1540 }
1541
Yan Zheng80ff3852008-10-30 14:20:02 -04001542 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001543 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1544 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001545 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001547 }
1548
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001549error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001550 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001551 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001552 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001553 EXTENT_DELALLOC | EXTENT_DEFRAG |
1554 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1555 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001556 PAGE_SET_WRITEBACK |
1557 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001558 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001560}
1561
Wang Shilong47059d92014-07-03 18:22:07 +08001562static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1563{
1564
1565 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1566 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1567 return 0;
1568
1569 /*
1570 * @defrag_bytes is a hint value, no spinlock held here,
1571 * if is not zero, it means the file is defragging.
1572 * Force cow if given extent needs to be defragged.
1573 */
1574 if (BTRFS_I(inode)->defrag_bytes &&
1575 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1576 EXTENT_DEFRAG, 0, NULL))
1577 return 1;
1578
1579 return 0;
1580}
1581
Chris Masond352ac62008-09-29 15:18:18 -04001582/*
1583 * extent_io.c call back to do delayed allocation processing
1584 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001585static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001586 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001587 unsigned long *nr_written,
1588 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001589{
Josef Bacikc6100a42017-05-05 11:57:13 -04001590 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001591 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001592 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001593 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001594
Wang Shilong47059d92014-07-03 18:22:07 +08001595 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001596 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001597 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001598 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001599 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001600 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001601 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001602 ret = cow_file_range(inode, locked_page, start, end, end,
1603 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001604 } else {
1605 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1606 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001607 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001608 page_started, nr_written,
1609 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001610 }
Qu Wenruo524272602017-03-08 10:25:52 +08001611 if (ret)
1612 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001613 return ret;
1614}
1615
Josef Bacikc6100a42017-05-05 11:57:13 -04001616static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001617 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001618{
Josef Bacikc6100a42017-05-05 11:57:13 -04001619 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001620 u64 size;
1621
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001622 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001623 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001624 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001625
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 size = orig->end - orig->start + 1;
1627 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001628 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 u64 new_size;
1630
1631 /*
Josef Bacikba117212015-03-13 15:01:24 -04001632 * See the explanation in btrfs_merge_extent_hook, the same
1633 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634 */
1635 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001636 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001637 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001638 num_extents += count_max_extents(new_size);
1639 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001640 return;
1641 }
1642
Josef Bacik9e0baf62011-07-15 15:16:44 +00001643 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001644 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001645 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001646}
1647
1648/*
1649 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1650 * extents so we can keep track of new extents that are just merged onto old
1651 * extents, such as when we are doing sequential writes, so we can properly
1652 * account for the metadata space we'll need.
1653 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001654static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001655 struct extent_state *new,
1656 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001657{
Josef Bacikc6100a42017-05-05 11:57:13 -04001658 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001660 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001661
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662 /* not delalloc, ignore it */
1663 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001664 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001665
Josef Bacik8461a3d2015-03-13 15:12:08 -04001666 if (new->start > other->start)
1667 new_size = new->end - other->start + 1;
1668 else
1669 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001670
1671 /* we're not bigger than the max, unreserve the space and go */
1672 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1673 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001674 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001675 spin_unlock(&BTRFS_I(inode)->lock);
1676 return;
1677 }
1678
1679 /*
Josef Bacikba117212015-03-13 15:01:24 -04001680 * We have to add up either side to figure out how many extents were
1681 * accounted for before we merged into one big extent. If the number of
1682 * extents we accounted for is <= the amount we need for the new range
1683 * then we can return, otherwise drop. Think of it like this
1684 *
1685 * [ 4k][MAX_SIZE]
1686 *
1687 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1688 * need 2 outstanding extents, on one side we have 1 and the other side
1689 * we have 1 so they are == and we can return. But in this case
1690 *
1691 * [MAX_SIZE+4k][MAX_SIZE+4k]
1692 *
1693 * Each range on their own accounts for 2 extents, but merged together
1694 * they are only 3 extents worth of accounting, so we need to drop in
1695 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001696 */
Josef Bacikba117212015-03-13 15:01:24 -04001697 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001698 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001699 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001700 num_extents += count_max_extents(old_size);
1701 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001702 return;
1703
Josef Bacik9e0baf62011-07-15 15:16:44 +00001704 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001705 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001706 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001707}
1708
Miao Xieeb73c1b2013-05-15 07:48:22 +00001709static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1710 struct inode *inode)
1711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001712 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1713
Miao Xieeb73c1b2013-05-15 07:48:22 +00001714 spin_lock(&root->delalloc_lock);
1715 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1716 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1717 &root->delalloc_inodes);
1718 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1719 &BTRFS_I(inode)->runtime_flags);
1720 root->nr_delalloc_inodes++;
1721 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 BUG_ON(!list_empty(&root->delalloc_root));
1724 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001725 &fs_info->delalloc_roots);
1726 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 }
1728 }
1729 spin_unlock(&root->delalloc_lock);
1730}
1731
1732static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001733 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001735 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001736
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001738 if (!list_empty(&inode->delalloc_inodes)) {
1739 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001741 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001742 root->nr_delalloc_inodes--;
1743 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001744 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 BUG_ON(list_empty(&root->delalloc_root));
1746 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001747 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001748 }
1749 }
1750 spin_unlock(&root->delalloc_lock);
1751}
1752
Chris Masond352ac62008-09-29 15:18:18 -04001753/*
1754 * extent_io.c set_bit_hook, used to track delayed allocation
1755 * bytes in this file, and to maintain the list of inodes that
1756 * have pending delalloc work to be done.
1757 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001758static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001759 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001760{
Josef Bacikc6100a42017-05-05 11:57:13 -04001761 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001762
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1764
Wang Shilong47059d92014-07-03 18:22:07 +08001765 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1766 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001767 /*
1768 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001769 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001770 * bit, which is only set or cleared with irqs on
1771 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001772 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001773 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001774 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001775 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001776 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001777
Josef Bacik8b62f872017-10-19 14:15:55 -04001778 spin_lock(&BTRFS_I(inode)->lock);
1779 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1780 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001781
Josef Bacik6a3891c2015-03-16 17:38:52 -04001782 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001783 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001784 return;
1785
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001786 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1787 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001788 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001789 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001790 if (*bits & EXTENT_DEFRAG)
1791 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001793 &BTRFS_I(inode)->runtime_flags))
1794 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001795 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001796 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001797
1798 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1799 (*bits & EXTENT_DELALLOC_NEW)) {
1800 spin_lock(&BTRFS_I(inode)->lock);
1801 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1802 state->start;
1803 spin_unlock(&BTRFS_I(inode)->lock);
1804 }
Chris Mason291d6732008-01-29 15:55:23 -05001805}
1806
Chris Masond352ac62008-09-29 15:18:18 -04001807/*
1808 * extent_io.c clear_bit_hook, see set_bit_hook for why
1809 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001810static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001811 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001812 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001813{
Josef Bacikc6100a42017-05-05 11:57:13 -04001814 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001815 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001816 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001817 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001818
Filipe Manana4a4b9642017-07-27 19:52:55 +01001819 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1820 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001821 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001822 spin_unlock(&inode->lock);
1823 }
Wang Shilong47059d92014-07-03 18:22:07 +08001824
Chris Mason75eff682008-12-15 15:54:40 -05001825 /*
1826 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001827 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001828 * bit, which is only set or cleared with irqs on
1829 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001830 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001831 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001832 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001833
Josef Bacik8b62f872017-10-19 14:15:55 -04001834 spin_lock(&inode->lock);
1835 btrfs_mod_outstanding_extents(inode, -num_extents);
1836 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001837
Josef Bacikb6d08f02013-09-27 14:57:43 -04001838 /*
1839 * We don't reserve metadata space for space cache inodes so we
1840 * don't need to call dellalloc_release_metadata if there is an
1841 * error.
1842 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001843 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001844 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001845 btrfs_delalloc_release_metadata(inode, len);
1846
Josef Bacik6a3891c2015-03-16 17:38:52 -04001847 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001849 return;
1850
Filipe Mananaa315e682017-03-06 23:04:20 +00001851 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1852 do_list && !(state->state & EXTENT_NORESERVE) &&
1853 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001854 btrfs_free_reserved_data_space_noquota(
1855 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001856 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001857
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001858 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1859 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001860 spin_lock(&inode->lock);
1861 inode->delalloc_bytes -= len;
1862 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001863 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001864 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001865 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001866 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001867 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001868
1869 if ((state->state & EXTENT_DELALLOC_NEW) &&
1870 (*bits & EXTENT_DELALLOC_NEW)) {
1871 spin_lock(&inode->lock);
1872 ASSERT(inode->new_delalloc_bytes >= len);
1873 inode->new_delalloc_bytes -= len;
1874 spin_unlock(&inode->lock);
1875 }
Chris Mason291d6732008-01-29 15:55:23 -05001876}
1877
Chris Masond352ac62008-09-29 15:18:18 -04001878/*
1879 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1880 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001881 *
1882 * return 1 if page cannot be merged to bio
1883 * return 0 if page can be merged to bio
1884 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001885 */
Mike Christie81a75f672016-06-05 14:31:54 -05001886int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001887 size_t size, struct bio *bio,
1888 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001889{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 struct inode *inode = page->mapping->host;
1891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001892 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001893 u64 length = 0;
1894 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001895 int ret;
1896
Chris Mason771ed682008-11-06 22:02:51 -05001897 if (bio_flags & EXTENT_BIO_COMPRESSED)
1898 return 0;
1899
Kent Overstreet4f024f32013-10-11 15:44:27 -07001900 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001901 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001902 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1903 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001904 if (ret < 0)
1905 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001906 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001907 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001908 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001909}
1910
Chris Masond352ac62008-09-29 15:18:18 -04001911/*
1912 * in order to insert checksums into the metadata in large chunks,
1913 * we wait until bio submission time. All the pages in the bio are
1914 * checksummed and sums are attached onto the ordered extent record.
1915 *
1916 * At IO completion time the cums attached on the ordered extent record
1917 * are inserted into the btree
1918 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001919static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001920 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001921 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001922{
Josef Bacikc6100a42017-05-05 11:57:13 -04001923 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001924 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001925
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001926 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001928 return 0;
1929}
Chris Masone0156402008-04-16 11:15:20 -04001930
Chris Mason4a69a412008-11-06 22:03:00 -05001931/*
1932 * in order to insert checksums into the metadata in large chunks,
1933 * we wait until bio submission time. All the pages in the bio are
1934 * checksummed and sums are attached onto the ordered extent record.
1935 *
1936 * At IO completion time the cums attached on the ordered extent record
1937 * are inserted into the btree
1938 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001939static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001940 int mirror_num, unsigned long bio_flags,
1941 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001942{
Josef Bacikc6100a42017-05-05 11:57:13 -04001943 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001945 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001946
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001947 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001948 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001949 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001950 bio_endio(bio);
1951 }
Stefan Behrens61891922012-11-05 18:51:52 +01001952 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001953}
1954
Chris Masond352ac62008-09-29 15:18:18 -04001955/*
Chris Masoncad321a2008-12-17 14:51:42 -05001956 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001957 * on write, or reading the csums from the tree before a read.
1958 *
1959 * Rules about async/sync submit,
1960 * a) read: sync submit
1961 *
1962 * b) write without checksum: sync submit
1963 *
1964 * c) write with checksum:
1965 * c-1) if bio is issued by fsync: sync submit
1966 * (sync_writers != 0)
1967 *
1968 * c-2) if root is reloc root: sync submit
1969 * (only in case of buffered IO)
1970 *
1971 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001972 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001973static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001974 int mirror_num, unsigned long bio_flags,
1975 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001976{
Josef Bacikc6100a42017-05-05 11:57:13 -04001977 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001978 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001979 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301980 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001981 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001982 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001983 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001984
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001985 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001986
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001987 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301988 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001989
Mike Christie37226b22016-06-05 14:31:52 -05001990 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001991 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001992 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001993 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001994
Chris Masond20f7042008-12-08 16:58:54 -05001995 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001996 ret = btrfs_submit_compressed_read(inode, bio,
1997 mirror_num,
1998 bio_flags);
1999 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002000 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002001 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002002 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002003 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002004 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002005 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002006 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002007 /* csum items have already been cloned */
2008 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2009 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002010 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002011 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2012 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002013 __btrfs_submit_bio_start,
2014 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002015 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002016 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002017 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002018 if (ret)
2019 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002020 }
2021
Chris Mason0b86a832008-03-24 15:01:56 -04002022mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002023 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002024
2025out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002026 if (ret) {
2027 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002028 bio_endio(bio);
2029 }
Stefan Behrens61891922012-11-05 18:51:52 +01002030 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002031}
Chris Mason6885f302008-02-20 16:11:05 -05002032
Chris Masond352ac62008-09-29 15:18:18 -04002033/*
2034 * given a list of ordered sums record them in the inode. This happens
2035 * at IO completion time based on sums calculated at bio submission time.
2036 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002037static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002038 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002039{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002040 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002041 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002042
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002043 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002044 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002045 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002046 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002047 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002048 if (ret)
2049 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002050 }
2051 return 0;
2052}
2053
Josef Bacik2ac55d42010-02-03 19:33:23 +00002054int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002055 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002056 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002057{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002058 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002059 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002060 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002061}
2062
Chris Masond352ac62008-09-29 15:18:18 -04002063/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002064struct btrfs_writepage_fixup {
2065 struct page *page;
2066 struct btrfs_work work;
2067};
2068
Christoph Hellwigb2950862008-12-02 09:54:17 -05002069static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002070{
2071 struct btrfs_writepage_fixup *fixup;
2072 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002073 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002074 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002075 struct page *page;
2076 struct inode *inode;
2077 u64 page_start;
2078 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002079 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002080
2081 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2082 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002083again:
Chris Mason247e7432008-07-17 12:53:51 -04002084 lock_page(page);
2085 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2086 ClearPageChecked(page);
2087 goto out_page;
2088 }
2089
2090 inode = page->mapping->host;
2091 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002092 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002093
David Sterbaff13db42015-12-03 14:30:40 +01002094 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002095 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002096
2097 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002098 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002099 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002100
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002101 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002102 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002103 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002104 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002105 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002106 unlock_page(page);
2107 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002108 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002109 goto again;
2110 }
Chris Mason247e7432008-07-17 12:53:51 -04002111
Qu Wenruo364ecf32017-02-27 15:10:38 +08002112 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002113 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002114 if (ret) {
2115 mapping_set_error(page->mapping, ret);
2116 end_extent_writepage(page, ret, page_start, page_end);
2117 ClearPageChecked(page);
2118 goto out;
2119 }
2120
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002121 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2122 &cached_state, 0);
2123 if (ret) {
2124 mapping_set_error(page->mapping, ret);
2125 end_extent_writepage(page, ret, page_start, page_end);
2126 ClearPageChecked(page);
2127 goto out;
2128 }
2129
Chris Mason247e7432008-07-17 12:53:51 -04002130 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002131 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002132 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002133out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002134 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002135 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002136out_page:
2137 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002138 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002139 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002140 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002141}
2142
2143/*
2144 * There are a few paths in the higher layers of the kernel that directly
2145 * set the page dirty bit without asking the filesystem if it is a
2146 * good idea. This causes problems because we want to make sure COW
2147 * properly happens and the data=ordered rules are followed.
2148 *
Chris Masonc8b97812008-10-29 14:49:59 -04002149 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002150 * hasn't been properly setup for IO. We kick off an async process
2151 * to fix it up. The async helper will wait for ordered extents, set
2152 * the delalloc bit and make it safe to write the page.
2153 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002154static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002155{
2156 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002157 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002158 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002159
Chris Mason8b62b722009-09-02 16:53:46 -04002160 /* this page is properly in the ordered list */
2161 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002162 return 0;
2163
2164 if (PageChecked(page))
2165 return -EAGAIN;
2166
2167 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2168 if (!fixup)
2169 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002170
Chris Mason247e7432008-07-17 12:53:51 -04002171 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002172 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002173 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2174 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002175 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002177 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002178}
2179
Yan Zhengd899e052008-10-30 14:25:28 -04002180static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2181 struct inode *inode, u64 file_pos,
2182 u64 disk_bytenr, u64 disk_num_bytes,
2183 u64 num_bytes, u64 ram_bytes,
2184 u8 compression, u8 encryption,
2185 u16 other_encoding, int extent_type)
2186{
2187 struct btrfs_root *root = BTRFS_I(inode)->root;
2188 struct btrfs_file_extent_item *fi;
2189 struct btrfs_path *path;
2190 struct extent_buffer *leaf;
2191 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002192 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002193 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002194 int ret;
2195
2196 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002197 if (!path)
2198 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002199
Chris Masona1ed8352009-09-11 12:27:37 -04002200 /*
2201 * we may be replacing one extent in the tree with another.
2202 * The new extent is pinned in the extent map, and we don't want
2203 * to drop it from the cache until it is completely in the btree.
2204 *
2205 * So, tell btrfs_drop_extents to leave this extent in the cache.
2206 * the caller is expected to unpin it and allow it to be merged
2207 * with the others.
2208 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002209 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2210 file_pos + num_bytes, NULL, 0,
2211 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002212 if (ret)
2213 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002214
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002215 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002216 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002217 ins.offset = file_pos;
2218 ins.type = BTRFS_EXTENT_DATA_KEY;
2219
2220 path->leave_spinning = 1;
2221 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2222 sizeof(*fi));
2223 if (ret)
2224 goto out;
2225 }
Yan Zhengd899e052008-10-30 14:25:28 -04002226 leaf = path->nodes[0];
2227 fi = btrfs_item_ptr(leaf, path->slots[0],
2228 struct btrfs_file_extent_item);
2229 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2230 btrfs_set_file_extent_type(leaf, fi, extent_type);
2231 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2232 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2233 btrfs_set_file_extent_offset(leaf, fi, 0);
2234 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2235 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2236 btrfs_set_file_extent_compression(leaf, fi, compression);
2237 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2238 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002239
Yan Zhengd899e052008-10-30 14:25:28 -04002240 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002241 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002242
2243 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002244
2245 ins.objectid = disk_bytenr;
2246 ins.offset = disk_num_bytes;
2247 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002248
Qu Wenruo297d7502015-09-08 17:08:37 +08002249 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002250 * Release the reserved range from inode dirty range map, as it is
2251 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002252 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002253 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2254 if (ret < 0)
2255 goto out;
2256 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002257 ret = btrfs_alloc_reserved_file_extent(trans, root,
2258 btrfs_ino(BTRFS_I(inode)),
2259 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260out:
Yan Zhengd899e052008-10-30 14:25:28 -04002261 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002262
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002263 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002264}
2265
Liu Bo38c227d2013-01-29 03:18:40 +00002266/* snapshot-aware defrag */
2267struct sa_defrag_extent_backref {
2268 struct rb_node node;
2269 struct old_sa_defrag_extent *old;
2270 u64 root_id;
2271 u64 inum;
2272 u64 file_pos;
2273 u64 extent_offset;
2274 u64 num_bytes;
2275 u64 generation;
2276};
2277
2278struct old_sa_defrag_extent {
2279 struct list_head list;
2280 struct new_sa_defrag_extent *new;
2281
2282 u64 extent_offset;
2283 u64 bytenr;
2284 u64 offset;
2285 u64 len;
2286 int count;
2287};
2288
2289struct new_sa_defrag_extent {
2290 struct rb_root root;
2291 struct list_head head;
2292 struct btrfs_path *path;
2293 struct inode *inode;
2294 u64 file_pos;
2295 u64 len;
2296 u64 bytenr;
2297 u64 disk_len;
2298 u8 compress_type;
2299};
2300
2301static int backref_comp(struct sa_defrag_extent_backref *b1,
2302 struct sa_defrag_extent_backref *b2)
2303{
2304 if (b1->root_id < b2->root_id)
2305 return -1;
2306 else if (b1->root_id > b2->root_id)
2307 return 1;
2308
2309 if (b1->inum < b2->inum)
2310 return -1;
2311 else if (b1->inum > b2->inum)
2312 return 1;
2313
2314 if (b1->file_pos < b2->file_pos)
2315 return -1;
2316 else if (b1->file_pos > b2->file_pos)
2317 return 1;
2318
2319 /*
2320 * [------------------------------] ===> (a range of space)
2321 * |<--->| |<---->| =============> (fs/file tree A)
2322 * |<---------------------------->| ===> (fs/file tree B)
2323 *
2324 * A range of space can refer to two file extents in one tree while
2325 * refer to only one file extent in another tree.
2326 *
2327 * So we may process a disk offset more than one time(two extents in A)
2328 * and locate at the same extent(one extent in B), then insert two same
2329 * backrefs(both refer to the extent in B).
2330 */
2331 return 0;
2332}
2333
2334static void backref_insert(struct rb_root *root,
2335 struct sa_defrag_extent_backref *backref)
2336{
2337 struct rb_node **p = &root->rb_node;
2338 struct rb_node *parent = NULL;
2339 struct sa_defrag_extent_backref *entry;
2340 int ret;
2341
2342 while (*p) {
2343 parent = *p;
2344 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2345
2346 ret = backref_comp(backref, entry);
2347 if (ret < 0)
2348 p = &(*p)->rb_left;
2349 else
2350 p = &(*p)->rb_right;
2351 }
2352
2353 rb_link_node(&backref->node, parent, p);
2354 rb_insert_color(&backref->node, root);
2355}
2356
2357/*
2358 * Note the backref might has changed, and in this case we just return 0.
2359 */
2360static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2361 void *ctx)
2362{
2363 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002364 struct old_sa_defrag_extent *old = ctx;
2365 struct new_sa_defrag_extent *new = old->new;
2366 struct btrfs_path *path = new->path;
2367 struct btrfs_key key;
2368 struct btrfs_root *root;
2369 struct sa_defrag_extent_backref *backref;
2370 struct extent_buffer *leaf;
2371 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002373 int slot;
2374 int ret;
2375 u64 extent_offset;
2376 u64 num_bytes;
2377
2378 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002379 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002380 return 0;
2381
2382 key.objectid = root_id;
2383 key.type = BTRFS_ROOT_ITEM_KEY;
2384 key.offset = (u64)-1;
2385
Liu Bo38c227d2013-01-29 03:18:40 +00002386 root = btrfs_read_fs_root_no_name(fs_info, &key);
2387 if (IS_ERR(root)) {
2388 if (PTR_ERR(root) == -ENOENT)
2389 return 0;
2390 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002391 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002392 inum, offset, root_id);
2393 return PTR_ERR(root);
2394 }
2395
2396 key.objectid = inum;
2397 key.type = BTRFS_EXTENT_DATA_KEY;
2398 if (offset > (u64)-1 << 32)
2399 key.offset = 0;
2400 else
2401 key.offset = offset;
2402
2403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302404 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002405 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002406 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002407
2408 while (1) {
2409 cond_resched();
2410
2411 leaf = path->nodes[0];
2412 slot = path->slots[0];
2413
2414 if (slot >= btrfs_header_nritems(leaf)) {
2415 ret = btrfs_next_leaf(root, path);
2416 if (ret < 0) {
2417 goto out;
2418 } else if (ret > 0) {
2419 ret = 0;
2420 goto out;
2421 }
2422 continue;
2423 }
2424
2425 path->slots[0]++;
2426
2427 btrfs_item_key_to_cpu(leaf, &key, slot);
2428
2429 if (key.objectid > inum)
2430 goto out;
2431
2432 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2433 continue;
2434
2435 extent = btrfs_item_ptr(leaf, slot,
2436 struct btrfs_file_extent_item);
2437
2438 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2439 continue;
2440
Liu Boe68afa42013-07-01 22:13:26 +08002441 /*
2442 * 'offset' refers to the exact key.offset,
2443 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2444 * (key.offset - extent_offset).
2445 */
2446 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002447 continue;
2448
Liu Boe68afa42013-07-01 22:13:26 +08002449 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002450 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002451
Liu Bo38c227d2013-01-29 03:18:40 +00002452 if (extent_offset >= old->extent_offset + old->offset +
2453 old->len || extent_offset + num_bytes <=
2454 old->extent_offset + old->offset)
2455 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002456 break;
2457 }
2458
2459 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2460 if (!backref) {
2461 ret = -ENOENT;
2462 goto out;
2463 }
2464
2465 backref->root_id = root_id;
2466 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002467 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002468 backref->num_bytes = num_bytes;
2469 backref->extent_offset = extent_offset;
2470 backref->generation = btrfs_file_extent_generation(leaf, extent);
2471 backref->old = old;
2472 backref_insert(&new->root, backref);
2473 old->count++;
2474out:
2475 btrfs_release_path(path);
2476 WARN_ON(ret);
2477 return ret;
2478}
2479
2480static noinline bool record_extent_backrefs(struct btrfs_path *path,
2481 struct new_sa_defrag_extent *new)
2482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002483 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002484 struct old_sa_defrag_extent *old, *tmp;
2485 int ret;
2486
2487 new->path = path;
2488
2489 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002490 ret = iterate_inodes_from_logical(old->bytenr +
2491 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002492 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002493 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002494 if (ret < 0 && ret != -ENOENT)
2495 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002496
2497 /* no backref to be processed for this extent */
2498 if (!old->count) {
2499 list_del(&old->list);
2500 kfree(old);
2501 }
2502 }
2503
2504 if (list_empty(&new->head))
2505 return false;
2506
2507 return true;
2508}
2509
2510static int relink_is_mergable(struct extent_buffer *leaf,
2511 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002512 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002513{
Liu Bo116e0022013-08-02 16:30:40 +08002514 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002515 return 0;
2516
2517 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2518 return 0;
2519
Liu Bo116e0022013-08-02 16:30:40 +08002520 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2521 return 0;
2522
2523 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002524 btrfs_file_extent_other_encoding(leaf, fi))
2525 return 0;
2526
2527 return 1;
2528}
2529
2530/*
2531 * Note the backref might has changed, and in this case we just return 0.
2532 */
2533static noinline int relink_extent_backref(struct btrfs_path *path,
2534 struct sa_defrag_extent_backref *prev,
2535 struct sa_defrag_extent_backref *backref)
2536{
2537 struct btrfs_file_extent_item *extent;
2538 struct btrfs_file_extent_item *item;
2539 struct btrfs_ordered_extent *ordered;
2540 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002541 struct btrfs_root *root;
2542 struct btrfs_key key;
2543 struct extent_buffer *leaf;
2544 struct old_sa_defrag_extent *old = backref->old;
2545 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002546 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002547 struct inode *inode;
2548 struct extent_state *cached = NULL;
2549 int ret = 0;
2550 u64 start;
2551 u64 len;
2552 u64 lock_start;
2553 u64 lock_end;
2554 bool merge = false;
2555 int index;
2556
2557 if (prev && prev->root_id == backref->root_id &&
2558 prev->inum == backref->inum &&
2559 prev->file_pos + prev->num_bytes == backref->file_pos)
2560 merge = true;
2561
2562 /* step 1: get root */
2563 key.objectid = backref->root_id;
2564 key.type = BTRFS_ROOT_ITEM_KEY;
2565 key.offset = (u64)-1;
2566
Liu Bo38c227d2013-01-29 03:18:40 +00002567 index = srcu_read_lock(&fs_info->subvol_srcu);
2568
2569 root = btrfs_read_fs_root_no_name(fs_info, &key);
2570 if (IS_ERR(root)) {
2571 srcu_read_unlock(&fs_info->subvol_srcu, index);
2572 if (PTR_ERR(root) == -ENOENT)
2573 return 0;
2574 return PTR_ERR(root);
2575 }
Liu Bo38c227d2013-01-29 03:18:40 +00002576
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002577 if (btrfs_root_readonly(root)) {
2578 srcu_read_unlock(&fs_info->subvol_srcu, index);
2579 return 0;
2580 }
2581
Liu Bo38c227d2013-01-29 03:18:40 +00002582 /* step 2: get inode */
2583 key.objectid = backref->inum;
2584 key.type = BTRFS_INODE_ITEM_KEY;
2585 key.offset = 0;
2586
2587 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2588 if (IS_ERR(inode)) {
2589 srcu_read_unlock(&fs_info->subvol_srcu, index);
2590 return 0;
2591 }
2592
2593 srcu_read_unlock(&fs_info->subvol_srcu, index);
2594
2595 /* step 3: relink backref */
2596 lock_start = backref->file_pos;
2597 lock_end = backref->file_pos + backref->num_bytes - 1;
2598 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002599 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002600
2601 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2602 if (ordered) {
2603 btrfs_put_ordered_extent(ordered);
2604 goto out_unlock;
2605 }
2606
2607 trans = btrfs_join_transaction(root);
2608 if (IS_ERR(trans)) {
2609 ret = PTR_ERR(trans);
2610 goto out_unlock;
2611 }
2612
2613 key.objectid = backref->inum;
2614 key.type = BTRFS_EXTENT_DATA_KEY;
2615 key.offset = backref->file_pos;
2616
2617 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2618 if (ret < 0) {
2619 goto out_free_path;
2620 } else if (ret > 0) {
2621 ret = 0;
2622 goto out_free_path;
2623 }
2624
2625 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2626 struct btrfs_file_extent_item);
2627
2628 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2629 backref->generation)
2630 goto out_free_path;
2631
2632 btrfs_release_path(path);
2633
2634 start = backref->file_pos;
2635 if (backref->extent_offset < old->extent_offset + old->offset)
2636 start += old->extent_offset + old->offset -
2637 backref->extent_offset;
2638
2639 len = min(backref->extent_offset + backref->num_bytes,
2640 old->extent_offset + old->offset + old->len);
2641 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2642
2643 ret = btrfs_drop_extents(trans, root, inode, start,
2644 start + len, 1);
2645 if (ret)
2646 goto out_free_path;
2647again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002648 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002649 key.type = BTRFS_EXTENT_DATA_KEY;
2650 key.offset = start;
2651
Liu Boa09a0a72013-03-11 09:20:58 +00002652 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002653 if (merge) {
2654 struct btrfs_file_extent_item *fi;
2655 u64 extent_len;
2656 struct btrfs_key found_key;
2657
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002658 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002659 if (ret < 0)
2660 goto out_free_path;
2661
2662 path->slots[0]--;
2663 leaf = path->nodes[0];
2664 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2665
2666 fi = btrfs_item_ptr(leaf, path->slots[0],
2667 struct btrfs_file_extent_item);
2668 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2669
Liu Bo116e0022013-08-02 16:30:40 +08002670 if (extent_len + found_key.offset == start &&
2671 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002672 btrfs_set_file_extent_num_bytes(leaf, fi,
2673 extent_len + len);
2674 btrfs_mark_buffer_dirty(leaf);
2675 inode_add_bytes(inode, len);
2676
2677 ret = 1;
2678 goto out_free_path;
2679 } else {
2680 merge = false;
2681 btrfs_release_path(path);
2682 goto again;
2683 }
2684 }
2685
2686 ret = btrfs_insert_empty_item(trans, root, path, &key,
2687 sizeof(*extent));
2688 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002689 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002690 goto out_free_path;
2691 }
2692
2693 leaf = path->nodes[0];
2694 item = btrfs_item_ptr(leaf, path->slots[0],
2695 struct btrfs_file_extent_item);
2696 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2697 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2698 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2699 btrfs_set_file_extent_num_bytes(leaf, item, len);
2700 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2701 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2702 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2703 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2704 btrfs_set_file_extent_encryption(leaf, item, 0);
2705 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2706
2707 btrfs_mark_buffer_dirty(leaf);
2708 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002709 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002710
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002711 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002712 new->disk_len, 0,
2713 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002714 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002715 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002716 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002717 goto out_free_path;
2718 }
2719
2720 ret = 1;
2721out_free_path:
2722 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002723 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002724 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002725out_unlock:
2726 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002727 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002728 iput(inode);
2729 return ret;
2730}
2731
Liu Bo6f519562013-10-29 10:45:05 +08002732static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2733{
2734 struct old_sa_defrag_extent *old, *tmp;
2735
2736 if (!new)
2737 return;
2738
2739 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002740 kfree(old);
2741 }
2742 kfree(new);
2743}
2744
Liu Bo38c227d2013-01-29 03:18:40 +00002745static void relink_file_extents(struct new_sa_defrag_extent *new)
2746{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002747 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002748 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002749 struct sa_defrag_extent_backref *backref;
2750 struct sa_defrag_extent_backref *prev = NULL;
2751 struct inode *inode;
2752 struct btrfs_root *root;
2753 struct rb_node *node;
2754 int ret;
2755
2756 inode = new->inode;
2757 root = BTRFS_I(inode)->root;
2758
2759 path = btrfs_alloc_path();
2760 if (!path)
2761 return;
2762
2763 if (!record_extent_backrefs(path, new)) {
2764 btrfs_free_path(path);
2765 goto out;
2766 }
2767 btrfs_release_path(path);
2768
2769 while (1) {
2770 node = rb_first(&new->root);
2771 if (!node)
2772 break;
2773 rb_erase(node, &new->root);
2774
2775 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2776
2777 ret = relink_extent_backref(path, prev, backref);
2778 WARN_ON(ret < 0);
2779
2780 kfree(prev);
2781
2782 if (ret == 1)
2783 prev = backref;
2784 else
2785 prev = NULL;
2786 cond_resched();
2787 }
2788 kfree(prev);
2789
2790 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002791out:
Liu Bo6f519562013-10-29 10:45:05 +08002792 free_sa_defrag_extent(new);
2793
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 atomic_dec(&fs_info->defrag_running);
2795 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002796}
2797
2798static struct new_sa_defrag_extent *
2799record_old_file_extents(struct inode *inode,
2800 struct btrfs_ordered_extent *ordered)
2801{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002802 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002803 struct btrfs_root *root = BTRFS_I(inode)->root;
2804 struct btrfs_path *path;
2805 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002806 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002807 struct new_sa_defrag_extent *new;
2808 int ret;
2809
2810 new = kmalloc(sizeof(*new), GFP_NOFS);
2811 if (!new)
2812 return NULL;
2813
2814 new->inode = inode;
2815 new->file_pos = ordered->file_offset;
2816 new->len = ordered->len;
2817 new->bytenr = ordered->start;
2818 new->disk_len = ordered->disk_len;
2819 new->compress_type = ordered->compress_type;
2820 new->root = RB_ROOT;
2821 INIT_LIST_HEAD(&new->head);
2822
2823 path = btrfs_alloc_path();
2824 if (!path)
2825 goto out_kfree;
2826
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002827 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002828 key.type = BTRFS_EXTENT_DATA_KEY;
2829 key.offset = new->file_pos;
2830
2831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2832 if (ret < 0)
2833 goto out_free_path;
2834 if (ret > 0 && path->slots[0] > 0)
2835 path->slots[0]--;
2836
2837 /* find out all the old extents for the file range */
2838 while (1) {
2839 struct btrfs_file_extent_item *extent;
2840 struct extent_buffer *l;
2841 int slot;
2842 u64 num_bytes;
2843 u64 offset;
2844 u64 end;
2845 u64 disk_bytenr;
2846 u64 extent_offset;
2847
2848 l = path->nodes[0];
2849 slot = path->slots[0];
2850
2851 if (slot >= btrfs_header_nritems(l)) {
2852 ret = btrfs_next_leaf(root, path);
2853 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002854 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002855 else if (ret > 0)
2856 break;
2857 continue;
2858 }
2859
2860 btrfs_item_key_to_cpu(l, &key, slot);
2861
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002862 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002863 break;
2864 if (key.type != BTRFS_EXTENT_DATA_KEY)
2865 break;
2866 if (key.offset >= new->file_pos + new->len)
2867 break;
2868
2869 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2870
2871 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2872 if (key.offset + num_bytes < new->file_pos)
2873 goto next;
2874
2875 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2876 if (!disk_bytenr)
2877 goto next;
2878
2879 extent_offset = btrfs_file_extent_offset(l, extent);
2880
2881 old = kmalloc(sizeof(*old), GFP_NOFS);
2882 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002883 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002884
2885 offset = max(new->file_pos, key.offset);
2886 end = min(new->file_pos + new->len, key.offset + num_bytes);
2887
2888 old->bytenr = disk_bytenr;
2889 old->extent_offset = extent_offset;
2890 old->offset = offset - key.offset;
2891 old->len = end - offset;
2892 old->new = new;
2893 old->count = 0;
2894 list_add_tail(&old->list, &new->head);
2895next:
2896 path->slots[0]++;
2897 cond_resched();
2898 }
2899
2900 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002901 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002902
2903 return new;
2904
Liu Bo38c227d2013-01-29 03:18:40 +00002905out_free_path:
2906 btrfs_free_path(path);
2907out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002908 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002909 return NULL;
2910}
2911
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002912static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002913 u64 start, u64 len)
2914{
2915 struct btrfs_block_group_cache *cache;
2916
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002918 ASSERT(cache);
2919
2920 spin_lock(&cache->lock);
2921 cache->delalloc_bytes -= len;
2922 spin_unlock(&cache->lock);
2923
2924 btrfs_put_block_group(cache);
2925}
2926
Chris Masond352ac62008-09-29 15:18:18 -04002927/* as ordered data IO finishes, this gets called so we can finish
2928 * an ordered extent if the range of bytes in the file it covers are
2929 * fully written.
2930 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002931static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002932{
Josef Bacik5fd02042012-05-02 14:00:54 -04002933 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002936 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002937 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002938 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002939 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002940 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002941 int ret = 0;
2942 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002943 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002944 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002945 bool range_locked = false;
2946 bool clear_new_delalloc_bytes = false;
2947
2948 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2949 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2950 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2951 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002952
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002953 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002954
Josef Bacik5fd02042012-05-02 14:00:54 -04002955 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2956 ret = -EIO;
2957 goto out;
2958 }
2959
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002960 btrfs_free_io_failure_record(BTRFS_I(inode),
2961 ordered_extent->file_offset,
2962 ordered_extent->file_offset +
2963 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002964
Josef Bacik77cef2e2013-08-29 13:57:21 -04002965 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2966 truncated = true;
2967 logical_len = ordered_extent->truncated_len;
2968 /* Truncated the entire extent, don't bother adding */
2969 if (!logical_len)
2970 goto out;
2971 }
2972
Yan, Zhengc2167752009-11-12 09:34:21 +00002973 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002974 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002975
2976 /*
2977 * For mwrite(mmap + memset to write) case, we still reserve
2978 * space for NOCOW range.
2979 * As NOCOW won't cause a new delayed ref, just free the space
2980 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002981 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002982 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002983 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2984 if (nolock)
2985 trans = btrfs_join_transaction_nolock(root);
2986 else
2987 trans = btrfs_join_transaction(root);
2988 if (IS_ERR(trans)) {
2989 ret = PTR_ERR(trans);
2990 trans = NULL;
2991 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002992 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002993 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002994 ret = btrfs_update_inode_fallback(trans, root, inode);
2995 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002996 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002997 goto out;
2998 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002999
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003000 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003001 lock_extent_bits(io_tree, ordered_extent->file_offset,
3002 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003003 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003004
Liu Bo38c227d2013-01-29 03:18:40 +00003005 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3006 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003007 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003008 if (ret) {
3009 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003010 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003011 /* the inode is shared */
3012 new = record_old_file_extents(inode, ordered_extent);
3013
3014 clear_extent_bit(io_tree, ordered_extent->file_offset,
3015 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003016 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003017 }
3018
Josef Bacik0cb59c92010-07-02 12:14:14 -04003019 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003020 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003021 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003022 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003023 if (IS_ERR(trans)) {
3024 ret = PTR_ERR(trans);
3025 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003026 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 }
Chris Masona79b7d42014-05-22 16:18:52 -07003028
Josef Bacik69fe2d72017-10-19 14:15:57 -04003029 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003030
Chris Masonc8b97812008-10-29 14:49:59 -04003031 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003032 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003033 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003034 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003035 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3036 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003037 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003038 ordered_extent->file_offset,
3039 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003040 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003041 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003043 ret = insert_reserved_file_extent(trans, inode,
3044 ordered_extent->file_offset,
3045 ordered_extent->start,
3046 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003047 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003048 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003049 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003050 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003051 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003052 ordered_extent->start,
3053 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003054 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003055 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3056 ordered_extent->file_offset, ordered_extent->len,
3057 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003059 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003060 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003061 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003062
Nikolay Borisovac01f262018-01-08 10:59:43 +02003063 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3064 if (ret) {
3065 btrfs_abort_transaction(trans, ret);
3066 goto out;
3067 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003068
Josef Bacik6c760c02012-11-09 10:53:21 -05003069 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3070 ret = btrfs_update_inode_fallback(trans, root, inode);
3071 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003072 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003073 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003074 }
3075 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003076out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003077 if (range_locked || clear_new_delalloc_bytes) {
3078 unsigned int clear_bits = 0;
3079
3080 if (range_locked)
3081 clear_bits |= EXTENT_LOCKED;
3082 if (clear_new_delalloc_bytes)
3083 clear_bits |= EXTENT_DELALLOC_NEW;
3084 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3085 ordered_extent->file_offset,
3086 ordered_extent->file_offset +
3087 ordered_extent->len - 1,
3088 clear_bits,
3089 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003090 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003091 }
3092
Miao Xiea698d0752012-09-20 01:51:59 -06003093 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003094 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003095
Josef Bacik77cef2e2013-08-29 13:57:21 -04003096 if (ret || truncated) {
3097 u64 start, end;
3098
3099 if (truncated)
3100 start = ordered_extent->file_offset + logical_len;
3101 else
3102 start = ordered_extent->file_offset;
3103 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003104 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003105
3106 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003107 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003108
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003109 /*
3110 * If the ordered extent had an IOERR or something else went
3111 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003112 * back to the allocator. We only free the extent in the
3113 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003114 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003115 if ((ret || !logical_len) &&
3116 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003117 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003118 btrfs_free_reserved_extent(fs_info,
3119 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003120 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003121 }
3122
3123
Josef Bacik5fd02042012-05-02 14:00:54 -04003124 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003125 * This needs to be done to make sure anybody waiting knows we are done
3126 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003127 */
3128 btrfs_remove_ordered_extent(inode, ordered_extent);
3129
Liu Bo38c227d2013-01-29 03:18:40 +00003130 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003131 if (new) {
3132 if (ret) {
3133 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003134 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003135 } else {
3136 relink_file_extents(new);
3137 }
3138 }
Liu Bo38c227d2013-01-29 03:18:40 +00003139
Chris Masone6dcd2d2008-07-17 12:53:50 -04003140 /* once for us */
3141 btrfs_put_ordered_extent(ordered_extent);
3142 /* once for the tree */
3143 btrfs_put_ordered_extent(ordered_extent);
3144
Josef Bacik5fd02042012-05-02 14:00:54 -04003145 return ret;
3146}
3147
3148static void finish_ordered_fn(struct btrfs_work *work)
3149{
3150 struct btrfs_ordered_extent *ordered_extent;
3151 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3152 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003153}
3154
David Sterbac3988d62017-02-17 15:18:32 +01003155static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003156 struct extent_state *state, int uptodate)
3157{
Josef Bacik5fd02042012-05-02 14:00:54 -04003158 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003159 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003160 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003161 struct btrfs_workqueue *wq;
3162 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003163
liubo1abe9b82011-03-24 11:18:59 +00003164 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3165
Chris Mason8b62b722009-09-02 16:53:46 -04003166 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003167 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3168 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003169 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003170
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003171 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003172 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003173 func = btrfs_freespace_write_helper;
3174 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003175 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003176 func = btrfs_endio_write_helper;
3177 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003178
Liu Bo9e0af232014-08-15 23:36:53 +08003179 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3180 NULL);
3181 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003182}
3183
Miao Xiedc380ae2014-09-12 18:43:55 +08003184static int __readpage_endio_check(struct inode *inode,
3185 struct btrfs_io_bio *io_bio,
3186 int icsum, struct page *page,
3187 int pgoff, u64 start, size_t len)
3188{
3189 char *kaddr;
3190 u32 csum_expected;
3191 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003192
3193 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3194
3195 kaddr = kmap_atomic(page);
3196 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003197 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003198 if (csum != csum_expected)
3199 goto zeroit;
3200
3201 kunmap_atomic(kaddr);
3202 return 0;
3203zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003204 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003205 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003206 memset(kaddr + pgoff, 1, len);
3207 flush_dcache_page(page);
3208 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003209 return -EIO;
3210}
3211
Chris Masond352ac62008-09-29 15:18:18 -04003212/*
Chris Masond352ac62008-09-29 15:18:18 -04003213 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003214 * if there's a match, we allow the bio to finish. If not, the code in
3215 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003216 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003217static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3218 u64 phy_offset, struct page *page,
3219 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003220{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003221 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003222 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003223 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003224 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003225
Chris Masond20f7042008-12-08 16:58:54 -05003226 if (PageChecked(page)) {
3227 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003228 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003229 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003230
3231 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003232 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003233
Yan Zheng17d217f2008-12-12 10:03:38 -05003234 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003235 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003236 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003237 return 0;
3238 }
3239
Miao Xiefacc8a222013-07-25 19:22:34 +08003240 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003241 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3242 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003243}
Chris Masonb888db22007-08-27 16:49:44 -04003244
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003245void btrfs_add_delayed_iput(struct inode *inode)
3246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003248 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003249
3250 if (atomic_add_unless(&inode->i_count, -1, 1))
3251 return;
3252
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003254 if (binode->delayed_iput_count == 0) {
3255 ASSERT(list_empty(&binode->delayed_iput));
3256 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3257 } else {
3258 binode->delayed_iput_count++;
3259 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003260 spin_unlock(&fs_info->delayed_iput_lock);
3261}
3262
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003263void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003264{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003265
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003266 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003267 while (!list_empty(&fs_info->delayed_iputs)) {
3268 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269
David Sterba8089fe62015-11-19 14:15:51 +01003270 inode = list_first_entry(&fs_info->delayed_iputs,
3271 struct btrfs_inode, delayed_iput);
3272 if (inode->delayed_iput_count) {
3273 inode->delayed_iput_count--;
3274 list_move_tail(&inode->delayed_iput,
3275 &fs_info->delayed_iputs);
3276 } else {
3277 list_del_init(&inode->delayed_iput);
3278 }
3279 spin_unlock(&fs_info->delayed_iput_lock);
3280 iput(&inode->vfs_inode);
3281 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003282 }
David Sterba8089fe62015-11-19 14:15:51 +01003283 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003284}
3285
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003287 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 * files in the subvolume, it removes orphan item and frees block_rsv
3289 * structure.
3290 */
3291void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3292 struct btrfs_root *root)
3293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003295 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 int ret;
3297
Josef Bacik8a35d952012-05-23 14:26:42 -04003298 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3300 return;
3301
Josef Bacik90290e12011-12-02 15:44:12 -05003302 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003303 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003304 spin_unlock(&root->orphan_lock);
3305 return;
3306 }
3307
3308 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3309 spin_unlock(&root->orphan_lock);
3310 return;
3311 }
3312
3313 block_rsv = root->orphan_block_rsv;
3314 root->orphan_block_rsv = NULL;
3315 spin_unlock(&root->orphan_lock);
3316
Miao Xie27cdeb72014-04-02 19:51:05 +08003317 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003319 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003321 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003322 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003323 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003324 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3325 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003326 }
3327
Josef Bacik90290e12011-12-02 15:44:12 -05003328 if (block_rsv) {
3329 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003330 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003331 }
3332}
3333
3334/*
Josef Bacik7b128762008-07-24 12:17:14 -04003335 * This creates an orphan entry for the given inode in case something goes
3336 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 *
3338 * NOTE: caller of this function should reserve 5 units of metadata for
3339 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003340 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003341int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3342 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003343{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003344 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3345 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 struct btrfs_block_rsv *block_rsv = NULL;
3347 int reserve = 0;
3348 int insert = 0;
3349 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003350
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003352 block_rsv = btrfs_alloc_block_rsv(fs_info,
3353 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003354 if (!block_rsv)
3355 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003356 }
3357
Yan, Zhengd68fc572010-05-16 10:49:58 -04003358 spin_lock(&root->orphan_lock);
3359 if (!root->orphan_block_rsv) {
3360 root->orphan_block_rsv = block_rsv;
3361 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003362 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 block_rsv = NULL;
3364 }
Josef Bacik7b128762008-07-24 12:17:14 -04003365
Josef Bacik8a35d952012-05-23 14:26:42 -04003366 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003367 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368#if 0
3369 /*
3370 * For proper ENOSPC handling, we should do orphan
3371 * cleanup when mounting. But this introduces backward
3372 * compatibility issue.
3373 */
3374 if (!xchg(&root->orphan_item_inserted, 1))
3375 insert = 2;
3376 else
3377 insert = 1;
3378#endif
3379 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003380 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003381 }
Josef Bacik7b128762008-07-24 12:17:14 -04003382
Josef Bacik72ac3c02012-05-23 14:13:11 -04003383 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003384 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003386 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003387
Yan, Zhengd68fc572010-05-16 10:49:58 -04003388 /* grab metadata reservation from transaction handle */
3389 if (reserve) {
3390 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003391 ASSERT(!ret);
3392 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003393 /*
3394 * dec doesn't need spin_lock as ->orphan_block_rsv
3395 * would be released only if ->orphan_inodes is
3396 * zero.
3397 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003398 atomic_dec(&root->orphan_inodes);
3399 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003400 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003401 if (insert)
3402 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003403 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003404 return ret;
3405 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003406 }
3407
3408 /* insert an orphan item to track this unlinked/truncated file */
3409 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003410 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003411 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003412 if (reserve) {
3413 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003414 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003415 btrfs_orphan_release_metadata(inode);
3416 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003417 /*
3418 * btrfs_orphan_commit_root may race with us and set
3419 * ->orphan_block_rsv to zero, in order to avoid that,
3420 * decrease ->orphan_inodes after everything is done.
3421 */
3422 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003423 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003424 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003425 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003426 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003427 return ret;
3428 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003429 }
3430 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003431 }
3432
3433 /* insert an orphan item to track subvolume contains orphan files */
3434 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003436 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003437 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003438 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003439 return ret;
3440 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 }
3442 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003443}
3444
3445/*
3446 * We have done the truncate/delete so we can go ahead and remove the orphan
3447 * item for this particular inode.
3448 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003449static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003450 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003451{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003452 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003453 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003454 int ret = 0;
3455
Josef Bacik8a35d952012-05-23 14:26:42 -04003456 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003457 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003458 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003459
Liu Bo1a932ef2018-01-25 11:02:54 -07003460 if (delete_item && trans)
3461 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3462
Josef Bacik72ac3c02012-05-23 14:13:11 -04003463 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003464 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003465 btrfs_orphan_release_metadata(inode);
3466
Liu Bo1a932ef2018-01-25 11:02:54 -07003467 /*
3468 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3469 * to zero, in order to avoid that, decrease ->orphan_inodes after
3470 * everything is done.
3471 */
3472 if (delete_item)
3473 atomic_dec(&root->orphan_inodes);
3474
Josef Bacik4ef31a42013-08-13 14:10:08 -04003475 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003476}
3477
3478/*
3479 * this cleans up any orphans that may be left on the list from the last use
3480 * of this root.
3481 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003482int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003484 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003485 struct btrfs_path *path;
3486 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003487 struct btrfs_key key, found_key;
3488 struct btrfs_trans_handle *trans;
3489 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003490 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003491 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3492
Yan, Zhengd68fc572010-05-16 10:49:58 -04003493 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003494 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003495
3496 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 if (!path) {
3498 ret = -ENOMEM;
3499 goto out;
3500 }
David Sterbae4058b52015-11-27 16:31:35 +01003501 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003502
3503 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003504 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003505 key.offset = (u64)-1;
3506
Josef Bacik7b128762008-07-24 12:17:14 -04003507 while (1) {
3508 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 if (ret < 0)
3510 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003511
3512 /*
3513 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003514 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003515 * find the key and see if we have stuff that matches
3516 */
3517 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003518 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003519 if (path->slots[0] == 0)
3520 break;
3521 path->slots[0]--;
3522 }
3523
3524 /* pull out the item */
3525 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003526 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3527
3528 /* make sure the item matches what we want */
3529 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3530 break;
David Sterba962a2982014-06-04 18:41:45 +02003531 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003532 break;
3533
3534 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003535 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003536
3537 /*
3538 * this is where we are basically btrfs_lookup, without the
3539 * crossing root thing. we store the inode number in the
3540 * offset of the orphan item.
3541 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003542
3543 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003544 btrfs_err(fs_info,
3545 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003546 ret = -EINVAL;
3547 goto out;
3548 }
3549
3550 last_objectid = found_key.offset;
3551
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003552 found_key.objectid = found_key.offset;
3553 found_key.type = BTRFS_INODE_ITEM_KEY;
3554 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003555 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303556 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003557 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003558 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003559
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003561 struct btrfs_root *dead_root;
3562 struct btrfs_fs_info *fs_info = root->fs_info;
3563 int is_dead_root = 0;
3564
3565 /*
3566 * this is an orphan in the tree root. Currently these
3567 * could come from 2 sources:
3568 * a) a snapshot deletion in progress
3569 * b) a free space cache inode
3570 * We need to distinguish those two, as the snapshot
3571 * orphan must not get deleted.
3572 * find_dead_roots already ran before us, so if this
3573 * is a snapshot deletion, we should find the root
3574 * in the dead_roots list
3575 */
3576 spin_lock(&fs_info->trans_lock);
3577 list_for_each_entry(dead_root, &fs_info->dead_roots,
3578 root_list) {
3579 if (dead_root->root_key.objectid ==
3580 found_key.objectid) {
3581 is_dead_root = 1;
3582 break;
3583 }
3584 }
3585 spin_unlock(&fs_info->trans_lock);
3586 if (is_dead_root) {
3587 /* prevent this orphan from being found again */
3588 key.offset = found_key.objectid - 1;
3589 continue;
3590 }
3591 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003592 /*
3593 * Inode is already gone but the orphan item is still there,
3594 * kill the orphan item.
3595 */
Filipe Manana67710892016-06-06 11:51:25 +01003596 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003597 trans = btrfs_start_transaction(root, 1);
3598 if (IS_ERR(trans)) {
3599 ret = PTR_ERR(trans);
3600 goto out;
3601 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003602 btrfs_debug(fs_info, "auto deleting %Lu",
3603 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003604 ret = btrfs_del_orphan_item(trans, root,
3605 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003606 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003607 if (ret)
3608 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003609 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003610 }
Josef Bacik7b128762008-07-24 12:17:14 -04003611
Josef Bacik7b128762008-07-24 12:17:14 -04003612 /*
3613 * add this inode to the orphan list so btrfs_orphan_del does
3614 * the proper thing when we hit it
3615 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003616 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3617 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003618 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003619
Josef Bacik7b128762008-07-24 12:17:14 -04003620 /* if we have links, this was a truncate, lets do that */
3621 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303622 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003623 iput(inode);
3624 continue;
3625 }
Josef Bacik7b128762008-07-24 12:17:14 -04003626 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003627
3628 /* 1 for the orphan item deletion. */
3629 trans = btrfs_start_transaction(root, 1);
3630 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003631 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003632 ret = PTR_ERR(trans);
3633 goto out;
3634 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003635 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003636 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003637 if (ret) {
3638 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003639 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003640 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003641
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003642 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003643 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003644 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003645 } else {
3646 nr_unlink++;
3647 }
3648
3649 /* this will do delete_inode and everything for us */
3650 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003651 if (ret)
3652 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003653 }
Miao Xie3254c872011-11-10 20:45:05 -05003654 /* release the path since we're done with it */
3655 btrfs_release_path(path);
3656
Yan, Zhengd68fc572010-05-16 10:49:58 -04003657 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3658
3659 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003660 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003661 (u64)-1);
3662
Miao Xie27cdeb72014-04-02 19:51:05 +08003663 if (root->orphan_block_rsv ||
3664 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003665 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003666 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003667 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003668 }
Josef Bacik7b128762008-07-24 12:17:14 -04003669
3670 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003671 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003672 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003673 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003674
3675out:
3676 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003677 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003678 btrfs_free_path(path);
3679 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003680}
3681
Chris Masond352ac62008-09-29 15:18:18 -04003682/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003683 * very simple check to peek ahead in the leaf looking for xattrs. If we
3684 * don't find any xattrs, we know there can't be any acls.
3685 *
3686 * slot is the slot the inode is in, objectid is the objectid of the inode
3687 */
3688static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003689 int slot, u64 objectid,
3690 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003691{
3692 u32 nritems = btrfs_header_nritems(leaf);
3693 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003694 static u64 xattr_access = 0;
3695 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003696 int scanned = 0;
3697
Josef Bacikf23b5a52013-06-19 10:16:26 -04003698 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003699 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3700 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3701 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3702 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003703 }
3704
Chris Mason46a53cc2009-04-27 11:47:50 -04003705 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003706 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003707 while (slot < nritems) {
3708 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3709
3710 /* we found a different objectid, there must not be acls */
3711 if (found_key.objectid != objectid)
3712 return 0;
3713
3714 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003715 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003716 if (*first_xattr_slot == -1)
3717 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003718 if (found_key.offset == xattr_access ||
3719 found_key.offset == xattr_default)
3720 return 1;
3721 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003722
3723 /*
3724 * we found a key greater than an xattr key, there can't
3725 * be any acls later on
3726 */
3727 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3728 return 0;
3729
3730 slot++;
3731 scanned++;
3732
3733 /*
3734 * it goes inode, inode backrefs, xattrs, extents,
3735 * so if there are a ton of hard links to an inode there can
3736 * be a lot of backrefs. Don't waste time searching too hard,
3737 * this is just an optimization
3738 */
3739 if (scanned >= 8)
3740 break;
3741 }
3742 /* we hit the end of the leaf before we found an xattr or
3743 * something larger than an xattr. We have to assume the inode
3744 * has acls
3745 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003746 if (*first_xattr_slot == -1)
3747 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003748 return 1;
3749}
3750
3751/*
Chris Masond352ac62008-09-29 15:18:18 -04003752 * read an inode from the btree into the in-memory inode
3753 */
Filipe Manana67710892016-06-06 11:51:25 +01003754static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003755{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003757 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003758 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 struct btrfs_inode_item *inode_item;
3760 struct btrfs_root *root = BTRFS_I(inode)->root;
3761 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003762 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003763 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003764 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003765 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003766 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003767 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003768
3769 ret = btrfs_fill_inode(inode, &rdev);
3770 if (!ret)
3771 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003772
3773 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003774 if (!path) {
3775 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003776 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003777 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003778
Chris Mason39279cc2007-06-12 06:35:45 -04003779 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003780
Chris Mason39279cc2007-06-12 06:35:45 -04003781 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003782 if (ret) {
3783 if (ret > 0)
3784 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003785 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003786 }
Chris Mason39279cc2007-06-12 06:35:45 -04003787
Chris Mason5f39d392007-10-15 16:14:19 -04003788 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003789
3790 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003791 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003792
Chris Mason5f39d392007-10-15 16:14:19 -04003793 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3794 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003795 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003796 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003797 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3798 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003799 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003800
David Sterbaa937b972014-12-12 17:39:12 +01003801 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3802 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003803
David Sterbaa937b972014-12-12 17:39:12 +01003804 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3805 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
David Sterbaa937b972014-12-12 17:39:12 +01003807 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3808 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003809
chandan r9cc97d62012-07-04 12:48:07 +05303810 BTRFS_I(inode)->i_otime.tv_sec =
3811 btrfs_timespec_sec(leaf, &inode_item->otime);
3812 BTRFS_I(inode)->i_otime.tv_nsec =
3813 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003814
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003815 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003816 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003817 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3818
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003819 inode_set_iversion_queried(inode,
3820 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003821 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003822 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 rdev = btrfs_inode_rdev(leaf, inode_item);
3824
Josef Bacikaec74772008-07-24 12:12:38 -04003825 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003826 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003827
3828cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003829 /*
3830 * If we were modified in the current generation and evicted from memory
3831 * and then re-read we need to do a full sync since we don't have any
3832 * idea about which extents were modified before we were evicted from
3833 * cache.
3834 *
3835 * This is required for both inode re-read from disk and delayed inode
3836 * in delayed_nodes_tree.
3837 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003838 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003839 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3840 &BTRFS_I(inode)->runtime_flags);
3841
Filipe Mananabde6c242015-07-24 00:00:19 +01003842 /*
3843 * We don't persist the id of the transaction where an unlink operation
3844 * against the inode was last made. So here we assume the inode might
3845 * have been evicted, and therefore the exact value of last_unlink_trans
3846 * lost, and set it to last_trans to avoid metadata inconsistencies
3847 * between the inode and its parent if the inode is fsync'ed and the log
3848 * replayed. For example, in the scenario:
3849 *
3850 * touch mydir/foo
3851 * ln mydir/foo mydir/bar
3852 * sync
3853 * unlink mydir/bar
3854 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3855 * xfs_io -c fsync mydir/foo
3856 * <power failure>
3857 * mount fs, triggers fsync log replay
3858 *
3859 * We must make sure that when we fsync our inode foo we also log its
3860 * parent inode, otherwise after log replay the parent still has the
3861 * dentry with the "bar" name but our inode foo has a link count of 1
3862 * and doesn't have an inode ref with the name "bar" anymore.
3863 *
3864 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003865 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003866 * transaction commits on fsync if our inode is a directory, or if our
3867 * inode is not a directory, logging its parent unnecessarily.
3868 */
3869 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3870
Miao Xie67de1172013-12-26 13:07:06 +08003871 path->slots[0]++;
3872 if (inode->i_nlink != 1 ||
3873 path->slots[0] >= btrfs_header_nritems(leaf))
3874 goto cache_acl;
3875
3876 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003877 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003878 goto cache_acl;
3879
3880 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3881 if (location.type == BTRFS_INODE_REF_KEY) {
3882 struct btrfs_inode_ref *ref;
3883
3884 ref = (struct btrfs_inode_ref *)ptr;
3885 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3886 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3887 struct btrfs_inode_extref *extref;
3888
3889 extref = (struct btrfs_inode_extref *)ptr;
3890 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3891 extref);
3892 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003893cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003894 /*
3895 * try to precache a NULL acl entry for files that don't have
3896 * any xattrs or acls
3897 */
Li Zefan33345d012011-04-20 10:31:50 +08003898 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003899 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003900 if (first_xattr_slot != -1) {
3901 path->slots[0] = first_xattr_slot;
3902 ret = btrfs_load_inode_props(inode, path);
3903 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003904 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003905 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003906 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003907 root->root_key.objectid, ret);
3908 }
3909 btrfs_free_path(path);
3910
Al Viro72c04902009-06-24 16:58:48 -04003911 if (!maybe_acls)
3912 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003913
Chris Mason39279cc2007-06-12 06:35:45 -04003914 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003915 case S_IFREG:
3916 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003917 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003918 inode->i_fop = &btrfs_file_operations;
3919 inode->i_op = &btrfs_file_inode_operations;
3920 break;
3921 case S_IFDIR:
3922 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003923 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 break;
3925 case S_IFLNK:
3926 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003927 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003928 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3929 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003930 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003931 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003932 init_special_inode(inode, inode->i_mode, rdev);
3933 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003934 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003935
3936 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003937 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003938
3939make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003940 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003941 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003942 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003943}
3944
Chris Masond352ac62008-09-29 15:18:18 -04003945/*
3946 * given a leaf and an inode, copy the inode fields into the leaf
3947 */
Chris Masone02119d2008-09-05 16:13:11 -04003948static void fill_inode_item(struct btrfs_trans_handle *trans,
3949 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003950 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003951 struct inode *inode)
3952{
Liu Bo51fab692012-12-27 09:01:21 +00003953 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003954
Liu Bo51fab692012-12-27 09:01:21 +00003955 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003956
Liu Bo51fab692012-12-27 09:01:21 +00003957 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3958 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3959 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3960 &token);
3961 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3962 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003963
David Sterbaa937b972014-12-12 17:39:12 +01003964 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003965 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003966 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003967 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003968
David Sterbaa937b972014-12-12 17:39:12 +01003969 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003970 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003971 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003972 inode->i_mtime.tv_nsec, &token);
3973
David Sterbaa937b972014-12-12 17:39:12 +01003974 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003975 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003976 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003977 inode->i_ctime.tv_nsec, &token);
3978
chandan r9cc97d62012-07-04 12:48:07 +05303979 btrfs_set_token_timespec_sec(leaf, &item->otime,
3980 BTRFS_I(inode)->i_otime.tv_sec, &token);
3981 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3982 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3983
Liu Bo51fab692012-12-27 09:01:21 +00003984 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3985 &token);
3986 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3987 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003988 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3989 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003990 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3991 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3992 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3993 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003994}
3995
Chris Masond352ac62008-09-29 15:18:18 -04003996/*
3997 * copy everything in the in-memory inode into the btree.
3998 */
Chris Mason21151332011-11-10 20:39:08 -05003999static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004000 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004001{
4002 struct btrfs_inode_item *inode_item;
4003 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004004 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004005 int ret;
4006
4007 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004008 if (!path)
4009 return -ENOMEM;
4010
Chris Masonb9473432009-03-13 11:00:37 -04004011 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004012 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4013 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004014 if (ret) {
4015 if (ret > 0)
4016 ret = -ENOENT;
4017 goto failed;
4018 }
4019
Chris Mason5f39d392007-10-15 16:14:19 -04004020 leaf = path->nodes[0];
4021 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004022 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004023
Chris Masone02119d2008-09-05 16:13:11 -04004024 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004025 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004026 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004027 ret = 0;
4028failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004029 btrfs_free_path(path);
4030 return ret;
4031}
4032
Chris Masond352ac62008-09-29 15:18:18 -04004033/*
Chris Mason21151332011-11-10 20:39:08 -05004034 * copy everything in the in-memory inode into the btree.
4035 */
4036noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root, struct inode *inode)
4038{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004039 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004040 int ret;
4041
4042 /*
4043 * If the inode is a free space inode, we can deadlock during commit
4044 * if we put it into the delayed code.
4045 *
4046 * The data relocation inode should also be directly updated
4047 * without delay
4048 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004049 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004050 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004051 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004052 btrfs_update_root_times(trans, root);
4053
Chris Mason21151332011-11-10 20:39:08 -05004054 ret = btrfs_delayed_update_inode(trans, root, inode);
4055 if (!ret)
4056 btrfs_set_inode_last_trans(trans, inode);
4057 return ret;
4058 }
4059
4060 return btrfs_update_inode_item(trans, root, inode);
4061}
4062
Josef Bacikbe6aef62012-10-22 15:43:12 -04004063noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4064 struct btrfs_root *root,
4065 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004066{
4067 int ret;
4068
4069 ret = btrfs_update_inode(trans, root, inode);
4070 if (ret == -ENOSPC)
4071 return btrfs_update_inode_item(trans, root, inode);
4072 return ret;
4073}
4074
4075/*
Chris Masond352ac62008-09-29 15:18:18 -04004076 * unlink helper that gets used here in inode.c and in the tree logging
4077 * recovery code. It remove a link in a directory with a given name, and
4078 * also drops the back refs in the inode to the directory
4079 */
Al Viro92986792011-03-04 17:14:37 +00004080static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4081 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004082 struct btrfs_inode *dir,
4083 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004084 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004085{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004086 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004088 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004089 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004090 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004091 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004092 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004093 u64 ino = btrfs_ino(inode);
4094 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004095
4096 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004097 if (!path) {
4098 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004099 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004100 }
4101
Chris Masonb9473432009-03-13 11:00:37 -04004102 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004103 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004104 name, name_len, -1);
4105 if (IS_ERR(di)) {
4106 ret = PTR_ERR(di);
4107 goto err;
4108 }
4109 if (!di) {
4110 ret = -ENOENT;
4111 goto err;
4112 }
Chris Mason5f39d392007-10-15 16:14:19 -04004113 leaf = path->nodes[0];
4114 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004115 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004116 if (ret)
4117 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004118 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004119
Miao Xie67de1172013-12-26 13:07:06 +08004120 /*
4121 * If we don't have dir index, we have to get it by looking up
4122 * the inode ref, since we get the inode ref, remove it directly,
4123 * it is unnecessary to do delayed deletion.
4124 *
4125 * But if we have dir index, needn't search inode ref to get it.
4126 * Since the inode ref is close to the inode item, it is better
4127 * that we delay to delete it, and just do this deletion when
4128 * we update the inode item.
4129 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004130 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004131 ret = btrfs_delayed_delete_inode_ref(inode);
4132 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004134 goto skip_backref;
4135 }
4136 }
4137
Li Zefan33345d012011-04-20 10:31:50 +08004138 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4139 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004140 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004141 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004142 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004143 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004144 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004145 goto err;
4146 }
Miao Xie67de1172013-12-26 13:07:06 +08004147skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004148 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004149 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004150 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004151 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 }
Chris Mason39279cc2007-06-12 06:35:45 -04004153
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004154 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4155 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004156 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004157 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 goto err;
4159 }
Chris Masone02119d2008-09-05 16:13:11 -04004160
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004161 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4162 index);
Chris Mason6418c962010-10-30 07:34:24 -04004163 if (ret == -ENOENT)
4164 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004165 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004166 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004167err:
4168 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004169 if (ret)
4170 goto out;
4171
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004172 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004173 inode_inc_iversion(&inode->vfs_inode);
4174 inode_inc_iversion(&dir->vfs_inode);
4175 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4176 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4177 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004178out:
Chris Mason39279cc2007-06-12 06:35:45 -04004179 return ret;
4180}
4181
Al Viro92986792011-03-04 17:14:37 +00004182int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4183 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004184 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004185 const char *name, int name_len)
4186{
4187 int ret;
4188 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4189 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004190 drop_nlink(&inode->vfs_inode);
4191 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004192 }
4193 return ret;
4194}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004195
4196/*
4197 * helper to start transaction for unlink and rmdir.
4198 *
Josef Bacikd52be812013-05-29 14:54:47 -04004199 * unlink and rmdir are special in btrfs, they do not always free space, so
4200 * if we cannot make our reservations the normal way try and see if there is
4201 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4202 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004203 */
Josef Bacikd52be812013-05-29 14:54:47 -04004204static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004205{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004207
Josef Bacike70bea52011-10-11 14:18:24 -04004208 /*
4209 * 1 for the possible orphan item
4210 * 1 for the dir item
4211 * 1 for the dir index
4212 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004213 * 1 for the inode
4214 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004215 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004216}
4217
Chris Mason39279cc2007-06-12 06:35:45 -04004218static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4219{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004220 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004221 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004222 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004223 int ret;
4224
Josef Bacikd52be812013-05-29 14:54:47 -04004225 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004226 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004227 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004228
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004229 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4230 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004231
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004232 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4233 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4234 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004235 if (ret)
4236 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004237
Yan, Zhenga22285a2010-05-16 10:48:46 -04004238 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004239 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004240 if (ret)
4241 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004242 }
Josef Bacik7b128762008-07-24 12:17:14 -04004243
Tsutomu Itohb5324022011-07-19 07:27:20 +00004244out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004245 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004246 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004247 return ret;
4248}
4249
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4251 struct btrfs_root *root,
4252 struct inode *dir, u64 objectid,
4253 const char *name, int name_len)
4254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004255 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 struct btrfs_path *path;
4257 struct extent_buffer *leaf;
4258 struct btrfs_dir_item *di;
4259 struct btrfs_key key;
4260 u64 index;
4261 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004262 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004263
4264 path = btrfs_alloc_path();
4265 if (!path)
4266 return -ENOMEM;
4267
Li Zefan33345d012011-04-20 10:31:50 +08004268 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270 if (IS_ERR_OR_NULL(di)) {
4271 if (!di)
4272 ret = -ENOENT;
4273 else
4274 ret = PTR_ERR(di);
4275 goto out;
4276 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004277
4278 leaf = path->nodes[0];
4279 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4280 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4281 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004283 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004284 goto out;
4285 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004286 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004288 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4289 root->root_key.objectid, dir_ino,
4290 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004291 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004292 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004293 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 goto out;
4295 }
Li Zefan33345d012011-04-20 10:31:50 +08004296 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004297 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004298 if (IS_ERR_OR_NULL(di)) {
4299 if (!di)
4300 ret = -ENOENT;
4301 else
4302 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004303 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 goto out;
4305 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004306
4307 leaf = path->nodes[0];
4308 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004309 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004310 index = key.offset;
4311 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004312 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004314 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004315 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004316 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004317 goto out;
4318 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004319
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004320 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004321 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004322 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004323 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004324 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004325 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004326out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004327 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004328 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004329}
4330
Chris Mason39279cc2007-06-12 06:35:45 -04004331static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4332{
David Howells2b0143b2015-03-17 22:25:59 +00004333 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004334 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004335 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004336 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004337 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004338
David Sterbab3ae2442012-09-13 16:04:34 -06004339 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004340 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004341 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004342 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004343
Josef Bacikd52be812013-05-29 14:54:47 -04004344 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004345 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004346 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004347
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004348 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004349 err = btrfs_unlink_subvol(trans, root, dir,
4350 BTRFS_I(inode)->location.objectid,
4351 dentry->d_name.name,
4352 dentry->d_name.len);
4353 goto out;
4354 }
4355
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004356 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004357 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004358 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004359
Filipe Manana44f714d2016-06-06 16:11:13 +01004360 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4361
Chris Mason39279cc2007-06-12 06:35:45 -04004362 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004363 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4364 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4365 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004366 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004367 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004368 /*
4369 * Propagate the last_unlink_trans value of the deleted dir to
4370 * its parent directory. This is to prevent an unrecoverable
4371 * log tree in the case we do something like this:
4372 * 1) create dir foo
4373 * 2) create snapshot under dir foo
4374 * 3) delete the snapshot
4375 * 4) rmdir foo
4376 * 5) mkdir foo
4377 * 6) fsync foo or some file inside foo
4378 */
4379 if (last_unlink_trans >= trans->transid)
4380 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4381 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004382out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004383 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004385
Chris Mason39279cc2007-06-12 06:35:45 -04004386 return err;
4387}
4388
Chris Mason28f75a02015-02-04 06:59:29 -08004389static int truncate_space_check(struct btrfs_trans_handle *trans,
4390 struct btrfs_root *root,
4391 u64 bytes_deleted)
4392{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004393 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004394 int ret;
4395
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004396 /*
4397 * This is only used to apply pressure to the enospc system, we don't
4398 * intend to use this reservation at all.
4399 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004400 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004401 bytes_deleted *= fs_info->nodesize;
4402 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004403 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004404 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004405 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004406 trans->transid,
4407 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004408 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004409 }
Chris Mason28f75a02015-02-04 06:59:29 -08004410 return ret;
4411
4412}
4413
Josef Bacikddfae632017-10-19 14:16:02 -04004414/*
4415 * Return this if we need to call truncate_block for the last bit of the
4416 * truncate.
4417 */
4418#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004419
Chris Mason323ac952008-10-01 19:05:46 -04004420/*
Chris Mason39279cc2007-06-12 06:35:45 -04004421 * this can truncate away extent items, csum items and directory items.
4422 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004423 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004424 *
4425 * csum items that cross the new i_size are truncated to the new size
4426 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004427 *
4428 * min_type is the minimum key type to truncate down to. If set to 0, this
4429 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004430 */
Yan, Zheng80825102009-11-12 09:35:36 +00004431int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4432 struct btrfs_root *root,
4433 struct inode *inode,
4434 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004435{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004436 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004437 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004438 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004440 struct btrfs_key key;
4441 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004442 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004443 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004444 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004446 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004447 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004448 int found_extent;
4449 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004450 int pending_del_nr = 0;
4451 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004452 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004453 int ret;
4454 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004455 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004456 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004457 bool be_nice = false;
4458 bool should_throttle = false;
4459 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004460
4461 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004462
Chris Mason28ed1342014-12-17 09:41:04 -08004463 /*
4464 * for non-free space inodes and ref cows, we want to back off from
4465 * time to time
4466 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004467 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004468 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004469 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004470
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004471 path = btrfs_alloc_path();
4472 if (!path)
4473 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004474 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004475
Josef Bacik5dc562c2012-08-17 13:14:17 -04004476 /*
4477 * We want to drop from the next block forward in case this new size is
4478 * not block aligned since we will be keeping the last block of the
4479 * extent just the way it is.
4480 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004481 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004482 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004483 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004484 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004485 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004486
Miao Xie16cdcec2011-04-22 18:12:22 +08004487 /*
4488 * This function is also used to drop the items in the log tree before
4489 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4490 * it is used to drop the loged items. So we shouldn't kill the delayed
4491 * items.
4492 */
4493 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004494 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004495
Li Zefan33345d012011-04-20 10:31:50 +08004496 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004497 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004498 key.type = (u8)-1;
4499
Chris Mason85e21ba2008-01-29 15:11:36 -05004500search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004501 /*
4502 * with a 16K leaf size and 128MB extents, you can actually queue
4503 * up a huge file in a single leaf. Most of the time that
4504 * bytes_deleted is > 0, it will be huge by the time we get here
4505 */
Byongho Leeee221842015-12-15 01:42:10 +09004506 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004507 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004508 err = -EAGAIN;
4509 goto error;
4510 }
4511 }
4512
4513
Chris Masonb9473432009-03-13 11:00:37 -04004514 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004515 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004516 if (ret < 0) {
4517 err = ret;
4518 goto out;
4519 }
Chris Masond3977122009-01-05 21:25:51 -05004520
Chris Mason85e21ba2008-01-29 15:11:36 -05004521 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004522 /* there are no items in the tree for us to truncate, we're
4523 * done
4524 */
Yan, Zheng80825102009-11-12 09:35:36 +00004525 if (path->slots[0] == 0)
4526 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004527 path->slots[0]--;
4528 }
4529
Chris Masond3977122009-01-05 21:25:51 -05004530 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004531 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004532 leaf = path->nodes[0];
4533 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004534 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004535
Li Zefan33345d012011-04-20 10:31:50 +08004536 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004537 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004538
Chris Mason85e21ba2008-01-29 15:11:36 -05004539 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004540 break;
4541
Chris Mason5f39d392007-10-15 16:14:19 -04004542 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004543 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004544 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004545 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004546 extent_type = btrfs_file_extent_type(leaf, fi);
4547 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004548 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004549 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004550
4551 trace_btrfs_truncate_show_fi_regular(
4552 BTRFS_I(inode), leaf, fi,
4553 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004554 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004555 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004556 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004557
4558 trace_btrfs_truncate_show_fi_inline(
4559 BTRFS_I(inode), leaf, fi, path->slots[0],
4560 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004561 }
Yan008630c2007-11-07 13:31:09 -05004562 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004563 }
Yan, Zheng80825102009-11-12 09:35:36 +00004564 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004565 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004566 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004567 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004568 break;
4569 if (found_key.offset >= new_size)
4570 del_item = 1;
4571 else
4572 del_item = 0;
4573 }
Chris Mason39279cc2007-06-12 06:35:45 -04004574 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004575 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004576 if (found_type != BTRFS_EXTENT_DATA_KEY)
4577 goto delete;
4578
4579 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004580 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004581 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004582 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004583 u64 orig_num_bytes =
4584 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004585 extent_num_bytes = ALIGN(new_size -
4586 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004587 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004588 btrfs_set_file_extent_num_bytes(leaf, fi,
4589 extent_num_bytes);
4590 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004591 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004592 if (test_bit(BTRFS_ROOT_REF_COWS,
4593 &root->state) &&
4594 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004595 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004596 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004597 } else {
Chris Masondb945352007-10-15 16:15:53 -04004598 extent_num_bytes =
4599 btrfs_file_extent_disk_num_bytes(leaf,
4600 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004601 extent_offset = found_key.offset -
4602 btrfs_file_extent_offset(leaf, fi);
4603
Chris Mason39279cc2007-06-12 06:35:45 -04004604 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004605 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004606 if (extent_start != 0) {
4607 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004608 if (test_bit(BTRFS_ROOT_REF_COWS,
4609 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004610 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 }
4612 }
Chris Mason90692182008-02-08 13:49:28 -05004613 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004614 /*
4615 * we can't truncate inline items that have had
4616 * special encodings
4617 */
4618 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004619 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004620 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4621 btrfs_file_extent_compression(leaf, fi) == 0) {
4622 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004623
Josef Bacikddfae632017-10-19 14:16:02 -04004624 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4625 size = btrfs_file_extent_calc_inline_size(size);
4626 btrfs_truncate_item(root->fs_info, path, size, 1);
4627 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004628 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004629 * We have to bail so the last_size is set to
4630 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004631 */
Josef Bacikddfae632017-10-19 14:16:02 -04004632 err = NEED_TRUNCATE_BLOCK;
4633 break;
Chris Mason90692182008-02-08 13:49:28 -05004634 }
Josef Bacikddfae632017-10-19 14:16:02 -04004635
4636 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4637 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004638 }
Chris Mason179e29e2007-11-01 11:28:41 -04004639delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004640 if (del_item)
4641 last_size = found_key.offset;
4642 else
4643 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004644 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004645 if (!pending_del_nr) {
4646 /* no pending yet, add ourselves */
4647 pending_del_slot = path->slots[0];
4648 pending_del_nr = 1;
4649 } else if (pending_del_nr &&
4650 path->slots[0] + 1 == pending_del_slot) {
4651 /* hop on the pending chunk */
4652 pending_del_nr++;
4653 pending_del_slot = path->slots[0];
4654 } else {
Chris Masond3977122009-01-05 21:25:51 -05004655 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004656 }
Chris Mason39279cc2007-06-12 06:35:45 -04004657 } else {
4658 break;
4659 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004660 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004661
Miao Xie27cdeb72014-04-02 19:51:05 +08004662 if (found_extent &&
4663 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004664 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004665 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004666 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004667 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004668 extent_num_bytes, 0,
4669 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004670 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004671 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004672 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4673 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004674 trans->delayed_ref_updates * 2,
4675 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004676 if (be_nice) {
4677 if (truncate_space_check(trans, root,
4678 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004679 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004680 }
4681 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004682 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004683 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004684 }
Chris Mason39279cc2007-06-12 06:35:45 -04004685 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004686
Yan, Zheng80825102009-11-12 09:35:36 +00004687 if (found_type == BTRFS_INODE_ITEM_KEY)
4688 break;
4689
4690 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004691 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004692 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004693 if (pending_del_nr) {
4694 ret = btrfs_del_items(trans, root, path,
4695 pending_del_slot,
4696 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004697 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004698 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004699 goto error;
4700 }
Yan, Zheng80825102009-11-12 09:35:36 +00004701 pending_del_nr = 0;
4702 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004703 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004704 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004705 unsigned long updates = trans->delayed_ref_updates;
4706 if (updates) {
4707 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004708 ret = btrfs_run_delayed_refs(trans,
4709 fs_info,
4710 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004711 if (ret && !err)
4712 err = ret;
4713 }
4714 }
Chris Mason28f75a02015-02-04 06:59:29 -08004715 /*
4716 * if we failed to refill our space rsv, bail out
4717 * and let the transaction restart
4718 */
4719 if (should_end) {
4720 err = -EAGAIN;
4721 goto error;
4722 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004723 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004724 } else {
4725 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004726 }
Chris Mason39279cc2007-06-12 06:35:45 -04004727 }
Yan, Zheng80825102009-11-12 09:35:36 +00004728out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004729 if (pending_del_nr) {
4730 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4731 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004732 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004733 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004734 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004735error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004736 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4737 ASSERT(last_size >= new_size);
4738 if (!err && last_size > new_size)
4739 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004740 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004741 }
Chris Mason28ed1342014-12-17 09:41:04 -08004742
Chris Mason39279cc2007-06-12 06:35:45 -04004743 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004744
Byongho Leeee221842015-12-15 01:42:10 +09004745 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004746 unsigned long updates = trans->delayed_ref_updates;
4747 if (updates) {
4748 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004749 ret = btrfs_run_delayed_refs(trans, fs_info,
4750 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004751 if (ret && !err)
4752 err = ret;
4753 }
4754 }
Yan, Zheng80825102009-11-12 09:35:36 +00004755 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004756}
4757
Chris Masona52d9a82007-08-27 16:49:44 -04004758/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004760 * @inode - inode that we're zeroing
4761 * @from - the offset to start zeroing
4762 * @len - the length to zero, 0 to zero the entire range respective to the
4763 * offset
4764 * @front - zero up to the offset instead of from the offset on
4765 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304766 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004767 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004768 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004770 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004773 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4775 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004776 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004777 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004778 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004779 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004780 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004782 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004783 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004784 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785 u64 block_start;
4786 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004787
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004788 if (IS_ALIGNED(offset, blocksize) &&
4789 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004790 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304791
Josef Bacik8b62f872017-10-19 14:15:55 -04004792 block_start = round_down(from, blocksize);
4793 block_end = block_start + blocksize - 1;
4794
Qu Wenruo364ecf32017-02-27 15:10:38 +08004795 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004796 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004797 if (ret)
4798 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004799
Chris Mason211c17f2008-05-15 09:13:45 -04004800again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004801 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004802 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004803 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004804 block_start, blocksize);
4805 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004806 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004807 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004808 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809
Chris Masona52d9a82007-08-27 16:49:44 -04004810 if (!PageUptodate(page)) {
4811 ret = btrfs_readpage(NULL, page);
4812 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004813 if (page->mapping != mapping) {
4814 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004815 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004816 goto again;
4817 }
Chris Masona52d9a82007-08-27 16:49:44 -04004818 if (!PageUptodate(page)) {
4819 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004820 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004821 }
4822 }
Chris Mason211c17f2008-05-15 09:13:45 -04004823 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004824
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 set_page_extent_mapped(page);
4827
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004831 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004833 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004834 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004835 btrfs_put_ordered_extent(ordered);
4836 goto again;
4837 }
4838
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304839 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004840 EXTENT_DIRTY | EXTENT_DELALLOC |
4841 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004842 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004843
Filipe Mananae3b8a482017-11-04 00:16:59 +00004844 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004845 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004846 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304847 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004848 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004849 goto out_unlock;
4850 }
4851
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304852 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004853 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004856 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 memset(kaddr + (block_start - page_offset(page)),
4858 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004859 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304860 memset(kaddr + (block_start - page_offset(page)) + offset,
4861 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004862 flush_dcache_page(page);
4863 kunmap(page);
4864 }
Chris Mason247e7432008-07-17 12:53:51 -04004865 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004866 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004867 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004868
Chris Mason89642222008-07-24 09:41:53 -04004869out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004870 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004871 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304872 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004873 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004874 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004875 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004876out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004877 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004878 return ret;
4879}
4880
Josef Bacik16e75492013-10-22 12:18:51 -04004881static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4882 u64 offset, u64 len)
4883{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004885 struct btrfs_trans_handle *trans;
4886 int ret;
4887
4888 /*
4889 * Still need to make sure the inode looks like it's been updated so
4890 * that any holes get logged if we fsync.
4891 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4893 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004894 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4895 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4896 return 0;
4897 }
4898
4899 /*
4900 * 1 - for the one we're dropping
4901 * 1 - for the one we're adding
4902 * 1 - for updating the inode.
4903 */
4904 trans = btrfs_start_transaction(root, 3);
4905 if (IS_ERR(trans))
4906 return PTR_ERR(trans);
4907
4908 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4909 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004910 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004911 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004912 return ret;
4913 }
4914
David Sterbaf85b7372017-01-20 14:54:07 +01004915 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4916 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004917 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004918 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004919 else
4920 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004921 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004922 return ret;
4923}
4924
Josef Bacik695a0d02011-03-04 15:46:53 -05004925/*
4926 * This function puts in dummy file extents for the area we're creating a hole
4927 * for. So if we are truncating this file to a larger size we need to insert
4928 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4929 * the range between oldsize and size
4930 */
Josef Bacika41ad392011-01-31 15:30:16 -05004931int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004932{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004934 struct btrfs_root *root = BTRFS_I(inode)->root;
4935 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004936 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004937 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4940 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004941 u64 last_byte;
4942 u64 cur_offset;
4943 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004944 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004945
Josef Bacika71754f2013-06-17 17:14:39 -04004946 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304947 * If our size started in the middle of a block we need to zero out the
4948 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004949 * expose stale data.
4950 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304951 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004952 if (err)
4953 return err;
4954
Yan Zheng9036c102008-10-30 14:19:41 -04004955 if (size <= hole_start)
4956 return 0;
4957
Yan Zheng9036c102008-10-30 14:19:41 -04004958 while (1) {
4959 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004960
David Sterbaff13db42015-12-03 14:30:40 +01004961 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004962 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004963 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004964 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004965 if (!ordered)
4966 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004967 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004968 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004969 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004970 btrfs_put_ordered_extent(ordered);
4971 }
4972
Yan Zheng9036c102008-10-30 14:19:41 -04004973 cur_offset = hole_start;
4974 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004975 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004976 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004977 if (IS_ERR(em)) {
4978 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004979 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004980 break;
4981 }
Yan Zheng9036c102008-10-30 14:19:41 -04004982 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004983 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004984 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004985 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004986 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004987
Josef Bacik16e75492013-10-22 12:18:51 -04004988 err = maybe_insert_hole(root, inode, cur_offset,
4989 hole_size);
4990 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004991 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004992 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 cur_offset + hole_size - 1, 0);
4994 hole_em = alloc_extent_map();
4995 if (!hole_em) {
4996 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4997 &BTRFS_I(inode)->runtime_flags);
4998 goto next;
4999 }
5000 hole_em->start = cur_offset;
5001 hole_em->len = hole_size;
5002 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005003
Josef Bacik5dc562c2012-08-17 13:14:17 -04005004 hole_em->block_start = EXTENT_MAP_HOLE;
5005 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005006 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005007 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005008 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005009 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005010 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005011
5012 while (1) {
5013 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005014 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005015 write_unlock(&em_tree->lock);
5016 if (err != -EEXIST)
5017 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005018 btrfs_drop_extent_cache(BTRFS_I(inode),
5019 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005020 cur_offset +
5021 hole_size - 1, 0);
5022 }
5023 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005024 }
Josef Bacik16e75492013-10-22 12:18:51 -04005025next:
Yan Zheng9036c102008-10-30 14:19:41 -04005026 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005027 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005028 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005029 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005030 break;
5031 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005032 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005033 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005034 return err;
5035}
5036
Eric Sandeen3972f262013-01-12 02:57:22 +00005037static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005038{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005039 struct btrfs_root *root = BTRFS_I(inode)->root;
5040 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005041 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005042 loff_t newsize = attr->ia_size;
5043 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005044 int ret;
5045
Eric Sandeen3972f262013-01-12 02:57:22 +00005046 /*
5047 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5048 * special case where we need to update the times despite not having
5049 * these flags set. For all other operations the VFS set these flags
5050 * explicitly if it wants a timestamp update.
5051 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005052 if (newsize != oldsize) {
5053 inode_inc_iversion(inode);
5054 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5055 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005056 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005057 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005058
Josef Bacika41ad392011-01-31 15:30:16 -05005059 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005060 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005061 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005062 * This is to ensure the snapshot captures a fully consistent
5063 * state of this file - if the snapshot captures this expanding
5064 * truncation, it must capture all writes that happened before
5065 * this truncation.
5066 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005067 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005068 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005069 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005070 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005071 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005072 }
Yan, Zheng80825102009-11-12 09:35:36 +00005073
Miao Xief4a2f4c2011-12-14 20:12:01 -05005074 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005075 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005076 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005077 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005078 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005079
5080 i_size_write(inode, newsize);
5081 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305082 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005083 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005084 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005085 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005086 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005087
Josef Bacika41ad392011-01-31 15:30:16 -05005088 /*
5089 * We're truncating a file that used to have good data down to
5090 * zero. Make sure it gets into the ordered flush list so that
5091 * any new writes get down to disk quickly.
5092 */
5093 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005094 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5095 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005096
Josef Bacikf3fe8202013-01-07 17:03:21 -05005097 /*
5098 * 1 for the orphan item we're going to add
5099 * 1 for the orphan item deletion.
5100 */
5101 trans = btrfs_start_transaction(root, 2);
5102 if (IS_ERR(trans))
5103 return PTR_ERR(trans);
5104
5105 /*
5106 * We need to do this in case we fail at _any_ point during the
5107 * actual truncate. Once we do the truncate_setsize we could
5108 * invalidate pages which forces any outstanding ordered io to
5109 * be instantly completed which will give us extents that need
5110 * to be truncated. If we fail to get an orphan inode down we
5111 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005112 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005113 * will be consistent.
5114 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005115 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005116 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005117 if (ret)
5118 return ret;
5119
Josef Bacika41ad392011-01-31 15:30:16 -05005120 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5121 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005122
5123 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005124 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005125 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005126 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005127
Josef Bacika41ad392011-01-31 15:30:16 -05005128 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005129 if (ret && inode->i_nlink) {
5130 int err;
5131
Liu Bo19fd2df2016-12-01 13:01:02 -08005132 /* To get a stable disk_i_size */
5133 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5134 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005135 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005136 return err;
5137 }
5138
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005139 /*
5140 * failed to truncate, disk_i_size is only adjusted down
5141 * as we remove extents, so it should represent the true
5142 * size of the inode, so reset the in memory size and
5143 * delete our orphan entry.
5144 */
5145 trans = btrfs_join_transaction(root);
5146 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005147 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005148 return ret;
5149 }
5150 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005151 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005152 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005153 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005154 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005155 }
Yan, Zheng80825102009-11-12 09:35:36 +00005156 }
5157
Josef Bacika41ad392011-01-31 15:30:16 -05005158 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005159}
5160
Chris Mason39279cc2007-06-12 06:35:45 -04005161static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5162{
David Howells2b0143b2015-03-17 22:25:59 +00005163 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005164 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005165 int err;
5166
Li Zefanb83cc962010-12-20 16:04:08 +08005167 if (btrfs_root_readonly(root))
5168 return -EROFS;
5169
Jan Kara31051c82016-05-26 16:55:18 +02005170 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005171 if (err)
5172 return err;
5173
Chris Mason5a3f23d2009-03-31 13:27:11 -04005174 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005175 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005176 if (err)
5177 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 }
Yan Zheng9036c102008-10-30 14:19:41 -04005179
Christoph Hellwig10257742010-06-04 11:30:02 +02005180 if (attr->ia_valid) {
5181 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005182 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005183 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005184
Josef Bacik22c44fe2011-11-30 10:45:38 -05005185 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005186 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005187 }
5188
Chris Mason39279cc2007-06-12 06:35:45 -04005189 return err;
5190}
Chris Mason61295eb2008-01-14 16:24:38 -05005191
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192/*
5193 * While truncating the inode pages during eviction, we get the VFS calling
5194 * btrfs_invalidatepage() against each page of the inode. This is slow because
5195 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5196 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5197 * extent_state structures over and over, wasting lots of time.
5198 *
5199 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5200 * those expensive operations on a per page basis and do only the ordered io
5201 * finishing, while we release here the extent_map and extent_state structures,
5202 * without the excessive merging and splitting.
5203 */
5204static void evict_inode_truncate_pages(struct inode *inode)
5205{
5206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5207 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5208 struct rb_node *node;
5209
5210 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005211 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212
5213 write_lock(&map_tree->lock);
5214 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5215 struct extent_map *em;
5216
5217 node = rb_first(&map_tree->map);
5218 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005219 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5220 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221 remove_extent_mapping(map_tree, em);
5222 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005223 if (need_resched()) {
5224 write_unlock(&map_tree->lock);
5225 cond_resched();
5226 write_lock(&map_tree->lock);
5227 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005228 }
5229 write_unlock(&map_tree->lock);
5230
Filipe Manana6ca07092015-05-26 00:55:42 +01005231 /*
5232 * Keep looping until we have no more ranges in the io tree.
5233 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005234 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5235 * still in progress (unlocked the pages in the bio but did not yet
5236 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 * ranges can still be locked and eviction started because before
5238 * submitting those bios, which are executed by a separate task (work
5239 * queue kthread), inode references (inode->i_count) were not taken
5240 * (which would be dropped in the end io callback of each bio).
5241 * Therefore here we effectively end up waiting for those bios and
5242 * anyone else holding locked ranges without having bumped the inode's
5243 * reference count - if we don't do it, when they access the inode's
5244 * io_tree to unlock a range it may be too late, leading to an
5245 * use-after-free issue.
5246 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005247 spin_lock(&io_tree->lock);
5248 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5249 struct extent_state *state;
5250 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005251 u64 start;
5252 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005253
5254 node = rb_first(&io_tree->state);
5255 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005256 start = state->start;
5257 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005258 spin_unlock(&io_tree->lock);
5259
David Sterbaff13db42015-12-03 14:30:40 +01005260 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005261
5262 /*
5263 * If still has DELALLOC flag, the extent didn't reach disk,
5264 * and its reserved space won't be freed by delayed_ref.
5265 * So we need to free its reserved space here.
5266 * (Refer to comment in btrfs_invalidatepage, case 2)
5267 *
5268 * Note, end is the bytenr of last byte, so we need + 1 here.
5269 */
5270 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005271 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005272
Filipe Manana6ca07092015-05-26 00:55:42 +01005273 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005274 EXTENT_LOCKED | EXTENT_DIRTY |
5275 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005276 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005277
Filipe Manana7064dd52014-08-08 02:47:05 +01005278 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279 spin_lock(&io_tree->lock);
5280 }
5281 spin_unlock(&io_tree->lock);
5282}
5283
Al Virobd555972010-06-07 11:35:40 -04005284void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005286 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005287 struct btrfs_trans_handle *trans;
5288 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005289 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005290 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005291 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005292 int ret;
5293
liubo1abe9b82011-03-24 11:18:59 +00005294 trace_btrfs_inode_evict(inode);
5295
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005296 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005297 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005298 return;
5299 }
5300
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005301 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005302
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005303 evict_inode_truncate_pages(inode);
5304
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005305 if (inode->i_nlink &&
5306 ((btrfs_root_refs(&root->root_item) != 0 &&
5307 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005308 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005309 goto no_delete;
5310
Chris Mason39279cc2007-06-12 06:35:45 -04005311 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005312 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005313 goto no_delete;
5314 }
Al Virobd555972010-06-07 11:35:40 -04005315 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005316 if (!special_file(inode->i_mode))
5317 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005318
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005319 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005320
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005321 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005322 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005323 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005324 goto no_delete;
5325 }
5326
Yan, Zheng76dda932009-09-21 16:00:26 -04005327 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005328 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5329 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005330 goto no_delete;
5331 }
5332
Nikolay Borisovaa790212017-01-10 20:35:40 +02005333 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005334 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005335 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005336 goto no_delete;
5337 }
5338
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005339 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005340 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005341 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005342 goto no_delete;
5343 }
Josef Bacik4a338542011-08-29 11:01:31 -04005344 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005345 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005346 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005347
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005348 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005349
Josef Bacik4289a662011-08-05 13:22:24 -04005350 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005351 * This is a bit simpler than btrfs_truncate since we've already
5352 * reserved our space for our orphan item in the unlink, so we just
5353 * need to reserve some slack space in case we add bytes and update
5354 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005355 */
Yan, Zheng80825102009-11-12 09:35:36 +00005356 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005357 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5358 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005359
Josef Bacik726c35f2011-09-26 15:46:06 -04005360 /*
5361 * Try and steal from the global reserve since we will
5362 * likely not use this space anyway, we want to try as
5363 * hard as possible to get this to work.
5364 */
5365 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005366 steal_from_global++;
5367 else
5368 steal_from_global = 0;
5369 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005370
Josef Bacik3bce8762015-02-24 12:35:51 -08005371 /*
5372 * steal_from_global == 0: we reserved stuff, hooray!
5373 * steal_from_global == 1: we didn't reserve stuff, boo!
5374 * steal_from_global == 2: we've committed, still not a lot of
5375 * room but maybe we'll have room in the global reserve this
5376 * time.
5377 * steal_from_global == 3: abandon all hope!
5378 */
5379 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005380 btrfs_warn(fs_info,
5381 "Could not get space for a delete, will truncate on mount %d",
5382 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005383 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005384 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005385 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005386 }
5387
Miao Xie0e8c36a2012-12-19 06:59:51 +00005388 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005389 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005390 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005391 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005392 goto no_delete;
5393 }
5394
Josef Bacik3bce8762015-02-24 12:35:51 -08005395 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005396 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005397 * sure there is room to do it, if not we need to commit and try
5398 * again.
5399 */
5400 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005401 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005402 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005403 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005404 else
5405 ret = -ENOSPC;
5406 }
5407
5408 /*
5409 * Couldn't steal from the global reserve, we have too much
5410 * pending stuff built up, commit the transaction and try it
5411 * again.
5412 */
5413 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005414 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005415 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005416 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005417 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005418 goto no_delete;
5419 }
5420 continue;
5421 } else {
5422 steal_from_global = 0;
5423 }
5424
Josef Bacik4289a662011-08-05 13:22:24 -04005425 trans->block_rsv = rsv;
5426
Yan, Zhengd68fc572010-05-16 10:49:58 -04005427 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005428 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005429 break;
5430
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005431 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005432 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005433 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005434 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005435 }
5436
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005437 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005438
Josef Bacik4ef31a42013-08-13 14:10:08 -04005439 /*
5440 * Errors here aren't a big deal, it just means we leave orphan items
5441 * in the tree. They will be cleaned up on the next mount.
5442 */
Yan, Zheng80825102009-11-12 09:35:36 +00005443 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005444 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005445 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005446 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005447 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005448 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005449
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005450 trans->block_rsv = &fs_info->trans_block_rsv;
5451 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005452 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005453 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005454
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005455 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005456 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005457no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005458 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005459 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005460}
5461
5462/*
5463 * this returns the key found in the dir entry in the location pointer.
5464 * If no dir entries were found, location->objectid is 0.
5465 */
5466static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5467 struct btrfs_key *location)
5468{
5469 const char *name = dentry->d_name.name;
5470 int namelen = dentry->d_name.len;
5471 struct btrfs_dir_item *di;
5472 struct btrfs_path *path;
5473 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005474 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005475
5476 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005477 if (!path)
5478 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005479
David Sterbaf85b7372017-01-20 14:54:07 +01005480 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5481 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005482 if (IS_ERR(di))
5483 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005484
David Sterbac7040052011-04-19 18:00:01 +02005485 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005486 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005487
Chris Mason5f39d392007-10-15 16:14:19 -04005488 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005489 if (location->type != BTRFS_INODE_ITEM_KEY &&
5490 location->type != BTRFS_ROOT_ITEM_KEY) {
5491 btrfs_warn(root->fs_info,
5492"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5493 __func__, name, btrfs_ino(BTRFS_I(dir)),
5494 location->objectid, location->type, location->offset);
5495 goto out_err;
5496 }
Chris Mason39279cc2007-06-12 06:35:45 -04005497out:
Chris Mason39279cc2007-06-12 06:35:45 -04005498 btrfs_free_path(path);
5499 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005500out_err:
5501 location->objectid = 0;
5502 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005503}
5504
5505/*
5506 * when we hit a tree root in a directory, the btrfs part of the inode
5507 * needs to be changed to reflect the root directory of the tree root. This
5508 * is kind of like crossing a mount point.
5509 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005510static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 struct inode *dir,
5512 struct dentry *dentry,
5513 struct btrfs_key *location,
5514 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005515{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 struct btrfs_path *path;
5517 struct btrfs_root *new_root;
5518 struct btrfs_root_ref *ref;
5519 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005520 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 int ret;
5522 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 path = btrfs_alloc_path();
5525 if (!path) {
5526 err = -ENOMEM;
5527 goto out;
5528 }
Chris Mason39279cc2007-06-12 06:35:45 -04005529
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005531 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5532 key.type = BTRFS_ROOT_REF_KEY;
5533 key.offset = location->objectid;
5534
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005535 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 if (ret) {
5537 if (ret < 0)
5538 err = ret;
5539 goto out;
5540 }
Chris Mason39279cc2007-06-12 06:35:45 -04005541
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005542 leaf = path->nodes[0];
5543 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005544 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5546 goto out;
5547
5548 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5549 (unsigned long)(ref + 1),
5550 dentry->d_name.len);
5551 if (ret)
5552 goto out;
5553
David Sterbab3b4aa72011-04-21 01:20:15 +02005554 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005555
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005556 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557 if (IS_ERR(new_root)) {
5558 err = PTR_ERR(new_root);
5559 goto out;
5560 }
5561
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005562 *sub_root = new_root;
5563 location->objectid = btrfs_root_dirid(&new_root->root_item);
5564 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005565 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005566 err = 0;
5567out:
5568 btrfs_free_path(path);
5569 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005570}
5571
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572static void inode_tree_add(struct inode *inode)
5573{
5574 struct btrfs_root *root = BTRFS_I(inode)->root;
5575 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005576 struct rb_node **p;
5577 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005578 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005579 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580
Al Viro1d3382cb2010-10-23 15:19:20 -04005581 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005582 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005583 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005585 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 while (*p) {
5587 parent = *p;
5588 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5589
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005590 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005591 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005592 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005593 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 else {
5595 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005596 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005597 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005598 RB_CLEAR_NODE(parent);
5599 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005600 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 }
5602 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005603 rb_link_node(new, parent, p);
5604 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 spin_unlock(&root->inode_lock);
5606}
5607
5608static void inode_tree_del(struct inode *inode)
5609{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005614 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005620 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005621
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005622 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005623 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005624 spin_lock(&root->inode_lock);
5625 empty = RB_EMPTY_ROOT(&root->inode_tree);
5626 spin_unlock(&root->inode_lock);
5627 if (empty)
5628 btrfs_add_dead_root(root);
5629 }
5630}
5631
Jeff Mahoney143bede2012-03-01 14:56:26 +01005632void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005633{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005634 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005635 struct rb_node *node;
5636 struct rb_node *prev;
5637 struct btrfs_inode *entry;
5638 struct inode *inode;
5639 u64 objectid = 0;
5640
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005641 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005642 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005643
5644 spin_lock(&root->inode_lock);
5645again:
5646 node = root->inode_tree.rb_node;
5647 prev = NULL;
5648 while (node) {
5649 prev = node;
5650 entry = rb_entry(node, struct btrfs_inode, rb_node);
5651
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005652 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005653 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005654 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005655 node = node->rb_right;
5656 else
5657 break;
5658 }
5659 if (!node) {
5660 while (prev) {
5661 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005662 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005663 node = prev;
5664 break;
5665 }
5666 prev = rb_next(prev);
5667 }
5668 }
5669 while (node) {
5670 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005671 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005672 inode = igrab(&entry->vfs_inode);
5673 if (inode) {
5674 spin_unlock(&root->inode_lock);
5675 if (atomic_read(&inode->i_count) > 1)
5676 d_prune_aliases(inode);
5677 /*
Al Viro45321ac2010-06-07 13:43:19 -04005678 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005679 * the inode cache when its usage count
5680 * hits zero.
5681 */
5682 iput(inode);
5683 cond_resched();
5684 spin_lock(&root->inode_lock);
5685 goto again;
5686 }
5687
5688 if (cond_resched_lock(&root->inode_lock))
5689 goto again;
5690
5691 node = rb_next(node);
5692 }
5693 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005694}
5695
Chris Masone02119d2008-09-05 16:13:11 -04005696static int btrfs_init_locked_inode(struct inode *inode, void *p)
5697{
5698 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005699 inode->i_ino = args->location->objectid;
5700 memcpy(&BTRFS_I(inode)->location, args->location,
5701 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005702 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005703 return 0;
5704}
5705
5706static int btrfs_find_actor(struct inode *inode, void *opaque)
5707{
5708 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005709 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005710 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005711}
5712
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005713static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005714 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005715 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005716{
5717 struct inode *inode;
5718 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005719 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005720
Chris Mason90d3e592014-01-09 17:28:00 -08005721 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005722 args.root = root;
5723
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005724 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005725 btrfs_init_locked_inode,
5726 (void *)&args);
5727 return inode;
5728}
5729
Balaji Rao1a54ef82008-07-21 02:01:04 +05305730/* Get an inode object given its location and corresponding root.
5731 * Returns in *is_new if the inode was read from disk
5732 */
5733struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005734 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305735{
5736 struct inode *inode;
5737
Chris Mason90d3e592014-01-09 17:28:00 -08005738 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305739 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005740 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305741
5742 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005743 int ret;
5744
5745 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005746 if (!is_bad_inode(inode)) {
5747 inode_tree_add(inode);
5748 unlock_new_inode(inode);
5749 if (new)
5750 *new = 1;
5751 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005752 unlock_new_inode(inode);
5753 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005754 ASSERT(ret < 0);
5755 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005756 }
5757 }
5758
Balaji Rao1a54ef82008-07-21 02:01:04 +05305759 return inode;
5760}
5761
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762static struct inode *new_simple_dir(struct super_block *s,
5763 struct btrfs_key *key,
5764 struct btrfs_root *root)
5765{
5766 struct inode *inode = new_inode(s);
5767
5768 if (!inode)
5769 return ERR_PTR(-ENOMEM);
5770
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771 BTRFS_I(inode)->root = root;
5772 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005773 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005774
5775 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005776 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005777 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005778 inode->i_fop = &simple_dir_operations;
5779 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005780 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305781 inode->i_atime = inode->i_mtime;
5782 inode->i_ctime = inode->i_mtime;
5783 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005784
5785 return inode;
5786}
5787
Chris Mason3de45862008-11-17 21:02:50 -05005788struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005789{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005790 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005791 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005792 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005793 struct btrfs_root *sub_root = root;
5794 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005795 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005796 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005797
5798 if (dentry->d_name.len > BTRFS_NAME_LEN)
5799 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005800
Jeff Layton39e3c952012-11-28 11:30:53 -05005801 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005802 if (ret < 0)
5803 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005804
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005805 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005806 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807
5808 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005809 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005810 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005811 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005812
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005813 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005814 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005815 &location, &sub_root);
5816 if (ret < 0) {
5817 if (ret != -ENOENT)
5818 inode = ERR_PTR(ret);
5819 else
5820 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5821 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005822 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005824 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005825
Julia Lawall34d19ba2011-01-24 19:55:19 +00005826 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005827 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005828 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005829 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005830 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005831 if (ret) {
5832 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005833 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005834 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005835 }
5836
Chris Mason3de45862008-11-17 21:02:50 -05005837 return inode;
5838}
5839
Nick Pigginfe15ce42011-01-07 17:49:23 +11005840static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005841{
5842 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005843 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005844
Li Zefan848cce02012-02-21 17:04:28 +08005845 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005846 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005847
Li Zefan848cce02012-02-21 17:04:28 +08005848 if (inode) {
5849 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005850 if (btrfs_root_refs(&root->root_item) == 0)
5851 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005852
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005853 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005854 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005855 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005856 return 0;
5857}
5858
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005859static void btrfs_dentry_release(struct dentry *dentry)
5860{
Daeseok Youn944a4512014-04-14 15:37:02 +09005861 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005862}
5863
Chris Mason3de45862008-11-17 21:02:50 -05005864static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005865 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005866{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005867 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005868
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005869 inode = btrfs_lookup_dentry(dir, dentry);
5870 if (IS_ERR(inode)) {
5871 if (PTR_ERR(inode) == -ENOENT)
5872 inode = NULL;
5873 else
5874 return ERR_CAST(inode);
5875 }
5876
Al Viro41d28bc2014-10-12 22:24:21 -04005877 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005878}
5879
Miao Xie16cdcec2011-04-22 18:12:22 +08005880unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005881 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5882};
5883
Josef Bacik23b5ec72017-07-24 15:14:25 -04005884/*
5885 * All this infrastructure exists because dir_emit can fault, and we are holding
5886 * the tree lock when doing readdir. For now just allocate a buffer and copy
5887 * our information into that, and then dir_emit from the buffer. This is
5888 * similar to what NFS does, only we don't keep the buffer around in pagecache
5889 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5890 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5891 * tree lock.
5892 */
5893static int btrfs_opendir(struct inode *inode, struct file *file)
5894{
5895 struct btrfs_file_private *private;
5896
5897 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5898 if (!private)
5899 return -ENOMEM;
5900 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5901 if (!private->filldir_buf) {
5902 kfree(private);
5903 return -ENOMEM;
5904 }
5905 file->private_data = private;
5906 return 0;
5907}
5908
5909struct dir_entry {
5910 u64 ino;
5911 u64 offset;
5912 unsigned type;
5913 int name_len;
5914};
5915
5916static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5917{
5918 while (entries--) {
5919 struct dir_entry *entry = addr;
5920 char *name = (char *)(entry + 1);
5921
5922 ctx->pos = entry->offset;
5923 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5924 entry->type))
5925 return 1;
5926 addr += sizeof(struct dir_entry) + entry->name_len;
5927 ctx->pos++;
5928 }
5929 return 0;
5930}
5931
Al Viro9cdda8d2013-05-22 16:48:09 -04005932static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005933{
Al Viro9cdda8d2013-05-22 16:48:09 -04005934 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005935 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 struct btrfs_dir_item *di;
5938 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005939 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005940 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005941 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005942 struct list_head ins_list;
5943 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005944 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005945 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005947 char *name_ptr;
5948 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005949 int entries = 0;
5950 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005951 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005952 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Al Viro9cdda8d2013-05-22 16:48:09 -04005954 if (!dir_emit_dots(file, ctx))
5955 return 0;
5956
David Woodhouse49593bf2008-08-17 17:08:36 +01005957 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005958 if (!path)
5959 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005960
Josef Bacik23b5ec72017-07-24 15:14:25 -04005961 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005962 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005963
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005964 INIT_LIST_HEAD(&ins_list);
5965 INIT_LIST_HEAD(&del_list);
5966 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005967
Josef Bacik23b5ec72017-07-24 15:14:25 -04005968again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005969 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005970 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005971 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005972
Chris Mason39279cc2007-06-12 06:35:45 -04005973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5974 if (ret < 0)
5975 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005976
5977 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005978 struct dir_entry *entry;
5979
Chris Mason5f39d392007-10-15 16:14:19 -04005980 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005981 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005982 if (slot >= btrfs_header_nritems(leaf)) {
5983 ret = btrfs_next_leaf(root, path);
5984 if (ret < 0)
5985 goto err;
5986 else if (ret > 0)
5987 break;
5988 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005989 }
Chris Mason3de45862008-11-17 21:02:50 -05005990
Chris Mason5f39d392007-10-15 16:14:19 -04005991 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5992
5993 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005994 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005995 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005996 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005997 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005998 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005999 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006000 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006001 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006003 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6004 PAGE_SIZE) {
6005 btrfs_release_path(path);
6006 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6007 if (ret)
6008 goto nopos;
6009 addr = private->filldir_buf;
6010 entries = 0;
6011 total_len = 0;
6012 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014
6015 entry = addr;
6016 entry->name_len = name_len;
6017 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006018 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6019 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006020 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006021 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006022 entry->ino = location.objectid;
6023 entry->offset = found_key.offset;
6024 entries++;
6025 addr += sizeof(struct dir_entry) + name_len;
6026 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006027next:
6028 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006029 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006030 btrfs_release_path(path);
6031
6032 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6033 if (ret)
6034 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006035
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006036 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006037 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006038 goto nopos;
6039
Zach Browndb62efb2013-07-11 16:19:42 -07006040 /*
6041 * Stop new entries from being returned after we return the last
6042 * entry.
6043 *
6044 * New directory entries are assigned a strictly increasing
6045 * offset. This means that new entries created during readdir
6046 * are *guaranteed* to be seen in the future by that readdir.
6047 * This has broken buggy programs which operate on names as
6048 * they're returned by readdir. Until we re-use freed offsets
6049 * we have this hack to stop new entries from being returned
6050 * under the assumption that they'll never reach this huge
6051 * offset.
6052 *
6053 * This is being careful not to overflow 32bit loff_t unless the
6054 * last entry requires it because doing so has broken 32bit apps
6055 * in the past.
6056 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006057 if (ctx->pos >= INT_MAX)
6058 ctx->pos = LLONG_MAX;
6059 else
6060 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006061nopos:
6062 ret = 0;
6063err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006064 if (put)
6065 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006066 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006067 return ret;
6068}
6069
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006070int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006071{
6072 struct btrfs_root *root = BTRFS_I(inode)->root;
6073 struct btrfs_trans_handle *trans;
6074 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006075 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006076
Josef Bacik72ac3c02012-05-23 14:13:11 -04006077 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006078 return 0;
6079
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006080 if (btrfs_fs_closing(root->fs_info) &&
6081 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006082 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006083
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006084 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006085 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006086 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006087 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006088 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006089 if (IS_ERR(trans))
6090 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006091 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006092 }
6093 return ret;
6094}
6095
6096/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006097 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006098 * inode changes. But, it is most likely to find the inode in cache.
6099 * FIXME, needs more benchmarking...there are no reasons other than performance
6100 * to keep or drop this code.
6101 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006102static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006103{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006104 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006105 struct btrfs_root *root = BTRFS_I(inode)->root;
6106 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006107 int ret;
6108
Josef Bacik72ac3c02012-05-23 14:13:11 -04006109 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006110 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006111
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006112 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006113 if (IS_ERR(trans))
6114 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006115
6116 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006117 if (ret && ret == -ENOSPC) {
6118 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006119 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006120 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006121 if (IS_ERR(trans))
6122 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006123
Chris Mason94b60442010-05-26 11:02:00 -04006124 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006125 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006126 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006127 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006128 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006129
6130 return ret;
6131}
6132
6133/*
6134 * This is a copy of file_update_time. We need this so we can return error on
6135 * ENOSPC for updating the inode in the case of file write and mmap writes.
6136 */
Josef Bacike41f9412012-03-26 09:46:47 -04006137static int btrfs_update_time(struct inode *inode, struct timespec *now,
6138 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006139{
Alexander Block2bc5565282012-06-15 09:49:33 +02006140 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006141 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006142
6143 if (btrfs_root_readonly(root))
6144 return -EROFS;
6145
Josef Bacike41f9412012-03-26 09:46:47 -04006146 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006147 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006148 if (flags & S_CTIME)
6149 inode->i_ctime = *now;
6150 if (flags & S_MTIME)
6151 inode->i_mtime = *now;
6152 if (flags & S_ATIME)
6153 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006154 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006155}
6156
Chris Masond352ac62008-09-29 15:18:18 -04006157/*
6158 * find the highest existing sequence number in a directory
6159 * and then set the in-memory index_cnt variable to reflect
6160 * free sequence numbers
6161 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006162static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006163{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006164 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006165 struct btrfs_key key, found_key;
6166 struct btrfs_path *path;
6167 struct extent_buffer *leaf;
6168 int ret;
6169
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006170 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006171 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006172 key.offset = (u64)-1;
6173
6174 path = btrfs_alloc_path();
6175 if (!path)
6176 return -ENOMEM;
6177
6178 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6179 if (ret < 0)
6180 goto out;
6181 /* FIXME: we should be able to handle this */
6182 if (ret == 0)
6183 goto out;
6184 ret = 0;
6185
6186 /*
6187 * MAGIC NUMBER EXPLANATION:
6188 * since we search a directory based on f_pos we have to start at 2
6189 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6190 * else has to start at 2
6191 */
6192 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006193 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006194 goto out;
6195 }
6196
6197 path->slots[0]--;
6198
6199 leaf = path->nodes[0];
6200 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6201
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006202 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006203 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006204 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006205 goto out;
6206 }
6207
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006208 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006209out:
6210 btrfs_free_path(path);
6211 return ret;
6212}
6213
Chris Masond352ac62008-09-29 15:18:18 -04006214/*
6215 * helper to find a free sequence number in a given directory. This current
6216 * code is very simple, later versions will do smarter things in the btree
6217 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006218int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006219{
6220 int ret = 0;
6221
Nikolay Borisov877574e2017-02-20 13:50:33 +02006222 if (dir->index_cnt == (u64)-1) {
6223 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006224 if (ret) {
6225 ret = btrfs_set_inode_index_count(dir);
6226 if (ret)
6227 return ret;
6228 }
Josef Bacikaec74772008-07-24 12:12:38 -04006229 }
6230
Nikolay Borisov877574e2017-02-20 13:50:33 +02006231 *index = dir->index_cnt;
6232 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006233
6234 return ret;
6235}
6236
Chris Masonb0d5d102014-09-08 13:08:51 -07006237static int btrfs_insert_inode_locked(struct inode *inode)
6238{
6239 struct btrfs_iget_args args;
6240 args.location = &BTRFS_I(inode)->location;
6241 args.root = BTRFS_I(inode)->root;
6242
6243 return insert_inode_locked4(inode,
6244 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6245 btrfs_find_actor, &args);
6246}
6247
Anand Jain19aee8d2017-07-18 17:37:05 +08006248/*
6249 * Inherit flags from the parent inode.
6250 *
6251 * Currently only the compression flags and the cow flags are inherited.
6252 */
6253static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6254{
6255 unsigned int flags;
6256
6257 if (!dir)
6258 return;
6259
6260 flags = BTRFS_I(dir)->flags;
6261
6262 if (flags & BTRFS_INODE_NOCOMPRESS) {
6263 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6264 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6265 } else if (flags & BTRFS_INODE_COMPRESS) {
6266 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6267 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6268 }
6269
6270 if (flags & BTRFS_INODE_NODATACOW) {
6271 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6272 if (S_ISREG(inode->i_mode))
6273 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6274 }
6275
6276 btrfs_update_iflags(inode);
6277}
6278
Chris Mason39279cc2007-06-12 06:35:45 -04006279static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6280 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006281 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006282 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006283 u64 ref_objectid, u64 objectid,
6284 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006286 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006287 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006288 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006289 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006290 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006291 struct btrfs_inode_ref *ref;
6292 struct btrfs_key key[2];
6293 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006294 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006295 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006296 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006297
Chris Mason5f39d392007-10-15 16:14:19 -04006298 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006299 if (!path)
6300 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006301
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006302 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006303 if (!inode) {
6304 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006305 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006306 }
Chris Mason39279cc2007-06-12 06:35:45 -04006307
Li Zefan581bb052011-04-20 10:06:11 +08006308 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006309 * O_TMPFILE, set link count to 0, so that after this point,
6310 * we fill in an inode item with the correct link count.
6311 */
6312 if (!name)
6313 set_nlink(inode, 0);
6314
6315 /*
Li Zefan581bb052011-04-20 10:06:11 +08006316 * we have to initialize this early, so we can reclaim the inode
6317 * number if we fail afterwards in this function.
6318 */
6319 inode->i_ino = objectid;
6320
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006321 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006322 trace_btrfs_inode_request(dir);
6323
Nikolay Borisov877574e2017-02-20 13:50:33 +02006324 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006325 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006326 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006327 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006328 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006329 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006330 } else if (dir) {
6331 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006332 }
6333 /*
6334 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006335 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006336 * number
6337 */
6338 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006339 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006340 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006341 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006342 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006343
Josef Bacik5dc562c2012-08-17 13:14:17 -04006344 /*
6345 * We could have gotten an inode number from somebody who was fsynced
6346 * and then removed in this same transaction, so let's just set full
6347 * sync since it will be a full sync anyway and this will blow away the
6348 * old info in the log.
6349 */
6350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6351
Chris Mason9c583092008-01-29 15:15:18 -05006352 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006353 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006354 key[0].offset = 0;
6355
Chris Mason9c583092008-01-29 15:15:18 -05006356 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006357
6358 if (name) {
6359 /*
6360 * Start new inodes with an inode_ref. This is slightly more
6361 * efficient for small numbers of hard links since they will
6362 * be packed into one item. Extended refs will kick in if we
6363 * add more hard links than can fit in the ref item.
6364 */
6365 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006366 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006367 key[1].offset = ref_objectid;
6368
6369 sizes[1] = name_len + sizeof(*ref);
6370 }
Chris Mason9c583092008-01-29 15:15:18 -05006371
Chris Masonb0d5d102014-09-08 13:08:51 -07006372 location = &BTRFS_I(inode)->location;
6373 location->objectid = objectid;
6374 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006375 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006376
6377 ret = btrfs_insert_inode_locked(inode);
6378 if (ret < 0)
6379 goto fail;
6380
Chris Masonb9473432009-03-13 11:00:37 -04006381 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006382 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006383 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006384 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006385
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006386 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006387 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306388
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006389 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306390 inode->i_atime = inode->i_mtime;
6391 inode->i_ctime = inode->i_mtime;
6392 BTRFS_I(inode)->i_otime = inode->i_mtime;
6393
Chris Mason5f39d392007-10-15 16:14:19 -04006394 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6395 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006396 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006397 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006398 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006399
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006400 if (name) {
6401 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6402 struct btrfs_inode_ref);
6403 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6404 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6405 ptr = (unsigned long)(ref + 1);
6406 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6407 }
Chris Mason9c583092008-01-29 15:15:18 -05006408
Chris Mason5f39d392007-10-15 16:14:19 -04006409 btrfs_mark_buffer_dirty(path->nodes[0]);
6410 btrfs_free_path(path);
6411
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006412 btrfs_inherit_iflags(inode, dir);
6413
Al Viro569254b2011-07-24 17:08:40 -04006414 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006415 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006416 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006417 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006418 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6419 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006420 }
6421
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006422 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006423
6424 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006425 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006426
Alexander Block8ea05e32012-07-25 17:35:53 +02006427 btrfs_update_root_times(trans, root);
6428
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006429 ret = btrfs_inode_inherit_props(trans, inode, dir);
6430 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006431 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006432 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006433 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006434
Chris Mason39279cc2007-06-12 06:35:45 -04006435 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006436
6437fail_unlock:
6438 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006439fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006440 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006441 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006442 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006443 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006444 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006445}
6446
6447static inline u8 btrfs_inode_type(struct inode *inode)
6448{
6449 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6450}
6451
Chris Masond352ac62008-09-29 15:18:18 -04006452/*
6453 * utility function to add 'inode' into 'parent_inode' with
6454 * a give name and a given sequence number.
6455 * if 'add_backref' is true, also insert a backref from the
6456 * inode to the parent directory.
6457 */
Chris Masone02119d2008-09-05 16:13:11 -04006458int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006459 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006460 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006461{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006463 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006464 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006465 struct btrfs_root *root = parent_inode->root;
6466 u64 ino = btrfs_ino(inode);
6467 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006468
Li Zefan33345d012011-04-20 10:31:50 +08006469 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006470 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006471 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006472 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006473 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006474 key.offset = 0;
6475 }
Chris Mason39279cc2007-06-12 06:35:45 -04006476
Li Zefan33345d012011-04-20 10:31:50 +08006477 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006478 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6479 root->root_key.objectid, parent_ino,
6480 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006481 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006482 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6483 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006484 }
6485
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006486 /* Nothing to clean up yet */
6487 if (ret)
6488 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006489
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006490 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6491 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006492 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006493 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006494 goto fail_dir_item;
6495 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006496 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006497 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006498 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006499
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006500 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006501 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006502 inode_inc_iversion(&parent_inode->vfs_inode);
6503 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6504 current_time(&parent_inode->vfs_inode);
6505 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006506 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006507 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006508 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006509
6510fail_dir_item:
6511 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6512 u64 local_index;
6513 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006514 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6515 root->root_key.objectid, parent_ino,
6516 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006517
6518 } else if (add_backref) {
6519 u64 local_index;
6520 int err;
6521
6522 err = btrfs_del_inode_ref(trans, root, name, name_len,
6523 ino, parent_ino, &local_index);
6524 }
6525 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006526}
6527
6528static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006529 struct btrfs_inode *dir, struct dentry *dentry,
6530 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006531{
Josef Bacika1b075d2010-11-19 20:36:11 +00006532 int err = btrfs_add_link(trans, dir, inode,
6533 dentry->d_name.name, dentry->d_name.len,
6534 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006535 if (err > 0)
6536 err = -EEXIST;
6537 return err;
6538}
6539
Josef Bacik618e21d2007-07-11 10:18:17 -04006540static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006541 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006542{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006543 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006544 struct btrfs_trans_handle *trans;
6545 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006546 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006547 int err;
6548 int drop_inode = 0;
6549 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006550 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006551
Josef Bacik9ed74f22009-09-11 16:12:44 -04006552 /*
6553 * 2 for inode item and ref
6554 * 2 for dir items
6555 * 1 for xattr if selinux is on
6556 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006557 trans = btrfs_start_transaction(root, 5);
6558 if (IS_ERR(trans))
6559 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006560
Li Zefan581bb052011-04-20 10:06:11 +08006561 err = btrfs_find_free_ino(root, &objectid);
6562 if (err)
6563 goto out_unlock;
6564
Josef Bacikaec74772008-07-24 12:12:38 -04006565 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006566 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6567 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006568 if (IS_ERR(inode)) {
6569 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006570 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006571 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006572
Casey Schauflerad19db72011-12-15 10:09:07 -05006573 /*
6574 * If the active LSM wants to access the inode during
6575 * d_instantiate it needs these. Smack checks to see
6576 * if the filesystem supports xattrs by looking at the
6577 * ops vector.
6578 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006579 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006580 init_special_inode(inode, inode->i_mode, rdev);
6581
6582 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006583 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006584 goto out_unlock_inode;
6585
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006586 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6587 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006588 if (err) {
6589 goto out_unlock_inode;
6590 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006591 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006592 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006593 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006594 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006595
Josef Bacik618e21d2007-07-11 10:18:17 -04006596out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006597 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006598 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 if (drop_inode) {
6600 inode_dec_link_count(inode);
6601 iput(inode);
6602 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006603 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006604
6605out_unlock_inode:
6606 drop_inode = 1;
6607 unlock_new_inode(inode);
6608 goto out_unlock;
6609
Josef Bacik618e21d2007-07-11 10:18:17 -04006610}
6611
Chris Mason39279cc2007-06-12 06:35:45 -04006612static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006613 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006614{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006615 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 struct btrfs_trans_handle *trans;
6617 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006618 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006619 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006620 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006621 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006622 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006623
Josef Bacik9ed74f22009-09-11 16:12:44 -04006624 /*
6625 * 2 for inode item and ref
6626 * 2 for dir items
6627 * 1 for xattr if selinux is on
6628 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006629 trans = btrfs_start_transaction(root, 5);
6630 if (IS_ERR(trans))
6631 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006632
Li Zefan581bb052011-04-20 10:06:11 +08006633 err = btrfs_find_free_ino(root, &objectid);
6634 if (err)
6635 goto out_unlock;
6636
Josef Bacikaec74772008-07-24 12:12:38 -04006637 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006638 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6639 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006640 if (IS_ERR(inode)) {
6641 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006642 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006643 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006644 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006645 /*
6646 * If the active LSM wants to access the inode during
6647 * d_instantiate it needs these. Smack checks to see
6648 * if the filesystem supports xattrs by looking at the
6649 * ops vector.
6650 */
6651 inode->i_fop = &btrfs_file_operations;
6652 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006653 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006654
6655 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6656 if (err)
6657 goto out_unlock_inode;
6658
6659 err = btrfs_update_inode(trans, root, inode);
6660 if (err)
6661 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006662
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006663 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6664 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006666 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006667
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006668 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006669 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006670 d_instantiate(dentry, inode);
6671
Chris Mason39279cc2007-06-12 06:35:45 -04006672out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006673 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006674 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006675 inode_dec_link_count(inode);
6676 iput(inode);
6677 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006678 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006679 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006680
6681out_unlock_inode:
6682 unlock_new_inode(inode);
6683 goto out_unlock;
6684
Chris Mason39279cc2007-06-12 06:35:45 -04006685}
6686
6687static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6688 struct dentry *dentry)
6689{
Filipe Manana271dba452016-01-05 16:24:05 +00006690 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006691 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006692 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006694 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006695 int err;
6696 int drop_inode = 0;
6697
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006698 /* do not allow sys_link's with other subvols of the same device */
6699 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006700 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006701
Mark Fashehf1863732012-08-08 11:32:27 -07006702 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006703 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006704
Nikolay Borisov877574e2017-02-20 13:50:33 +02006705 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006706 if (err)
6707 goto fail;
6708
Yan, Zhenga22285a2010-05-16 10:48:46 -04006709 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006710 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006711 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006712 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006713 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006714 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006715 if (IS_ERR(trans)) {
6716 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006717 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006718 goto fail;
6719 }
Chris Mason5f39d392007-10-15 16:14:19 -04006720
Miao Xie67de1172013-12-26 13:07:06 +08006721 /* There are several dir indexes for this inode, clear the cache. */
6722 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006723 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006724 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006725 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006726 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006727 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006728
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006729 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6730 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006731
Yan, Zhenga5719522009-09-24 09:17:31 -04006732 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006733 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006734 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006735 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006736 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006737 if (err)
6738 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006739 if (inode->i_nlink == 1) {
6740 /*
6741 * If new hard link count is 1, it's a file created
6742 * with open(2) O_TMPFILE flag.
6743 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006744 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006745 if (err)
6746 goto fail;
6747 }
Al Viro08c422c2011-12-23 07:58:13 -05006748 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006749 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006750 }
Chris Mason39279cc2007-06-12 06:35:45 -04006751
Chris Mason1832a6d2007-12-21 16:27:21 -05006752fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006753 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006754 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006755 if (drop_inode) {
6756 inode_dec_link_count(inode);
6757 iput(inode);
6758 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006759 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006760 return err;
6761}
6762
Al Viro18bb1db2011-07-26 01:41:39 -04006763static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006764{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006765 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006766 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006767 struct btrfs_trans_handle *trans;
6768 struct btrfs_root *root = BTRFS_I(dir)->root;
6769 int err = 0;
6770 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006771 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006772 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006773
Josef Bacik9ed74f22009-09-11 16:12:44 -04006774 /*
6775 * 2 items for inode and ref
6776 * 2 items for dir items
6777 * 1 for xattr if selinux is on
6778 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006779 trans = btrfs_start_transaction(root, 5);
6780 if (IS_ERR(trans))
6781 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006782
Li Zefan581bb052011-04-20 10:06:11 +08006783 err = btrfs_find_free_ino(root, &objectid);
6784 if (err)
6785 goto out_fail;
6786
Josef Bacikaec74772008-07-24 12:12:38 -04006787 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006788 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6789 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006790 if (IS_ERR(inode)) {
6791 err = PTR_ERR(inode);
6792 goto out_fail;
6793 }
Chris Mason5f39d392007-10-15 16:14:19 -04006794
Chris Mason39279cc2007-06-12 06:35:45 -04006795 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006796 /* these must be set before we unlock the inode */
6797 inode->i_op = &btrfs_dir_inode_operations;
6798 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006799
Eric Paris2a7dba32011-02-01 11:05:39 -05006800 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006801 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006802 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006803
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006804 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006805 err = btrfs_update_inode(trans, root, inode);
6806 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006807 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006808
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006809 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6810 dentry->d_name.name,
6811 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006812 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006813 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006814
Chris Mason39279cc2007-06-12 06:35:45 -04006815 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006816 /*
6817 * mkdir is special. We're unlocking after we call d_instantiate
6818 * to avoid a race with nfsd calling d_instantiate.
6819 */
6820 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006821 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006822
6823out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006824 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006825 if (drop_on_err) {
6826 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006827 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006828 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006829 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006830 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006831
6832out_fail_inode:
6833 unlock_new_inode(inode);
6834 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006835}
6836
Chris Masonc8b97812008-10-29 14:49:59 -04006837static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006838 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006839 size_t pg_offset, u64 extent_offset,
6840 struct btrfs_file_extent_item *item)
6841{
6842 int ret;
6843 struct extent_buffer *leaf = path->nodes[0];
6844 char *tmp;
6845 size_t max_size;
6846 unsigned long inline_size;
6847 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006848 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006849
6850 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006851 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006852 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6853 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006854 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006855 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006856 if (!tmp)
6857 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006858 ptr = btrfs_file_extent_inline_start(item);
6859
6860 read_extent_buffer(leaf, tmp, ptr, inline_size);
6861
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006862 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006863 ret = btrfs_decompress(compress_type, tmp, page,
6864 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006865
6866 /*
6867 * decompression code contains a memset to fill in any space between the end
6868 * of the uncompressed data and the end of max_size in case the decompressed
6869 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6870 * the end of an inline extent and the beginning of the next block, so we
6871 * cover that region here.
6872 */
6873
6874 if (max_size + pg_offset < PAGE_SIZE) {
6875 char *map = kmap(page);
6876 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6877 kunmap(page);
6878 }
Chris Masonc8b97812008-10-29 14:49:59 -04006879 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006880 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006881}
6882
Chris Masond352ac62008-09-29 15:18:18 -04006883/*
6884 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006885 * the ugly parts come from merging extents from the disk with the in-ram
6886 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006887 * where the in-ram extents might be locked pending data=ordered completion.
6888 *
6889 * This also copies inline extents directly into the page.
6890 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006891struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6892 struct page *page,
6893 size_t pg_offset, u64 start, u64 len,
6894 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006895{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006897 int ret;
6898 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006899 u64 extent_start = 0;
6900 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006901 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006902 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006903 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006904 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006905 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006906 struct extent_buffer *leaf;
6907 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006909 struct extent_map_tree *em_tree = &inode->extent_tree;
6910 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006911 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006912
Chris Mason890871b2009-09-02 16:24:52 -04006913 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006914 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006915 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006916 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006917 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006918
Chris Masona52d9a82007-08-27 16:49:44 -04006919 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006920 if (em->start > start || em->start + em->len <= start)
6921 free_extent_map(em);
6922 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006923 free_extent_map(em);
6924 else
6925 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006926 }
David Sterba172ddd62011-04-21 00:48:27 +02006927 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006928 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006929 err = -ENOMEM;
6930 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006931 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006932 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006933 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006934 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006935 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006936 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006937
6938 if (!path) {
6939 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006940 if (!path) {
6941 err = -ENOMEM;
6942 goto out;
6943 }
6944 /*
6945 * Chances are we'll be called again, so go ahead and do
6946 * readahead
6947 */
David Sterbae4058b52015-11-27 16:31:35 +01006948 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006949 }
6950
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006951 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006952 if (ret < 0) {
6953 err = ret;
6954 goto out;
6955 }
6956
6957 if (ret != 0) {
6958 if (path->slots[0] == 0)
6959 goto not_found;
6960 path->slots[0]--;
6961 }
6962
Chris Mason5f39d392007-10-15 16:14:19 -04006963 leaf = path->nodes[0];
6964 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006965 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006966 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006967 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006968 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006969 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006970 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006971 /*
6972 * If we backup past the first extent we want to move forward
6973 * and see if there is an extent in front of us, otherwise we'll
6974 * say there is a hole for our whole search range which can
6975 * cause problems.
6976 */
6977 extent_end = start;
6978 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006979 }
6980
Chris Mason5f39d392007-10-15 16:14:19 -04006981 found_type = btrfs_file_extent_type(leaf, item);
6982 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006983 if (found_type == BTRFS_FILE_EXTENT_REG ||
6984 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006985 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006986 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006987
6988 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6989 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006990 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6991 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006992 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006993 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006994 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006995
6996 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6997 path->slots[0],
6998 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006999 }
Josef Bacik25a50342013-10-14 12:08:38 -04007000next:
Yan Zheng9036c102008-10-30 14:19:41 -04007001 if (start >= extent_end) {
7002 path->slots[0]++;
7003 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7004 ret = btrfs_next_leaf(root, path);
7005 if (ret < 0) {
7006 err = ret;
7007 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007008 }
Yan Zheng9036c102008-10-30 14:19:41 -04007009 if (ret > 0)
7010 goto not_found;
7011 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007012 }
Yan Zheng9036c102008-10-30 14:19:41 -04007013 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7014 if (found_key.objectid != objectid ||
7015 found_key.type != BTRFS_EXTENT_DATA_KEY)
7016 goto not_found;
7017 if (start + len <= found_key.offset)
7018 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007019 if (start > found_key.offset)
7020 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007021 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007022 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007023 em->len = found_key.offset - start;
7024 goto not_found_em;
7025 }
7026
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007027 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007028 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007029
Yan Zhengd899e052008-10-30 14:25:28 -04007030 if (found_type == BTRFS_FILE_EXTENT_REG ||
7031 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007032 goto insert;
7033 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007034 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007035 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007036 size_t size;
7037 size_t extent_offset;
7038 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007039
Filipe Manana7ffbb592014-06-09 03:48:05 +01007040 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007041 goto out;
Yan689f9342007-10-29 11:41:07 -04007042
Chris Mason514ac8a2014-01-03 21:07:00 -08007043 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007044 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007045 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7046 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007047 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007048 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007049 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007050 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007051 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007052 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007053 if (btrfs_file_extent_compression(leaf, item) !=
7054 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007055 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007056 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007057 if (ret) {
7058 err = ret;
7059 goto out;
7060 }
Chris Masonc8b97812008-10-29 14:49:59 -04007061 } else {
7062 map = kmap(page);
7063 read_extent_buffer(leaf, map + pg_offset, ptr,
7064 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007065 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007066 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007067 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007068 copy_size);
7069 }
Chris Masonc8b97812008-10-29 14:49:59 -04007070 kunmap(page);
7071 }
Chris Mason179e29e2007-11-01 11:28:41 -04007072 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007073 }
Chris Masond1310b22008-01-24 16:13:08 -05007074 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007075 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007076 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007077 }
7078not_found:
7079 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007080 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007081 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007082not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007083 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007084insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007085 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007086 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007087 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007088 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7089 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007090 err = -EIO;
7091 goto out;
7092 }
Chris Masond1310b22008-01-24 16:13:08 -05007093
7094 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007095 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007096 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007097 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007098out:
liubo1abe9b82011-03-24 11:18:59 +00007099
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007100 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007101
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007102 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007103 if (err) {
7104 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007105 return ERR_PTR(err);
7106 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007107 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007108 return em;
7109}
7110
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007111struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7112 struct page *page,
7113 size_t pg_offset, u64 start, u64 len,
7114 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007115{
7116 struct extent_map *em;
7117 struct extent_map *hole_em = NULL;
7118 u64 range_start = start;
7119 u64 end;
7120 u64 found;
7121 u64 found_end;
7122 int err = 0;
7123
7124 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7125 if (IS_ERR(em))
7126 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007127 /*
7128 * If our em maps to:
7129 * - a hole or
7130 * - a pre-alloc extent,
7131 * there might actually be delalloc bytes behind it.
7132 */
7133 if (em->block_start != EXTENT_MAP_HOLE &&
7134 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7135 return em;
7136 else
7137 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007138
7139 /* check to see if we've wrapped (len == -1 or similar) */
7140 end = start + len;
7141 if (end < start)
7142 end = (u64)-1;
7143 else
7144 end -= 1;
7145
7146 em = NULL;
7147
7148 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007149 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007150 end, len, EXTENT_DELALLOC, 1);
7151 found_end = range_start + found;
7152 if (found_end < range_start)
7153 found_end = (u64)-1;
7154
7155 /*
7156 * we didn't find anything useful, return
7157 * the original results from get_extent()
7158 */
7159 if (range_start > end || found_end <= start) {
7160 em = hole_em;
7161 hole_em = NULL;
7162 goto out;
7163 }
7164
7165 /* adjust the range_start to make sure it doesn't
7166 * go backwards from the start they passed in
7167 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307168 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007169 found = found_end - range_start;
7170
7171 if (found > 0) {
7172 u64 hole_start = start;
7173 u64 hole_len = len;
7174
David Sterba172ddd62011-04-21 00:48:27 +02007175 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007176 if (!em) {
7177 err = -ENOMEM;
7178 goto out;
7179 }
7180 /*
7181 * when btrfs_get_extent can't find anything it
7182 * returns one huge hole
7183 *
7184 * make sure what it found really fits our range, and
7185 * adjust to make sure it is based on the start from
7186 * the caller
7187 */
7188 if (hole_em) {
7189 u64 calc_end = extent_map_end(hole_em);
7190
7191 if (calc_end <= start || (hole_em->start > end)) {
7192 free_extent_map(hole_em);
7193 hole_em = NULL;
7194 } else {
7195 hole_start = max(hole_em->start, start);
7196 hole_len = calc_end - hole_start;
7197 }
7198 }
7199 em->bdev = NULL;
7200 if (hole_em && range_start > hole_start) {
7201 /* our hole starts before our delalloc, so we
7202 * have to return just the parts of the hole
7203 * that go until the delalloc starts
7204 */
7205 em->len = min(hole_len,
7206 range_start - hole_start);
7207 em->start = hole_start;
7208 em->orig_start = hole_start;
7209 /*
7210 * don't adjust block start at all,
7211 * it is fixed at EXTENT_MAP_HOLE
7212 */
7213 em->block_start = hole_em->block_start;
7214 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007215 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7216 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007217 } else {
7218 em->start = range_start;
7219 em->len = found;
7220 em->orig_start = range_start;
7221 em->block_start = EXTENT_MAP_DELALLOC;
7222 em->block_len = found;
7223 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007224 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007225 return hole_em;
7226 }
7227out:
7228
7229 free_extent_map(hole_em);
7230 if (err) {
7231 free_extent_map(em);
7232 return ERR_PTR(err);
7233 }
7234 return em;
7235}
7236
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007237static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7238 const u64 start,
7239 const u64 len,
7240 const u64 orig_start,
7241 const u64 block_start,
7242 const u64 block_len,
7243 const u64 orig_block_len,
7244 const u64 ram_bytes,
7245 const int type)
7246{
7247 struct extent_map *em = NULL;
7248 int ret;
7249
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007250 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007251 em = create_io_em(inode, start, len, orig_start,
7252 block_start, block_len, orig_block_len,
7253 ram_bytes,
7254 BTRFS_COMPRESS_NONE, /* compress_type */
7255 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007256 if (IS_ERR(em))
7257 goto out;
7258 }
7259 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7260 len, block_len, type);
7261 if (ret) {
7262 if (em) {
7263 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007264 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007265 start + len - 1, 0);
7266 }
7267 em = ERR_PTR(ret);
7268 }
7269 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007270
7271 return em;
7272}
7273
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7275 u64 start, u64 len)
7276{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007279 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007280 struct btrfs_key ins;
7281 u64 alloc_hint;
7282 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283
Josef Bacik4b46fce2010-05-23 11:00:55 -04007284 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007285 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007286 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007287 if (ret)
7288 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007289
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007290 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7291 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007292 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007293 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007294 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007295 btrfs_free_reserved_extent(fs_info, ins.objectid,
7296 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007297
Josef Bacik4b46fce2010-05-23 11:00:55 -04007298 return em;
7299}
7300
Chris Mason46bfbb52010-05-26 11:04:10 -04007301/*
7302 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7303 * block must be cow'd
7304 */
Josef Bacik00361582013-08-14 14:02:47 -04007305noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007306 u64 *orig_start, u64 *orig_block_len,
7307 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007308{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007309 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007310 struct btrfs_path *path;
7311 int ret;
7312 struct extent_buffer *leaf;
7313 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007314 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 struct btrfs_file_extent_item *fi;
7316 struct btrfs_key key;
7317 u64 disk_bytenr;
7318 u64 backref_offset;
7319 u64 extent_end;
7320 u64 num_bytes;
7321 int slot;
7322 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007323 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007324
Chris Mason46bfbb52010-05-26 11:04:10 -04007325 path = btrfs_alloc_path();
7326 if (!path)
7327 return -ENOMEM;
7328
David Sterbaf85b7372017-01-20 14:54:07 +01007329 ret = btrfs_lookup_file_extent(NULL, root, path,
7330 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007331 if (ret < 0)
7332 goto out;
7333
7334 slot = path->slots[0];
7335 if (ret == 1) {
7336 if (slot == 0) {
7337 /* can't find the item, must cow */
7338 ret = 0;
7339 goto out;
7340 }
7341 slot--;
7342 }
7343 ret = 0;
7344 leaf = path->nodes[0];
7345 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007346 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007347 key.type != BTRFS_EXTENT_DATA_KEY) {
7348 /* not our file or wrong item type, must cow */
7349 goto out;
7350 }
7351
7352 if (key.offset > offset) {
7353 /* Wrong offset, must cow */
7354 goto out;
7355 }
7356
7357 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7358 found_type = btrfs_file_extent_type(leaf, fi);
7359 if (found_type != BTRFS_FILE_EXTENT_REG &&
7360 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7361 /* not a regular extent, must cow */
7362 goto out;
7363 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007364
7365 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7366 goto out;
7367
Miao Xiee77751a2013-12-27 21:11:50 +08007368 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7369 if (extent_end <= offset)
7370 goto out;
7371
Chris Mason46bfbb52010-05-26 11:04:10 -04007372 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007373 if (disk_bytenr == 0)
7374 goto out;
7375
7376 if (btrfs_file_extent_compression(leaf, fi) ||
7377 btrfs_file_extent_encryption(leaf, fi) ||
7378 btrfs_file_extent_other_encoding(leaf, fi))
7379 goto out;
7380
Chris Mason46bfbb52010-05-26 11:04:10 -04007381 backref_offset = btrfs_file_extent_offset(leaf, fi);
7382
Josef Bacik7ee9e442013-06-21 16:37:03 -04007383 if (orig_start) {
7384 *orig_start = key.offset - backref_offset;
7385 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7386 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7387 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007388
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007389 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007390 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007391
7392 num_bytes = min(offset + *len, extent_end) - offset;
7393 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7394 u64 range_end;
7395
Jeff Mahoneyda170662016-06-15 09:22:56 -04007396 range_end = round_up(offset + num_bytes,
7397 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007398 ret = test_range_bit(io_tree, offset, range_end,
7399 EXTENT_DELALLOC, 0, NULL);
7400 if (ret) {
7401 ret = -EAGAIN;
7402 goto out;
7403 }
7404 }
7405
Josef Bacik1bda19e2013-10-18 12:10:36 -04007406 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007407
7408 /*
7409 * look for other files referencing this extent, if we
7410 * find any we must cow
7411 */
Josef Bacik00361582013-08-14 14:02:47 -04007412
Liu Boe4c3b2d2017-01-30 12:25:28 -08007413 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007414 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007415 if (ret) {
7416 ret = 0;
7417 goto out;
7418 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007419
7420 /*
7421 * adjust disk_bytenr and num_bytes to cover just the bytes
7422 * in this extent we are about to write. If there
7423 * are any csums in that range we have to cow in order
7424 * to keep the csums correct
7425 */
7426 disk_bytenr += backref_offset;
7427 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007428 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7429 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007430 /*
7431 * all of the above have passed, it is safe to overwrite this extent
7432 * without cow
7433 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007434 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007435 ret = 1;
7436out:
7437 btrfs_free_path(path);
7438 return ret;
7439}
7440
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007441bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7442{
7443 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007444 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007445 void **pagep = NULL;
7446 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007447 unsigned long start_idx;
7448 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007449
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007450 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007451
7452 /*
7453 * end is the last byte in the last page. end == start is legal
7454 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007455 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007456
7457 rcu_read_lock();
7458
7459 /* Most of the code in this while loop is lifted from
7460 * find_get_page. It's been modified to begin searching from a
7461 * page and return just the first page found in that range. If the
7462 * found idx is less than or equal to the end idx then we know that
7463 * a page exists. If no pages are found or if those pages are
7464 * outside of the range then we're fine (yay!) */
7465 while (page == NULL &&
7466 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7467 page = radix_tree_deref_slot(pagep);
7468 if (unlikely(!page))
7469 break;
7470
7471 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007472 if (radix_tree_deref_retry(page)) {
7473 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007474 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007475 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007476 /*
7477 * Otherwise, shmem/tmpfs must be storing a swap entry
7478 * here as an exceptional entry: so return it without
7479 * attempting to raise page count.
7480 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007481 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007482 break; /* TODO: Is this relevant for this use case? */
7483 }
7484
Filipe Manana91405152014-06-05 13:22:24 +01007485 if (!page_cache_get_speculative(page)) {
7486 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007487 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007488 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007489
7490 /*
7491 * Has the page moved?
7492 * This is part of the lockless pagecache protocol. See
7493 * include/linux/pagemap.h for details.
7494 */
7495 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007496 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007497 page = NULL;
7498 }
7499 }
7500
7501 if (page) {
7502 if (page->index <= end_idx)
7503 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007504 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007505 }
7506
7507 rcu_read_unlock();
7508 return found;
7509}
7510
Josef Bacikeb838e72012-07-31 16:28:48 -04007511static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7512 struct extent_state **cached_state, int writing)
7513{
7514 struct btrfs_ordered_extent *ordered;
7515 int ret = 0;
7516
7517 while (1) {
7518 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007519 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007520 /*
7521 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007522 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007523 * extents in this range.
7524 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007525 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007526 lockend - lockstart + 1);
7527
7528 /*
7529 * We need to make sure there are no buffered pages in this
7530 * range either, we could have raced between the invalidate in
7531 * generic_file_direct_write and locking the extent. The
7532 * invalidate needs to happen so that reads after a write do not
7533 * get stale data.
7534 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007535 if (!ordered &&
7536 (!writing ||
7537 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007538 break;
7539
7540 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007541 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007542
7543 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007544 /*
7545 * If we are doing a DIO read and the ordered extent we
7546 * found is for a buffered write, we can not wait for it
7547 * to complete and retry, because if we do so we can
7548 * deadlock with concurrent buffered writes on page
7549 * locks. This happens only if our DIO read covers more
7550 * than one extent map, if at this point has already
7551 * created an ordered extent for a previous extent map
7552 * and locked its range in the inode's io tree, and a
7553 * concurrent write against that previous extent map's
7554 * range and this range started (we unlock the ranges
7555 * in the io tree only when the bios complete and
7556 * buffered writes always lock pages before attempting
7557 * to lock range in the io tree).
7558 */
7559 if (writing ||
7560 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7561 btrfs_start_ordered_extent(inode, ordered, 1);
7562 else
7563 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007564 btrfs_put_ordered_extent(ordered);
7565 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007567 * We could trigger writeback for this range (and wait
7568 * for it to complete) and then invalidate the pages for
7569 * this range (through invalidate_inode_pages2_range()),
7570 * but that can lead us to a deadlock with a concurrent
7571 * call to readpages() (a buffered read or a defrag call
7572 * triggered a readahead) on a page lock due to an
7573 * ordered dio extent we created before but did not have
7574 * yet a corresponding bio submitted (whence it can not
7575 * complete), which makes readpages() wait for that
7576 * ordered extent to complete while holding a lock on
7577 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007578 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007579 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007580 }
7581
Filipe Mananaade77022016-02-18 14:28:55 +00007582 if (ret)
7583 break;
7584
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 cond_resched();
7586 }
7587
7588 return ret;
7589}
7590
Liu Bo6f9994d2017-01-31 07:50:22 -08007591/* The callers of this must take lock_extent() */
7592static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7593 u64 orig_start, u64 block_start,
7594 u64 block_len, u64 orig_block_len,
7595 u64 ram_bytes, int compress_type,
7596 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007597{
7598 struct extent_map_tree *em_tree;
7599 struct extent_map *em;
7600 struct btrfs_root *root = BTRFS_I(inode)->root;
7601 int ret;
7602
Liu Bo6f9994d2017-01-31 07:50:22 -08007603 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7604 type == BTRFS_ORDERED_COMPRESSED ||
7605 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007606 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007607
Josef Bacik69ffb542012-09-11 15:40:07 -04007608 em_tree = &BTRFS_I(inode)->extent_tree;
7609 em = alloc_extent_map();
7610 if (!em)
7611 return ERR_PTR(-ENOMEM);
7612
7613 em->start = start;
7614 em->orig_start = orig_start;
7615 em->len = len;
7616 em->block_len = block_len;
7617 em->block_start = block_start;
7618 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007619 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007620 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007621 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007622 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007623 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007624 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007625 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007626 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7627 em->compress_type = compress_type;
7628 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007629
7630 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007631 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007632 em->start + em->len - 1, 0);
7633 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007634 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007635 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007636 /*
7637 * The caller has taken lock_extent(), who could race with us
7638 * to add em?
7639 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007640 } while (ret == -EEXIST);
7641
7642 if (ret) {
7643 free_extent_map(em);
7644 return ERR_PTR(ret);
7645 }
7646
Liu Bo6f9994d2017-01-31 07:50:22 -08007647 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007648 return em;
7649}
7650
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7652 struct buffer_head *bh_result, int create)
7653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007655 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007656 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307657 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007660 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007661 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007662 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007663
Miao Xie172a5042013-02-21 02:48:22 -07007664 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007665 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007666 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007667 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007668
Josef Bacikc3298612012-08-03 16:49:19 -04007669 lockstart = start;
7670 lockend = start + len - 1;
7671
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007672 if (current->journal_info) {
7673 /*
7674 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007675 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007676 * confused.
7677 */
chandan50745b02015-08-28 21:10:13 +05307678 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007679 current->journal_info = NULL;
7680 }
7681
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 /*
7683 * If this errors out it's because we couldn't invalidate pagecache for
7684 * this range and we need to fallback to buffered.
7685 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007686 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7687 create)) {
7688 ret = -ENOTBLK;
7689 goto err;
7690 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007691
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007692 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007693 if (IS_ERR(em)) {
7694 ret = PTR_ERR(em);
7695 goto unlock_err;
7696 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697
7698 /*
7699 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7700 * io. INLINE is special, and we could probably kludge it in here, but
7701 * it's still buffered so for safety lets just fall back to the generic
7702 * buffered path.
7703 *
7704 * For COMPRESSED we _have_ to read the entire extent in so we can
7705 * decompress it, so there will be buffering required no matter what we
7706 * do, so go ahead and fallback to buffered.
7707 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007708 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 * to buffered IO. Don't blame me, this is the price we pay for using
7710 * the generic code.
7711 */
7712 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7713 em->block_start == EXTENT_MAP_INLINE) {
7714 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007715 ret = -ENOTBLK;
7716 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717 }
7718
7719 /* Just a good old fashioned hole, return */
7720 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7721 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7722 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007723 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 }
7725
7726 /*
7727 * We don't allocate a new extent in the following cases
7728 *
7729 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7730 * existing extent.
7731 * 2) The extent is marked as PREALLOC. We're good to go here and can
7732 * just use the extent.
7733 *
7734 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007735 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007736 len = min(len, em->len - (start - em->start));
7737 lockstart = start + len;
7738 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007739 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740
7741 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7742 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7743 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007744 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007745 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746
7747 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7748 type = BTRFS_ORDERED_PREALLOC;
7749 else
7750 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007751 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007752 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007753
Josef Bacik00361582013-08-14 14:02:47 -04007754 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007755 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007756 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007757 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007758
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007759 em2 = btrfs_create_dio_extent(inode, start, len,
7760 orig_start, block_start,
7761 len, orig_block_len,
7762 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007763 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007764 if (type == BTRFS_ORDERED_PREALLOC) {
7765 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007766 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007767 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007768 if (em2 && IS_ERR(em2)) {
7769 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007770 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007771 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007772 /*
7773 * For inode marked NODATACOW or extent marked PREALLOC,
7774 * use the existing or preallocated extent, so does not
7775 * need to adjust btrfs_space_info's bytes_may_use.
7776 */
7777 btrfs_free_reserved_data_space_noquota(inode,
7778 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007779 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007780 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 }
Josef Bacik00361582013-08-14 14:02:47 -04007782
Chris Mason46bfbb52010-05-26 11:04:10 -04007783 /*
7784 * this will cow the extent, reset the len in case we changed
7785 * it above
7786 */
7787 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007788 free_extent_map(em);
7789 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007790 if (IS_ERR(em)) {
7791 ret = PTR_ERR(em);
7792 goto unlock_err;
7793 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007794 len = min(len, em->len - (start - em->start));
7795unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007796 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7797 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007798 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007799 bh_result->b_bdev = em->bdev;
7800 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007801 if (create) {
7802 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7803 set_buffer_new(bh_result);
7804
7805 /*
7806 * Need to update the i_size under the extent lock so buffered
7807 * readers will get the updated i_size when we unlock.
7808 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007809 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007810 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007811
chandan50745b02015-08-28 21:10:13 +05307812 WARN_ON(dio_data->reserve < len);
7813 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007814 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307815 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007816 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817
Josef Bacikeb838e72012-07-31 16:28:48 -04007818 /*
7819 * In the case of write we need to clear and unlock the entire range,
7820 * in the case of read we need to unlock only the end area that we
7821 * aren't using if there is any left over space.
7822 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007823 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007824 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7825 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007826 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007827 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007828 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007829 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007830
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831 free_extent_map(em);
7832
7833 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007834
7835unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007836 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007837 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007838err:
chandan50745b02015-08-28 21:10:13 +05307839 if (dio_data)
7840 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007841 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842}
7843
Omar Sandoval58efbc92017-08-22 23:45:59 -07007844static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7845 struct bio *bio,
7846 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007847{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007849 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007850
Mike Christie37226b22016-06-05 14:31:52 -05007851 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007852
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007853 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007854 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007855 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007856
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007857 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007858
Miao Xie8b110e32014-09-12 18:44:03 +08007859 return ret;
7860}
7861
7862static int btrfs_check_dio_repairable(struct inode *inode,
7863 struct bio *failed_bio,
7864 struct io_failure_record *failrec,
7865 int failed_mirror)
7866{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007867 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007868 int num_copies;
7869
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007870 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007871 if (num_copies == 1) {
7872 /*
7873 * we only have a single copy of the data, so don't bother with
7874 * all the retry and error correction code that follows. no
7875 * matter what the error is, it is very likely to persist.
7876 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007877 btrfs_debug(fs_info,
7878 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7879 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007880 return 0;
7881 }
7882
7883 failrec->failed_mirror = failed_mirror;
7884 failrec->this_mirror++;
7885 if (failrec->this_mirror == failed_mirror)
7886 failrec->this_mirror++;
7887
7888 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007889 btrfs_debug(fs_info,
7890 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7891 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007892 return 0;
7893 }
7894
7895 return 1;
7896}
7897
Omar Sandoval58efbc92017-08-22 23:45:59 -07007898static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7899 struct page *page, unsigned int pgoff,
7900 u64 start, u64 end, int failed_mirror,
7901 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007902{
7903 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007904 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7905 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007906 struct bio *bio;
7907 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007908 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007909 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007910 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007911 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007912 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007913
Mike Christie37226b22016-06-05 14:31:52 -05007914 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007915
7916 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7917 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007918 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007919
7920 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7921 failed_mirror);
7922 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007923 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007924 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007925 }
7926
Liu Bo17347ce2017-05-15 15:33:27 -07007927 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007928 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007929 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007930 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007931 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007932
7933 isector = start - btrfs_io_bio(failed_bio)->logical;
7934 isector >>= inode->i_sb->s_blocksize_bits;
7935 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307936 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007937 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007938
7939 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007940 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007941 read_mode, failrec->this_mirror, failrec->in_validation);
7942
Omar Sandoval58efbc92017-08-22 23:45:59 -07007943 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7944 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007945 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007946 bio_put(bio);
7947 }
7948
Omar Sandoval58efbc92017-08-22 23:45:59 -07007949 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007950}
7951
7952struct btrfs_retry_complete {
7953 struct completion done;
7954 struct inode *inode;
7955 u64 start;
7956 int uptodate;
7957};
7958
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007959static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007960{
7961 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007962 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007963 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007964 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007965 int i;
7966
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007967 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007968 goto end;
7969
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007971 io_tree = &BTRFS_I(inode)->io_tree;
7972 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007973 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974
Miao Xie8b110e32014-09-12 18:44:03 +08007975 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007976 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007977 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007978 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7979 io_tree, done->start, bvec->bv_page,
7980 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007981end:
7982 complete(&done->done);
7983 bio_put(bio);
7984}
7985
Omar Sandoval58efbc92017-08-22 23:45:59 -07007986static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7987 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007988{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307989 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007990 struct bio_vec bvec;
7991 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007992 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007993 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 unsigned int pgoff;
7995 u32 sectorsize;
7996 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007997 blk_status_t ret;
7998 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007999
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008001 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308002
Miao Xiec1dc0892014-09-12 18:43:56 +08008003 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008004 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008005 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008006
Liu Bo17347ce2017-05-15 15:33:27 -07008007 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8008 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8009 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308010
8011next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008012 done.uptodate = 0;
8013 done.start = start;
8014 init_completion(&done.done);
8015
Liu Bo17347ce2017-05-15 15:33:27 -07008016 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017 pgoff, start, start + sectorsize - 1,
8018 io_bio->mirror_num,
8019 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008020 if (ret) {
8021 err = ret;
8022 goto next;
8023 }
Miao Xie8b110e32014-09-12 18:44:03 +08008024
David Sterba9c17f6c2017-07-19 19:26:45 +02008025 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008026
8027 if (!done.uptodate) {
8028 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008030 }
8031
Liu Bo629ebf42017-05-15 17:20:07 -07008032next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 start += sectorsize;
8034
Liu Bo97bf5a52017-04-07 13:11:10 -07008035 nr_sectors--;
8036 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008038 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308039 goto next_block_or_try_again;
8040 }
Miao Xie8b110e32014-09-12 18:44:03 +08008041 }
8042
Liu Bo629ebf42017-05-15 17:20:07 -07008043 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008044}
8045
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008046static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008047{
8048 struct btrfs_retry_complete *done = bio->bi_private;
8049 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008050 struct extent_io_tree *io_tree, *failure_tree;
8051 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008052 struct bio_vec *bvec;
8053 int uptodate;
8054 int ret;
8055 int i;
8056
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008057 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008058 goto end;
8059
8060 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008063 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308064
Josef Bacik7870d082017-05-05 11:57:15 -04008065 io_tree = &BTRFS_I(inode)->io_tree;
8066 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8067
David Sterbac09abff2017-07-13 18:10:07 +02008068 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008069 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008070 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8071 bvec->bv_offset, done->start,
8072 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008073 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008074 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8075 failure_tree, io_tree, done->start,
8076 bvec->bv_page,
8077 btrfs_ino(BTRFS_I(inode)),
8078 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008079 else
8080 uptodate = 0;
8081 }
8082
8083 done->uptodate = uptodate;
8084end:
8085 complete(&done->done);
8086 bio_put(bio);
8087}
8088
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008089static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8090 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008091{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308092 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008093 struct bio_vec bvec;
8094 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008095 struct btrfs_retry_complete done;
8096 u64 start;
8097 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098 u32 sectorsize;
8099 int nr_sectors;
8100 unsigned int pgoff;
8101 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008102 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008103 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008104 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008105
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308106 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008107 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308108
Omar Sandoval58efbc92017-08-22 23:45:59 -07008109 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008110 start = io_bio->logical;
8111 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008112 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008113
Liu Bo17347ce2017-05-15 15:33:27 -07008114 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8115 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116
Liu Bo17347ce2017-05-15 15:33:27 -07008117 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008119 if (uptodate) {
8120 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8121 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8122 bvec.bv_page, pgoff, start, sectorsize);
8123 if (likely(!ret))
8124 goto next;
8125 }
Miao Xie8b110e32014-09-12 18:44:03 +08008126try_again:
8127 done.uptodate = 0;
8128 done.start = start;
8129 init_completion(&done.done);
8130
Omar Sandoval58efbc92017-08-22 23:45:59 -07008131 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8132 pgoff, start, start + sectorsize - 1,
8133 io_bio->mirror_num, btrfs_retry_endio,
8134 &done);
8135 if (status) {
8136 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008137 goto next;
8138 }
8139
David Sterba9c17f6c2017-07-19 19:26:45 +02008140 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008141
8142 if (!done.uptodate) {
8143 /* We might have another mirror, so try again */
8144 goto try_again;
8145 }
8146next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 offset += sectorsize;
8148 start += sectorsize;
8149
8150 ASSERT(nr_sectors);
8151
Liu Bo97bf5a52017-04-07 13:11:10 -07008152 nr_sectors--;
8153 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308154 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008155 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308156 goto next_block;
8157 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008158 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008159
8160 return err;
8161}
8162
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008163static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8164 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008165{
8166 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8167
8168 if (skip_csum) {
8169 if (unlikely(err))
8170 return __btrfs_correct_data_nocsum(inode, io_bio);
8171 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008172 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008173 } else {
8174 return __btrfs_subio_endio_read(inode, io_bio, err);
8175 }
8176}
8177
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008178static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008179{
8180 struct btrfs_dio_private *dip = bio->bi_private;
8181 struct inode *inode = dip->inode;
8182 struct bio *dio_bio;
8183 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008184 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008185
Liu Bo99c4e3b92017-09-15 15:06:51 -06008186 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008187 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008188
Josef Bacik4b46fce2010-05-23 11:00:55 -04008189 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008190 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008191 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192
Josef Bacik4b46fce2010-05-23 11:00:55 -04008193 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008194
Liu Bo99c4e3b92017-09-15 15:06:51 -06008195 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008196 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008197
8198 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008199 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008200 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008201}
8202
Qu Wenruo524272602017-03-08 10:25:52 +08008203static void __endio_write_update_ordered(struct inode *inode,
8204 const u64 offset, const u64 bytes,
8205 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008206{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008207 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008208 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008209 struct btrfs_workqueue *wq;
8210 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008211 u64 ordered_offset = offset;
8212 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008213 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008214 int ret;
8215
Qu Wenruo524272602017-03-08 10:25:52 +08008216 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8217 wq = fs_info->endio_freespace_worker;
8218 func = btrfs_freespace_write_helper;
8219 } else {
8220 wq = fs_info->endio_write_workers;
8221 func = btrfs_endio_write_helper;
8222 }
8223
Chris Mason163cf092010-11-28 19:56:33 -05008224again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008225 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008226 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8227 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008228 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008229 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008230 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008231 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008232
Qu Wenruo524272602017-03-08 10:25:52 +08008233 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8234 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008235out_test:
8236 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008237 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8238 * in the range, we can exit.
8239 */
8240 if (ordered_offset == last_offset)
8241 return;
8242 /*
Chris Mason163cf092010-11-28 19:56:33 -05008243 * our bio might span multiple ordered extents. If we haven't
8244 * completed the accounting for the whole dio, go back and try again
8245 */
Filipe Manana14543772015-11-24 16:23:54 +00008246 if (ordered_offset < offset + bytes) {
8247 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008248 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008249 goto again;
8250 }
Filipe Manana14543772015-11-24 16:23:54 +00008251}
8252
8253static void btrfs_endio_direct_write(struct bio *bio)
8254{
8255 struct btrfs_dio_private *dip = bio->bi_private;
8256 struct bio *dio_bio = dip->dio_bio;
8257
Qu Wenruo524272602017-03-08 10:25:52 +08008258 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008259 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008260
Josef Bacik4b46fce2010-05-23 11:00:55 -04008261 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008262
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008263 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008264 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008265 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008266}
8267
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008268static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008269 struct bio *bio, int mirror_num,
8270 unsigned long bio_flags, u64 offset)
8271{
Josef Bacikc6100a42017-05-05 11:57:13 -04008272 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008273 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008274 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008275 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008276 return 0;
8277}
8278
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008279static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008280{
8281 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008282 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008283
Miao Xie8b110e32014-09-12 18:44:03 +08008284 if (err)
8285 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008286 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008287 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8288 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008289 (unsigned long long)bio->bi_iter.bi_sector,
8290 bio->bi_iter.bi_size, err);
8291
8292 if (dip->subio_endio)
8293 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008294
8295 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008296 dip->errors = 1;
8297
8298 /*
8299 * before atomic variable goto zero, we must make sure
8300 * dip->errors is perceived to be set.
8301 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008302 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008303 }
8304
8305 /* if there are more bios still pending for this dio, just exit */
8306 if (!atomic_dec_and_test(&dip->pending_bios))
8307 goto out;
8308
Chris Mason9be33952013-05-17 18:30:14 -04008309 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008310 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008311 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008312 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008313 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008314 }
8315out:
8316 bio_put(bio);
8317}
8318
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008319static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008320 struct btrfs_dio_private *dip,
8321 struct bio *bio,
8322 u64 file_offset)
8323{
8324 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8325 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008326 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008327
8328 /*
8329 * We load all the csum data we need when we submit
8330 * the first bio to reduce the csum tree search and
8331 * contention.
8332 */
8333 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008334 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008335 file_offset);
8336 if (ret)
8337 return ret;
8338 }
8339
8340 if (bio == dip->orig_bio)
8341 return 0;
8342
8343 file_offset -= dip->logical_offset;
8344 file_offset >>= inode->i_sb->s_blocksize_bits;
8345 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8346
8347 return 0;
8348}
8349
Omar Sandoval58efbc92017-08-22 23:45:59 -07008350static inline blk_status_t
8351__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008352 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008354 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008355 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008356 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008357 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008358
Liu Bo4c274bc2017-11-01 17:19:27 -06008359 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008360 if (async_submit)
8361 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8362
Josef Bacik5fd02042012-05-02 14:00:54 -04008363 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008364 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008365 if (ret)
8366 goto err;
8367 }
Miao Xiee65e1532010-11-22 03:04:43 +00008368
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008369 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008370 goto map;
8371
8372 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008373 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8374 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008375 __btrfs_submit_bio_start_direct_io,
8376 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008377 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008378 } else if (write) {
8379 /*
8380 * If we aren't doing async submit, calculate the csum of the
8381 * bio now.
8382 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008383 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008384 if (ret)
8385 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008386 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008387 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008388 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008389 if (ret)
8390 goto err;
8391 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008392map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008393 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008394err:
Miao Xiee65e1532010-11-22 03:04:43 +00008395 return ret;
8396}
8397
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008398static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008399{
8400 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008401 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008402 struct bio *bio;
8403 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008404 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008405 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008406 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008407 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008408 u64 submit_len;
8409 int clone_offset = 0;
8410 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308411 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008412 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008413
Kent Overstreet4f024f32013-10-11 15:44:27 -07008414 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008415 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008416 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8417 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008418 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008419 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008420
Liu Bo725130b2017-05-16 09:51:39 -07008421 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008422 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008423 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008424 goto submit;
8425 }
8426
David Woodhouse53b381b2013-01-29 18:40:14 -05008427 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008428 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008429 async_submit = 0;
8430 else
8431 async_submit = 1;
8432
Liu Bo725130b2017-05-16 09:51:39 -07008433 /* bio split */
8434 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008435 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008436 do {
Liu Bo725130b2017-05-16 09:51:39 -07008437 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008438
Liu Bo725130b2017-05-16 09:51:39 -07008439 /*
8440 * This will never fail as it's passing GPF_NOFS and
8441 * the allocation is backed by btrfs_bioset.
8442 */
Liu Boe4770942017-05-16 10:57:14 -07008443 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008444 clone_len);
8445 bio->bi_private = dip;
8446 bio->bi_end_io = btrfs_end_dio_bio;
8447 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008448
Liu Bo725130b2017-05-16 09:51:39 -07008449 ASSERT(submit_len >= clone_len);
8450 submit_len -= clone_len;
8451 if (submit_len == 0)
8452 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008453
Liu Bo725130b2017-05-16 09:51:39 -07008454 /*
8455 * Increase the count before we submit the bio so we know
8456 * the end IO handler won't happen before we increase the
8457 * count. Otherwise, the dip might get freed before we're
8458 * done setting it up.
8459 */
8460 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008461
Linus Torvalds66ba7722017-09-09 13:27:51 -07008462 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008463 async_submit);
8464 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008465 bio_put(bio);
8466 atomic_dec(&dip->pending_bios);
8467 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008468 }
Liu Bo725130b2017-05-16 09:51:39 -07008469
8470 clone_offset += clone_len;
8471 start_sector += clone_len >> 9;
8472 file_offset += clone_len;
8473
8474 map_length = submit_len;
8475 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8476 start_sector << 9, &map_length, NULL, 0);
8477 if (ret)
8478 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008479 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008480
Josef Bacik02f57c72011-04-06 14:25:44 -04008481submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008482 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008483 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008484 return 0;
8485
8486 bio_put(bio);
8487out_err:
8488 dip->errors = 1;
8489 /*
8490 * before atomic variable goto zero, we must
8491 * make sure dip->errors is perceived to be set.
8492 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008493 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008494 if (atomic_dec_and_test(&dip->pending_bios))
8495 bio_io_error(dip->orig_bio);
8496
8497 /* bio_end_io() will handle error, so we needn't return it */
8498 return 0;
8499}
8500
Mike Christie8a4c1e42016-06-05 14:31:50 -05008501static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8502 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008503{
Filipe Manana61de7182015-07-01 12:13:10 +01008504 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008505 struct bio *bio = NULL;
8506 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008507 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008508 int ret = 0;
8509
David Sterba8b6c1d52017-06-02 17:48:13 +02008510 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008511
Miao Xiec1dc0892014-09-12 18:43:56 +08008512 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008513 if (!dip) {
8514 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008515 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008516 }
8517
8518 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008519 dip->inode = inode;
8520 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008521 dip->bytes = dio_bio->bi_iter.bi_size;
8522 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008523 bio->bi_private = dip;
8524 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008525 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008526 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008527 io_bio = btrfs_io_bio(bio);
8528 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008529
Miao Xiec1dc0892014-09-12 18:43:56 +08008530 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008531 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008532 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008533 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008534 dip->subio_endio = btrfs_subio_endio_read;
8535 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008536
Filipe Mananaf28a4922015-12-08 19:23:20 +00008537 /*
8538 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8539 * even if we fail to submit a bio, because in such case we do the
8540 * corresponding error handling below and it must not be done a second
8541 * time by btrfs_direct_IO().
8542 */
8543 if (write) {
8544 struct btrfs_dio_data *dio_data = current->journal_info;
8545
8546 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8547 dip->bytes;
8548 dio_data->unsubmitted_oe_range_start =
8549 dio_data->unsubmitted_oe_range_end;
8550 }
8551
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008552 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008553 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008554 return;
Chris Mason9be33952013-05-17 18:30:14 -04008555
Liu Bo3892ac92017-04-17 15:00:28 -07008556 if (io_bio->end_io)
8557 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008558
Josef Bacik4b46fce2010-05-23 11:00:55 -04008559free_ordered:
8560 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008561 * If we arrived here it means either we failed to submit the dip
8562 * or we either failed to clone the dio_bio or failed to allocate the
8563 * dip. If we cloned the dio_bio and allocated the dip, we can just
8564 * call bio_endio against our io_bio so that we get proper resource
8565 * cleanup if we fail to submit the dip, otherwise, we must do the
8566 * same as btrfs_endio_direct_[write|read] because we can't call these
8567 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008568 */
Liu Bo3892ac92017-04-17 15:00:28 -07008569 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008570 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008571 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008572 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008573 * and all the cleanup and final put for dio_bio (through
8574 * dio_end_io()).
8575 */
8576 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008577 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008578 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008579 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008580 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008581 file_offset,
8582 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008583 false);
Filipe Manana14543772015-11-24 16:23:54 +00008584 else
Filipe Manana61de7182015-07-01 12:13:10 +01008585 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8586 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008587
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008588 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008589 /*
8590 * Releases and cleans up our dio_bio, no need to bio_put()
8591 * nor bio_endio()/bio_io_error() against dio_bio.
8592 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008593 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008594 }
Liu Bo3892ac92017-04-17 15:00:28 -07008595 if (bio)
8596 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008597 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008598}
8599
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008600static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008601 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008602{
8603 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008604 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008605 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008606 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008607
8608 if (offset & blocksize_mask)
8609 goto out;
8610
Al Viro28060d52014-03-22 05:15:17 -04008611 if (iov_iter_alignment(iter) & blocksize_mask)
8612 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008613
Al Viro28060d52014-03-22 05:15:17 -04008614 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008615 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008616 return 0;
8617 /*
8618 * Check to make sure we don't have duplicate iov_base's in this
8619 * iovec, if so return EINVAL, otherwise we'll get csum errors
8620 * when reading back.
8621 */
8622 for (seg = 0; seg < iter->nr_segs; seg++) {
8623 for (i = seg + 1; i < iter->nr_segs; i++) {
8624 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008625 goto out;
8626 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008627 }
8628 retval = 0;
8629out:
8630 return retval;
8631}
Josef Bacikeb838e72012-07-31 16:28:48 -04008632
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008633static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008634{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008635 struct file *file = iocb->ki_filp;
8636 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308638 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008639 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008640 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008641 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008642 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008643 bool wakeup = true;
8644 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008645 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008646
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008647 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008648 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008649
Jens Axboefe0f07d2015-04-15 17:05:48 -06008650 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008651
Josef Bacik0e267c42013-07-02 10:38:02 -04008652 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008653 * The generic stuff only does filemap_write_and_wait_range, which
8654 * isn't enough if we've written compressed pages to this area, so
8655 * we need to flush the dirty pages again to make absolutely sure
8656 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008657 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008658 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008659 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8660 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008661 filemap_fdatawrite_range(inode->i_mapping, offset,
8662 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008663
Omar Sandoval6f673762015-03-16 04:33:52 -07008664 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008665 /*
8666 * If the write DIO is beyond the EOF, we need update
8667 * the isize, but it is protected by i_mutex. So we can
8668 * not unlock the i_mutex at this case.
8669 */
8670 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008671 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008672 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008673 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008674 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8675 ret = -EAGAIN;
8676 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008677 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008678 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8679 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008680 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008681 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008682
8683 /*
8684 * We need to know how many extents we reserved so that we can
8685 * do the accounting properly if we go over the number we
8686 * originally calculated. Abuse current->journal_info for this.
8687 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008688 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008689 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008690 dio_data.unsubmitted_oe_range_start = (u64)offset;
8691 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308692 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308693 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008694 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8695 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008696 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008697 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8698 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008699 }
8700
Omar Sandoval17f8c842015-03-16 04:33:50 -07008701 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008702 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008703 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008704 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008705 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308706 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008707 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008708 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308709 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008710 btrfs_delalloc_release_space(inode, data_reserved,
8711 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008712 /*
8713 * On error we might have left some ordered extents
8714 * without submitting corresponding bios for them, so
8715 * cleanup them up to avoid other tasks getting them
8716 * and waiting for them to complete forever.
8717 */
8718 if (dio_data.unsubmitted_oe_range_start <
8719 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008720 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008721 dio_data.unsubmitted_oe_range_start,
8722 dio_data.unsubmitted_oe_range_end -
8723 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008724 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008725 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008726 btrfs_delalloc_release_space(inode, data_reserved,
8727 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008728 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008729 }
Miao Xie38851cc2013-02-08 07:04:11 +00008730out:
Miao Xie2e60a512013-02-08 07:01:08 +00008731 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008732 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008733 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008734 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008735
Qu Wenruo364ecf32017-02-27 15:10:38 +08008736 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008737 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008738}
8739
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008740#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8741
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008742static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8743 __u64 start, __u64 len)
8744{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008745 int ret;
8746
8747 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8748 if (ret)
8749 return ret;
8750
David Sterba2135fb92017-06-23 04:09:57 +02008751 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008752}
8753
Chris Mason9ebefb182007-06-15 13:50:00 -04008754int btrfs_readpage(struct file *file, struct page *page)
8755{
Chris Masond1310b22008-01-24 16:13:08 -05008756 struct extent_io_tree *tree;
8757 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008758 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008759}
Chris Mason1832a6d2007-12-21 16:27:21 -05008760
Chris Mason39279cc2007-06-12 06:35:45 -04008761static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8762{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008763 struct inode *inode = page->mapping->host;
8764 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008765
8766 if (current->flags & PF_MEMALLOC) {
8767 redirty_page_for_writepage(wbc, page);
8768 unlock_page(page);
8769 return 0;
8770 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008771
8772 /*
8773 * If we are under memory pressure we will call this directly from the
8774 * VM, we need to make sure we have the inode referenced for the ordered
8775 * extent. If not just return like we didn't do anything.
8776 */
8777 if (!igrab(inode)) {
8778 redirty_page_for_writepage(wbc, page);
8779 return AOP_WRITEPAGE_ACTIVATE;
8780 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008781 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008782 btrfs_add_delayed_iput(inode);
8783 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008784}
Chris Mason39279cc2007-06-12 06:35:45 -04008785
Eric Sandeen48a3b632013-04-25 20:41:01 +00008786static int btrfs_writepages(struct address_space *mapping,
8787 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008788{
Chris Masond1310b22008-01-24 16:13:08 -05008789 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008790
Chris Masond1310b22008-01-24 16:13:08 -05008791 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008792 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008793}
8794
Chris Mason3ab2fb52007-11-08 10:59:22 -05008795static int
8796btrfs_readpages(struct file *file, struct address_space *mapping,
8797 struct list_head *pages, unsigned nr_pages)
8798{
Chris Masond1310b22008-01-24 16:13:08 -05008799 struct extent_io_tree *tree;
8800 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008801 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008802}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008803static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008804{
Chris Masond1310b22008-01-24 16:13:08 -05008805 struct extent_io_tree *tree;
8806 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008807 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008808
Chris Masond1310b22008-01-24 16:13:08 -05008809 tree = &BTRFS_I(page->mapping->host)->io_tree;
8810 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008811 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008812 if (ret == 1) {
8813 ClearPagePrivate(page);
8814 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008815 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008816 }
8817 return ret;
8818}
Chris Mason39279cc2007-06-12 06:35:45 -04008819
Chris Masone6dcd2d2008-07-17 12:53:50 -04008820static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8821{
Chris Mason98509cf2008-09-11 15:51:43 -04008822 if (PageWriteback(page) || PageDirty(page))
8823 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008824 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825}
8826
Lukas Czernerd47992f2013-05-21 23:17:23 -04008827static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8828 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008829{
Josef Bacik5fd02042012-05-02 14:00:54 -04008830 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008831 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008832 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008833 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008834 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008835 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308836 u64 start;
8837 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008838 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008839
Chris Mason8b62b722009-09-02 16:53:46 -04008840 /*
8841 * we have the page locked, so new writeback can't start,
8842 * and the dirty bit won't be cleared while we are here.
8843 *
8844 * Wait for IO on this page so that we can safely clear
8845 * the PagePrivate2 bit and do ordered accounting
8846 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008847 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008848
Josef Bacik5fd02042012-05-02 14:00:54 -04008849 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 if (offset) {
8851 btrfs_releasepage(page, GFP_NOFS);
8852 return;
8853 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008854
8855 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008856 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308857again:
8858 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008859 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308860 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008861 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308862 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008863 /*
8864 * IO on this page will never be started, so we need
8865 * to account for any ordered extents now
8866 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008867 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308868 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008869 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008870 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008871 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008872 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008873 /*
8874 * whoever cleared the private bit is responsible
8875 * for the finish_ordered_io
8876 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008877 if (TestClearPagePrivate2(page)) {
8878 struct btrfs_ordered_inode_tree *tree;
8879 u64 new_len;
8880
8881 tree = &BTRFS_I(inode)->ordered_tree;
8882
8883 spin_lock_irq(&tree->lock);
8884 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308885 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008886 if (new_len < ordered->truncated_len)
8887 ordered->truncated_len = new_len;
8888 spin_unlock_irq(&tree->lock);
8889
8890 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308891 start,
8892 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008893 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008894 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008896 if (!inode_evicting) {
8897 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308898 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008899 &cached_state);
8900 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308901
8902 start = end + 1;
8903 if (start < page_end)
8904 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008906
Qu Wenruob9d0b382015-09-29 10:35:16 +08008907 /*
8908 * Qgroup reserved space handler
8909 * Page here will be either
8910 * 1) Already written to disk
8911 * In this case, its reserved space is released from data rsv map
8912 * and will be freed by delayed_ref handler finally.
8913 * So even we call qgroup_free_data(), it won't decrease reserved
8914 * space.
8915 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008916 * This means the reserved space should be freed here. However,
8917 * if a truncate invalidates the page (by clearing PageDirty)
8918 * and the page is accounted for while allocating extent
8919 * in btrfs_check_data_free_space() we let delayed_ref to
8920 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008921 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008922 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008923 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008924 if (!inode_evicting) {
8925 clear_extent_bit(tree, page_start, page_end,
8926 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008927 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8928 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008929 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008930
8931 __btrfs_releasepage(page, GFP_NOFS);
8932 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008933
Chris Mason4a096752008-07-21 10:29:44 -04008934 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008935 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008936 ClearPagePrivate(page);
8937 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008938 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008939 }
Chris Mason39279cc2007-06-12 06:35:45 -04008940}
8941
Chris Mason9ebefb182007-06-15 13:50:00 -04008942/*
8943 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8944 * called from a page fault handler when a page is first dirtied. Hence we must
8945 * be careful to check for EOF conditions here. We set the page up correctly
8946 * for a written page which means we get ENOSPC checking when writing into
8947 * holes and correct delalloc and unwritten extent mapping on filesystems that
8948 * support these features.
8949 *
8950 * We are not allowed to take the i_mutex here so we have to play games to
8951 * protect against truncate races as the page could now be beyond EOF. Because
8952 * vmtruncate() writes the inode size before removing pages, once we have the
8953 * page lock we can determine safely if the page is beyond EOF. If it is not
8954 * beyond EOF, then the page is guaranteed safe against truncation until we
8955 * unlock the page.
8956 */
Dave Jiang11bac802017-02-24 14:56:41 -08008957int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008958{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008959 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008960 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8963 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008964 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008965 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008966 char *kaddr;
8967 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008968 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008969 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008970 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308971 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008972 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008973 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308974 u64 end;
8975
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008976 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008977
Jan Karab2b5ef52012-06-12 16:20:45 +02008978 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008979 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008980 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308981 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008982
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308983 /*
8984 * Reserving delalloc space after obtaining the page lock can lead to
8985 * deadlock. For example, if a dirty page is locked by this function
8986 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8987 * dirty page write out, then the btrfs_writepage() function could
8988 * end up waiting indefinitely to get a lock on the page currently
8989 * being processed by btrfs_page_mkwrite() function.
8990 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008991 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308992 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008993 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008994 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008995 reserved = 1;
8996 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008997 if (ret) {
8998 if (ret == -ENOMEM)
8999 ret = VM_FAULT_OOM;
9000 else /* -ENOSPC, -EIO, etc */
9001 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009002 if (reserved)
9003 goto out;
9004 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009005 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009006
Nick Piggin56a76f82009-03-31 15:23:23 -07009007 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009008again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009009 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009010 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009011
Chris Mason9ebefb182007-06-15 13:50:00 -04009012 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009014 /* page got truncated out from underneath us */
9015 goto out_unlock;
9016 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017 wait_on_page_writeback(page);
9018
David Sterbaff13db42015-12-03 14:30:40 +01009019 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020 set_page_extent_mapped(page);
9021
Chris Masoneb84ae02008-07-17 13:53:27 -04009022 /*
9023 * we can't set the delalloc bits if there are pending ordered
9024 * extents. Drop our locks and wait for them to finish
9025 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009026 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9027 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009029 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009030 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009031 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009032 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009033 btrfs_put_ordered_extent(ordered);
9034 goto again;
9035 }
9036
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009037 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009038 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009039 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009040 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309041 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009042 btrfs_delalloc_release_space(inode, data_reserved,
9043 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309044 }
9045 }
9046
Josef Bacikfbf19082009-10-01 17:10:23 -04009047 /*
Liu Bo54160342016-12-13 12:15:19 -08009048 * page_mkwrite gets called when the page is firstly dirtied after it's
9049 * faulted in, but write(2) could also dirty a page and set delalloc
9050 * bits, thus in this case for space account reason, we still need to
9051 * clear any delalloc bits within this page range since we have to
9052 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009053 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309054 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009055 EXTENT_DIRTY | EXTENT_DELALLOC |
9056 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009057 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009058
Filipe Mananae3b8a482017-11-04 00:16:59 +00009059 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009060 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009061 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009062 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009063 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009064 ret = VM_FAULT_SIGBUS;
9065 goto out_unlock;
9066 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009067 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009068
9069 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009070 if (page_start + PAGE_SIZE > size)
9071 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009072 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009073 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009074
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009075 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009076 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009077 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009078 flush_dcache_page(page);
9079 kunmap(page);
9080 }
Chris Mason247e7432008-07-17 12:53:51 -04009081 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009082 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009083 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009084
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009085 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009086 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009087 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009088
David Sterbae43bbe52017-12-12 21:43:52 +01009089 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009090
9091out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009092 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009093 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009094 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009095 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009096 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009097 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009098 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009099out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009100 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009101 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9102 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009103out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009104 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009105 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009106 return ret;
9107}
9108
Josef Bacika41ad392011-01-31 15:30:16 -05009109static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009110{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009111 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009112 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009113 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009114 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009115 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009116 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009117 u64 mask = fs_info->sectorsize - 1;
9118 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009119
Josef Bacik0ef8b722013-10-25 16:13:35 -04009120 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9121 (u64)-1);
9122 if (ret)
9123 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009124
Josef Bacikfcb80c22011-05-03 10:40:22 -04009125 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009126 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009127 * 3 things going on here
9128 *
9129 * 1) We need to reserve space for our orphan item and the space to
9130 * delete our orphan item. Lord knows we don't want to have a dangling
9131 * orphan item because we didn't reserve space to remove it.
9132 *
9133 * 2) We need to reserve space to update our inode.
9134 *
9135 * 3) We need to have something to cache all the space that is going to
9136 * be free'd up by the truncate operation, but also have some slack
9137 * space reserved in case it uses space during the truncate (thank you
9138 * very much snapshotting).
9139 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009140 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009141 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009142 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009143 * doesn't end up using space reserved for updating the inode or
9144 * removing the orphan item. We also need to be able to stop the
9145 * transaction and start a new one, which means we need to be able to
9146 * update the inode several times, and we have no idea of knowing how
9147 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009148 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009149 * Then there is the orphan item, which does indeed need to be held on
9150 * to for the whole operation, and we need nobody to touch this reserved
9151 * space except the orphan code.
9152 *
9153 * So that leaves us with
9154 *
9155 * 1) root->orphan_block_rsv - for the orphan deletion.
9156 * 2) rsv - for the truncate reservation, which we will steal from the
9157 * transaction reservation.
9158 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9159 * updating the inode.
9160 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009161 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009162 if (!rsv)
9163 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009164 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009165 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009166
Josef Bacik907cbce2011-08-08 13:46:15 -04009167 /*
Josef Bacik07127182011-08-19 10:29:59 -04009168 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009169 * 1 for updating the inode.
9170 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009171 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009172 if (IS_ERR(trans)) {
9173 err = PTR_ERR(trans);
9174 goto out;
9175 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009176
Josef Bacik907cbce2011-08-08 13:46:15 -04009177 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009178 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009179 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009180 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009181
Chris Mason5a3f23d2009-03-31 13:27:11 -04009182 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009183 * So if we truncate and then write and fsync we normally would just
9184 * write the extents that changed, which is a problem if we need to
9185 * first truncate that entire inode. So set this flag so we write out
9186 * all of the extents in the inode to the sync log so we're completely
9187 * safe.
9188 */
9189 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009190 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009191
Yan, Zheng80825102009-11-12 09:35:36 +00009192 while (1) {
9193 ret = btrfs_truncate_inode_items(trans, root, inode,
9194 inode->i_size,
9195 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009196 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009197 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009198 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009199 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009200 }
Chris Mason39279cc2007-06-12 06:35:45 -04009201
Yan, Zheng80825102009-11-12 09:35:36 +00009202 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009203 if (ret) {
9204 err = ret;
9205 break;
9206 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009207
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009208 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009209 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009210
9211 trans = btrfs_start_transaction(root, 2);
9212 if (IS_ERR(trans)) {
9213 ret = err = PTR_ERR(trans);
9214 trans = NULL;
9215 break;
9216 }
9217
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009218 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009219 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009220 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009221 BUG_ON(ret); /* shouldn't happen */
9222 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009223 }
9224
Josef Bacikddfae632017-10-19 14:16:02 -04009225 /*
9226 * We can't call btrfs_truncate_block inside a trans handle as we could
9227 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9228 * we've truncated everything except the last little bit, and can do
9229 * btrfs_truncate_block and then update the disk_i_size.
9230 */
9231 if (ret == NEED_TRUNCATE_BLOCK) {
9232 btrfs_end_transaction(trans);
9233 btrfs_btree_balance_dirty(fs_info);
9234
9235 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9236 if (ret)
9237 goto out;
9238 trans = btrfs_start_transaction(root, 1);
9239 if (IS_ERR(trans)) {
9240 ret = PTR_ERR(trans);
9241 goto out;
9242 }
9243 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9244 }
9245
Yan, Zheng80825102009-11-12 09:35:36 +00009246 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009247 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009248 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009249 if (ret)
9250 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009251 }
9252
Chris Mason917c16b2011-11-08 14:49:59 -05009253 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009254 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009255 ret = btrfs_update_inode(trans, root, inode);
9256 if (ret && !err)
9257 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009258
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009259 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009260 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009261 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009262out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009263 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009264
Josef Bacik3893e332011-01-31 16:03:11 -05009265 if (ret && !err)
9266 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009267
Josef Bacik3893e332011-01-31 16:03:11 -05009268 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009269}
9270
Sven Wegener3b963622008-06-09 21:57:42 -04009271/*
Chris Masond352ac62008-09-29 15:18:18 -04009272 * create a new subvolume directory/inode (helper for the ioctl).
9273 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009274int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009275 struct btrfs_root *new_root,
9276 struct btrfs_root *parent_root,
9277 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009278{
Chris Mason39279cc2007-06-12 06:35:45 -04009279 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009280 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009281 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009282
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009283 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9284 new_dirid, new_dirid,
9285 S_IFDIR | (~current_umask() & S_IRWXUGO),
9286 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009287 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009288 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009289 inode->i_op = &btrfs_dir_inode_operations;
9290 inode->i_fop = &btrfs_dir_file_operations;
9291
Miklos Szeredibfe86842011-10-28 14:13:29 +02009292 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009293 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009294 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009295
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009296 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9297 if (err)
9298 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009299 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009300 new_root->root_key.objectid, err);
9301
Yan, Zheng76dda932009-09-21 16:00:26 -04009302 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009303
Yan, Zheng76dda932009-09-21 16:00:26 -04009304 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009305 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009306}
9307
Chris Mason39279cc2007-06-12 06:35:45 -04009308struct inode *btrfs_alloc_inode(struct super_block *sb)
9309{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009310 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009311 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009312 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009313
David Sterba712e36c2017-10-31 17:08:27 +01009314 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009315 if (!ei)
9316 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317
9318 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009319 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009320 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009321 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009322 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009323 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009324 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009325 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326 ei->disk_i_size = 0;
9327 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009328 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009329 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009330 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009331 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009332 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009333 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009334
Josef Bacik9e0baf62011-07-15 15:16:44 +00009335 spin_lock_init(&ei->lock);
9336 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009337 if (sb->s_magic != BTRFS_TEST_MAGIC)
9338 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9339 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009340 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009341 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009342 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009343
Miao Xie16cdcec2011-04-22 18:12:22 +08009344 ei->delayed_node = NULL;
9345
chandan r9cc97d62012-07-04 12:48:07 +05309346 ei->i_otime.tv_sec = 0;
9347 ei->i_otime.tv_nsec = 0;
9348
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009349 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009350 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009351 extent_io_tree_init(&ei->io_tree, inode);
9352 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009353 ei->io_tree.track_uptodate = 1;
9354 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009355 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009356 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009357 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009358 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009359 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009360 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009361 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009362 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009363
9364 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009365}
9366
Josef Bacikaaedb552013-10-11 14:44:09 -04009367#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9368void btrfs_test_destroy_inode(struct inode *inode)
9369{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009370 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009371 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9372}
9373#endif
9374
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009375static void btrfs_i_callback(struct rcu_head *head)
9376{
9377 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009378 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9379}
9380
Chris Mason39279cc2007-06-12 06:35:45 -04009381void btrfs_destroy_inode(struct inode *inode)
9382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009384 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009385 struct btrfs_root *root = BTRFS_I(inode)->root;
9386
Al Virob3d9b7a2012-06-09 13:51:19 -04009387 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009388 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009389 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9390 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009391 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009392 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009393 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009394 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009395 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009396
Chris Mason5a3f23d2009-03-31 13:27:11 -04009397 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009398 * This can happen where we create an inode, but somebody else also
9399 * created the same inode and we need to destroy the one we already
9400 * created.
9401 */
9402 if (!root)
9403 goto free;
9404
Josef Bacik8a35d952012-05-23 14:26:42 -04009405 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9406 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009407 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009408 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009409 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009410 }
Josef Bacik7b128762008-07-24 12:17:14 -04009411
Chris Masond3977122009-01-05 21:25:51 -05009412 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009413 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9414 if (!ordered)
9415 break;
9416 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009417 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009418 "found ordered extent %llu %llu on inode cleanup",
9419 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009420 btrfs_remove_ordered_extent(inode, ordered);
9421 btrfs_put_ordered_extent(ordered);
9422 btrfs_put_ordered_extent(ordered);
9423 }
9424 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009425 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009426 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009427 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009428free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009429 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009430}
9431
Al Viro45321ac2010-06-07 13:43:19 -04009432int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009433{
9434 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009435
Naohiro Aota6379ef92013-06-06 09:56:34 +00009436 if (root == NULL)
9437 return 1;
9438
Liu Bofa6ac872013-02-20 14:10:23 +00009439 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009440 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009441 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009442 else
Al Viro45321ac2010-06-07 13:43:19 -04009443 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009444}
9445
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009446static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009447{
9448 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9449
9450 inode_init_once(&ei->vfs_inode);
9451}
9452
9453void btrfs_destroy_cachep(void)
9454{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009455 /*
9456 * Make sure all delayed rcu free inodes are flushed before we
9457 * destroy cache.
9458 */
9459 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009460 kmem_cache_destroy(btrfs_inode_cachep);
9461 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009462 kmem_cache_destroy(btrfs_path_cachep);
9463 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009464}
9465
Liu Bof5c29bd2017-11-02 17:21:50 -06009466int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009467{
David Sterba837e1972012-09-07 03:00:48 -06009468 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009469 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009470 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9471 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009472 if (!btrfs_inode_cachep)
9473 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009474
David Sterba837e1972012-09-07 03:00:48 -06009475 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009476 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009477 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009478 if (!btrfs_trans_handle_cachep)
9479 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009480
David Sterba837e1972012-09-07 03:00:48 -06009481 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009482 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009483 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009484 if (!btrfs_path_cachep)
9485 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009486
David Sterba837e1972012-09-07 03:00:48 -06009487 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009488 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009489 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009490 if (!btrfs_free_space_cachep)
9491 goto fail;
9492
Chris Mason39279cc2007-06-12 06:35:45 -04009493 return 0;
9494fail:
9495 btrfs_destroy_cachep();
9496 return -ENOMEM;
9497}
9498
David Howellsa528d352017-01-31 16:46:22 +00009499static int btrfs_getattr(const struct path *path, struct kstat *stat,
9500 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009501{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009502 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009503 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009504 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009505 u32 bi_flags = BTRFS_I(inode)->flags;
9506
9507 stat->result_mask |= STATX_BTIME;
9508 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9509 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9510 if (bi_flags & BTRFS_INODE_APPEND)
9511 stat->attributes |= STATX_ATTR_APPEND;
9512 if (bi_flags & BTRFS_INODE_COMPRESS)
9513 stat->attributes |= STATX_ATTR_COMPRESSED;
9514 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9515 stat->attributes |= STATX_ATTR_IMMUTABLE;
9516 if (bi_flags & BTRFS_INODE_NODUMP)
9517 stat->attributes |= STATX_ATTR_NODUMP;
9518
9519 stat->attributes_mask |= (STATX_ATTR_APPEND |
9520 STATX_ATTR_COMPRESSED |
9521 STATX_ATTR_IMMUTABLE |
9522 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009523
Chris Mason39279cc2007-06-12 06:35:45 -04009524 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009525 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009526
9527 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009528 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009529 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009530 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009531 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009532 return 0;
9533}
9534
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009535static int btrfs_rename_exchange(struct inode *old_dir,
9536 struct dentry *old_dentry,
9537 struct inode *new_dir,
9538 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009539{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009540 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009541 struct btrfs_trans_handle *trans;
9542 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009543 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009544 struct inode *new_inode = new_dentry->d_inode;
9545 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009546 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009547 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009548 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9549 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 u64 old_idx = 0;
9551 u64 new_idx = 0;
9552 u64 root_objectid;
9553 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009554 bool root_log_pinned = false;
9555 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556
9557 /* we only allow rename subvolume link between subvolumes */
9558 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9559 return -EXDEV;
9560
9561 /* close the race window with snapshot create/destroy ioctl */
9562 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009563 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009564 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009565 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566
9567 /*
9568 * We want to reserve the absolute worst case amount of items. So if
9569 * both inodes are subvols and we need to unlink them then that would
9570 * require 4 item modifications, but if they are both normal inodes it
9571 * would require 5 item modifications, so we'll assume their normal
9572 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9573 * should cover the worst case number of items we'll modify.
9574 */
9575 trans = btrfs_start_transaction(root, 12);
9576 if (IS_ERR(trans)) {
9577 ret = PTR_ERR(trans);
9578 goto out_notrans;
9579 }
9580
9581 /*
9582 * We need to find a free sequence number both in the source and
9583 * in the destination directory for the exchange.
9584 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009585 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 if (ret)
9587 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009588 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 if (ret)
9590 goto out_fail;
9591
9592 BTRFS_I(old_inode)->dir_index = 0ULL;
9593 BTRFS_I(new_inode)->dir_index = 0ULL;
9594
9595 /* Reference for the source. */
9596 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9597 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009598 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009600 btrfs_pin_log_trans(root);
9601 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 ret = btrfs_insert_inode_ref(trans, dest,
9603 new_dentry->d_name.name,
9604 new_dentry->d_name.len,
9605 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009606 btrfs_ino(BTRFS_I(new_dir)),
9607 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 if (ret)
9609 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009610 }
9611
9612 /* And now for the dest. */
9613 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9614 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009615 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009617 btrfs_pin_log_trans(dest);
9618 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 ret = btrfs_insert_inode_ref(trans, root,
9620 old_dentry->d_name.name,
9621 old_dentry->d_name.len,
9622 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009623 btrfs_ino(BTRFS_I(old_dir)),
9624 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 if (ret)
9626 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 }
9628
9629 /* Update inode version and ctime/mtime. */
9630 inode_inc_iversion(old_dir);
9631 inode_inc_iversion(new_dir);
9632 inode_inc_iversion(old_inode);
9633 inode_inc_iversion(new_inode);
9634 old_dir->i_ctime = old_dir->i_mtime = ctime;
9635 new_dir->i_ctime = new_dir->i_mtime = ctime;
9636 old_inode->i_ctime = ctime;
9637 new_inode->i_ctime = ctime;
9638
9639 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009640 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9641 BTRFS_I(old_inode), 1);
9642 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9643 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 }
9645
9646 /* src is a subvolume */
9647 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9648 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9649 ret = btrfs_unlink_subvol(trans, root, old_dir,
9650 root_objectid,
9651 old_dentry->d_name.name,
9652 old_dentry->d_name.len);
9653 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009654 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9655 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 old_dentry->d_name.name,
9657 old_dentry->d_name.len);
9658 if (!ret)
9659 ret = btrfs_update_inode(trans, root, old_inode);
9660 }
9661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009662 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 goto out_fail;
9664 }
9665
9666 /* dest is a subvolume */
9667 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9668 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9669 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9670 root_objectid,
9671 new_dentry->d_name.name,
9672 new_dentry->d_name.len);
9673 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009674 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9675 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 new_dentry->d_name.name,
9677 new_dentry->d_name.len);
9678 if (!ret)
9679 ret = btrfs_update_inode(trans, dest, new_inode);
9680 }
9681 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009682 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009683 goto out_fail;
9684 }
9685
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009686 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009687 new_dentry->d_name.name,
9688 new_dentry->d_name.len, 0, old_idx);
9689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009690 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 goto out_fail;
9692 }
9693
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009694 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 old_dentry->d_name.name,
9696 old_dentry->d_name.len, 0, new_idx);
9697 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009698 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699 goto out_fail;
9700 }
9701
9702 if (old_inode->i_nlink == 1)
9703 BTRFS_I(old_inode)->dir_index = old_idx;
9704 if (new_inode->i_nlink == 1)
9705 BTRFS_I(new_inode)->dir_index = new_idx;
9706
Filipe Manana86e8aa02016-05-05 02:02:27 +01009707 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009709 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9710 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009712 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009714 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009716 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9717 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009719 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720 }
9721out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009722 /*
9723 * If we have pinned a log and an error happened, we unpin tasks
9724 * trying to sync the log and force them to fallback to a transaction
9725 * commit if the log currently contains any of the inodes involved in
9726 * this rename operation (to ensure we do not persist a log with an
9727 * inconsistent state for any of these inodes or leading to any
9728 * inconsistencies when replayed). If the transaction was aborted, the
9729 * abortion reason is propagated to userspace when attempting to commit
9730 * the transaction. If the log does not contain any of these inodes, we
9731 * allow the tasks to sync it.
9732 */
9733 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009734 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9735 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9736 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009737 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009738 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009739 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009740
9741 if (root_log_pinned) {
9742 btrfs_end_log_trans(root);
9743 root_log_pinned = false;
9744 }
9745 if (dest_log_pinned) {
9746 btrfs_end_log_trans(dest);
9747 dest_log_pinned = false;
9748 }
9749 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009750 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751out_notrans:
9752 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009753 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009755 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756
9757 return ret;
9758}
9759
9760static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9761 struct btrfs_root *root,
9762 struct inode *dir,
9763 struct dentry *dentry)
9764{
9765 int ret;
9766 struct inode *inode;
9767 u64 objectid;
9768 u64 index;
9769
9770 ret = btrfs_find_free_ino(root, &objectid);
9771 if (ret)
9772 return ret;
9773
9774 inode = btrfs_new_inode(trans, root, dir,
9775 dentry->d_name.name,
9776 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009777 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009778 objectid,
9779 S_IFCHR | WHITEOUT_MODE,
9780 &index);
9781
9782 if (IS_ERR(inode)) {
9783 ret = PTR_ERR(inode);
9784 return ret;
9785 }
9786
9787 inode->i_op = &btrfs_special_inode_operations;
9788 init_special_inode(inode, inode->i_mode,
9789 WHITEOUT_DEV);
9790
9791 ret = btrfs_init_inode_security(trans, inode, dir,
9792 &dentry->d_name);
9793 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009794 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009796 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9797 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009799 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800
9801 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009802out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009804 if (ret)
9805 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 iput(inode);
9807
Filipe Mananac9901612016-05-05 01:41:57 +01009808 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809}
9810
Chris Mason39279cc2007-06-12 06:35:45 -04009811static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812 struct inode *new_dir, struct dentry *new_dentry,
9813 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009814{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009815 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009816 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009817 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009818 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9819 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009820 struct inode *new_inode = d_inode(new_dentry);
9821 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009822 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009823 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009824 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009825 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009826 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009827
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009828 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009829 return -EPERM;
9830
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009831 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009832 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009833 return -EXDEV;
9834
Li Zefan33345d012011-04-20 10:31:50 +08009835 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009836 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009837 return -ENOTEMPTY;
9838
Chris Mason39279cc2007-06-12 06:35:45 -04009839 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009840 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009841 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009842
9843
9844 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009845 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009846 new_dentry->d_name.name,
9847 new_dentry->d_name.len);
9848
9849 if (ret) {
9850 if (ret == -EEXIST) {
9851 /* we shouldn't get
9852 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309853 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009854 return ret;
9855 }
9856 } else {
9857 /* maybe -EOVERFLOW */
9858 return ret;
9859 }
9860 }
9861 ret = 0;
9862
Chris Mason5a3f23d2009-03-31 13:27:11 -04009863 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009864 * we're using rename to replace one file with another. Start IO on it
9865 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009866 */
Chris Mason8d875f92014-08-12 10:47:42 -07009867 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009868 filemap_flush(old_inode->i_mapping);
9869
Yan, Zheng76dda932009-09-21 16:00:26 -04009870 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009871 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009872 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009873 /*
9874 * We want to reserve the absolute worst case amount of items. So if
9875 * both inodes are subvols and we need to unlink them then that would
9876 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009877 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009878 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9879 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009880 * If our rename has the whiteout flag, we need more 5 units for the
9881 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9882 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009883 */
Filipe Manana5062af32016-05-05 10:26:26 +01009884 trans_num_items = 11;
9885 if (flags & RENAME_WHITEOUT)
9886 trans_num_items += 5;
9887 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009888 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009889 ret = PTR_ERR(trans);
9890 goto out_notrans;
9891 }
Chris Mason5f39d392007-10-15 16:14:19 -04009892
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009893 if (dest != root)
9894 btrfs_record_root_in_trans(trans, dest);
9895
Nikolay Borisov877574e2017-02-20 13:50:33 +02009896 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009897 if (ret)
9898 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009899
Miao Xie67de1172013-12-26 13:07:06 +08009900 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009901 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009902 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009903 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009904 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009905 btrfs_pin_log_trans(root);
9906 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009907 ret = btrfs_insert_inode_ref(trans, dest,
9908 new_dentry->d_name.name,
9909 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009910 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009911 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009912 if (ret)
9913 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009914 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009915
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009916 inode_inc_iversion(old_dir);
9917 inode_inc_iversion(new_dir);
9918 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009919 old_dir->i_ctime = old_dir->i_mtime =
9920 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009921 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009922
Chris Mason12fcfd22009-03-24 10:24:20 -04009923 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009924 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9925 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009926
Li Zefan33345d012011-04-20 10:31:50 +08009927 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009928 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9929 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9930 old_dentry->d_name.name,
9931 old_dentry->d_name.len);
9932 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009933 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9934 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009935 old_dentry->d_name.name,
9936 old_dentry->d_name.len);
9937 if (!ret)
9938 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009939 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009940 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009941 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009942 goto out_fail;
9943 }
Chris Mason39279cc2007-06-12 06:35:45 -04009944
9945 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009946 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009947 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009948 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009949 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9950 root_objectid = BTRFS_I(new_inode)->location.objectid;
9951 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9952 root_objectid,
9953 new_dentry->d_name.name,
9954 new_dentry->d_name.len);
9955 BUG_ON(new_inode->i_nlink == 0);
9956 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009957 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9958 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009959 new_dentry->d_name.name,
9960 new_dentry->d_name.len);
9961 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009962 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009963 ret = btrfs_orphan_add(trans,
9964 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009965 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009966 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009967 goto out_fail;
9968 }
Chris Mason39279cc2007-06-12 06:35:45 -04009969 }
Josef Bacikaec74772008-07-24 12:12:38 -04009970
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009971 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009972 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009973 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009974 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009975 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009976 goto out_fail;
9977 }
Chris Mason39279cc2007-06-12 06:35:45 -04009978
Miao Xie67de1172013-12-26 13:07:06 +08009979 if (old_inode->i_nlink == 1)
9980 BTRFS_I(old_inode)->dir_index = index;
9981
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009982 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009983 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009984
David Sterbaf85b7372017-01-20 14:54:07 +01009985 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9986 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009987 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009988 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009989 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009990
9991 if (flags & RENAME_WHITEOUT) {
9992 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9993 old_dentry);
9994
9995 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009996 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009997 goto out_fail;
9998 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009999 }
Chris Mason39279cc2007-06-12 06:35:45 -040010000out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010001 /*
10002 * If we have pinned the log and an error happened, we unpin tasks
10003 * trying to sync the log and force them to fallback to a transaction
10004 * commit if the log currently contains any of the inodes involved in
10005 * this rename operation (to ensure we do not persist a log with an
10006 * inconsistent state for any of these inodes or leading to any
10007 * inconsistencies when replayed). If the transaction was aborted, the
10008 * abortion reason is propagated to userspace when attempting to commit
10009 * the transaction. If the log does not contain any of these inodes, we
10010 * allow the tasks to sync it.
10011 */
10012 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010013 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10014 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10015 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010016 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010017 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010018 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010019
10020 btrfs_end_log_trans(root);
10021 log_pinned = false;
10022 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010023 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010024out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010025 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010026 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010027
Chris Mason39279cc2007-06-12 06:35:45 -040010028 return ret;
10029}
10030
Miklos Szeredi80ace852014-07-23 15:15:32 +020010031static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10032 struct inode *new_dir, struct dentry *new_dentry,
10033 unsigned int flags)
10034{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010035 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010036 return -EINVAL;
10037
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010038 if (flags & RENAME_EXCHANGE)
10039 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10040 new_dentry);
10041
10042 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010043}
10044
Miao Xie8ccf6f192012-10-25 09:28:04 +000010045static void btrfs_run_delalloc_work(struct btrfs_work *work)
10046{
10047 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010048 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010049
10050 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10051 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010052 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010053 filemap_flush(inode->i_mapping);
10054 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10055 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010056 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010057
10058 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010059 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010060 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010061 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062 complete(&delalloc_work->completion);
10063}
10064
10065struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010066 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010067{
10068 struct btrfs_delalloc_work *work;
10069
David Sterba100d5702015-12-08 14:39:32 +010010070 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010071 if (!work)
10072 return NULL;
10073
10074 init_completion(&work->completion);
10075 INIT_LIST_HEAD(&work->list);
10076 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010077 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010078 WARN_ON_ONCE(!inode);
10079 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10080 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010081
10082 return work;
10083}
10084
10085void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10086{
10087 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010088 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010089}
10090
Chris Masond352ac62008-09-29 15:18:18 -040010091/*
10092 * some fairly slow code that needs optimization. This walks the list
10093 * of all the inodes with pending delalloc and forces them to disk.
10094 */
Miao Xie6c255e62014-03-06 13:55:01 +080010095static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10096 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010097{
Chris Masonea8c2812008-08-04 23:17:27 -040010098 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010099 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010100 struct btrfs_delalloc_work *work, *next;
10101 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010102 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010103 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010104
Miao Xie8ccf6f192012-10-25 09:28:04 +000010105 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010106 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010107
Miao Xie573bfb72014-03-06 13:55:03 +080010108 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010109 spin_lock(&root->delalloc_lock);
10110 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010111 while (!list_empty(&splice)) {
10112 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010113 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010114
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 list_move_tail(&binode->delalloc_inodes,
10116 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010117 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010118 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010119 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010120 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010121 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010122 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010123
David Sterba651d4942015-11-27 19:24:16 +010010124 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010125 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010126 if (delay_iput)
10127 btrfs_add_delayed_iput(inode);
10128 else
10129 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010130 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010131 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010132 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010133 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010134 btrfs_queue_work(root->fs_info->flush_workers,
10135 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010136 ret++;
10137 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010138 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010139 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010140 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010141 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010142 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010143
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010144out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010145 list_for_each_entry_safe(work, next, &works, list) {
10146 list_del_init(&work->list);
10147 btrfs_wait_and_free_delalloc_work(work);
10148 }
10149
Miao Xieeb73c1b2013-05-15 07:48:22 +000010150 if (!list_empty_careful(&splice)) {
10151 spin_lock(&root->delalloc_lock);
10152 list_splice_tail(&splice, &root->delalloc_inodes);
10153 spin_unlock(&root->delalloc_lock);
10154 }
Miao Xie573bfb72014-03-06 13:55:03 +080010155 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010156 return ret;
10157}
10158
10159int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010161 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010162 int ret;
10163
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010164 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010165 return -EROFS;
10166
Miao Xie6c255e62014-03-06 13:55:01 +080010167 ret = __start_delalloc_inodes(root, delay_iput, -1);
10168 if (ret > 0)
10169 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 return ret;
10171}
10172
Miao Xie6c255e62014-03-06 13:55:01 +080010173int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10174 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175{
10176 struct btrfs_root *root;
10177 struct list_head splice;
10178 int ret;
10179
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010180 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010181 return -EROFS;
10182
10183 INIT_LIST_HEAD(&splice);
10184
Miao Xie573bfb72014-03-06 13:55:03 +080010185 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010186 spin_lock(&fs_info->delalloc_root_lock);
10187 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010188 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010189 root = list_first_entry(&splice, struct btrfs_root,
10190 delalloc_root);
10191 root = btrfs_grab_fs_root(root);
10192 BUG_ON(!root);
10193 list_move_tail(&root->delalloc_root,
10194 &fs_info->delalloc_roots);
10195 spin_unlock(&fs_info->delalloc_root_lock);
10196
Miao Xie6c255e62014-03-06 13:55:01 +080010197 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010199 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010200 goto out;
10201
Miao Xie6c255e62014-03-06 13:55:01 +080010202 if (nr != -1) {
10203 nr -= ret;
10204 WARN_ON(nr < 0);
10205 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010206 spin_lock(&fs_info->delalloc_root_lock);
10207 }
10208 spin_unlock(&fs_info->delalloc_root_lock);
10209
Miao Xie6c255e62014-03-06 13:55:01 +080010210 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010211out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010212 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 spin_lock(&fs_info->delalloc_root_lock);
10214 list_splice_tail(&splice, &fs_info->delalloc_roots);
10215 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010216 }
Miao Xie573bfb72014-03-06 13:55:03 +080010217 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010218 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010219}
10220
Chris Mason39279cc2007-06-12 06:35:45 -040010221static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10222 const char *symname)
10223{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010224 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010225 struct btrfs_trans_handle *trans;
10226 struct btrfs_root *root = BTRFS_I(dir)->root;
10227 struct btrfs_path *path;
10228 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010229 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010230 int err;
10231 int drop_inode = 0;
10232 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010233 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010234 int name_len;
10235 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010236 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010237 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010238 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010239
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010240 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010241 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010242 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010243
Josef Bacik9ed74f22009-09-11 16:12:44 -040010244 /*
10245 * 2 items for inode item and ref
10246 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010247 * 1 item for updating parent inode item
10248 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010249 * 1 item for xattr if selinux is on
10250 */
Filipe Manana9269d122015-12-31 18:16:29 +000010251 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010252 if (IS_ERR(trans))
10253 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010254
Li Zefan581bb052011-04-20 10:06:11 +080010255 err = btrfs_find_free_ino(root, &objectid);
10256 if (err)
10257 goto out_unlock;
10258
Josef Bacikaec74772008-07-24 12:12:38 -040010259 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010260 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10261 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010262 if (IS_ERR(inode)) {
10263 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010264 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010265 }
Chris Mason39279cc2007-06-12 06:35:45 -040010266
Casey Schauflerad19db72011-12-15 10:09:07 -050010267 /*
10268 * If the active LSM wants to access the inode during
10269 * d_instantiate it needs these. Smack checks to see
10270 * if the filesystem supports xattrs by looking at the
10271 * ops vector.
10272 */
10273 inode->i_fop = &btrfs_file_operations;
10274 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010275 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010276 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10277
10278 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10279 if (err)
10280 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010281
Chris Mason39279cc2007-06-12 06:35:45 -040010282 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010283 if (!path) {
10284 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010285 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010286 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010287 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010288 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010289 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010290 datasize = btrfs_file_extent_calc_inline_size(name_len);
10291 err = btrfs_insert_empty_item(trans, root, path, &key,
10292 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010293 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010294 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010295 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010296 }
Chris Mason5f39d392007-10-15 16:14:19 -040010297 leaf = path->nodes[0];
10298 ei = btrfs_item_ptr(leaf, path->slots[0],
10299 struct btrfs_file_extent_item);
10300 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10301 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010302 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010303 btrfs_set_file_extent_encryption(leaf, ei, 0);
10304 btrfs_set_file_extent_compression(leaf, ei, 0);
10305 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10306 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10307
Chris Mason39279cc2007-06-12 06:35:45 -040010308 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010309 write_extent_buffer(leaf, symname, ptr, name_len);
10310 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010311 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010312
Chris Mason39279cc2007-06-12 06:35:45 -040010313 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010314 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010315 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010316 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010317 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010318 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010319 /*
10320 * Last step, add directory indexes for our symlink inode. This is the
10321 * last step to avoid extra cleanup of these indexes if an error happens
10322 * elsewhere above.
10323 */
10324 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010325 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10326 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010327 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010328 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010329 goto out_unlock_inode;
10330 }
10331
10332 unlock_new_inode(inode);
10333 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010334
10335out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010336 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010337 if (drop_inode) {
10338 inode_dec_link_count(inode);
10339 iput(inode);
10340 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010341 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010342 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010343
10344out_unlock_inode:
10345 drop_inode = 1;
10346 unlock_new_inode(inode);
10347 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010348}
Chris Mason16432982008-04-10 10:23:21 -040010349
Josef Bacik0af3d002010-06-21 14:48:16 -040010350static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10351 u64 start, u64 num_bytes, u64 min_size,
10352 loff_t actual_len, u64 *alloc_hint,
10353 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010356 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10357 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010358 struct btrfs_root *root = BTRFS_I(inode)->root;
10359 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010360 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010361 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010362 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010363 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010364 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010365 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010366 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010367
Josef Bacik0af3d002010-06-21 14:48:16 -040010368 if (trans)
10369 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010370 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010371 if (own_trans) {
10372 trans = btrfs_start_transaction(root, 3);
10373 if (IS_ERR(trans)) {
10374 ret = PTR_ERR(trans);
10375 break;
10376 }
Yan Zhengd899e052008-10-30 14:25:28 -040010377 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010378
Byongho Leeee221842015-12-15 01:42:10 +090010379 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010380 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010381 /*
10382 * If we are severely fragmented we could end up with really
10383 * small allocations, so if the allocator is returning small
10384 * chunks lets make its job easier by only searching for those
10385 * sized chunks.
10386 */
10387 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010388 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10389 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010390 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010391 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010392 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010393 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010394 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010395 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010396
Josef Bacik0b670dc2015-09-23 17:11:16 -040010397 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010398 ret = insert_reserved_file_extent(trans, inode,
10399 cur_offset, ins.objectid,
10400 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010401 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010402 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010403 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010404 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010405 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010406 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010407 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010408 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010409 break;
10410 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010411
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010412 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010413 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010414
Josef Bacik5dc562c2012-08-17 13:14:17 -040010415 em = alloc_extent_map();
10416 if (!em) {
10417 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10418 &BTRFS_I(inode)->runtime_flags);
10419 goto next;
10420 }
10421
10422 em->start = cur_offset;
10423 em->orig_start = cur_offset;
10424 em->len = ins.offset;
10425 em->block_start = ins.objectid;
10426 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010427 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010428 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010429 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010430 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10431 em->generation = trans->transid;
10432
10433 while (1) {
10434 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010435 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010436 write_unlock(&em_tree->lock);
10437 if (ret != -EEXIST)
10438 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010439 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010440 cur_offset + ins.offset - 1,
10441 0);
10442 }
10443 free_extent_map(em);
10444next:
Yan Zhengd899e052008-10-30 14:25:28 -040010445 num_bytes -= ins.offset;
10446 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010447 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010448
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010449 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010450 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010451 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010452 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010453 (actual_len > inode->i_size) &&
10454 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010455 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010456 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010457 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010458 i_size = cur_offset;
10459 i_size_write(inode, i_size);
10460 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010461 }
10462
Yan Zhengd899e052008-10-30 14:25:28 -040010463 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010464
10465 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010466 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010467 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010468 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010469 break;
10470 }
Yan Zhengd899e052008-10-30 14:25:28 -040010471
Josef Bacik0af3d002010-06-21 14:48:16 -040010472 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010473 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010474 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010475 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010476 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010477 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010478 return ret;
10479}
10480
Josef Bacik0af3d002010-06-21 14:48:16 -040010481int btrfs_prealloc_file_range(struct inode *inode, int mode,
10482 u64 start, u64 num_bytes, u64 min_size,
10483 loff_t actual_len, u64 *alloc_hint)
10484{
10485 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10486 min_size, actual_len, alloc_hint,
10487 NULL);
10488}
10489
10490int btrfs_prealloc_file_range_trans(struct inode *inode,
10491 struct btrfs_trans_handle *trans, int mode,
10492 u64 start, u64 num_bytes, u64 min_size,
10493 loff_t actual_len, u64 *alloc_hint)
10494{
10495 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10496 min_size, actual_len, alloc_hint, trans);
10497}
10498
Chris Masone6dcd2d2008-07-17 12:53:50 -040010499static int btrfs_set_page_dirty(struct page *page)
10500{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010501 return __set_page_dirty_nobuffers(page);
10502}
10503
Al Viro10556cb22011-06-20 19:28:19 -040010504static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010505{
Li Zefanb83cc962010-12-20 16:04:08 +080010506 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010507 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010508
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010509 if (mask & MAY_WRITE &&
10510 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10511 if (btrfs_root_readonly(root))
10512 return -EROFS;
10513 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10514 return -EACCES;
10515 }
Al Viro2830ba72011-06-20 19:16:29 -040010516 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010517}
Chris Mason39279cc2007-06-12 06:35:45 -040010518
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010519static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10520{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010521 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010522 struct btrfs_trans_handle *trans;
10523 struct btrfs_root *root = BTRFS_I(dir)->root;
10524 struct inode *inode = NULL;
10525 u64 objectid;
10526 u64 index;
10527 int ret = 0;
10528
10529 /*
10530 * 5 units required for adding orphan entry
10531 */
10532 trans = btrfs_start_transaction(root, 5);
10533 if (IS_ERR(trans))
10534 return PTR_ERR(trans);
10535
10536 ret = btrfs_find_free_ino(root, &objectid);
10537 if (ret)
10538 goto out;
10539
10540 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010541 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010542 if (IS_ERR(inode)) {
10543 ret = PTR_ERR(inode);
10544 inode = NULL;
10545 goto out;
10546 }
10547
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010548 inode->i_fop = &btrfs_file_operations;
10549 inode->i_op = &btrfs_file_inode_operations;
10550
10551 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010552 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10553
Chris Masonb0d5d102014-09-08 13:08:51 -070010554 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10555 if (ret)
10556 goto out_inode;
10557
10558 ret = btrfs_update_inode(trans, root, inode);
10559 if (ret)
10560 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010561 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010562 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010563 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564
Filipe Manana5762b5c2014-08-01 00:10:32 +010010565 /*
10566 * We set number of links to 0 in btrfs_new_inode(), and here we set
10567 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10568 * through:
10569 *
10570 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10571 */
10572 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010573 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010574 d_tmpfile(dentry, inode);
10575 mark_inode_dirty(inode);
10576
10577out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010578 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010579 if (ret)
10580 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010581 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010582 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010583
10584out_inode:
10585 unlock_new_inode(inode);
10586 goto out;
10587
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010588}
10589
David Sterba20a7db82017-02-17 16:24:29 +010010590__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010591static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010592{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010593 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010594}
10595
Josef Bacikc6100a42017-05-05 11:57:13 -040010596static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10597{
10598 struct inode *inode = private_data;
10599 return btrfs_sb(inode->i_sb);
10600}
10601
10602static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10603 u64 start, u64 end)
10604{
10605 struct inode *inode = private_data;
10606 u64 isize;
10607
10608 isize = i_size_read(inode);
10609 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10610 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10611 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10612 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10613 }
10614}
10615
10616void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10617{
10618 struct inode *inode = private_data;
10619 unsigned long index = start >> PAGE_SHIFT;
10620 unsigned long end_index = end >> PAGE_SHIFT;
10621 struct page *page;
10622
10623 while (index <= end_index) {
10624 page = find_get_page(inode->i_mapping, index);
10625 ASSERT(page); /* Pages should be in the extent_io_tree */
10626 set_page_writeback(page);
10627 put_page(page);
10628 index++;
10629 }
10630}
10631
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010632static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010633 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010634 .lookup = btrfs_lookup,
10635 .create = btrfs_create,
10636 .unlink = btrfs_unlink,
10637 .link = btrfs_link,
10638 .mkdir = btrfs_mkdir,
10639 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010640 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010641 .symlink = btrfs_symlink,
10642 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010643 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010644 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010645 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010646 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010647 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010648 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010649 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010650};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010651static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010652 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010653 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010654 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010655};
Yan, Zheng76dda932009-09-21 16:00:26 -040010656
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010657static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010658 .llseek = generic_file_llseek,
10659 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010660 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010661 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010662 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010663#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010664 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010665#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010666 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010667 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010668};
10669
David Sterba20e55062015-11-19 11:42:28 +010010670static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010671 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010672 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010673 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010674 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010675 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010676 .tree_fs_info = iotree_fs_info,
10677 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010678
10679 /* optional callbacks */
10680 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010681 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010682 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010683 .set_bit_hook = btrfs_set_bit_hook,
10684 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010685 .merge_extent_hook = btrfs_merge_extent_hook,
10686 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010687 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010688};
10689
Chris Mason35054392009-01-21 13:11:13 -050010690/*
10691 * btrfs doesn't support the bmap operation because swapfiles
10692 * use bmap to make a mapping of extents in the file. They assume
10693 * these extents won't change over the life of the file and they
10694 * use the bmap result to do IO directly to the drive.
10695 *
10696 * the btrfs bmap call would return logical addresses that aren't
10697 * suitable for IO and they also will change frequently as COW
10698 * operations happen. So, swapfile + btrfs == corruption.
10699 *
10700 * For now we're avoiding this by dropping bmap.
10701 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010702static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010703 .readpage = btrfs_readpage,
10704 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010705 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010706 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010707 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010708 .invalidatepage = btrfs_invalidatepage,
10709 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010710 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010711 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010712};
10713
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010714static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010715 .readpage = btrfs_readpage,
10716 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010717 .invalidatepage = btrfs_invalidatepage,
10718 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010719};
10720
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010721static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010722 .getattr = btrfs_getattr,
10723 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010724 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010725 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010726 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010727 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010728 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010729 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010730};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010731static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010732 .getattr = btrfs_getattr,
10733 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010734 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010735 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010736 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010737 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010738 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010739};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010740static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010741 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010742 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010743 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010744 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010745 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010746 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010747};
Yan, Zheng76dda932009-09-21 16:00:26 -040010748
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010749const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010750 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010751 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010752};