blob: 4d3a4d1507a1f15bcb6a00aba15b3b922224f97c [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
Josef Bacik8a35d952012-05-23 14:26:42 -04003358 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003359 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003360#if 0
3361 /*
3362 * For proper ENOSPC handling, we should do orphan
3363 * cleanup when mounting. But this introduces backward
3364 * compatibility issue.
3365 */
3366 if (!xchg(&root->orphan_item_inserted, 1))
3367 insert = 2;
3368 else
3369 insert = 1;
3370#endif
3371 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 }
Josef Bacik7b128762008-07-24 12:17:14 -04003373
Josef Bacik72ac3c02012-05-23 14:13:11 -04003374 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003375 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003376 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003377
3378 spin_lock(&root->orphan_lock);
3379 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3380 if (!root->orphan_block_rsv) {
3381 root->orphan_block_rsv = block_rsv;
3382 } else if (block_rsv) {
3383 btrfs_free_block_rsv(fs_info, block_rsv);
3384 block_rsv = NULL;
3385 }
3386
3387 if (insert)
3388 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003389 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003390
Yan, Zhengd68fc572010-05-16 10:49:58 -04003391 /* grab metadata reservation from transaction handle */
3392 if (reserve) {
3393 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003394 ASSERT(!ret);
3395 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003396 /*
3397 * dec doesn't need spin_lock as ->orphan_block_rsv
3398 * would be released only if ->orphan_inodes is
3399 * zero.
3400 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003401 atomic_dec(&root->orphan_inodes);
3402 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003403 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003404 if (insert)
3405 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003406 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003407 return ret;
3408 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 }
3410
3411 /* insert an orphan item to track this unlinked/truncated file */
3412 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003413 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003414 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003415 if (reserve) {
3416 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003417 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003418 btrfs_orphan_release_metadata(inode);
3419 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003420 /*
3421 * btrfs_orphan_commit_root may race with us and set
3422 * ->orphan_block_rsv to zero, in order to avoid that,
3423 * decrease ->orphan_inodes after everything is done.
3424 */
3425 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003426 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003427 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003428 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003429 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003430 return ret;
3431 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003432 }
3433 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003434 }
3435
3436 /* insert an orphan item to track subvolume contains orphan files */
3437 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003438 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003439 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003440 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003441 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003442 return ret;
3443 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003444 }
3445 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003446}
3447
3448/*
3449 * We have done the truncate/delete so we can go ahead and remove the orphan
3450 * item for this particular inode.
3451 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003452static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003453 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003454{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003455 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003456 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003457 int ret = 0;
3458
Josef Bacik8a35d952012-05-23 14:26:42 -04003459 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003460 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003461 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003462
Liu Bo1a932ef2018-01-25 11:02:54 -07003463 if (delete_item && trans)
3464 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3465
Josef Bacik72ac3c02012-05-23 14:13:11 -04003466 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003467 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003468 btrfs_orphan_release_metadata(inode);
3469
Liu Bo1a932ef2018-01-25 11:02:54 -07003470 /*
3471 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3472 * to zero, in order to avoid that, decrease ->orphan_inodes after
3473 * everything is done.
3474 */
3475 if (delete_item)
3476 atomic_dec(&root->orphan_inodes);
3477
Josef Bacik4ef31a42013-08-13 14:10:08 -04003478 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003479}
3480
3481/*
3482 * this cleans up any orphans that may be left on the list from the last use
3483 * of this root.
3484 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003487 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003488 struct btrfs_path *path;
3489 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003490 struct btrfs_key key, found_key;
3491 struct btrfs_trans_handle *trans;
3492 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003493 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003494 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3495
Yan, Zhengd68fc572010-05-16 10:49:58 -04003496 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003498
3499 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500 if (!path) {
3501 ret = -ENOMEM;
3502 goto out;
3503 }
David Sterbae4058b52015-11-27 16:31:35 +01003504 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003505
3506 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003507 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003508 key.offset = (u64)-1;
3509
Josef Bacik7b128762008-07-24 12:17:14 -04003510 while (1) {
3511 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003512 if (ret < 0)
3513 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003514
3515 /*
3516 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003517 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003518 * find the key and see if we have stuff that matches
3519 */
3520 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003522 if (path->slots[0] == 0)
3523 break;
3524 path->slots[0]--;
3525 }
3526
3527 /* pull out the item */
3528 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003529 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3530
3531 /* make sure the item matches what we want */
3532 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3533 break;
David Sterba962a2982014-06-04 18:41:45 +02003534 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003535 break;
3536
3537 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003538 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003539
3540 /*
3541 * this is where we are basically btrfs_lookup, without the
3542 * crossing root thing. we store the inode number in the
3543 * offset of the orphan item.
3544 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003545
3546 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003547 btrfs_err(fs_info,
3548 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003549 ret = -EINVAL;
3550 goto out;
3551 }
3552
3553 last_objectid = found_key.offset;
3554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003555 found_key.objectid = found_key.offset;
3556 found_key.type = BTRFS_INODE_ITEM_KEY;
3557 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003558 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303559 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003560 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003561 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003562
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003563 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003564 struct btrfs_root *dead_root;
3565 struct btrfs_fs_info *fs_info = root->fs_info;
3566 int is_dead_root = 0;
3567
3568 /*
3569 * this is an orphan in the tree root. Currently these
3570 * could come from 2 sources:
3571 * a) a snapshot deletion in progress
3572 * b) a free space cache inode
3573 * We need to distinguish those two, as the snapshot
3574 * orphan must not get deleted.
3575 * find_dead_roots already ran before us, so if this
3576 * is a snapshot deletion, we should find the root
3577 * in the dead_roots list
3578 */
3579 spin_lock(&fs_info->trans_lock);
3580 list_for_each_entry(dead_root, &fs_info->dead_roots,
3581 root_list) {
3582 if (dead_root->root_key.objectid ==
3583 found_key.objectid) {
3584 is_dead_root = 1;
3585 break;
3586 }
3587 }
3588 spin_unlock(&fs_info->trans_lock);
3589 if (is_dead_root) {
3590 /* prevent this orphan from being found again */
3591 key.offset = found_key.objectid - 1;
3592 continue;
3593 }
3594 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003595 /*
3596 * Inode is already gone but the orphan item is still there,
3597 * kill the orphan item.
3598 */
Filipe Manana67710892016-06-06 11:51:25 +01003599 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003600 trans = btrfs_start_transaction(root, 1);
3601 if (IS_ERR(trans)) {
3602 ret = PTR_ERR(trans);
3603 goto out;
3604 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003605 btrfs_debug(fs_info, "auto deleting %Lu",
3606 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003607 ret = btrfs_del_orphan_item(trans, root,
3608 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003609 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003610 if (ret)
3611 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003612 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003613 }
Josef Bacik7b128762008-07-24 12:17:14 -04003614
Josef Bacik7b128762008-07-24 12:17:14 -04003615 /*
3616 * add this inode to the orphan list so btrfs_orphan_del does
3617 * the proper thing when we hit it
3618 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003619 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3620 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003621 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003622
Josef Bacik7b128762008-07-24 12:17:14 -04003623 /* if we have links, this was a truncate, lets do that */
3624 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303625 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003626 iput(inode);
3627 continue;
3628 }
Josef Bacik7b128762008-07-24 12:17:14 -04003629 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003630
3631 /* 1 for the orphan item deletion. */
3632 trans = btrfs_start_transaction(root, 1);
3633 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003634 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003635 ret = PTR_ERR(trans);
3636 goto out;
3637 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003638 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003639 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003640 if (ret) {
3641 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003642 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003643 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003644
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003645 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003646 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003647 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003648 } else {
3649 nr_unlink++;
3650 }
3651
3652 /* this will do delete_inode and everything for us */
3653 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003654 if (ret)
3655 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003656 }
Miao Xie3254c872011-11-10 20:45:05 -05003657 /* release the path since we're done with it */
3658 btrfs_release_path(path);
3659
Yan, Zhengd68fc572010-05-16 10:49:58 -04003660 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3661
3662 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003663 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003664 (u64)-1);
3665
Miao Xie27cdeb72014-04-02 19:51:05 +08003666 if (root->orphan_block_rsv ||
3667 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003668 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003669 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003670 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003671 }
Josef Bacik7b128762008-07-24 12:17:14 -04003672
3673 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003674 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003675 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003676 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003677
3678out:
3679 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003680 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003681 btrfs_free_path(path);
3682 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003683}
3684
Chris Masond352ac62008-09-29 15:18:18 -04003685/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003686 * very simple check to peek ahead in the leaf looking for xattrs. If we
3687 * don't find any xattrs, we know there can't be any acls.
3688 *
3689 * slot is the slot the inode is in, objectid is the objectid of the inode
3690 */
3691static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 int slot, u64 objectid,
3693 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003694{
3695 u32 nritems = btrfs_header_nritems(leaf);
3696 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003697 static u64 xattr_access = 0;
3698 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003699 int scanned = 0;
3700
Josef Bacikf23b5a52013-06-19 10:16:26 -04003701 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003702 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3703 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3704 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3705 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003706 }
3707
Chris Mason46a53cc2009-04-27 11:47:50 -04003708 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003709 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 while (slot < nritems) {
3711 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3712
3713 /* we found a different objectid, there must not be acls */
3714 if (found_key.objectid != objectid)
3715 return 0;
3716
3717 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003718 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003719 if (*first_xattr_slot == -1)
3720 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003721 if (found_key.offset == xattr_access ||
3722 found_key.offset == xattr_default)
3723 return 1;
3724 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003725
3726 /*
3727 * we found a key greater than an xattr key, there can't
3728 * be any acls later on
3729 */
3730 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3731 return 0;
3732
3733 slot++;
3734 scanned++;
3735
3736 /*
3737 * it goes inode, inode backrefs, xattrs, extents,
3738 * so if there are a ton of hard links to an inode there can
3739 * be a lot of backrefs. Don't waste time searching too hard,
3740 * this is just an optimization
3741 */
3742 if (scanned >= 8)
3743 break;
3744 }
3745 /* we hit the end of the leaf before we found an xattr or
3746 * something larger than an xattr. We have to assume the inode
3747 * has acls
3748 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003749 if (*first_xattr_slot == -1)
3750 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003751 return 1;
3752}
3753
3754/*
Chris Masond352ac62008-09-29 15:18:18 -04003755 * read an inode from the btree into the in-memory inode
3756 */
Filipe Manana67710892016-06-06 11:51:25 +01003757static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003760 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003761 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 struct btrfs_inode_item *inode_item;
3763 struct btrfs_root *root = BTRFS_I(inode)->root;
3764 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003765 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003766 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003767 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003768 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003769 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003770 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003771
3772 ret = btrfs_fill_inode(inode, &rdev);
3773 if (!ret)
3774 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003775
3776 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003777 if (!path) {
3778 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003779 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003780 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003781
Chris Mason39279cc2007-06-12 06:35:45 -04003782 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003783
Chris Mason39279cc2007-06-12 06:35:45 -04003784 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003785 if (ret) {
3786 if (ret > 0)
3787 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003789 }
Chris Mason39279cc2007-06-12 06:35:45 -04003790
Chris Mason5f39d392007-10-15 16:14:19 -04003791 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003792
3793 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003794 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003795
Chris Mason5f39d392007-10-15 16:14:19 -04003796 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3797 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003798 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003799 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003800 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3801 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003802 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003803
David Sterbaa937b972014-12-12 17:39:12 +01003804 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3805 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
David Sterbaa937b972014-12-12 17:39:12 +01003807 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3808 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003809
David Sterbaa937b972014-12-12 17:39:12 +01003810 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3811 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003812
chandan r9cc97d62012-07-04 12:48:07 +05303813 BTRFS_I(inode)->i_otime.tv_sec =
3814 btrfs_timespec_sec(leaf, &inode_item->otime);
3815 BTRFS_I(inode)->i_otime.tv_nsec =
3816 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003817
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003818 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003819 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003820 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3821
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003822 inode_set_iversion_queried(inode,
3823 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003824 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003825 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003826 rdev = btrfs_inode_rdev(leaf, inode_item);
3827
Josef Bacikaec74772008-07-24 12:12:38 -04003828 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003829 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003830
3831cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003832 /*
3833 * If we were modified in the current generation and evicted from memory
3834 * and then re-read we need to do a full sync since we don't have any
3835 * idea about which extents were modified before we were evicted from
3836 * cache.
3837 *
3838 * This is required for both inode re-read from disk and delayed inode
3839 * in delayed_nodes_tree.
3840 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003841 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003842 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3843 &BTRFS_I(inode)->runtime_flags);
3844
Filipe Mananabde6c242015-07-24 00:00:19 +01003845 /*
3846 * We don't persist the id of the transaction where an unlink operation
3847 * against the inode was last made. So here we assume the inode might
3848 * have been evicted, and therefore the exact value of last_unlink_trans
3849 * lost, and set it to last_trans to avoid metadata inconsistencies
3850 * between the inode and its parent if the inode is fsync'ed and the log
3851 * replayed. For example, in the scenario:
3852 *
3853 * touch mydir/foo
3854 * ln mydir/foo mydir/bar
3855 * sync
3856 * unlink mydir/bar
3857 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3858 * xfs_io -c fsync mydir/foo
3859 * <power failure>
3860 * mount fs, triggers fsync log replay
3861 *
3862 * We must make sure that when we fsync our inode foo we also log its
3863 * parent inode, otherwise after log replay the parent still has the
3864 * dentry with the "bar" name but our inode foo has a link count of 1
3865 * and doesn't have an inode ref with the name "bar" anymore.
3866 *
3867 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003868 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003869 * transaction commits on fsync if our inode is a directory, or if our
3870 * inode is not a directory, logging its parent unnecessarily.
3871 */
3872 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3873
Miao Xie67de1172013-12-26 13:07:06 +08003874 path->slots[0]++;
3875 if (inode->i_nlink != 1 ||
3876 path->slots[0] >= btrfs_header_nritems(leaf))
3877 goto cache_acl;
3878
3879 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003880 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003881 goto cache_acl;
3882
3883 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3884 if (location.type == BTRFS_INODE_REF_KEY) {
3885 struct btrfs_inode_ref *ref;
3886
3887 ref = (struct btrfs_inode_ref *)ptr;
3888 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3889 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3890 struct btrfs_inode_extref *extref;
3891
3892 extref = (struct btrfs_inode_extref *)ptr;
3893 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3894 extref);
3895 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003896cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003897 /*
3898 * try to precache a NULL acl entry for files that don't have
3899 * any xattrs or acls
3900 */
Li Zefan33345d012011-04-20 10:31:50 +08003901 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003902 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003903 if (first_xattr_slot != -1) {
3904 path->slots[0] = first_xattr_slot;
3905 ret = btrfs_load_inode_props(inode, path);
3906 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003907 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003908 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003909 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003910 root->root_key.objectid, ret);
3911 }
3912 btrfs_free_path(path);
3913
Al Viro72c04902009-06-24 16:58:48 -04003914 if (!maybe_acls)
3915 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003916
Chris Mason39279cc2007-06-12 06:35:45 -04003917 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003918 case S_IFREG:
3919 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003920 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003921 inode->i_fop = &btrfs_file_operations;
3922 inode->i_op = &btrfs_file_inode_operations;
3923 break;
3924 case S_IFDIR:
3925 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003926 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003927 break;
3928 case S_IFLNK:
3929 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003930 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3932 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003933 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003934 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003935 init_special_inode(inode, inode->i_mode, rdev);
3936 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003937 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003938
3939 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003940 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003941
3942make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003943 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003944 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003945 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003946}
3947
Chris Masond352ac62008-09-29 15:18:18 -04003948/*
3949 * given a leaf and an inode, copy the inode fields into the leaf
3950 */
Chris Masone02119d2008-09-05 16:13:11 -04003951static void fill_inode_item(struct btrfs_trans_handle *trans,
3952 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003953 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003954 struct inode *inode)
3955{
Liu Bo51fab692012-12-27 09:01:21 +00003956 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003957
Liu Bo51fab692012-12-27 09:01:21 +00003958 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003959
Liu Bo51fab692012-12-27 09:01:21 +00003960 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3961 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3962 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3963 &token);
3964 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3965 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003966
David Sterbaa937b972014-12-12 17:39:12 +01003967 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003968 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003969 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003970 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003971
David Sterbaa937b972014-12-12 17:39:12 +01003972 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003973 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003974 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003975 inode->i_mtime.tv_nsec, &token);
3976
David Sterbaa937b972014-12-12 17:39:12 +01003977 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003978 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003979 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003980 inode->i_ctime.tv_nsec, &token);
3981
chandan r9cc97d62012-07-04 12:48:07 +05303982 btrfs_set_token_timespec_sec(leaf, &item->otime,
3983 BTRFS_I(inode)->i_otime.tv_sec, &token);
3984 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3985 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3986
Liu Bo51fab692012-12-27 09:01:21 +00003987 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3988 &token);
3989 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3990 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003991 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3992 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003993 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3994 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3995 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3996 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003997}
3998
Chris Masond352ac62008-09-29 15:18:18 -04003999/*
4000 * copy everything in the in-memory inode into the btree.
4001 */
Chris Mason21151332011-11-10 20:39:08 -05004002static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004003 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004004{
4005 struct btrfs_inode_item *inode_item;
4006 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004007 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004008 int ret;
4009
4010 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004011 if (!path)
4012 return -ENOMEM;
4013
Chris Masonb9473432009-03-13 11:00:37 -04004014 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004015 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4016 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004017 if (ret) {
4018 if (ret > 0)
4019 ret = -ENOENT;
4020 goto failed;
4021 }
4022
Chris Mason5f39d392007-10-15 16:14:19 -04004023 leaf = path->nodes[0];
4024 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004025 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004026
Chris Masone02119d2008-09-05 16:13:11 -04004027 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004028 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004029 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004030 ret = 0;
4031failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004032 btrfs_free_path(path);
4033 return ret;
4034}
4035
Chris Masond352ac62008-09-29 15:18:18 -04004036/*
Chris Mason21151332011-11-10 20:39:08 -05004037 * copy everything in the in-memory inode into the btree.
4038 */
4039noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4040 struct btrfs_root *root, struct inode *inode)
4041{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004042 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004043 int ret;
4044
4045 /*
4046 * If the inode is a free space inode, we can deadlock during commit
4047 * if we put it into the delayed code.
4048 *
4049 * The data relocation inode should also be directly updated
4050 * without delay
4051 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004052 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004053 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004054 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004055 btrfs_update_root_times(trans, root);
4056
Chris Mason21151332011-11-10 20:39:08 -05004057 ret = btrfs_delayed_update_inode(trans, root, inode);
4058 if (!ret)
4059 btrfs_set_inode_last_trans(trans, inode);
4060 return ret;
4061 }
4062
4063 return btrfs_update_inode_item(trans, root, inode);
4064}
4065
Josef Bacikbe6aef62012-10-22 15:43:12 -04004066noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4067 struct btrfs_root *root,
4068 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004069{
4070 int ret;
4071
4072 ret = btrfs_update_inode(trans, root, inode);
4073 if (ret == -ENOSPC)
4074 return btrfs_update_inode_item(trans, root, inode);
4075 return ret;
4076}
4077
4078/*
Chris Masond352ac62008-09-29 15:18:18 -04004079 * unlink helper that gets used here in inode.c and in the tree logging
4080 * recovery code. It remove a link in a directory with a given name, and
4081 * also drops the back refs in the inode to the directory
4082 */
Al Viro92986792011-03-04 17:14:37 +00004083static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4084 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004085 struct btrfs_inode *dir,
4086 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004087 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004088{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004089 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004090 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004092 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004094 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004095 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004096 u64 ino = btrfs_ino(inode);
4097 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004098
4099 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004100 if (!path) {
4101 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004102 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004103 }
4104
Chris Masonb9473432009-03-13 11:00:37 -04004105 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004106 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004107 name, name_len, -1);
4108 if (IS_ERR(di)) {
4109 ret = PTR_ERR(di);
4110 goto err;
4111 }
4112 if (!di) {
4113 ret = -ENOENT;
4114 goto err;
4115 }
Chris Mason5f39d392007-10-15 16:14:19 -04004116 leaf = path->nodes[0];
4117 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004118 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004119 if (ret)
4120 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004121 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004122
Miao Xie67de1172013-12-26 13:07:06 +08004123 /*
4124 * If we don't have dir index, we have to get it by looking up
4125 * the inode ref, since we get the inode ref, remove it directly,
4126 * it is unnecessary to do delayed deletion.
4127 *
4128 * But if we have dir index, needn't search inode ref to get it.
4129 * Since the inode ref is close to the inode item, it is better
4130 * that we delay to delete it, and just do this deletion when
4131 * we update the inode item.
4132 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004134 ret = btrfs_delayed_delete_inode_ref(inode);
4135 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004136 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004137 goto skip_backref;
4138 }
4139 }
4140
Li Zefan33345d012011-04-20 10:31:50 +08004141 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4142 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004143 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004144 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004145 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004146 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004147 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004148 goto err;
4149 }
Miao Xie67de1172013-12-26 13:07:06 +08004150skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004151 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004153 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004154 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 }
Chris Mason39279cc2007-06-12 06:35:45 -04004156
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4158 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004160 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 goto err;
4162 }
Chris Masone02119d2008-09-05 16:13:11 -04004163
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004164 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4165 index);
Chris Mason6418c962010-10-30 07:34:24 -04004166 if (ret == -ENOENT)
4167 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004168 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004169 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004170err:
4171 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004172 if (ret)
4173 goto out;
4174
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004175 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004176 inode_inc_iversion(&inode->vfs_inode);
4177 inode_inc_iversion(&dir->vfs_inode);
4178 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4179 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4180 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004181out:
Chris Mason39279cc2007-06-12 06:35:45 -04004182 return ret;
4183}
4184
Al Viro92986792011-03-04 17:14:37 +00004185int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4186 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004187 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004188 const char *name, int name_len)
4189{
4190 int ret;
4191 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4192 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004193 drop_nlink(&inode->vfs_inode);
4194 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004195 }
4196 return ret;
4197}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004198
4199/*
4200 * helper to start transaction for unlink and rmdir.
4201 *
Josef Bacikd52be812013-05-29 14:54:47 -04004202 * unlink and rmdir are special in btrfs, they do not always free space, so
4203 * if we cannot make our reservations the normal way try and see if there is
4204 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4205 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206 */
Josef Bacikd52be812013-05-29 14:54:47 -04004207static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004209 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210
Josef Bacike70bea52011-10-11 14:18:24 -04004211 /*
4212 * 1 for the possible orphan item
4213 * 1 for the dir item
4214 * 1 for the dir index
4215 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004216 * 1 for the inode
4217 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004218 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004219}
4220
Chris Mason39279cc2007-06-12 06:35:45 -04004221static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4222{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004223 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004224 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004225 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004226 int ret;
4227
Josef Bacikd52be812013-05-29 14:54:47 -04004228 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004229 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004230 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004231
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004232 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4233 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004234
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004235 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4236 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4237 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004238 if (ret)
4239 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004240
Yan, Zhenga22285a2010-05-16 10:48:46 -04004241 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004242 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004243 if (ret)
4244 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004245 }
Josef Bacik7b128762008-07-24 12:17:14 -04004246
Tsutomu Itohb5324022011-07-19 07:27:20 +00004247out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004248 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004249 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004250 return ret;
4251}
4252
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4254 struct btrfs_root *root,
4255 struct inode *dir, u64 objectid,
4256 const char *name, int name_len)
4257{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004258 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259 struct btrfs_path *path;
4260 struct extent_buffer *leaf;
4261 struct btrfs_dir_item *di;
4262 struct btrfs_key key;
4263 u64 index;
4264 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004265 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004266
4267 path = btrfs_alloc_path();
4268 if (!path)
4269 return -ENOMEM;
4270
Li Zefan33345d012011-04-20 10:31:50 +08004271 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004272 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (IS_ERR_OR_NULL(di)) {
4274 if (!di)
4275 ret = -ENOENT;
4276 else
4277 ret = PTR_ERR(di);
4278 goto out;
4279 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280
4281 leaf = path->nodes[0];
4282 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4283 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4284 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004285 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004286 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 goto out;
4288 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004289 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004291 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4292 root->root_key.objectid, dir_ino,
4293 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004295 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004296 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 goto out;
4298 }
Li Zefan33345d012011-04-20 10:31:50 +08004299 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004300 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 if (IS_ERR_OR_NULL(di)) {
4302 if (!di)
4303 ret = -ENOENT;
4304 else
4305 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004306 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 goto out;
4308 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004309
4310 leaf = path->nodes[0];
4311 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004312 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313 index = key.offset;
4314 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004315 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004316
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004317 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004318 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004319 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004320 goto out;
4321 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004322
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004323 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004324 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004325 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004326 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004327 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004328 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004330 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004331 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004332}
4333
Chris Mason39279cc2007-06-12 06:35:45 -04004334static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4335{
David Howells2b0143b2015-03-17 22:25:59 +00004336 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004337 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004338 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004339 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004340 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004341
David Sterbab3ae2442012-09-13 16:04:34 -06004342 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004343 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004344 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004345 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004346
Josef Bacikd52be812013-05-29 14:54:47 -04004347 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004348 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004349 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004350
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004351 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004352 err = btrfs_unlink_subvol(trans, root, dir,
4353 BTRFS_I(inode)->location.objectid,
4354 dentry->d_name.name,
4355 dentry->d_name.len);
4356 goto out;
4357 }
4358
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004359 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004360 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004361 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004362
Filipe Manana44f714d2016-06-06 16:11:13 +01004363 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4364
Chris Mason39279cc2007-06-12 06:35:45 -04004365 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004366 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4367 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4368 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004369 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004370 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004371 /*
4372 * Propagate the last_unlink_trans value of the deleted dir to
4373 * its parent directory. This is to prevent an unrecoverable
4374 * log tree in the case we do something like this:
4375 * 1) create dir foo
4376 * 2) create snapshot under dir foo
4377 * 3) delete the snapshot
4378 * 4) rmdir foo
4379 * 5) mkdir foo
4380 * 6) fsync foo or some file inside foo
4381 */
4382 if (last_unlink_trans >= trans->transid)
4383 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4384 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004385out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004386 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004387 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004388
Chris Mason39279cc2007-06-12 06:35:45 -04004389 return err;
4390}
4391
Chris Mason28f75a02015-02-04 06:59:29 -08004392static int truncate_space_check(struct btrfs_trans_handle *trans,
4393 struct btrfs_root *root,
4394 u64 bytes_deleted)
4395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004396 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004397 int ret;
4398
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004399 /*
4400 * This is only used to apply pressure to the enospc system, we don't
4401 * intend to use this reservation at all.
4402 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004403 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004404 bytes_deleted *= fs_info->nodesize;
4405 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004406 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004407 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004408 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004409 trans->transid,
4410 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004411 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004412 }
Chris Mason28f75a02015-02-04 06:59:29 -08004413 return ret;
4414
4415}
4416
Josef Bacikddfae632017-10-19 14:16:02 -04004417/*
4418 * Return this if we need to call truncate_block for the last bit of the
4419 * truncate.
4420 */
4421#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004422
Chris Mason323ac952008-10-01 19:05:46 -04004423/*
Chris Mason39279cc2007-06-12 06:35:45 -04004424 * this can truncate away extent items, csum items and directory items.
4425 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004426 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004427 *
4428 * csum items that cross the new i_size are truncated to the new size
4429 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004430 *
4431 * min_type is the minimum key type to truncate down to. If set to 0, this
4432 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004433 */
Yan, Zheng80825102009-11-12 09:35:36 +00004434int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4435 struct btrfs_root *root,
4436 struct inode *inode,
4437 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004439 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004440 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004441 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004442 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004443 struct btrfs_key key;
4444 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004446 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004447 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004448 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004449 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004450 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004451 int found_extent;
4452 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004453 int pending_del_nr = 0;
4454 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004455 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004456 int ret;
4457 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004458 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004459 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004460 bool be_nice = false;
4461 bool should_throttle = false;
4462 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004463
4464 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004465
Chris Mason28ed1342014-12-17 09:41:04 -08004466 /*
4467 * for non-free space inodes and ref cows, we want to back off from
4468 * time to time
4469 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004470 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004471 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004472 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004473
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004474 path = btrfs_alloc_path();
4475 if (!path)
4476 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004477 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004478
Josef Bacik5dc562c2012-08-17 13:14:17 -04004479 /*
4480 * We want to drop from the next block forward in case this new size is
4481 * not block aligned since we will be keeping the last block of the
4482 * extent just the way it is.
4483 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004484 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004485 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004486 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004488 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004489
Miao Xie16cdcec2011-04-22 18:12:22 +08004490 /*
4491 * This function is also used to drop the items in the log tree before
4492 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4493 * it is used to drop the loged items. So we shouldn't kill the delayed
4494 * items.
4495 */
4496 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004497 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004498
Li Zefan33345d012011-04-20 10:31:50 +08004499 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004501 key.type = (u8)-1;
4502
Chris Mason85e21ba2008-01-29 15:11:36 -05004503search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004504 /*
4505 * with a 16K leaf size and 128MB extents, you can actually queue
4506 * up a huge file in a single leaf. Most of the time that
4507 * bytes_deleted is > 0, it will be huge by the time we get here
4508 */
Byongho Leeee221842015-12-15 01:42:10 +09004509 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004510 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004511 err = -EAGAIN;
4512 goto error;
4513 }
4514 }
4515
4516
Chris Masonb9473432009-03-13 11:00:37 -04004517 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004519 if (ret < 0) {
4520 err = ret;
4521 goto out;
4522 }
Chris Masond3977122009-01-05 21:25:51 -05004523
Chris Mason85e21ba2008-01-29 15:11:36 -05004524 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004525 /* there are no items in the tree for us to truncate, we're
4526 * done
4527 */
Yan, Zheng80825102009-11-12 09:35:36 +00004528 if (path->slots[0] == 0)
4529 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004530 path->slots[0]--;
4531 }
4532
Chris Masond3977122009-01-05 21:25:51 -05004533 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004534 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004535 leaf = path->nodes[0];
4536 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004537 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004538
Li Zefan33345d012011-04-20 10:31:50 +08004539 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004540 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004541
Chris Mason85e21ba2008-01-29 15:11:36 -05004542 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004543 break;
4544
Chris Mason5f39d392007-10-15 16:14:19 -04004545 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004546 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004547 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004548 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004549 extent_type = btrfs_file_extent_type(leaf, fi);
4550 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004551 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004552 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004553
4554 trace_btrfs_truncate_show_fi_regular(
4555 BTRFS_I(inode), leaf, fi,
4556 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004557 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004558 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004559 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004560
4561 trace_btrfs_truncate_show_fi_inline(
4562 BTRFS_I(inode), leaf, fi, path->slots[0],
4563 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004564 }
Yan008630c2007-11-07 13:31:09 -05004565 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004566 }
Yan, Zheng80825102009-11-12 09:35:36 +00004567 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004568 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004569 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004570 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004571 break;
4572 if (found_key.offset >= new_size)
4573 del_item = 1;
4574 else
4575 del_item = 0;
4576 }
Chris Mason39279cc2007-06-12 06:35:45 -04004577 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004578 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004579 if (found_type != BTRFS_EXTENT_DATA_KEY)
4580 goto delete;
4581
4582 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004583 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004584 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004585 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004586 u64 orig_num_bytes =
4587 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004588 extent_num_bytes = ALIGN(new_size -
4589 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004590 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004591 btrfs_set_file_extent_num_bytes(leaf, fi,
4592 extent_num_bytes);
4593 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004594 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004595 if (test_bit(BTRFS_ROOT_REF_COWS,
4596 &root->state) &&
4597 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004598 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004599 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004600 } else {
Chris Masondb945352007-10-15 16:15:53 -04004601 extent_num_bytes =
4602 btrfs_file_extent_disk_num_bytes(leaf,
4603 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004604 extent_offset = found_key.offset -
4605 btrfs_file_extent_offset(leaf, fi);
4606
Chris Mason39279cc2007-06-12 06:35:45 -04004607 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004608 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004609 if (extent_start != 0) {
4610 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004611 if (test_bit(BTRFS_ROOT_REF_COWS,
4612 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004613 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004614 }
4615 }
Chris Mason90692182008-02-08 13:49:28 -05004616 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004617 /*
4618 * we can't truncate inline items that have had
4619 * special encodings
4620 */
4621 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004622 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004623 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4624 btrfs_file_extent_compression(leaf, fi) == 0) {
4625 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004626
Josef Bacikddfae632017-10-19 14:16:02 -04004627 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4628 size = btrfs_file_extent_calc_inline_size(size);
4629 btrfs_truncate_item(root->fs_info, path, size, 1);
4630 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004631 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004632 * We have to bail so the last_size is set to
4633 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004634 */
Josef Bacikddfae632017-10-19 14:16:02 -04004635 err = NEED_TRUNCATE_BLOCK;
4636 break;
Chris Mason90692182008-02-08 13:49:28 -05004637 }
Josef Bacikddfae632017-10-19 14:16:02 -04004638
4639 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4640 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004641 }
Chris Mason179e29e2007-11-01 11:28:41 -04004642delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004643 if (del_item)
4644 last_size = found_key.offset;
4645 else
4646 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004647 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004648 if (!pending_del_nr) {
4649 /* no pending yet, add ourselves */
4650 pending_del_slot = path->slots[0];
4651 pending_del_nr = 1;
4652 } else if (pending_del_nr &&
4653 path->slots[0] + 1 == pending_del_slot) {
4654 /* hop on the pending chunk */
4655 pending_del_nr++;
4656 pending_del_slot = path->slots[0];
4657 } else {
Chris Masond3977122009-01-05 21:25:51 -05004658 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004659 }
Chris Mason39279cc2007-06-12 06:35:45 -04004660 } else {
4661 break;
4662 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004663 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004664
Miao Xie27cdeb72014-04-02 19:51:05 +08004665 if (found_extent &&
4666 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004668 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004669 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004670 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004671 extent_num_bytes, 0,
4672 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004673 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004674 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004675 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4676 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004677 trans->delayed_ref_updates * 2,
4678 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004679 if (be_nice) {
4680 if (truncate_space_check(trans, root,
4681 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004682 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004683 }
4684 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004685 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004686 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004687 }
Chris Mason39279cc2007-06-12 06:35:45 -04004688 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004689
Yan, Zheng80825102009-11-12 09:35:36 +00004690 if (found_type == BTRFS_INODE_ITEM_KEY)
4691 break;
4692
4693 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004694 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004695 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004696 if (pending_del_nr) {
4697 ret = btrfs_del_items(trans, root, path,
4698 pending_del_slot,
4699 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004700 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004701 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004702 goto error;
4703 }
Yan, Zheng80825102009-11-12 09:35:36 +00004704 pending_del_nr = 0;
4705 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004706 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004707 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004708 unsigned long updates = trans->delayed_ref_updates;
4709 if (updates) {
4710 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004711 ret = btrfs_run_delayed_refs(trans,
4712 fs_info,
4713 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004714 if (ret && !err)
4715 err = ret;
4716 }
4717 }
Chris Mason28f75a02015-02-04 06:59:29 -08004718 /*
4719 * if we failed to refill our space rsv, bail out
4720 * and let the transaction restart
4721 */
4722 if (should_end) {
4723 err = -EAGAIN;
4724 goto error;
4725 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004726 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004727 } else {
4728 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004729 }
Chris Mason39279cc2007-06-12 06:35:45 -04004730 }
Yan, Zheng80825102009-11-12 09:35:36 +00004731out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004732 if (pending_del_nr) {
4733 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4734 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004735 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004736 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004737 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004738error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004739 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4740 ASSERT(last_size >= new_size);
4741 if (!err && last_size > new_size)
4742 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004743 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004744 }
Chris Mason28ed1342014-12-17 09:41:04 -08004745
Chris Mason39279cc2007-06-12 06:35:45 -04004746 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004747
Byongho Leeee221842015-12-15 01:42:10 +09004748 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004749 unsigned long updates = trans->delayed_ref_updates;
4750 if (updates) {
4751 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004752 ret = btrfs_run_delayed_refs(trans, fs_info,
4753 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004754 if (ret && !err)
4755 err = ret;
4756 }
4757 }
Yan, Zheng80825102009-11-12 09:35:36 +00004758 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004759}
4760
Chris Masona52d9a82007-08-27 16:49:44 -04004761/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004763 * @inode - inode that we're zeroing
4764 * @from - the offset to start zeroing
4765 * @len - the length to zero, 0 to zero the entire range respective to the
4766 * offset
4767 * @front - zero up to the offset instead of from the offset on
4768 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004770 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004771 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304772int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004773 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004774{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004775 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004776 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004777 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4778 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004779 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004780 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004782 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004783 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304784 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004785 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004786 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004787 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 u64 block_start;
4789 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004790
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004791 if (IS_ALIGNED(offset, blocksize) &&
4792 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004793 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304794
Josef Bacik8b62f872017-10-19 14:15:55 -04004795 block_start = round_down(from, blocksize);
4796 block_end = block_start + blocksize - 1;
4797
Qu Wenruo364ecf32017-02-27 15:10:38 +08004798 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004799 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004800 if (ret)
4801 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004802
Chris Mason211c17f2008-05-15 09:13:45 -04004803again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004804 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004805 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004806 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004807 block_start, blocksize);
4808 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004809 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004810 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004811 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004812
Chris Masona52d9a82007-08-27 16:49:44 -04004813 if (!PageUptodate(page)) {
4814 ret = btrfs_readpage(NULL, page);
4815 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004816 if (page->mapping != mapping) {
4817 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004818 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004819 goto again;
4820 }
Chris Masona52d9a82007-08-27 16:49:44 -04004821 if (!PageUptodate(page)) {
4822 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004823 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004824 }
4825 }
Chris Mason211c17f2008-05-15 09:13:45 -04004826 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004827
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 set_page_extent_mapped(page);
4830
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004834 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004835 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004836 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004837 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004838 btrfs_put_ordered_extent(ordered);
4839 goto again;
4840 }
4841
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304842 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004843 EXTENT_DIRTY | EXTENT_DELALLOC |
4844 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004845 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004846
Filipe Mananae3b8a482017-11-04 00:16:59 +00004847 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004848 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004849 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304850 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004851 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004852 goto out_unlock;
4853 }
4854
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004856 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004858 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004859 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304860 memset(kaddr + (block_start - page_offset(page)),
4861 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004862 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304863 memset(kaddr + (block_start - page_offset(page)) + offset,
4864 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004865 flush_dcache_page(page);
4866 kunmap(page);
4867 }
Chris Mason247e7432008-07-17 12:53:51 -04004868 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004869 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004870 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004871
Chris Mason89642222008-07-24 09:41:53 -04004872out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004873 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004874 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304875 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004876 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004877 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004878 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004879out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004880 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004881 return ret;
4882}
4883
Josef Bacik16e75492013-10-22 12:18:51 -04004884static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4885 u64 offset, u64 len)
4886{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004888 struct btrfs_trans_handle *trans;
4889 int ret;
4890
4891 /*
4892 * Still need to make sure the inode looks like it's been updated so
4893 * that any holes get logged if we fsync.
4894 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004895 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4896 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004897 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4898 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4899 return 0;
4900 }
4901
4902 /*
4903 * 1 - for the one we're dropping
4904 * 1 - for the one we're adding
4905 * 1 - for updating the inode.
4906 */
4907 trans = btrfs_start_transaction(root, 3);
4908 if (IS_ERR(trans))
4909 return PTR_ERR(trans);
4910
4911 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4912 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004913 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004914 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004915 return ret;
4916 }
4917
David Sterbaf85b7372017-01-20 14:54:07 +01004918 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4919 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004920 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004921 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004922 else
4923 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004924 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004925 return ret;
4926}
4927
Josef Bacik695a0d02011-03-04 15:46:53 -05004928/*
4929 * This function puts in dummy file extents for the area we're creating a hole
4930 * for. So if we are truncating this file to a larger size we need to insert
4931 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4932 * the range between oldsize and size
4933 */
Josef Bacika41ad392011-01-31 15:30:16 -05004934int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004937 struct btrfs_root *root = BTRFS_I(inode)->root;
4938 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004939 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004940 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004941 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004942 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4943 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004944 u64 last_byte;
4945 u64 cur_offset;
4946 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004947 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004948
Josef Bacika71754f2013-06-17 17:14:39 -04004949 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304950 * If our size started in the middle of a block we need to zero out the
4951 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004952 * expose stale data.
4953 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304954 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004955 if (err)
4956 return err;
4957
Yan Zheng9036c102008-10-30 14:19:41 -04004958 if (size <= hole_start)
4959 return 0;
4960
Yan Zheng9036c102008-10-30 14:19:41 -04004961 while (1) {
4962 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004963
David Sterbaff13db42015-12-03 14:30:40 +01004964 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004965 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004966 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004967 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004968 if (!ordered)
4969 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004970 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004971 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004972 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004973 btrfs_put_ordered_extent(ordered);
4974 }
4975
Yan Zheng9036c102008-10-30 14:19:41 -04004976 cur_offset = hole_start;
4977 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004978 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004979 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004980 if (IS_ERR(em)) {
4981 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004982 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004983 break;
4984 }
Yan Zheng9036c102008-10-30 14:19:41 -04004985 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004986 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004987 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004988 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004989 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004990
Josef Bacik16e75492013-10-22 12:18:51 -04004991 err = maybe_insert_hole(root, inode, cur_offset,
4992 hole_size);
4993 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004994 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004995 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004996 cur_offset + hole_size - 1, 0);
4997 hole_em = alloc_extent_map();
4998 if (!hole_em) {
4999 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5000 &BTRFS_I(inode)->runtime_flags);
5001 goto next;
5002 }
5003 hole_em->start = cur_offset;
5004 hole_em->len = hole_size;
5005 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005006
Josef Bacik5dc562c2012-08-17 13:14:17 -04005007 hole_em->block_start = EXTENT_MAP_HOLE;
5008 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005009 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005010 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005011 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005012 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005013 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005014
5015 while (1) {
5016 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005017 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005018 write_unlock(&em_tree->lock);
5019 if (err != -EEXIST)
5020 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005021 btrfs_drop_extent_cache(BTRFS_I(inode),
5022 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005023 cur_offset +
5024 hole_size - 1, 0);
5025 }
5026 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005027 }
Josef Bacik16e75492013-10-22 12:18:51 -04005028next:
Yan Zheng9036c102008-10-30 14:19:41 -04005029 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005030 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005031 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005032 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005033 break;
5034 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005035 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005036 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005037 return err;
5038}
5039
Eric Sandeen3972f262013-01-12 02:57:22 +00005040static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005041{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005042 struct btrfs_root *root = BTRFS_I(inode)->root;
5043 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005044 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005045 loff_t newsize = attr->ia_size;
5046 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005047 int ret;
5048
Eric Sandeen3972f262013-01-12 02:57:22 +00005049 /*
5050 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5051 * special case where we need to update the times despite not having
5052 * these flags set. For all other operations the VFS set these flags
5053 * explicitly if it wants a timestamp update.
5054 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005055 if (newsize != oldsize) {
5056 inode_inc_iversion(inode);
5057 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5058 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005059 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005060 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005061
Josef Bacika41ad392011-01-31 15:30:16 -05005062 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005063 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005064 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005065 * This is to ensure the snapshot captures a fully consistent
5066 * state of this file - if the snapshot captures this expanding
5067 * truncation, it must capture all writes that happened before
5068 * this truncation.
5069 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005070 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005071 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005072 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005073 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005074 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005075 }
Yan, Zheng80825102009-11-12 09:35:36 +00005076
Miao Xief4a2f4c2011-12-14 20:12:01 -05005077 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005078 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005079 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005080 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005081 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005082
5083 i_size_write(inode, newsize);
5084 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305085 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005086 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005087 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005088 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005089 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005090
Josef Bacika41ad392011-01-31 15:30:16 -05005091 /*
5092 * We're truncating a file that used to have good data down to
5093 * zero. Make sure it gets into the ordered flush list so that
5094 * any new writes get down to disk quickly.
5095 */
5096 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005097 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5098 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005099
Josef Bacikf3fe8202013-01-07 17:03:21 -05005100 /*
5101 * 1 for the orphan item we're going to add
5102 * 1 for the orphan item deletion.
5103 */
5104 trans = btrfs_start_transaction(root, 2);
5105 if (IS_ERR(trans))
5106 return PTR_ERR(trans);
5107
5108 /*
5109 * We need to do this in case we fail at _any_ point during the
5110 * actual truncate. Once we do the truncate_setsize we could
5111 * invalidate pages which forces any outstanding ordered io to
5112 * be instantly completed which will give us extents that need
5113 * to be truncated. If we fail to get an orphan inode down we
5114 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005115 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005116 * will be consistent.
5117 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005118 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005119 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005120 if (ret)
5121 return ret;
5122
Josef Bacika41ad392011-01-31 15:30:16 -05005123 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5124 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005125
5126 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005127 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005128 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005129 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005130
Josef Bacika41ad392011-01-31 15:30:16 -05005131 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005132 if (ret && inode->i_nlink) {
5133 int err;
5134
Liu Bo19fd2df2016-12-01 13:01:02 -08005135 /* To get a stable disk_i_size */
5136 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5137 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005138 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005139 return err;
5140 }
5141
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005142 /*
5143 * failed to truncate, disk_i_size is only adjusted down
5144 * as we remove extents, so it should represent the true
5145 * size of the inode, so reset the in memory size and
5146 * delete our orphan entry.
5147 */
5148 trans = btrfs_join_transaction(root);
5149 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005150 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005151 return ret;
5152 }
5153 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005154 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005155 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005156 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005157 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005158 }
Yan, Zheng80825102009-11-12 09:35:36 +00005159 }
5160
Josef Bacika41ad392011-01-31 15:30:16 -05005161 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005162}
5163
Chris Mason39279cc2007-06-12 06:35:45 -04005164static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5165{
David Howells2b0143b2015-03-17 22:25:59 +00005166 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005167 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005168 int err;
5169
Li Zefanb83cc962010-12-20 16:04:08 +08005170 if (btrfs_root_readonly(root))
5171 return -EROFS;
5172
Jan Kara31051c82016-05-26 16:55:18 +02005173 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005174 if (err)
5175 return err;
5176
Chris Mason5a3f23d2009-03-31 13:27:11 -04005177 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005178 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005179 if (err)
5180 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005181 }
Yan Zheng9036c102008-10-30 14:19:41 -04005182
Christoph Hellwig10257742010-06-04 11:30:02 +02005183 if (attr->ia_valid) {
5184 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005185 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005186 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005187
Josef Bacik22c44fe2011-11-30 10:45:38 -05005188 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005189 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005190 }
5191
Chris Mason39279cc2007-06-12 06:35:45 -04005192 return err;
5193}
Chris Mason61295eb2008-01-14 16:24:38 -05005194
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005195/*
5196 * While truncating the inode pages during eviction, we get the VFS calling
5197 * btrfs_invalidatepage() against each page of the inode. This is slow because
5198 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5199 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5200 * extent_state structures over and over, wasting lots of time.
5201 *
5202 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5203 * those expensive operations on a per page basis and do only the ordered io
5204 * finishing, while we release here the extent_map and extent_state structures,
5205 * without the excessive merging and splitting.
5206 */
5207static void evict_inode_truncate_pages(struct inode *inode)
5208{
5209 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5210 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5211 struct rb_node *node;
5212
5213 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005214 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005215
5216 write_lock(&map_tree->lock);
5217 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5218 struct extent_map *em;
5219
5220 node = rb_first(&map_tree->map);
5221 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005222 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5223 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005224 remove_extent_mapping(map_tree, em);
5225 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005226 if (need_resched()) {
5227 write_unlock(&map_tree->lock);
5228 cond_resched();
5229 write_lock(&map_tree->lock);
5230 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005231 }
5232 write_unlock(&map_tree->lock);
5233
Filipe Manana6ca07092015-05-26 00:55:42 +01005234 /*
5235 * Keep looping until we have no more ranges in the io tree.
5236 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005237 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5238 * still in progress (unlocked the pages in the bio but did not yet
5239 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 * ranges can still be locked and eviction started because before
5241 * submitting those bios, which are executed by a separate task (work
5242 * queue kthread), inode references (inode->i_count) were not taken
5243 * (which would be dropped in the end io callback of each bio).
5244 * Therefore here we effectively end up waiting for those bios and
5245 * anyone else holding locked ranges without having bumped the inode's
5246 * reference count - if we don't do it, when they access the inode's
5247 * io_tree to unlock a range it may be too late, leading to an
5248 * use-after-free issue.
5249 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005250 spin_lock(&io_tree->lock);
5251 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5252 struct extent_state *state;
5253 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005254 u64 start;
5255 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005256
5257 node = rb_first(&io_tree->state);
5258 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005259 start = state->start;
5260 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005261 spin_unlock(&io_tree->lock);
5262
David Sterbaff13db42015-12-03 14:30:40 +01005263 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005264
5265 /*
5266 * If still has DELALLOC flag, the extent didn't reach disk,
5267 * and its reserved space won't be freed by delayed_ref.
5268 * So we need to free its reserved space here.
5269 * (Refer to comment in btrfs_invalidatepage, case 2)
5270 *
5271 * Note, end is the bytenr of last byte, so we need + 1 here.
5272 */
5273 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005274 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005275
Filipe Manana6ca07092015-05-26 00:55:42 +01005276 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005277 EXTENT_LOCKED | EXTENT_DIRTY |
5278 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005279 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005280
Filipe Manana7064dd52014-08-08 02:47:05 +01005281 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005282 spin_lock(&io_tree->lock);
5283 }
5284 spin_unlock(&io_tree->lock);
5285}
5286
Al Virobd555972010-06-07 11:35:40 -04005287void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005288{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005289 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005290 struct btrfs_trans_handle *trans;
5291 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005292 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005293 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005294 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005295 int ret;
5296
liubo1abe9b82011-03-24 11:18:59 +00005297 trace_btrfs_inode_evict(inode);
5298
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005299 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005300 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005301 return;
5302 }
5303
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005304 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005305
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005306 evict_inode_truncate_pages(inode);
5307
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005308 if (inode->i_nlink &&
5309 ((btrfs_root_refs(&root->root_item) != 0 &&
5310 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005311 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005312 goto no_delete;
5313
Chris Mason39279cc2007-06-12 06:35:45 -04005314 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005315 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005316 goto no_delete;
5317 }
Al Virobd555972010-06-07 11:35:40 -04005318 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005319 if (!special_file(inode->i_mode))
5320 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005321
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005322 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005323
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005324 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005325 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005326 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005327 goto no_delete;
5328 }
5329
Yan, Zheng76dda932009-09-21 16:00:26 -04005330 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005331 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5332 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005333 goto no_delete;
5334 }
5335
Nikolay Borisovaa790212017-01-10 20:35:40 +02005336 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005337 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005338 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005339 goto no_delete;
5340 }
5341
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005342 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005343 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005344 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005345 goto no_delete;
5346 }
Josef Bacik4a338542011-08-29 11:01:31 -04005347 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005348 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005349 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005350
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005351 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005352
Josef Bacik4289a662011-08-05 13:22:24 -04005353 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005354 * This is a bit simpler than btrfs_truncate since we've already
5355 * reserved our space for our orphan item in the unlink, so we just
5356 * need to reserve some slack space in case we add bytes and update
5357 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005358 */
Yan, Zheng80825102009-11-12 09:35:36 +00005359 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005360 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5361 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005362
Josef Bacik726c35f2011-09-26 15:46:06 -04005363 /*
5364 * Try and steal from the global reserve since we will
5365 * likely not use this space anyway, we want to try as
5366 * hard as possible to get this to work.
5367 */
5368 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005369 steal_from_global++;
5370 else
5371 steal_from_global = 0;
5372 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005373
Josef Bacik3bce8762015-02-24 12:35:51 -08005374 /*
5375 * steal_from_global == 0: we reserved stuff, hooray!
5376 * steal_from_global == 1: we didn't reserve stuff, boo!
5377 * steal_from_global == 2: we've committed, still not a lot of
5378 * room but maybe we'll have room in the global reserve this
5379 * time.
5380 * steal_from_global == 3: abandon all hope!
5381 */
5382 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005383 btrfs_warn(fs_info,
5384 "Could not get space for a delete, will truncate on mount %d",
5385 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005386 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005387 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005388 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005389 }
5390
Miao Xie0e8c36a2012-12-19 06:59:51 +00005391 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005392 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005393 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005394 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005395 goto no_delete;
5396 }
5397
Josef Bacik3bce8762015-02-24 12:35:51 -08005398 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005399 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005400 * sure there is room to do it, if not we need to commit and try
5401 * again.
5402 */
5403 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005404 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005405 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005406 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005407 else
5408 ret = -ENOSPC;
5409 }
5410
5411 /*
5412 * Couldn't steal from the global reserve, we have too much
5413 * pending stuff built up, commit the transaction and try it
5414 * again.
5415 */
5416 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005417 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005418 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005419 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005420 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005421 goto no_delete;
5422 }
5423 continue;
5424 } else {
5425 steal_from_global = 0;
5426 }
5427
Josef Bacik4289a662011-08-05 13:22:24 -04005428 trans->block_rsv = rsv;
5429
Yan, Zhengd68fc572010-05-16 10:49:58 -04005430 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005431 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005432 break;
5433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005434 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005435 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005436 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005437 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005438 }
5439
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005440 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005441
Josef Bacik4ef31a42013-08-13 14:10:08 -04005442 /*
5443 * Errors here aren't a big deal, it just means we leave orphan items
5444 * in the tree. They will be cleaned up on the next mount.
5445 */
Yan, Zheng80825102009-11-12 09:35:36 +00005446 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005447 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005448 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005449 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005450 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005451 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005452
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005453 trans->block_rsv = &fs_info->trans_block_rsv;
5454 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005455 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005456 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005457
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005458 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005459 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005460no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005461 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005462 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005463}
5464
5465/*
5466 * this returns the key found in the dir entry in the location pointer.
5467 * If no dir entries were found, location->objectid is 0.
5468 */
5469static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5470 struct btrfs_key *location)
5471{
5472 const char *name = dentry->d_name.name;
5473 int namelen = dentry->d_name.len;
5474 struct btrfs_dir_item *di;
5475 struct btrfs_path *path;
5476 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005477 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005478
5479 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005480 if (!path)
5481 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005482
David Sterbaf85b7372017-01-20 14:54:07 +01005483 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5484 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005485 if (IS_ERR(di))
5486 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005487
David Sterbac7040052011-04-19 18:00:01 +02005488 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005489 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005490
Chris Mason5f39d392007-10-15 16:14:19 -04005491 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005492 if (location->type != BTRFS_INODE_ITEM_KEY &&
5493 location->type != BTRFS_ROOT_ITEM_KEY) {
5494 btrfs_warn(root->fs_info,
5495"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5496 __func__, name, btrfs_ino(BTRFS_I(dir)),
5497 location->objectid, location->type, location->offset);
5498 goto out_err;
5499 }
Chris Mason39279cc2007-06-12 06:35:45 -04005500out:
Chris Mason39279cc2007-06-12 06:35:45 -04005501 btrfs_free_path(path);
5502 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005503out_err:
5504 location->objectid = 0;
5505 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005506}
5507
5508/*
5509 * when we hit a tree root in a directory, the btrfs part of the inode
5510 * needs to be changed to reflect the root directory of the tree root. This
5511 * is kind of like crossing a mount point.
5512 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005513static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 struct inode *dir,
5515 struct dentry *dentry,
5516 struct btrfs_key *location,
5517 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005518{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 struct btrfs_path *path;
5520 struct btrfs_root *new_root;
5521 struct btrfs_root_ref *ref;
5522 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005523 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 int ret;
5525 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005526
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 path = btrfs_alloc_path();
5528 if (!path) {
5529 err = -ENOMEM;
5530 goto out;
5531 }
Chris Mason39279cc2007-06-12 06:35:45 -04005532
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005533 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005534 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5535 key.type = BTRFS_ROOT_REF_KEY;
5536 key.offset = location->objectid;
5537
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005538 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 if (ret) {
5540 if (ret < 0)
5541 err = ret;
5542 goto out;
5543 }
Chris Mason39279cc2007-06-12 06:35:45 -04005544
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 leaf = path->nodes[0];
5546 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005547 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5549 goto out;
5550
5551 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5552 (unsigned long)(ref + 1),
5553 dentry->d_name.len);
5554 if (ret)
5555 goto out;
5556
David Sterbab3b4aa72011-04-21 01:20:15 +02005557 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005558
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005559 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005560 if (IS_ERR(new_root)) {
5561 err = PTR_ERR(new_root);
5562 goto out;
5563 }
5564
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565 *sub_root = new_root;
5566 location->objectid = btrfs_root_dirid(&new_root->root_item);
5567 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005568 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005569 err = 0;
5570out:
5571 btrfs_free_path(path);
5572 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005573}
5574
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575static void inode_tree_add(struct inode *inode)
5576{
5577 struct btrfs_root *root = BTRFS_I(inode)->root;
5578 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005579 struct rb_node **p;
5580 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005581 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005582 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583
Al Viro1d3382cb2010-10-23 15:19:20 -04005584 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005585 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005586 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005588 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 while (*p) {
5590 parent = *p;
5591 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5592
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005593 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005594 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005595 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005596 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597 else {
5598 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005599 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005600 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005601 RB_CLEAR_NODE(parent);
5602 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005603 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 }
5605 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005606 rb_link_node(new, parent, p);
5607 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608 spin_unlock(&root->inode_lock);
5609}
5610
5611static void inode_tree_del(struct inode *inode)
5612{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005613 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005617 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005621 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005623 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005624
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005625 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005626 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005627 spin_lock(&root->inode_lock);
5628 empty = RB_EMPTY_ROOT(&root->inode_tree);
5629 spin_unlock(&root->inode_lock);
5630 if (empty)
5631 btrfs_add_dead_root(root);
5632 }
5633}
5634
Jeff Mahoney143bede2012-03-01 14:56:26 +01005635void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005636{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005637 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005638 struct rb_node *node;
5639 struct rb_node *prev;
5640 struct btrfs_inode *entry;
5641 struct inode *inode;
5642 u64 objectid = 0;
5643
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005644 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005645 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005646
5647 spin_lock(&root->inode_lock);
5648again:
5649 node = root->inode_tree.rb_node;
5650 prev = NULL;
5651 while (node) {
5652 prev = node;
5653 entry = rb_entry(node, struct btrfs_inode, rb_node);
5654
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005655 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005657 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005658 node = node->rb_right;
5659 else
5660 break;
5661 }
5662 if (!node) {
5663 while (prev) {
5664 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005665 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005666 node = prev;
5667 break;
5668 }
5669 prev = rb_next(prev);
5670 }
5671 }
5672 while (node) {
5673 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005674 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005675 inode = igrab(&entry->vfs_inode);
5676 if (inode) {
5677 spin_unlock(&root->inode_lock);
5678 if (atomic_read(&inode->i_count) > 1)
5679 d_prune_aliases(inode);
5680 /*
Al Viro45321ac2010-06-07 13:43:19 -04005681 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005682 * the inode cache when its usage count
5683 * hits zero.
5684 */
5685 iput(inode);
5686 cond_resched();
5687 spin_lock(&root->inode_lock);
5688 goto again;
5689 }
5690
5691 if (cond_resched_lock(&root->inode_lock))
5692 goto again;
5693
5694 node = rb_next(node);
5695 }
5696 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697}
5698
Chris Masone02119d2008-09-05 16:13:11 -04005699static int btrfs_init_locked_inode(struct inode *inode, void *p)
5700{
5701 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005702 inode->i_ino = args->location->objectid;
5703 memcpy(&BTRFS_I(inode)->location, args->location,
5704 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005705 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 return 0;
5707}
5708
5709static int btrfs_find_actor(struct inode *inode, void *opaque)
5710{
5711 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005712 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005713 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005714}
5715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005716static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005717 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005718 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005719{
5720 struct inode *inode;
5721 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005722 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005723
Chris Mason90d3e592014-01-09 17:28:00 -08005724 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005725 args.root = root;
5726
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005727 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005728 btrfs_init_locked_inode,
5729 (void *)&args);
5730 return inode;
5731}
5732
Balaji Rao1a54ef82008-07-21 02:01:04 +05305733/* Get an inode object given its location and corresponding root.
5734 * Returns in *is_new if the inode was read from disk
5735 */
5736struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005737 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305738{
5739 struct inode *inode;
5740
Chris Mason90d3e592014-01-09 17:28:00 -08005741 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305742 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005743 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305744
5745 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005746 int ret;
5747
5748 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005749 if (!is_bad_inode(inode)) {
5750 inode_tree_add(inode);
5751 unlock_new_inode(inode);
5752 if (new)
5753 *new = 1;
5754 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005755 unlock_new_inode(inode);
5756 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005757 ASSERT(ret < 0);
5758 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005759 }
5760 }
5761
Balaji Rao1a54ef82008-07-21 02:01:04 +05305762 return inode;
5763}
5764
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005765static struct inode *new_simple_dir(struct super_block *s,
5766 struct btrfs_key *key,
5767 struct btrfs_root *root)
5768{
5769 struct inode *inode = new_inode(s);
5770
5771 if (!inode)
5772 return ERR_PTR(-ENOMEM);
5773
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005774 BTRFS_I(inode)->root = root;
5775 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005776 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005777
5778 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005779 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005780 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005781 inode->i_fop = &simple_dir_operations;
5782 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005783 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305784 inode->i_atime = inode->i_mtime;
5785 inode->i_ctime = inode->i_mtime;
5786 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005787
5788 return inode;
5789}
5790
Chris Mason3de45862008-11-17 21:02:50 -05005791struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005792{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005793 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005794 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005795 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005796 struct btrfs_root *sub_root = root;
5797 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005798 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005799 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005800
5801 if (dentry->d_name.len > BTRFS_NAME_LEN)
5802 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005803
Jeff Layton39e3c952012-11-28 11:30:53 -05005804 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005805 if (ret < 0)
5806 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005807
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005808 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005809 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005810
5811 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005812 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005813 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005815
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005816 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005817 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005818 &location, &sub_root);
5819 if (ret < 0) {
5820 if (ret != -ENOENT)
5821 inode = ERR_PTR(ret);
5822 else
5823 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5824 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005825 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005826 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005827 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005828
Julia Lawall34d19ba2011-01-24 19:55:19 +00005829 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005830 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005831 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005832 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005833 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005834 if (ret) {
5835 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005836 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005837 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005838 }
5839
Chris Mason3de45862008-11-17 21:02:50 -05005840 return inode;
5841}
5842
Nick Pigginfe15ce42011-01-07 17:49:23 +11005843static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005844{
5845 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005846 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005847
Li Zefan848cce02012-02-21 17:04:28 +08005848 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005849 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005850
Li Zefan848cce02012-02-21 17:04:28 +08005851 if (inode) {
5852 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005853 if (btrfs_root_refs(&root->root_item) == 0)
5854 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005855
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005856 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005857 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005858 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005859 return 0;
5860}
5861
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005862static void btrfs_dentry_release(struct dentry *dentry)
5863{
Daeseok Youn944a4512014-04-14 15:37:02 +09005864 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005865}
5866
Chris Mason3de45862008-11-17 21:02:50 -05005867static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005868 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005869{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005870 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005871
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005872 inode = btrfs_lookup_dentry(dir, dentry);
5873 if (IS_ERR(inode)) {
5874 if (PTR_ERR(inode) == -ENOENT)
5875 inode = NULL;
5876 else
5877 return ERR_CAST(inode);
5878 }
5879
Al Viro41d28bc2014-10-12 22:24:21 -04005880 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005881}
5882
Miao Xie16cdcec2011-04-22 18:12:22 +08005883unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005884 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5885};
5886
Josef Bacik23b5ec72017-07-24 15:14:25 -04005887/*
5888 * All this infrastructure exists because dir_emit can fault, and we are holding
5889 * the tree lock when doing readdir. For now just allocate a buffer and copy
5890 * our information into that, and then dir_emit from the buffer. This is
5891 * similar to what NFS does, only we don't keep the buffer around in pagecache
5892 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5893 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5894 * tree lock.
5895 */
5896static int btrfs_opendir(struct inode *inode, struct file *file)
5897{
5898 struct btrfs_file_private *private;
5899
5900 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5901 if (!private)
5902 return -ENOMEM;
5903 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5904 if (!private->filldir_buf) {
5905 kfree(private);
5906 return -ENOMEM;
5907 }
5908 file->private_data = private;
5909 return 0;
5910}
5911
5912struct dir_entry {
5913 u64 ino;
5914 u64 offset;
5915 unsigned type;
5916 int name_len;
5917};
5918
5919static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5920{
5921 while (entries--) {
5922 struct dir_entry *entry = addr;
5923 char *name = (char *)(entry + 1);
5924
5925 ctx->pos = entry->offset;
5926 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5927 entry->type))
5928 return 1;
5929 addr += sizeof(struct dir_entry) + entry->name_len;
5930 ctx->pos++;
5931 }
5932 return 0;
5933}
5934
Al Viro9cdda8d2013-05-22 16:48:09 -04005935static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005936{
Al Viro9cdda8d2013-05-22 16:48:09 -04005937 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005938 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005939 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005940 struct btrfs_dir_item *di;
5941 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005942 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005943 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005944 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005945 struct list_head ins_list;
5946 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005947 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005948 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005949 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005950 char *name_ptr;
5951 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005952 int entries = 0;
5953 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005954 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005955 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005956
Al Viro9cdda8d2013-05-22 16:48:09 -04005957 if (!dir_emit_dots(file, ctx))
5958 return 0;
5959
David Woodhouse49593bf2008-08-17 17:08:36 +01005960 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005961 if (!path)
5962 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005963
Josef Bacik23b5ec72017-07-24 15:14:25 -04005964 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005965 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005966
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005967 INIT_LIST_HEAD(&ins_list);
5968 INIT_LIST_HEAD(&del_list);
5969 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005970
Josef Bacik23b5ec72017-07-24 15:14:25 -04005971again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005972 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005973 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005974 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005975
Chris Mason39279cc2007-06-12 06:35:45 -04005976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5977 if (ret < 0)
5978 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005979
5980 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005981 struct dir_entry *entry;
5982
Chris Mason5f39d392007-10-15 16:14:19 -04005983 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005984 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005985 if (slot >= btrfs_header_nritems(leaf)) {
5986 ret = btrfs_next_leaf(root, path);
5987 if (ret < 0)
5988 goto err;
5989 else if (ret > 0)
5990 break;
5991 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005992 }
Chris Mason3de45862008-11-17 21:02:50 -05005993
Chris Mason5f39d392007-10-15 16:14:19 -04005994 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5995
5996 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005997 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005998 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005999 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006000 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006001 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006003 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006004 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006005 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006006 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6007 PAGE_SIZE) {
6008 btrfs_release_path(path);
6009 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6010 if (ret)
6011 goto nopos;
6012 addr = private->filldir_buf;
6013 entries = 0;
6014 total_len = 0;
6015 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006016 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006017
6018 entry = addr;
6019 entry->name_len = name_len;
6020 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006021 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6022 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006023 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006024 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006025 entry->ino = location.objectid;
6026 entry->offset = found_key.offset;
6027 entries++;
6028 addr += sizeof(struct dir_entry) + name_len;
6029 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006030next:
6031 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006032 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006033 btrfs_release_path(path);
6034
6035 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6036 if (ret)
6037 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006038
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006039 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006040 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006041 goto nopos;
6042
Zach Browndb62efb2013-07-11 16:19:42 -07006043 /*
6044 * Stop new entries from being returned after we return the last
6045 * entry.
6046 *
6047 * New directory entries are assigned a strictly increasing
6048 * offset. This means that new entries created during readdir
6049 * are *guaranteed* to be seen in the future by that readdir.
6050 * This has broken buggy programs which operate on names as
6051 * they're returned by readdir. Until we re-use freed offsets
6052 * we have this hack to stop new entries from being returned
6053 * under the assumption that they'll never reach this huge
6054 * offset.
6055 *
6056 * This is being careful not to overflow 32bit loff_t unless the
6057 * last entry requires it because doing so has broken 32bit apps
6058 * in the past.
6059 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006060 if (ctx->pos >= INT_MAX)
6061 ctx->pos = LLONG_MAX;
6062 else
6063 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006064nopos:
6065 ret = 0;
6066err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006067 if (put)
6068 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006069 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006070 return ret;
6071}
6072
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006073int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006074{
6075 struct btrfs_root *root = BTRFS_I(inode)->root;
6076 struct btrfs_trans_handle *trans;
6077 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006078 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006079
Josef Bacik72ac3c02012-05-23 14:13:11 -04006080 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006081 return 0;
6082
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006083 if (btrfs_fs_closing(root->fs_info) &&
6084 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006085 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006086
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006087 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006088 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006089 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006090 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006091 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006092 if (IS_ERR(trans))
6093 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006094 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006095 }
6096 return ret;
6097}
6098
6099/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006100 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006101 * inode changes. But, it is most likely to find the inode in cache.
6102 * FIXME, needs more benchmarking...there are no reasons other than performance
6103 * to keep or drop this code.
6104 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006105static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006106{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006107 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006108 struct btrfs_root *root = BTRFS_I(inode)->root;
6109 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006110 int ret;
6111
Josef Bacik72ac3c02012-05-23 14:13:11 -04006112 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006113 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006114
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006115 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006116 if (IS_ERR(trans))
6117 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006118
6119 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006120 if (ret && ret == -ENOSPC) {
6121 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006122 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006123 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006124 if (IS_ERR(trans))
6125 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006126
Chris Mason94b60442010-05-26 11:02:00 -04006127 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006128 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006129 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006130 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006131 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006132
6133 return ret;
6134}
6135
6136/*
6137 * This is a copy of file_update_time. We need this so we can return error on
6138 * ENOSPC for updating the inode in the case of file write and mmap writes.
6139 */
Josef Bacike41f9412012-03-26 09:46:47 -04006140static int btrfs_update_time(struct inode *inode, struct timespec *now,
6141 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006142{
Alexander Block2bc5565282012-06-15 09:49:33 +02006143 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006144 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006145
6146 if (btrfs_root_readonly(root))
6147 return -EROFS;
6148
Josef Bacike41f9412012-03-26 09:46:47 -04006149 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006150 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006151 if (flags & S_CTIME)
6152 inode->i_ctime = *now;
6153 if (flags & S_MTIME)
6154 inode->i_mtime = *now;
6155 if (flags & S_ATIME)
6156 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006157 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006158}
6159
Chris Masond352ac62008-09-29 15:18:18 -04006160/*
6161 * find the highest existing sequence number in a directory
6162 * and then set the in-memory index_cnt variable to reflect
6163 * free sequence numbers
6164 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006165static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006166{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006167 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006168 struct btrfs_key key, found_key;
6169 struct btrfs_path *path;
6170 struct extent_buffer *leaf;
6171 int ret;
6172
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006173 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006174 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006175 key.offset = (u64)-1;
6176
6177 path = btrfs_alloc_path();
6178 if (!path)
6179 return -ENOMEM;
6180
6181 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6182 if (ret < 0)
6183 goto out;
6184 /* FIXME: we should be able to handle this */
6185 if (ret == 0)
6186 goto out;
6187 ret = 0;
6188
6189 /*
6190 * MAGIC NUMBER EXPLANATION:
6191 * since we search a directory based on f_pos we have to start at 2
6192 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6193 * else has to start at 2
6194 */
6195 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006196 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006197 goto out;
6198 }
6199
6200 path->slots[0]--;
6201
6202 leaf = path->nodes[0];
6203 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6204
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006205 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006206 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006207 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006208 goto out;
6209 }
6210
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006211 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006212out:
6213 btrfs_free_path(path);
6214 return ret;
6215}
6216
Chris Masond352ac62008-09-29 15:18:18 -04006217/*
6218 * helper to find a free sequence number in a given directory. This current
6219 * code is very simple, later versions will do smarter things in the btree
6220 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006221int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006222{
6223 int ret = 0;
6224
Nikolay Borisov877574e2017-02-20 13:50:33 +02006225 if (dir->index_cnt == (u64)-1) {
6226 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006227 if (ret) {
6228 ret = btrfs_set_inode_index_count(dir);
6229 if (ret)
6230 return ret;
6231 }
Josef Bacikaec74772008-07-24 12:12:38 -04006232 }
6233
Nikolay Borisov877574e2017-02-20 13:50:33 +02006234 *index = dir->index_cnt;
6235 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006236
6237 return ret;
6238}
6239
Chris Masonb0d5d102014-09-08 13:08:51 -07006240static int btrfs_insert_inode_locked(struct inode *inode)
6241{
6242 struct btrfs_iget_args args;
6243 args.location = &BTRFS_I(inode)->location;
6244 args.root = BTRFS_I(inode)->root;
6245
6246 return insert_inode_locked4(inode,
6247 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6248 btrfs_find_actor, &args);
6249}
6250
Anand Jain19aee8d2017-07-18 17:37:05 +08006251/*
6252 * Inherit flags from the parent inode.
6253 *
6254 * Currently only the compression flags and the cow flags are inherited.
6255 */
6256static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6257{
6258 unsigned int flags;
6259
6260 if (!dir)
6261 return;
6262
6263 flags = BTRFS_I(dir)->flags;
6264
6265 if (flags & BTRFS_INODE_NOCOMPRESS) {
6266 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6267 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6268 } else if (flags & BTRFS_INODE_COMPRESS) {
6269 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6270 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6271 }
6272
6273 if (flags & BTRFS_INODE_NODATACOW) {
6274 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6275 if (S_ISREG(inode->i_mode))
6276 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6277 }
6278
6279 btrfs_update_iflags(inode);
6280}
6281
Chris Mason39279cc2007-06-12 06:35:45 -04006282static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6283 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006284 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006285 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006286 u64 ref_objectid, u64 objectid,
6287 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006288{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006289 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006290 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006291 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006292 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006293 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006294 struct btrfs_inode_ref *ref;
6295 struct btrfs_key key[2];
6296 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006297 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006298 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006299 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006300
Chris Mason5f39d392007-10-15 16:14:19 -04006301 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006302 if (!path)
6303 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006304
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006305 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006306 if (!inode) {
6307 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006308 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006309 }
Chris Mason39279cc2007-06-12 06:35:45 -04006310
Li Zefan581bb052011-04-20 10:06:11 +08006311 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006312 * O_TMPFILE, set link count to 0, so that after this point,
6313 * we fill in an inode item with the correct link count.
6314 */
6315 if (!name)
6316 set_nlink(inode, 0);
6317
6318 /*
Li Zefan581bb052011-04-20 10:06:11 +08006319 * we have to initialize this early, so we can reclaim the inode
6320 * number if we fail afterwards in this function.
6321 */
6322 inode->i_ino = objectid;
6323
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006324 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006325 trace_btrfs_inode_request(dir);
6326
Nikolay Borisov877574e2017-02-20 13:50:33 +02006327 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006328 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006329 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006330 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006331 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006332 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006333 } else if (dir) {
6334 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006335 }
6336 /*
6337 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006338 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006339 * number
6340 */
6341 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006342 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006343 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006344 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006345 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006346
Josef Bacik5dc562c2012-08-17 13:14:17 -04006347 /*
6348 * We could have gotten an inode number from somebody who was fsynced
6349 * and then removed in this same transaction, so let's just set full
6350 * sync since it will be a full sync anyway and this will blow away the
6351 * old info in the log.
6352 */
6353 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6354
Chris Mason9c583092008-01-29 15:15:18 -05006355 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006356 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006357 key[0].offset = 0;
6358
Chris Mason9c583092008-01-29 15:15:18 -05006359 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006360
6361 if (name) {
6362 /*
6363 * Start new inodes with an inode_ref. This is slightly more
6364 * efficient for small numbers of hard links since they will
6365 * be packed into one item. Extended refs will kick in if we
6366 * add more hard links than can fit in the ref item.
6367 */
6368 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006369 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006370 key[1].offset = ref_objectid;
6371
6372 sizes[1] = name_len + sizeof(*ref);
6373 }
Chris Mason9c583092008-01-29 15:15:18 -05006374
Chris Masonb0d5d102014-09-08 13:08:51 -07006375 location = &BTRFS_I(inode)->location;
6376 location->objectid = objectid;
6377 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006378 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006379
6380 ret = btrfs_insert_inode_locked(inode);
6381 if (ret < 0)
6382 goto fail;
6383
Chris Masonb9473432009-03-13 11:00:37 -04006384 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006385 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006386 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006387 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006388
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006389 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006390 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306391
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006392 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306393 inode->i_atime = inode->i_mtime;
6394 inode->i_ctime = inode->i_mtime;
6395 BTRFS_I(inode)->i_otime = inode->i_mtime;
6396
Chris Mason5f39d392007-10-15 16:14:19 -04006397 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6398 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006399 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006400 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006401 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006402
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006403 if (name) {
6404 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6405 struct btrfs_inode_ref);
6406 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6407 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6408 ptr = (unsigned long)(ref + 1);
6409 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6410 }
Chris Mason9c583092008-01-29 15:15:18 -05006411
Chris Mason5f39d392007-10-15 16:14:19 -04006412 btrfs_mark_buffer_dirty(path->nodes[0]);
6413 btrfs_free_path(path);
6414
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006415 btrfs_inherit_iflags(inode, dir);
6416
Al Viro569254b2011-07-24 17:08:40 -04006417 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006418 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006419 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006420 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006421 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6422 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006423 }
6424
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006425 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006426
6427 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006428 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006429
Alexander Block8ea05e32012-07-25 17:35:53 +02006430 btrfs_update_root_times(trans, root);
6431
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006432 ret = btrfs_inode_inherit_props(trans, inode, dir);
6433 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006434 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006435 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006436 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006437
Chris Mason39279cc2007-06-12 06:35:45 -04006438 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006439
6440fail_unlock:
6441 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006442fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006443 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006444 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006445 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006446 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006447 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006448}
6449
6450static inline u8 btrfs_inode_type(struct inode *inode)
6451{
6452 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6453}
6454
Chris Masond352ac62008-09-29 15:18:18 -04006455/*
6456 * utility function to add 'inode' into 'parent_inode' with
6457 * a give name and a given sequence number.
6458 * if 'add_backref' is true, also insert a backref from the
6459 * inode to the parent directory.
6460 */
Chris Masone02119d2008-09-05 16:13:11 -04006461int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006462 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006463 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006464{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006465 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006466 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006467 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006468 struct btrfs_root *root = parent_inode->root;
6469 u64 ino = btrfs_ino(inode);
6470 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006471
Li Zefan33345d012011-04-20 10:31:50 +08006472 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006473 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006474 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006475 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006476 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006477 key.offset = 0;
6478 }
Chris Mason39279cc2007-06-12 06:35:45 -04006479
Li Zefan33345d012011-04-20 10:31:50 +08006480 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006481 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6482 root->root_key.objectid, parent_ino,
6483 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006484 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006485 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6486 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006487 }
6488
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489 /* Nothing to clean up yet */
6490 if (ret)
6491 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006492
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006493 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6494 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006495 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006496 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006497 goto fail_dir_item;
6498 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006499 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006501 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006502
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006503 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006504 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006505 inode_inc_iversion(&parent_inode->vfs_inode);
6506 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6507 current_time(&parent_inode->vfs_inode);
6508 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006509 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006510 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006511 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006512
6513fail_dir_item:
6514 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6515 u64 local_index;
6516 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006517 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6518 root->root_key.objectid, parent_ino,
6519 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006520
6521 } else if (add_backref) {
6522 u64 local_index;
6523 int err;
6524
6525 err = btrfs_del_inode_ref(trans, root, name, name_len,
6526 ino, parent_ino, &local_index);
6527 }
6528 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006529}
6530
6531static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006532 struct btrfs_inode *dir, struct dentry *dentry,
6533 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006534{
Josef Bacika1b075d2010-11-19 20:36:11 +00006535 int err = btrfs_add_link(trans, dir, inode,
6536 dentry->d_name.name, dentry->d_name.len,
6537 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006538 if (err > 0)
6539 err = -EEXIST;
6540 return err;
6541}
6542
Josef Bacik618e21d2007-07-11 10:18:17 -04006543static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006544 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006545{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006546 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006547 struct btrfs_trans_handle *trans;
6548 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006549 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006550 int err;
6551 int drop_inode = 0;
6552 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006553 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006554
Josef Bacik9ed74f22009-09-11 16:12:44 -04006555 /*
6556 * 2 for inode item and ref
6557 * 2 for dir items
6558 * 1 for xattr if selinux is on
6559 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006560 trans = btrfs_start_transaction(root, 5);
6561 if (IS_ERR(trans))
6562 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006563
Li Zefan581bb052011-04-20 10:06:11 +08006564 err = btrfs_find_free_ino(root, &objectid);
6565 if (err)
6566 goto out_unlock;
6567
Josef Bacikaec74772008-07-24 12:12:38 -04006568 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006569 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6570 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006571 if (IS_ERR(inode)) {
6572 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006573 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006574 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006575
Casey Schauflerad19db72011-12-15 10:09:07 -05006576 /*
6577 * If the active LSM wants to access the inode during
6578 * d_instantiate it needs these. Smack checks to see
6579 * if the filesystem supports xattrs by looking at the
6580 * ops vector.
6581 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006582 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006583 init_special_inode(inode, inode->i_mode, rdev);
6584
6585 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006586 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006587 goto out_unlock_inode;
6588
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006589 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6590 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 if (err) {
6592 goto out_unlock_inode;
6593 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006594 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006596 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006597 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006598
Josef Bacik618e21d2007-07-11 10:18:17 -04006599out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006600 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006601 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006602 if (drop_inode) {
6603 inode_dec_link_count(inode);
6604 iput(inode);
6605 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006606 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006607
6608out_unlock_inode:
6609 drop_inode = 1;
6610 unlock_new_inode(inode);
6611 goto out_unlock;
6612
Josef Bacik618e21d2007-07-11 10:18:17 -04006613}
6614
Chris Mason39279cc2007-06-12 06:35:45 -04006615static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006616 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006617{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006618 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 struct btrfs_trans_handle *trans;
6620 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006621 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006622 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006623 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006624 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006625 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006626
Josef Bacik9ed74f22009-09-11 16:12:44 -04006627 /*
6628 * 2 for inode item and ref
6629 * 2 for dir items
6630 * 1 for xattr if selinux is on
6631 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006632 trans = btrfs_start_transaction(root, 5);
6633 if (IS_ERR(trans))
6634 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006635
Li Zefan581bb052011-04-20 10:06:11 +08006636 err = btrfs_find_free_ino(root, &objectid);
6637 if (err)
6638 goto out_unlock;
6639
Josef Bacikaec74772008-07-24 12:12:38 -04006640 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006641 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6642 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006643 if (IS_ERR(inode)) {
6644 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006645 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006646 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006647 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006648 /*
6649 * If the active LSM wants to access the inode during
6650 * d_instantiate it needs these. Smack checks to see
6651 * if the filesystem supports xattrs by looking at the
6652 * ops vector.
6653 */
6654 inode->i_fop = &btrfs_file_operations;
6655 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006656 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006657
6658 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6659 if (err)
6660 goto out_unlock_inode;
6661
6662 err = btrfs_update_inode(trans, root, inode);
6663 if (err)
6664 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006665
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006666 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6667 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006669 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006670
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006671 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006672 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006673 d_instantiate(dentry, inode);
6674
Chris Mason39279cc2007-06-12 06:35:45 -04006675out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006676 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006677 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006678 inode_dec_link_count(inode);
6679 iput(inode);
6680 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006681 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006682 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006683
6684out_unlock_inode:
6685 unlock_new_inode(inode);
6686 goto out_unlock;
6687
Chris Mason39279cc2007-06-12 06:35:45 -04006688}
6689
6690static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6691 struct dentry *dentry)
6692{
Filipe Manana271dba42016-01-05 16:24:05 +00006693 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006694 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006695 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006696 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006697 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006698 int err;
6699 int drop_inode = 0;
6700
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006701 /* do not allow sys_link's with other subvols of the same device */
6702 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006703 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006704
Mark Fashehf1863732012-08-08 11:32:27 -07006705 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006706 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006707
Nikolay Borisov877574e2017-02-20 13:50:33 +02006708 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006709 if (err)
6710 goto fail;
6711
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006713 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006715 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006717 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006718 if (IS_ERR(trans)) {
6719 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006720 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006721 goto fail;
6722 }
Chris Mason5f39d392007-10-15 16:14:19 -04006723
Miao Xie67de1172013-12-26 13:07:06 +08006724 /* There are several dir indexes for this inode, clear the cache. */
6725 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006726 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006727 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006728 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006729 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006730 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006731
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006732 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6733 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006734
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006736 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006737 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006738 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006739 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006740 if (err)
6741 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006742 if (inode->i_nlink == 1) {
6743 /*
6744 * If new hard link count is 1, it's a file created
6745 * with open(2) O_TMPFILE flag.
6746 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006747 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006748 if (err)
6749 goto fail;
6750 }
Al Viro08c422c2011-12-23 07:58:13 -05006751 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006752 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006753 }
Chris Mason39279cc2007-06-12 06:35:45 -04006754
Chris Mason1832a6d2007-12-21 16:27:21 -05006755fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006756 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006757 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006758 if (drop_inode) {
6759 inode_dec_link_count(inode);
6760 iput(inode);
6761 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006762 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006763 return err;
6764}
6765
Al Viro18bb1db2011-07-26 01:41:39 -04006766static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006767{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006768 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006769 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006770 struct btrfs_trans_handle *trans;
6771 struct btrfs_root *root = BTRFS_I(dir)->root;
6772 int err = 0;
6773 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006774 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006775 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006776
Josef Bacik9ed74f22009-09-11 16:12:44 -04006777 /*
6778 * 2 items for inode and ref
6779 * 2 items for dir items
6780 * 1 for xattr if selinux is on
6781 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006782 trans = btrfs_start_transaction(root, 5);
6783 if (IS_ERR(trans))
6784 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006785
Li Zefan581bb052011-04-20 10:06:11 +08006786 err = btrfs_find_free_ino(root, &objectid);
6787 if (err)
6788 goto out_fail;
6789
Josef Bacikaec74772008-07-24 12:12:38 -04006790 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006791 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6792 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006793 if (IS_ERR(inode)) {
6794 err = PTR_ERR(inode);
6795 goto out_fail;
6796 }
Chris Mason5f39d392007-10-15 16:14:19 -04006797
Chris Mason39279cc2007-06-12 06:35:45 -04006798 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006799 /* these must be set before we unlock the inode */
6800 inode->i_op = &btrfs_dir_inode_operations;
6801 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006802
Eric Paris2a7dba32011-02-01 11:05:39 -05006803 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006804 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006805 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006806
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006807 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006808 err = btrfs_update_inode(trans, root, inode);
6809 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006810 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006811
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006812 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6813 dentry->d_name.name,
6814 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006815 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006816 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006817
Chris Mason39279cc2007-06-12 06:35:45 -04006818 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006819 /*
6820 * mkdir is special. We're unlocking after we call d_instantiate
6821 * to avoid a race with nfsd calling d_instantiate.
6822 */
6823 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006824 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006825
6826out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006827 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006828 if (drop_on_err) {
6829 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006830 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006831 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006832 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006833 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006834
6835out_fail_inode:
6836 unlock_new_inode(inode);
6837 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006838}
6839
Chris Masonc8b97812008-10-29 14:49:59 -04006840static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006841 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006842 size_t pg_offset, u64 extent_offset,
6843 struct btrfs_file_extent_item *item)
6844{
6845 int ret;
6846 struct extent_buffer *leaf = path->nodes[0];
6847 char *tmp;
6848 size_t max_size;
6849 unsigned long inline_size;
6850 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006851 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006852
6853 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006854 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006855 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6856 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006857 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006858 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006859 if (!tmp)
6860 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006861 ptr = btrfs_file_extent_inline_start(item);
6862
6863 read_extent_buffer(leaf, tmp, ptr, inline_size);
6864
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006865 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006866 ret = btrfs_decompress(compress_type, tmp, page,
6867 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006868
6869 /*
6870 * decompression code contains a memset to fill in any space between the end
6871 * of the uncompressed data and the end of max_size in case the decompressed
6872 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6873 * the end of an inline extent and the beginning of the next block, so we
6874 * cover that region here.
6875 */
6876
6877 if (max_size + pg_offset < PAGE_SIZE) {
6878 char *map = kmap(page);
6879 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6880 kunmap(page);
6881 }
Chris Masonc8b97812008-10-29 14:49:59 -04006882 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006883 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006884}
6885
Chris Masond352ac62008-09-29 15:18:18 -04006886/*
6887 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006888 * the ugly parts come from merging extents from the disk with the in-ram
6889 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006890 * where the in-ram extents might be locked pending data=ordered completion.
6891 *
6892 * This also copies inline extents directly into the page.
6893 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006894struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6895 struct page *page,
6896 size_t pg_offset, u64 start, u64 len,
6897 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006898{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006899 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006900 int ret;
6901 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006902 u64 extent_start = 0;
6903 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006904 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006905 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006906 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006907 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006909 struct extent_buffer *leaf;
6910 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006911 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006912 struct extent_map_tree *em_tree = &inode->extent_tree;
6913 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006914 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006915
Chris Mason890871b2009-09-02 16:24:52 -04006916 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006917 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006918 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006919 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006920 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006921
Chris Masona52d9a82007-08-27 16:49:44 -04006922 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006923 if (em->start > start || em->start + em->len <= start)
6924 free_extent_map(em);
6925 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006926 free_extent_map(em);
6927 else
6928 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006929 }
David Sterba172ddd62011-04-21 00:48:27 +02006930 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006931 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006932 err = -ENOMEM;
6933 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006934 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006935 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006936 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006937 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006938 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006939 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006940
6941 if (!path) {
6942 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006943 if (!path) {
6944 err = -ENOMEM;
6945 goto out;
6946 }
6947 /*
6948 * Chances are we'll be called again, so go ahead and do
6949 * readahead
6950 */
David Sterbae4058b52015-11-27 16:31:35 +01006951 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006952 }
6953
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006954 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006955 if (ret < 0) {
6956 err = ret;
6957 goto out;
6958 }
6959
6960 if (ret != 0) {
6961 if (path->slots[0] == 0)
6962 goto not_found;
6963 path->slots[0]--;
6964 }
6965
Chris Mason5f39d392007-10-15 16:14:19 -04006966 leaf = path->nodes[0];
6967 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006968 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006969 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006970 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006971 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006972 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006973 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006974 /*
6975 * If we backup past the first extent we want to move forward
6976 * and see if there is an extent in front of us, otherwise we'll
6977 * say there is a hole for our whole search range which can
6978 * cause problems.
6979 */
6980 extent_end = start;
6981 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006982 }
6983
Chris Mason5f39d392007-10-15 16:14:19 -04006984 found_type = btrfs_file_extent_type(leaf, item);
6985 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006986 if (found_type == BTRFS_FILE_EXTENT_REG ||
6987 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006988 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006989 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006990
6991 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6992 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006993 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6994 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006995 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006996 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006997 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006998
6999 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7000 path->slots[0],
7001 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007002 }
Josef Bacik25a50342013-10-14 12:08:38 -04007003next:
Yan Zheng9036c102008-10-30 14:19:41 -04007004 if (start >= extent_end) {
7005 path->slots[0]++;
7006 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7007 ret = btrfs_next_leaf(root, path);
7008 if (ret < 0) {
7009 err = ret;
7010 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007011 }
Yan Zheng9036c102008-10-30 14:19:41 -04007012 if (ret > 0)
7013 goto not_found;
7014 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007015 }
Yan Zheng9036c102008-10-30 14:19:41 -04007016 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7017 if (found_key.objectid != objectid ||
7018 found_key.type != BTRFS_EXTENT_DATA_KEY)
7019 goto not_found;
7020 if (start + len <= found_key.offset)
7021 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007022 if (start > found_key.offset)
7023 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007024 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007025 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007026 em->len = found_key.offset - start;
7027 goto not_found_em;
7028 }
7029
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007030 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007031 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007032
Yan Zhengd899e052008-10-30 14:25:28 -04007033 if (found_type == BTRFS_FILE_EXTENT_REG ||
7034 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007035 goto insert;
7036 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007037 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007038 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007039 size_t size;
7040 size_t extent_offset;
7041 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007042
Filipe Manana7ffbb592014-06-09 03:48:05 +01007043 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007044 goto out;
Yan689f9342007-10-29 11:41:07 -04007045
Chris Mason514ac8a2014-01-03 21:07:00 -08007046 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007047 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007048 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7049 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007050 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007051 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007052 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007053 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007054 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007055 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007056 if (btrfs_file_extent_compression(leaf, item) !=
7057 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007058 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007059 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007060 if (ret) {
7061 err = ret;
7062 goto out;
7063 }
Chris Masonc8b97812008-10-29 14:49:59 -04007064 } else {
7065 map = kmap(page);
7066 read_extent_buffer(leaf, map + pg_offset, ptr,
7067 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007068 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007069 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007070 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007071 copy_size);
7072 }
Chris Masonc8b97812008-10-29 14:49:59 -04007073 kunmap(page);
7074 }
Chris Mason179e29e2007-11-01 11:28:41 -04007075 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007076 }
Chris Masond1310b22008-01-24 16:13:08 -05007077 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007078 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007079 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007080 }
7081not_found:
7082 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007083 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007084 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007085not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007086 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007087insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007088 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007089 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007090 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007091 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7092 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007093 err = -EIO;
7094 goto out;
7095 }
Chris Masond1310b22008-01-24 16:13:08 -05007096
7097 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007098 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007099 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007100 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007101out:
liubo1abe9b82011-03-24 11:18:59 +00007102
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007103 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007104
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007105 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007106 if (err) {
7107 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007108 return ERR_PTR(err);
7109 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007110 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007111 return em;
7112}
7113
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007114struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7115 struct page *page,
7116 size_t pg_offset, u64 start, u64 len,
7117 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007118{
7119 struct extent_map *em;
7120 struct extent_map *hole_em = NULL;
7121 u64 range_start = start;
7122 u64 end;
7123 u64 found;
7124 u64 found_end;
7125 int err = 0;
7126
7127 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7128 if (IS_ERR(em))
7129 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007130 /*
7131 * If our em maps to:
7132 * - a hole or
7133 * - a pre-alloc extent,
7134 * there might actually be delalloc bytes behind it.
7135 */
7136 if (em->block_start != EXTENT_MAP_HOLE &&
7137 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7138 return em;
7139 else
7140 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007141
7142 /* check to see if we've wrapped (len == -1 or similar) */
7143 end = start + len;
7144 if (end < start)
7145 end = (u64)-1;
7146 else
7147 end -= 1;
7148
7149 em = NULL;
7150
7151 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007152 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007153 end, len, EXTENT_DELALLOC, 1);
7154 found_end = range_start + found;
7155 if (found_end < range_start)
7156 found_end = (u64)-1;
7157
7158 /*
7159 * we didn't find anything useful, return
7160 * the original results from get_extent()
7161 */
7162 if (range_start > end || found_end <= start) {
7163 em = hole_em;
7164 hole_em = NULL;
7165 goto out;
7166 }
7167
7168 /* adjust the range_start to make sure it doesn't
7169 * go backwards from the start they passed in
7170 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307171 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007172 found = found_end - range_start;
7173
7174 if (found > 0) {
7175 u64 hole_start = start;
7176 u64 hole_len = len;
7177
David Sterba172ddd62011-04-21 00:48:27 +02007178 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007179 if (!em) {
7180 err = -ENOMEM;
7181 goto out;
7182 }
7183 /*
7184 * when btrfs_get_extent can't find anything it
7185 * returns one huge hole
7186 *
7187 * make sure what it found really fits our range, and
7188 * adjust to make sure it is based on the start from
7189 * the caller
7190 */
7191 if (hole_em) {
7192 u64 calc_end = extent_map_end(hole_em);
7193
7194 if (calc_end <= start || (hole_em->start > end)) {
7195 free_extent_map(hole_em);
7196 hole_em = NULL;
7197 } else {
7198 hole_start = max(hole_em->start, start);
7199 hole_len = calc_end - hole_start;
7200 }
7201 }
7202 em->bdev = NULL;
7203 if (hole_em && range_start > hole_start) {
7204 /* our hole starts before our delalloc, so we
7205 * have to return just the parts of the hole
7206 * that go until the delalloc starts
7207 */
7208 em->len = min(hole_len,
7209 range_start - hole_start);
7210 em->start = hole_start;
7211 em->orig_start = hole_start;
7212 /*
7213 * don't adjust block start at all,
7214 * it is fixed at EXTENT_MAP_HOLE
7215 */
7216 em->block_start = hole_em->block_start;
7217 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007218 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7219 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007220 } else {
7221 em->start = range_start;
7222 em->len = found;
7223 em->orig_start = range_start;
7224 em->block_start = EXTENT_MAP_DELALLOC;
7225 em->block_len = found;
7226 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007227 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007228 return hole_em;
7229 }
7230out:
7231
7232 free_extent_map(hole_em);
7233 if (err) {
7234 free_extent_map(em);
7235 return ERR_PTR(err);
7236 }
7237 return em;
7238}
7239
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007240static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7241 const u64 start,
7242 const u64 len,
7243 const u64 orig_start,
7244 const u64 block_start,
7245 const u64 block_len,
7246 const u64 orig_block_len,
7247 const u64 ram_bytes,
7248 const int type)
7249{
7250 struct extent_map *em = NULL;
7251 int ret;
7252
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007253 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007254 em = create_io_em(inode, start, len, orig_start,
7255 block_start, block_len, orig_block_len,
7256 ram_bytes,
7257 BTRFS_COMPRESS_NONE, /* compress_type */
7258 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007259 if (IS_ERR(em))
7260 goto out;
7261 }
7262 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7263 len, block_len, type);
7264 if (ret) {
7265 if (em) {
7266 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007267 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007268 start + len - 1, 0);
7269 }
7270 em = ERR_PTR(ret);
7271 }
7272 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007273
7274 return em;
7275}
7276
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7278 u64 start, u64 len)
7279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007282 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 struct btrfs_key ins;
7284 u64 alloc_hint;
7285 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007286
Josef Bacik4b46fce2010-05-23 11:00:55 -04007287 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007288 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007289 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007290 if (ret)
7291 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007292
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007293 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7294 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007295 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007296 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007297 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007298 btrfs_free_reserved_extent(fs_info, ins.objectid,
7299 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007300
Josef Bacik4b46fce2010-05-23 11:00:55 -04007301 return em;
7302}
7303
Chris Mason46bfbb52010-05-26 11:04:10 -04007304/*
7305 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7306 * block must be cow'd
7307 */
Josef Bacik00361582013-08-14 14:02:47 -04007308noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007309 u64 *orig_start, u64 *orig_block_len,
7310 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007311{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007312 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007313 struct btrfs_path *path;
7314 int ret;
7315 struct extent_buffer *leaf;
7316 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007317 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007318 struct btrfs_file_extent_item *fi;
7319 struct btrfs_key key;
7320 u64 disk_bytenr;
7321 u64 backref_offset;
7322 u64 extent_end;
7323 u64 num_bytes;
7324 int slot;
7325 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007326 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007327
Chris Mason46bfbb52010-05-26 11:04:10 -04007328 path = btrfs_alloc_path();
7329 if (!path)
7330 return -ENOMEM;
7331
David Sterbaf85b7372017-01-20 14:54:07 +01007332 ret = btrfs_lookup_file_extent(NULL, root, path,
7333 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007334 if (ret < 0)
7335 goto out;
7336
7337 slot = path->slots[0];
7338 if (ret == 1) {
7339 if (slot == 0) {
7340 /* can't find the item, must cow */
7341 ret = 0;
7342 goto out;
7343 }
7344 slot--;
7345 }
7346 ret = 0;
7347 leaf = path->nodes[0];
7348 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007349 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 key.type != BTRFS_EXTENT_DATA_KEY) {
7351 /* not our file or wrong item type, must cow */
7352 goto out;
7353 }
7354
7355 if (key.offset > offset) {
7356 /* Wrong offset, must cow */
7357 goto out;
7358 }
7359
7360 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7361 found_type = btrfs_file_extent_type(leaf, fi);
7362 if (found_type != BTRFS_FILE_EXTENT_REG &&
7363 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7364 /* not a regular extent, must cow */
7365 goto out;
7366 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007367
7368 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7369 goto out;
7370
Miao Xiee77751a2013-12-27 21:11:50 +08007371 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7372 if (extent_end <= offset)
7373 goto out;
7374
Chris Mason46bfbb52010-05-26 11:04:10 -04007375 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007376 if (disk_bytenr == 0)
7377 goto out;
7378
7379 if (btrfs_file_extent_compression(leaf, fi) ||
7380 btrfs_file_extent_encryption(leaf, fi) ||
7381 btrfs_file_extent_other_encoding(leaf, fi))
7382 goto out;
7383
Chris Mason46bfbb52010-05-26 11:04:10 -04007384 backref_offset = btrfs_file_extent_offset(leaf, fi);
7385
Josef Bacik7ee9e442013-06-21 16:37:03 -04007386 if (orig_start) {
7387 *orig_start = key.offset - backref_offset;
7388 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7389 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7390 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007391
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007392 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007393 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007394
7395 num_bytes = min(offset + *len, extent_end) - offset;
7396 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7397 u64 range_end;
7398
Jeff Mahoneyda170662016-06-15 09:22:56 -04007399 range_end = round_up(offset + num_bytes,
7400 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007401 ret = test_range_bit(io_tree, offset, range_end,
7402 EXTENT_DELALLOC, 0, NULL);
7403 if (ret) {
7404 ret = -EAGAIN;
7405 goto out;
7406 }
7407 }
7408
Josef Bacik1bda19e2013-10-18 12:10:36 -04007409 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007410
7411 /*
7412 * look for other files referencing this extent, if we
7413 * find any we must cow
7414 */
Josef Bacik00361582013-08-14 14:02:47 -04007415
Liu Boe4c3b2d2017-01-30 12:25:28 -08007416 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007417 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007418 if (ret) {
7419 ret = 0;
7420 goto out;
7421 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007422
7423 /*
7424 * adjust disk_bytenr and num_bytes to cover just the bytes
7425 * in this extent we are about to write. If there
7426 * are any csums in that range we have to cow in order
7427 * to keep the csums correct
7428 */
7429 disk_bytenr += backref_offset;
7430 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007431 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7432 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007433 /*
7434 * all of the above have passed, it is safe to overwrite this extent
7435 * without cow
7436 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007437 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 ret = 1;
7439out:
7440 btrfs_free_path(path);
7441 return ret;
7442}
7443
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007444bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7445{
7446 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007447 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007448 void **pagep = NULL;
7449 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007450 unsigned long start_idx;
7451 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007452
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007453 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007454
7455 /*
7456 * end is the last byte in the last page. end == start is legal
7457 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007458 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007459
7460 rcu_read_lock();
7461
7462 /* Most of the code in this while loop is lifted from
7463 * find_get_page. It's been modified to begin searching from a
7464 * page and return just the first page found in that range. If the
7465 * found idx is less than or equal to the end idx then we know that
7466 * a page exists. If no pages are found or if those pages are
7467 * outside of the range then we're fine (yay!) */
7468 while (page == NULL &&
7469 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7470 page = radix_tree_deref_slot(pagep);
7471 if (unlikely(!page))
7472 break;
7473
7474 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007475 if (radix_tree_deref_retry(page)) {
7476 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007477 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007478 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007479 /*
7480 * Otherwise, shmem/tmpfs must be storing a swap entry
7481 * here as an exceptional entry: so return it without
7482 * attempting to raise page count.
7483 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007484 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007485 break; /* TODO: Is this relevant for this use case? */
7486 }
7487
Filipe Manana91405152014-06-05 13:22:24 +01007488 if (!page_cache_get_speculative(page)) {
7489 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007490 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007491 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007492
7493 /*
7494 * Has the page moved?
7495 * This is part of the lockless pagecache protocol. See
7496 * include/linux/pagemap.h for details.
7497 */
7498 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007499 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007500 page = NULL;
7501 }
7502 }
7503
7504 if (page) {
7505 if (page->index <= end_idx)
7506 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007507 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007508 }
7509
7510 rcu_read_unlock();
7511 return found;
7512}
7513
Josef Bacikeb838e72012-07-31 16:28:48 -04007514static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7515 struct extent_state **cached_state, int writing)
7516{
7517 struct btrfs_ordered_extent *ordered;
7518 int ret = 0;
7519
7520 while (1) {
7521 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007522 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007523 /*
7524 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007525 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007526 * extents in this range.
7527 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007528 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007529 lockend - lockstart + 1);
7530
7531 /*
7532 * We need to make sure there are no buffered pages in this
7533 * range either, we could have raced between the invalidate in
7534 * generic_file_direct_write and locking the extent. The
7535 * invalidate needs to happen so that reads after a write do not
7536 * get stale data.
7537 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007538 if (!ordered &&
7539 (!writing ||
7540 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 break;
7542
7543 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007544 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007545
7546 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007547 /*
7548 * If we are doing a DIO read and the ordered extent we
7549 * found is for a buffered write, we can not wait for it
7550 * to complete and retry, because if we do so we can
7551 * deadlock with concurrent buffered writes on page
7552 * locks. This happens only if our DIO read covers more
7553 * than one extent map, if at this point has already
7554 * created an ordered extent for a previous extent map
7555 * and locked its range in the inode's io tree, and a
7556 * concurrent write against that previous extent map's
7557 * range and this range started (we unlock the ranges
7558 * in the io tree only when the bios complete and
7559 * buffered writes always lock pages before attempting
7560 * to lock range in the io tree).
7561 */
7562 if (writing ||
7563 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7564 btrfs_start_ordered_extent(inode, ordered, 1);
7565 else
7566 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 btrfs_put_ordered_extent(ordered);
7568 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007569 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007570 * We could trigger writeback for this range (and wait
7571 * for it to complete) and then invalidate the pages for
7572 * this range (through invalidate_inode_pages2_range()),
7573 * but that can lead us to a deadlock with a concurrent
7574 * call to readpages() (a buffered read or a defrag call
7575 * triggered a readahead) on a page lock due to an
7576 * ordered dio extent we created before but did not have
7577 * yet a corresponding bio submitted (whence it can not
7578 * complete), which makes readpages() wait for that
7579 * ordered extent to complete while holding a lock on
7580 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007581 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007582 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 }
7584
Filipe Mananaade77022016-02-18 14:28:55 +00007585 if (ret)
7586 break;
7587
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 cond_resched();
7589 }
7590
7591 return ret;
7592}
7593
Liu Bo6f9994d2017-01-31 07:50:22 -08007594/* The callers of this must take lock_extent() */
7595static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7596 u64 orig_start, u64 block_start,
7597 u64 block_len, u64 orig_block_len,
7598 u64 ram_bytes, int compress_type,
7599 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007600{
7601 struct extent_map_tree *em_tree;
7602 struct extent_map *em;
7603 struct btrfs_root *root = BTRFS_I(inode)->root;
7604 int ret;
7605
Liu Bo6f9994d2017-01-31 07:50:22 -08007606 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7607 type == BTRFS_ORDERED_COMPRESSED ||
7608 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007609 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007610
Josef Bacik69ffb542012-09-11 15:40:07 -04007611 em_tree = &BTRFS_I(inode)->extent_tree;
7612 em = alloc_extent_map();
7613 if (!em)
7614 return ERR_PTR(-ENOMEM);
7615
7616 em->start = start;
7617 em->orig_start = orig_start;
7618 em->len = len;
7619 em->block_len = block_len;
7620 em->block_start = block_start;
7621 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007622 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007623 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007624 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007625 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007626 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007627 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007628 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007629 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7630 em->compress_type = compress_type;
7631 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007632
7633 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007634 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007635 em->start + em->len - 1, 0);
7636 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007637 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007638 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007639 /*
7640 * The caller has taken lock_extent(), who could race with us
7641 * to add em?
7642 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007643 } while (ret == -EEXIST);
7644
7645 if (ret) {
7646 free_extent_map(em);
7647 return ERR_PTR(ret);
7648 }
7649
Liu Bo6f9994d2017-01-31 07:50:22 -08007650 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007651 return em;
7652}
7653
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7655 struct buffer_head *bh_result, int create)
7656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007657 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307660 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007661 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007662 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007663 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007665 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007666
Miao Xie172a5042013-02-21 02:48:22 -07007667 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007668 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007669 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007670 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007671
Josef Bacikc3298612012-08-03 16:49:19 -04007672 lockstart = start;
7673 lockend = start + len - 1;
7674
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007675 if (current->journal_info) {
7676 /*
7677 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007678 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007679 * confused.
7680 */
chandan50745b02015-08-28 21:10:13 +05307681 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007682 current->journal_info = NULL;
7683 }
7684
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 /*
7686 * If this errors out it's because we couldn't invalidate pagecache for
7687 * this range and we need to fallback to buffered.
7688 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007689 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7690 create)) {
7691 ret = -ENOTBLK;
7692 goto err;
7693 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007694
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007695 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007696 if (IS_ERR(em)) {
7697 ret = PTR_ERR(em);
7698 goto unlock_err;
7699 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007700
7701 /*
7702 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7703 * io. INLINE is special, and we could probably kludge it in here, but
7704 * it's still buffered so for safety lets just fall back to the generic
7705 * buffered path.
7706 *
7707 * For COMPRESSED we _have_ to read the entire extent in so we can
7708 * decompress it, so there will be buffering required no matter what we
7709 * do, so go ahead and fallback to buffered.
7710 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007711 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007712 * to buffered IO. Don't blame me, this is the price we pay for using
7713 * the generic code.
7714 */
7715 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7716 em->block_start == EXTENT_MAP_INLINE) {
7717 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007718 ret = -ENOTBLK;
7719 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720 }
7721
7722 /* Just a good old fashioned hole, return */
7723 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7724 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7725 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007726 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727 }
7728
7729 /*
7730 * We don't allocate a new extent in the following cases
7731 *
7732 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7733 * existing extent.
7734 * 2) The extent is marked as PREALLOC. We're good to go here and can
7735 * just use the extent.
7736 *
7737 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007738 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007739 len = min(len, em->len - (start - em->start));
7740 lockstart = start + len;
7741 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007742 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007743
7744 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7745 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7746 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007748 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749
7750 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7751 type = BTRFS_ORDERED_PREALLOC;
7752 else
7753 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007754 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007755 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007756
Josef Bacik00361582013-08-14 14:02:47 -04007757 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007758 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007759 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007760 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007761
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007762 em2 = btrfs_create_dio_extent(inode, start, len,
7763 orig_start, block_start,
7764 len, orig_block_len,
7765 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007766 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007767 if (type == BTRFS_ORDERED_PREALLOC) {
7768 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007769 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007770 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007771 if (em2 && IS_ERR(em2)) {
7772 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007773 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007774 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007775 /*
7776 * For inode marked NODATACOW or extent marked PREALLOC,
7777 * use the existing or preallocated extent, so does not
7778 * need to adjust btrfs_space_info's bytes_may_use.
7779 */
7780 btrfs_free_reserved_data_space_noquota(inode,
7781 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007782 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007784 }
Josef Bacik00361582013-08-14 14:02:47 -04007785
Chris Mason46bfbb52010-05-26 11:04:10 -04007786 /*
7787 * this will cow the extent, reset the len in case we changed
7788 * it above
7789 */
7790 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007791 free_extent_map(em);
7792 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007793 if (IS_ERR(em)) {
7794 ret = PTR_ERR(em);
7795 goto unlock_err;
7796 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007797 len = min(len, em->len - (start - em->start));
7798unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007799 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7800 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007801 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802 bh_result->b_bdev = em->bdev;
7803 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007804 if (create) {
7805 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7806 set_buffer_new(bh_result);
7807
7808 /*
7809 * Need to update the i_size under the extent lock so buffered
7810 * readers will get the updated i_size when we unlock.
7811 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007812 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007813 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007814
chandan50745b02015-08-28 21:10:13 +05307815 WARN_ON(dio_data->reserve < len);
7816 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007817 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307818 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007819 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007820
Josef Bacikeb838e72012-07-31 16:28:48 -04007821 /*
7822 * In the case of write we need to clear and unlock the entire range,
7823 * in the case of read we need to unlock only the end area that we
7824 * aren't using if there is any left over space.
7825 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007826 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007827 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7828 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007829 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007830 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007831 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007832 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007833
Josef Bacik4b46fce2010-05-23 11:00:55 -04007834 free_extent_map(em);
7835
7836 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007837
7838unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007839 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007840 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007841err:
chandan50745b02015-08-28 21:10:13 +05307842 if (dio_data)
7843 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007844 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007845}
7846
Omar Sandoval58efbc92017-08-22 23:45:59 -07007847static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7848 struct bio *bio,
7849 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007850{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007851 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007852 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007853
Mike Christie37226b22016-06-05 14:31:52 -05007854 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007855
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007856 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007857 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007858 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007859
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007860 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007861
Miao Xie8b110e32014-09-12 18:44:03 +08007862 return ret;
7863}
7864
7865static int btrfs_check_dio_repairable(struct inode *inode,
7866 struct bio *failed_bio,
7867 struct io_failure_record *failrec,
7868 int failed_mirror)
7869{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007871 int num_copies;
7872
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007873 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007874 if (num_copies == 1) {
7875 /*
7876 * we only have a single copy of the data, so don't bother with
7877 * all the retry and error correction code that follows. no
7878 * matter what the error is, it is very likely to persist.
7879 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007880 btrfs_debug(fs_info,
7881 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7882 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007883 return 0;
7884 }
7885
7886 failrec->failed_mirror = failed_mirror;
7887 failrec->this_mirror++;
7888 if (failrec->this_mirror == failed_mirror)
7889 failrec->this_mirror++;
7890
7891 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007892 btrfs_debug(fs_info,
7893 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7894 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007895 return 0;
7896 }
7897
7898 return 1;
7899}
7900
Omar Sandoval58efbc92017-08-22 23:45:59 -07007901static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7902 struct page *page, unsigned int pgoff,
7903 u64 start, u64 end, int failed_mirror,
7904 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007905{
7906 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007907 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7908 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007909 struct bio *bio;
7910 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007911 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007912 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007913 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007914 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007915 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007916
Mike Christie37226b22016-06-05 14:31:52 -05007917 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007918
7919 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7920 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007921 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007922
7923 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7924 failed_mirror);
7925 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007926 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007927 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007928 }
7929
Liu Bo17347ce2017-05-15 15:33:27 -07007930 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007931 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007932 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007933 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007934 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007935
7936 isector = start - btrfs_io_bio(failed_bio)->logical;
7937 isector >>= inode->i_sb->s_blocksize_bits;
7938 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007940 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007941
7942 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007943 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007944 read_mode, failrec->this_mirror, failrec->in_validation);
7945
Omar Sandoval58efbc92017-08-22 23:45:59 -07007946 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7947 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007948 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007949 bio_put(bio);
7950 }
7951
Omar Sandoval58efbc92017-08-22 23:45:59 -07007952 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007953}
7954
7955struct btrfs_retry_complete {
7956 struct completion done;
7957 struct inode *inode;
7958 u64 start;
7959 int uptodate;
7960};
7961
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007962static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007963{
7964 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007965 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007966 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007967 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007968 int i;
7969
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007970 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007971 goto end;
7972
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307973 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007974 io_tree = &BTRFS_I(inode)->io_tree;
7975 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007976 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977
Miao Xie8b110e32014-09-12 18:44:03 +08007978 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007979 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007980 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007981 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7982 io_tree, done->start, bvec->bv_page,
7983 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007984end:
7985 complete(&done->done);
7986 bio_put(bio);
7987}
7988
Omar Sandoval58efbc92017-08-22 23:45:59 -07007989static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7990 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007991{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007993 struct bio_vec bvec;
7994 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007995 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007996 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307997 unsigned int pgoff;
7998 u32 sectorsize;
7999 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008000 blk_status_t ret;
8001 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008002
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008004 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005
Miao Xiec1dc0892014-09-12 18:43:56 +08008006 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008007 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008008 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008009
Liu Bo17347ce2017-05-15 15:33:27 -07008010 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8011 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8012 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013
8014next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008015 done.uptodate = 0;
8016 done.start = start;
8017 init_completion(&done.done);
8018
Liu Bo17347ce2017-05-15 15:33:27 -07008019 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 pgoff, start, start + sectorsize - 1,
8021 io_bio->mirror_num,
8022 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008023 if (ret) {
8024 err = ret;
8025 goto next;
8026 }
Miao Xie8b110e32014-09-12 18:44:03 +08008027
David Sterba9c17f6c2017-07-19 19:26:45 +02008028 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008029
8030 if (!done.uptodate) {
8031 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308032 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008033 }
8034
Liu Bo629ebf42017-05-15 17:20:07 -07008035next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 start += sectorsize;
8037
Liu Bo97bf5a52017-04-07 13:11:10 -07008038 nr_sectors--;
8039 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308040 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008041 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 goto next_block_or_try_again;
8043 }
Miao Xie8b110e32014-09-12 18:44:03 +08008044 }
8045
Liu Bo629ebf42017-05-15 17:20:07 -07008046 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008047}
8048
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008049static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008050{
8051 struct btrfs_retry_complete *done = bio->bi_private;
8052 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008053 struct extent_io_tree *io_tree, *failure_tree;
8054 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008055 struct bio_vec *bvec;
8056 int uptodate;
8057 int ret;
8058 int i;
8059
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008060 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008061 goto end;
8062
8063 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308064
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008066 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308067
Josef Bacik7870d082017-05-05 11:57:15 -04008068 io_tree = &BTRFS_I(inode)->io_tree;
8069 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8070
David Sterbac09abff2017-07-13 18:10:07 +02008071 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008072 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008073 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8074 bvec->bv_offset, done->start,
8075 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008076 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008077 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8078 failure_tree, io_tree, done->start,
8079 bvec->bv_page,
8080 btrfs_ino(BTRFS_I(inode)),
8081 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008082 else
8083 uptodate = 0;
8084 }
8085
8086 done->uptodate = uptodate;
8087end:
8088 complete(&done->done);
8089 bio_put(bio);
8090}
8091
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008092static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8093 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008094{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308095 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008096 struct bio_vec bvec;
8097 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008098 struct btrfs_retry_complete done;
8099 u64 start;
8100 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 u32 sectorsize;
8102 int nr_sectors;
8103 unsigned int pgoff;
8104 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008105 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008106 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008107 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008108
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308109 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008110 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308111
Omar Sandoval58efbc92017-08-22 23:45:59 -07008112 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008113 start = io_bio->logical;
8114 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008115 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008116
Liu Bo17347ce2017-05-15 15:33:27 -07008117 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8118 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308119
Liu Bo17347ce2017-05-15 15:33:27 -07008120 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008122 if (uptodate) {
8123 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8124 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8125 bvec.bv_page, pgoff, start, sectorsize);
8126 if (likely(!ret))
8127 goto next;
8128 }
Miao Xie8b110e32014-09-12 18:44:03 +08008129try_again:
8130 done.uptodate = 0;
8131 done.start = start;
8132 init_completion(&done.done);
8133
Omar Sandoval58efbc92017-08-22 23:45:59 -07008134 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8135 pgoff, start, start + sectorsize - 1,
8136 io_bio->mirror_num, btrfs_retry_endio,
8137 &done);
8138 if (status) {
8139 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008140 goto next;
8141 }
8142
David Sterba9c17f6c2017-07-19 19:26:45 +02008143 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008144
8145 if (!done.uptodate) {
8146 /* We might have another mirror, so try again */
8147 goto try_again;
8148 }
8149next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150 offset += sectorsize;
8151 start += sectorsize;
8152
8153 ASSERT(nr_sectors);
8154
Liu Bo97bf5a52017-04-07 13:11:10 -07008155 nr_sectors--;
8156 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308157 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008158 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308159 goto next_block;
8160 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008161 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008162
8163 return err;
8164}
8165
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008166static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8167 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008168{
8169 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8170
8171 if (skip_csum) {
8172 if (unlikely(err))
8173 return __btrfs_correct_data_nocsum(inode, io_bio);
8174 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008175 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008176 } else {
8177 return __btrfs_subio_endio_read(inode, io_bio, err);
8178 }
8179}
8180
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008181static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008182{
8183 struct btrfs_dio_private *dip = bio->bi_private;
8184 struct inode *inode = dip->inode;
8185 struct bio *dio_bio;
8186 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008187 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008188
Liu Bo99c4e3b2017-09-15 15:06:51 -06008189 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008190 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008191
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008193 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008194 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195
Josef Bacik4b46fce2010-05-23 11:00:55 -04008196 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008197
Liu Bo99c4e3b2017-09-15 15:06:51 -06008198 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008199 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008200
8201 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008202 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008203 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008204}
8205
Qu Wenruo524272602017-03-08 10:25:52 +08008206static void __endio_write_update_ordered(struct inode *inode,
8207 const u64 offset, const u64 bytes,
8208 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008209{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008210 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008211 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008212 struct btrfs_workqueue *wq;
8213 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008214 u64 ordered_offset = offset;
8215 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008216 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008217 int ret;
8218
Qu Wenruo524272602017-03-08 10:25:52 +08008219 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8220 wq = fs_info->endio_freespace_worker;
8221 func = btrfs_freespace_write_helper;
8222 } else {
8223 wq = fs_info->endio_write_workers;
8224 func = btrfs_endio_write_helper;
8225 }
8226
Chris Mason163cf092010-11-28 19:56:33 -05008227again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008228 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008229 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8230 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008231 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008232 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008234 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008235
Qu Wenruo524272602017-03-08 10:25:52 +08008236 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8237 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008238out_test:
8239 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008240 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8241 * in the range, we can exit.
8242 */
8243 if (ordered_offset == last_offset)
8244 return;
8245 /*
Chris Mason163cf092010-11-28 19:56:33 -05008246 * our bio might span multiple ordered extents. If we haven't
8247 * completed the accounting for the whole dio, go back and try again
8248 */
Filipe Manana14543772015-11-24 16:23:54 +00008249 if (ordered_offset < offset + bytes) {
8250 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008251 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008252 goto again;
8253 }
Filipe Manana14543772015-11-24 16:23:54 +00008254}
8255
8256static void btrfs_endio_direct_write(struct bio *bio)
8257{
8258 struct btrfs_dio_private *dip = bio->bi_private;
8259 struct bio *dio_bio = dip->dio_bio;
8260
Qu Wenruo524272602017-03-08 10:25:52 +08008261 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008262 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008263
Josef Bacik4b46fce2010-05-23 11:00:55 -04008264 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008265
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008266 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008267 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008268 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008269}
8270
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008271static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008272 struct bio *bio, int mirror_num,
8273 unsigned long bio_flags, u64 offset)
8274{
Josef Bacikc6100a42017-05-05 11:57:13 -04008275 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008276 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008277 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008278 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008279 return 0;
8280}
8281
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008282static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008283{
8284 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008285 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008286
Miao Xie8b110e32014-09-12 18:44:03 +08008287 if (err)
8288 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008289 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008290 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8291 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008292 (unsigned long long)bio->bi_iter.bi_sector,
8293 bio->bi_iter.bi_size, err);
8294
8295 if (dip->subio_endio)
8296 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008297
8298 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008299 dip->errors = 1;
8300
8301 /*
8302 * before atomic variable goto zero, we must make sure
8303 * dip->errors is perceived to be set.
8304 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008305 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008306 }
8307
8308 /* if there are more bios still pending for this dio, just exit */
8309 if (!atomic_dec_and_test(&dip->pending_bios))
8310 goto out;
8311
Chris Mason9be33952013-05-17 18:30:14 -04008312 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008313 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008314 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008315 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008316 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008317 }
8318out:
8319 bio_put(bio);
8320}
8321
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008322static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008323 struct btrfs_dio_private *dip,
8324 struct bio *bio,
8325 u64 file_offset)
8326{
8327 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8328 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008329 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008330
8331 /*
8332 * We load all the csum data we need when we submit
8333 * the first bio to reduce the csum tree search and
8334 * contention.
8335 */
8336 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008337 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008338 file_offset);
8339 if (ret)
8340 return ret;
8341 }
8342
8343 if (bio == dip->orig_bio)
8344 return 0;
8345
8346 file_offset -= dip->logical_offset;
8347 file_offset >>= inode->i_sb->s_blocksize_bits;
8348 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8349
8350 return 0;
8351}
8352
Omar Sandoval58efbc92017-08-22 23:45:59 -07008353static inline blk_status_t
8354__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008355 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008358 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008359 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008360 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008361
Liu Bo4c274bc2017-11-01 17:19:27 -06008362 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008363 if (async_submit)
8364 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8365
Josef Bacik5fd02042012-05-02 14:00:54 -04008366 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008367 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008368 if (ret)
8369 goto err;
8370 }
Miao Xiee65e1532010-11-22 03:04:43 +00008371
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008372 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008373 goto map;
8374
8375 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008376 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8377 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008378 __btrfs_submit_bio_start_direct_io,
8379 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008380 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008381 } else if (write) {
8382 /*
8383 * If we aren't doing async submit, calculate the csum of the
8384 * bio now.
8385 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008386 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008387 if (ret)
8388 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008389 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008390 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008391 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008392 if (ret)
8393 goto err;
8394 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008395map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008396 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008397err:
Miao Xiee65e1532010-11-22 03:04:43 +00008398 return ret;
8399}
8400
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008401static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008402{
8403 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008404 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008405 struct bio *bio;
8406 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008407 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008408 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008409 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008410 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008411 u64 submit_len;
8412 int clone_offset = 0;
8413 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308414 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008415 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008416
Kent Overstreet4f024f32013-10-11 15:44:27 -07008417 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008418 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008419 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8420 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008421 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008422 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008423
Liu Bo725130b2017-05-16 09:51:39 -07008424 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008425 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008426 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008427 goto submit;
8428 }
8429
David Woodhouse53b381b2013-01-29 18:40:14 -05008430 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008431 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008432 async_submit = 0;
8433 else
8434 async_submit = 1;
8435
Liu Bo725130b2017-05-16 09:51:39 -07008436 /* bio split */
8437 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008438 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008439 do {
Liu Bo725130b2017-05-16 09:51:39 -07008440 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008441
Liu Bo725130b2017-05-16 09:51:39 -07008442 /*
8443 * This will never fail as it's passing GPF_NOFS and
8444 * the allocation is backed by btrfs_bioset.
8445 */
Liu Boe4770942017-05-16 10:57:14 -07008446 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008447 clone_len);
8448 bio->bi_private = dip;
8449 bio->bi_end_io = btrfs_end_dio_bio;
8450 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008451
Liu Bo725130b2017-05-16 09:51:39 -07008452 ASSERT(submit_len >= clone_len);
8453 submit_len -= clone_len;
8454 if (submit_len == 0)
8455 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008456
Liu Bo725130b2017-05-16 09:51:39 -07008457 /*
8458 * Increase the count before we submit the bio so we know
8459 * the end IO handler won't happen before we increase the
8460 * count. Otherwise, the dip might get freed before we're
8461 * done setting it up.
8462 */
8463 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008464
Linus Torvalds66ba7722017-09-09 13:27:51 -07008465 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008466 async_submit);
8467 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008468 bio_put(bio);
8469 atomic_dec(&dip->pending_bios);
8470 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008471 }
Liu Bo725130b2017-05-16 09:51:39 -07008472
8473 clone_offset += clone_len;
8474 start_sector += clone_len >> 9;
8475 file_offset += clone_len;
8476
8477 map_length = submit_len;
8478 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8479 start_sector << 9, &map_length, NULL, 0);
8480 if (ret)
8481 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008482 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008483
Josef Bacik02f57c72011-04-06 14:25:44 -04008484submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008485 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008486 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008487 return 0;
8488
8489 bio_put(bio);
8490out_err:
8491 dip->errors = 1;
8492 /*
8493 * before atomic variable goto zero, we must
8494 * make sure dip->errors is perceived to be set.
8495 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008496 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008497 if (atomic_dec_and_test(&dip->pending_bios))
8498 bio_io_error(dip->orig_bio);
8499
8500 /* bio_end_io() will handle error, so we needn't return it */
8501 return 0;
8502}
8503
Mike Christie8a4c1e42016-06-05 14:31:50 -05008504static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8505 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008506{
Filipe Manana61de7182015-07-01 12:13:10 +01008507 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008508 struct bio *bio = NULL;
8509 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008510 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008511 int ret = 0;
8512
David Sterba8b6c1d52017-06-02 17:48:13 +02008513 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008514
Miao Xiec1dc0892014-09-12 18:43:56 +08008515 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008516 if (!dip) {
8517 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008518 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008519 }
8520
8521 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522 dip->inode = inode;
8523 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008524 dip->bytes = dio_bio->bi_iter.bi_size;
8525 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008526 bio->bi_private = dip;
8527 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008528 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008529 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008530 io_bio = btrfs_io_bio(bio);
8531 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008532
Miao Xiec1dc0892014-09-12 18:43:56 +08008533 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008534 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008536 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008537 dip->subio_endio = btrfs_subio_endio_read;
8538 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008539
Filipe Mananaf28a4922015-12-08 19:23:20 +00008540 /*
8541 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8542 * even if we fail to submit a bio, because in such case we do the
8543 * corresponding error handling below and it must not be done a second
8544 * time by btrfs_direct_IO().
8545 */
8546 if (write) {
8547 struct btrfs_dio_data *dio_data = current->journal_info;
8548
8549 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8550 dip->bytes;
8551 dio_data->unsubmitted_oe_range_start =
8552 dio_data->unsubmitted_oe_range_end;
8553 }
8554
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008555 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008556 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008557 return;
Chris Mason9be33952013-05-17 18:30:14 -04008558
Liu Bo3892ac92017-04-17 15:00:28 -07008559 if (io_bio->end_io)
8560 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008561
Josef Bacik4b46fce2010-05-23 11:00:55 -04008562free_ordered:
8563 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008564 * If we arrived here it means either we failed to submit the dip
8565 * or we either failed to clone the dio_bio or failed to allocate the
8566 * dip. If we cloned the dio_bio and allocated the dip, we can just
8567 * call bio_endio against our io_bio so that we get proper resource
8568 * cleanup if we fail to submit the dip, otherwise, we must do the
8569 * same as btrfs_endio_direct_[write|read] because we can't call these
8570 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008571 */
Liu Bo3892ac92017-04-17 15:00:28 -07008572 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008573 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008574 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008575 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008576 * and all the cleanup and final put for dio_bio (through
8577 * dio_end_io()).
8578 */
8579 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008580 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008581 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008582 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008583 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008584 file_offset,
8585 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008586 false);
Filipe Manana14543772015-11-24 16:23:54 +00008587 else
Filipe Manana61de7182015-07-01 12:13:10 +01008588 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8589 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008590
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008591 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008592 /*
8593 * Releases and cleans up our dio_bio, no need to bio_put()
8594 * nor bio_endio()/bio_io_error() against dio_bio.
8595 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008596 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008597 }
Liu Bo3892ac92017-04-17 15:00:28 -07008598 if (bio)
8599 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008600 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008601}
8602
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008603static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008604 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008605{
8606 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008607 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008608 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008609 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008610
8611 if (offset & blocksize_mask)
8612 goto out;
8613
Al Viro28060d52014-03-22 05:15:17 -04008614 if (iov_iter_alignment(iter) & blocksize_mask)
8615 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008616
Al Viro28060d52014-03-22 05:15:17 -04008617 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008618 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008619 return 0;
8620 /*
8621 * Check to make sure we don't have duplicate iov_base's in this
8622 * iovec, if so return EINVAL, otherwise we'll get csum errors
8623 * when reading back.
8624 */
8625 for (seg = 0; seg < iter->nr_segs; seg++) {
8626 for (i = seg + 1; i < iter->nr_segs; i++) {
8627 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008628 goto out;
8629 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008630 }
8631 retval = 0;
8632out:
8633 return retval;
8634}
Josef Bacikeb838e72012-07-31 16:28:48 -04008635
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008636static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008637{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008638 struct file *file = iocb->ki_filp;
8639 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008640 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308641 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008642 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008643 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008644 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008645 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008646 bool wakeup = true;
8647 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008648 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008649
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008650 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008651 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008652
Jens Axboefe0f07d2015-04-15 17:05:48 -06008653 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008654
Josef Bacik0e267c42013-07-02 10:38:02 -04008655 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008656 * The generic stuff only does filemap_write_and_wait_range, which
8657 * isn't enough if we've written compressed pages to this area, so
8658 * we need to flush the dirty pages again to make absolutely sure
8659 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008660 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008661 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008662 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8663 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008664 filemap_fdatawrite_range(inode->i_mapping, offset,
8665 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008666
Omar Sandoval6f673762015-03-16 04:33:52 -07008667 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008668 /*
8669 * If the write DIO is beyond the EOF, we need update
8670 * the isize, but it is protected by i_mutex. So we can
8671 * not unlock the i_mutex at this case.
8672 */
8673 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008674 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008675 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008676 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008677 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8678 ret = -EAGAIN;
8679 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008680 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008681 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8682 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008683 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008684 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008685
8686 /*
8687 * We need to know how many extents we reserved so that we can
8688 * do the accounting properly if we go over the number we
8689 * originally calculated. Abuse current->journal_info for this.
8690 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008691 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008692 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008693 dio_data.unsubmitted_oe_range_start = (u64)offset;
8694 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308695 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308696 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008697 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8698 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008699 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008700 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8701 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008702 }
8703
Omar Sandoval17f8c842015-03-16 04:33:50 -07008704 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008705 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008706 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008707 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008708 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308709 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008710 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008711 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308712 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008713 btrfs_delalloc_release_space(inode, data_reserved,
8714 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008715 /*
8716 * On error we might have left some ordered extents
8717 * without submitting corresponding bios for them, so
8718 * cleanup them up to avoid other tasks getting them
8719 * and waiting for them to complete forever.
8720 */
8721 if (dio_data.unsubmitted_oe_range_start <
8722 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008723 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008724 dio_data.unsubmitted_oe_range_start,
8725 dio_data.unsubmitted_oe_range_end -
8726 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008727 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008728 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008729 btrfs_delalloc_release_space(inode, data_reserved,
8730 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008731 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008732 }
Miao Xie38851cc2013-02-08 07:04:11 +00008733out:
Miao Xie2e60a512013-02-08 07:01:08 +00008734 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008735 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008736 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008737 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008738
Qu Wenruo364ecf32017-02-27 15:10:38 +08008739 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008740 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008741}
8742
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008743#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8744
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008745static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8746 __u64 start, __u64 len)
8747{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008748 int ret;
8749
8750 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8751 if (ret)
8752 return ret;
8753
David Sterba2135fb92017-06-23 04:09:57 +02008754 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008755}
8756
Chris Mason9ebefb182007-06-15 13:50:00 -04008757int btrfs_readpage(struct file *file, struct page *page)
8758{
Chris Masond1310b22008-01-24 16:13:08 -05008759 struct extent_io_tree *tree;
8760 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008761 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008762}
Chris Mason1832a6d2007-12-21 16:27:21 -05008763
Chris Mason39279cc2007-06-12 06:35:45 -04008764static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8765{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008766 struct inode *inode = page->mapping->host;
8767 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008768
8769 if (current->flags & PF_MEMALLOC) {
8770 redirty_page_for_writepage(wbc, page);
8771 unlock_page(page);
8772 return 0;
8773 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008774
8775 /*
8776 * If we are under memory pressure we will call this directly from the
8777 * VM, we need to make sure we have the inode referenced for the ordered
8778 * extent. If not just return like we didn't do anything.
8779 */
8780 if (!igrab(inode)) {
8781 redirty_page_for_writepage(wbc, page);
8782 return AOP_WRITEPAGE_ACTIVATE;
8783 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008784 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008785 btrfs_add_delayed_iput(inode);
8786 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008787}
Chris Mason39279cc2007-06-12 06:35:45 -04008788
Eric Sandeen48a3b632013-04-25 20:41:01 +00008789static int btrfs_writepages(struct address_space *mapping,
8790 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008791{
Chris Masond1310b22008-01-24 16:13:08 -05008792 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008793
Chris Masond1310b22008-01-24 16:13:08 -05008794 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008795 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008796}
8797
Chris Mason3ab2fb52007-11-08 10:59:22 -05008798static int
8799btrfs_readpages(struct file *file, struct address_space *mapping,
8800 struct list_head *pages, unsigned nr_pages)
8801{
Chris Masond1310b22008-01-24 16:13:08 -05008802 struct extent_io_tree *tree;
8803 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008804 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008805}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008806static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008807{
Chris Masond1310b22008-01-24 16:13:08 -05008808 struct extent_io_tree *tree;
8809 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008810 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008811
Chris Masond1310b22008-01-24 16:13:08 -05008812 tree = &BTRFS_I(page->mapping->host)->io_tree;
8813 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008814 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008815 if (ret == 1) {
8816 ClearPagePrivate(page);
8817 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008818 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008819 }
8820 return ret;
8821}
Chris Mason39279cc2007-06-12 06:35:45 -04008822
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8824{
Chris Mason98509cf2008-09-11 15:51:43 -04008825 if (PageWriteback(page) || PageDirty(page))
8826 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008827 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008828}
8829
Lukas Czernerd47992f2013-05-21 23:17:23 -04008830static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8831 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008832{
Josef Bacik5fd02042012-05-02 14:00:54 -04008833 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008834 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008836 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008838 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308839 u64 start;
8840 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008841 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008842
Chris Mason8b62b722009-09-02 16:53:46 -04008843 /*
8844 * we have the page locked, so new writeback can't start,
8845 * and the dirty bit won't be cleared while we are here.
8846 *
8847 * Wait for IO on this page so that we can safely clear
8848 * the PagePrivate2 bit and do ordered accounting
8849 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008851
Josef Bacik5fd02042012-05-02 14:00:54 -04008852 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008853 if (offset) {
8854 btrfs_releasepage(page, GFP_NOFS);
8855 return;
8856 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008857
8858 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008859 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308860again:
8861 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008862 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308863 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008864 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308865 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008866 /*
8867 * IO on this page will never be started, so we need
8868 * to account for any ordered extents now
8869 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008870 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308871 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008872 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008873 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008874 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008875 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008876 /*
8877 * whoever cleared the private bit is responsible
8878 * for the finish_ordered_io
8879 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008880 if (TestClearPagePrivate2(page)) {
8881 struct btrfs_ordered_inode_tree *tree;
8882 u64 new_len;
8883
8884 tree = &BTRFS_I(inode)->ordered_tree;
8885
8886 spin_lock_irq(&tree->lock);
8887 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308888 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008889 if (new_len < ordered->truncated_len)
8890 ordered->truncated_len = new_len;
8891 spin_unlock_irq(&tree->lock);
8892
8893 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308894 start,
8895 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008896 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008897 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008898 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008899 if (!inode_evicting) {
8900 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308901 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008902 &cached_state);
8903 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308904
8905 start = end + 1;
8906 if (start < page_end)
8907 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008909
Qu Wenruob9d0b382015-09-29 10:35:16 +08008910 /*
8911 * Qgroup reserved space handler
8912 * Page here will be either
8913 * 1) Already written to disk
8914 * In this case, its reserved space is released from data rsv map
8915 * and will be freed by delayed_ref handler finally.
8916 * So even we call qgroup_free_data(), it won't decrease reserved
8917 * space.
8918 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008919 * This means the reserved space should be freed here. However,
8920 * if a truncate invalidates the page (by clearing PageDirty)
8921 * and the page is accounted for while allocating extent
8922 * in btrfs_check_data_free_space() we let delayed_ref to
8923 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008924 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008925 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008926 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008927 if (!inode_evicting) {
8928 clear_extent_bit(tree, page_start, page_end,
8929 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008930 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8931 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008932 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008933
8934 __btrfs_releasepage(page, GFP_NOFS);
8935 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008936
Chris Mason4a096752008-07-21 10:29:44 -04008937 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008938 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008939 ClearPagePrivate(page);
8940 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008941 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008942 }
Chris Mason39279cc2007-06-12 06:35:45 -04008943}
8944
Chris Mason9ebefb182007-06-15 13:50:00 -04008945/*
8946 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8947 * called from a page fault handler when a page is first dirtied. Hence we must
8948 * be careful to check for EOF conditions here. We set the page up correctly
8949 * for a written page which means we get ENOSPC checking when writing into
8950 * holes and correct delalloc and unwritten extent mapping on filesystems that
8951 * support these features.
8952 *
8953 * We are not allowed to take the i_mutex here so we have to play games to
8954 * protect against truncate races as the page could now be beyond EOF. Because
8955 * vmtruncate() writes the inode size before removing pages, once we have the
8956 * page lock we can determine safely if the page is beyond EOF. If it is not
8957 * beyond EOF, then the page is guaranteed safe against truncation until we
8958 * unlock the page.
8959 */
Dave Jiang11bac802017-02-24 14:56:41 -08008960int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008961{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008962 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008963 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8966 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008967 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008968 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008969 char *kaddr;
8970 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008971 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008972 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008973 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308974 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008975 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308977 u64 end;
8978
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008980
Jan Karab2b5ef52012-06-12 16:20:45 +02008981 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008982 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008983 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308984 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008985
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 /*
8987 * Reserving delalloc space after obtaining the page lock can lead to
8988 * deadlock. For example, if a dirty page is locked by this function
8989 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8990 * dirty page write out, then the btrfs_writepage() function could
8991 * end up waiting indefinitely to get a lock on the page currently
8992 * being processed by btrfs_page_mkwrite() function.
8993 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008994 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308995 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008996 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008997 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008998 reserved = 1;
8999 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009000 if (ret) {
9001 if (ret == -ENOMEM)
9002 ret = VM_FAULT_OOM;
9003 else /* -ENOSPC, -EIO, etc */
9004 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009005 if (reserved)
9006 goto out;
9007 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009008 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009009
Nick Piggin56a76f82009-03-31 15:23:23 -07009010 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009011again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009012 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009013 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009014
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009017 /* page got truncated out from underneath us */
9018 goto out_unlock;
9019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020 wait_on_page_writeback(page);
9021
David Sterbaff13db42015-12-03 14:30:40 +01009022 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023 set_page_extent_mapped(page);
9024
Chris Masoneb84ae02008-07-17 13:53:27 -04009025 /*
9026 * we can't set the delalloc bits if there are pending ordered
9027 * extents. Drop our locks and wait for them to finish
9028 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009029 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9030 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009031 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009032 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009033 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009034 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009035 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009036 btrfs_put_ordered_extent(ordered);
9037 goto again;
9038 }
9039
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009040 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009041 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009042 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009043 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309044 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009045 btrfs_delalloc_release_space(inode, data_reserved,
9046 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309047 }
9048 }
9049
Josef Bacikfbf19082009-10-01 17:10:23 -04009050 /*
Liu Bo54160342016-12-13 12:15:19 -08009051 * page_mkwrite gets called when the page is firstly dirtied after it's
9052 * faulted in, but write(2) could also dirty a page and set delalloc
9053 * bits, thus in this case for space account reason, we still need to
9054 * clear any delalloc bits within this page range since we have to
9055 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009056 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309057 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009058 EXTENT_DIRTY | EXTENT_DELALLOC |
9059 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009060 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009061
Filipe Mananae3b8a482017-11-04 00:16:59 +00009062 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009063 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009064 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009065 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009066 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009067 ret = VM_FAULT_SIGBUS;
9068 goto out_unlock;
9069 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009070 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009071
9072 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009073 if (page_start + PAGE_SIZE > size)
9074 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009075 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009076 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009077
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009078 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009079 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009080 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009081 flush_dcache_page(page);
9082 kunmap(page);
9083 }
Chris Mason247e7432008-07-17 12:53:51 -04009084 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009085 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009086 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009087
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009088 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009089 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009090 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009091
David Sterbae43bbe52017-12-12 21:43:52 +01009092 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009093
9094out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009095 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009096 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009097 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009098 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009099 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009100 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009101 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009102out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009103 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009104 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9105 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009106out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009107 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009108 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009109 return ret;
9110}
9111
Josef Bacika41ad392011-01-31 15:30:16 -05009112static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009113{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009115 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009116 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009117 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009118 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009119 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009120 u64 mask = fs_info->sectorsize - 1;
9121 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009122
Josef Bacik0ef8b722013-10-25 16:13:35 -04009123 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9124 (u64)-1);
9125 if (ret)
9126 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009127
Josef Bacikfcb80c22011-05-03 10:40:22 -04009128 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009129 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009130 * 3 things going on here
9131 *
9132 * 1) We need to reserve space for our orphan item and the space to
9133 * delete our orphan item. Lord knows we don't want to have a dangling
9134 * orphan item because we didn't reserve space to remove it.
9135 *
9136 * 2) We need to reserve space to update our inode.
9137 *
9138 * 3) We need to have something to cache all the space that is going to
9139 * be free'd up by the truncate operation, but also have some slack
9140 * space reserved in case it uses space during the truncate (thank you
9141 * very much snapshotting).
9142 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009143 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009144 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009145 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009146 * doesn't end up using space reserved for updating the inode or
9147 * removing the orphan item. We also need to be able to stop the
9148 * transaction and start a new one, which means we need to be able to
9149 * update the inode several times, and we have no idea of knowing how
9150 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009151 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009152 * Then there is the orphan item, which does indeed need to be held on
9153 * to for the whole operation, and we need nobody to touch this reserved
9154 * space except the orphan code.
9155 *
9156 * So that leaves us with
9157 *
9158 * 1) root->orphan_block_rsv - for the orphan deletion.
9159 * 2) rsv - for the truncate reservation, which we will steal from the
9160 * transaction reservation.
9161 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9162 * updating the inode.
9163 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009164 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009165 if (!rsv)
9166 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009167 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009168 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009169
Josef Bacik907cbce2011-08-08 13:46:15 -04009170 /*
Josef Bacik07127182011-08-19 10:29:59 -04009171 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009172 * 1 for updating the inode.
9173 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009174 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009175 if (IS_ERR(trans)) {
9176 err = PTR_ERR(trans);
9177 goto out;
9178 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009179
Josef Bacik907cbce2011-08-08 13:46:15 -04009180 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009181 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009182 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009183 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009184
Chris Mason5a3f23d2009-03-31 13:27:11 -04009185 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009186 * So if we truncate and then write and fsync we normally would just
9187 * write the extents that changed, which is a problem if we need to
9188 * first truncate that entire inode. So set this flag so we write out
9189 * all of the extents in the inode to the sync log so we're completely
9190 * safe.
9191 */
9192 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009193 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009194
Yan, Zheng80825102009-11-12 09:35:36 +00009195 while (1) {
9196 ret = btrfs_truncate_inode_items(trans, root, inode,
9197 inode->i_size,
9198 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009199 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009200 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009201 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009202 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009203 }
Chris Mason39279cc2007-06-12 06:35:45 -04009204
Yan, Zheng80825102009-11-12 09:35:36 +00009205 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009206 if (ret) {
9207 err = ret;
9208 break;
9209 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009210
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009211 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009212 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009213
9214 trans = btrfs_start_transaction(root, 2);
9215 if (IS_ERR(trans)) {
9216 ret = err = PTR_ERR(trans);
9217 trans = NULL;
9218 break;
9219 }
9220
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009221 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009222 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009223 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009224 BUG_ON(ret); /* shouldn't happen */
9225 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009226 }
9227
Josef Bacikddfae632017-10-19 14:16:02 -04009228 /*
9229 * We can't call btrfs_truncate_block inside a trans handle as we could
9230 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9231 * we've truncated everything except the last little bit, and can do
9232 * btrfs_truncate_block and then update the disk_i_size.
9233 */
9234 if (ret == NEED_TRUNCATE_BLOCK) {
9235 btrfs_end_transaction(trans);
9236 btrfs_btree_balance_dirty(fs_info);
9237
9238 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9239 if (ret)
9240 goto out;
9241 trans = btrfs_start_transaction(root, 1);
9242 if (IS_ERR(trans)) {
9243 ret = PTR_ERR(trans);
9244 goto out;
9245 }
9246 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9247 }
9248
Yan, Zheng80825102009-11-12 09:35:36 +00009249 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009251 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009252 if (ret)
9253 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009254 }
9255
Chris Mason917c16b2011-11-08 14:49:59 -05009256 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009257 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009258 ret = btrfs_update_inode(trans, root, inode);
9259 if (ret && !err)
9260 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009261
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009262 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009263 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009264 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009265out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009266 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009267
Josef Bacik3893e332011-01-31 16:03:11 -05009268 if (ret && !err)
9269 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009270
Josef Bacik3893e332011-01-31 16:03:11 -05009271 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009272}
9273
Sven Wegener3b963622008-06-09 21:57:42 -04009274/*
Chris Masond352ac62008-09-29 15:18:18 -04009275 * create a new subvolume directory/inode (helper for the ioctl).
9276 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009277int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009278 struct btrfs_root *new_root,
9279 struct btrfs_root *parent_root,
9280 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009281{
Chris Mason39279cc2007-06-12 06:35:45 -04009282 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009283 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009284 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009285
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009286 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9287 new_dirid, new_dirid,
9288 S_IFDIR | (~current_umask() & S_IRWXUGO),
9289 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009290 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009291 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009292 inode->i_op = &btrfs_dir_inode_operations;
9293 inode->i_fop = &btrfs_dir_file_operations;
9294
Miklos Szeredibfe86842011-10-28 14:13:29 +02009295 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009296 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009297 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009298
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009299 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9300 if (err)
9301 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009302 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009303 new_root->root_key.objectid, err);
9304
Yan, Zheng76dda932009-09-21 16:00:26 -04009305 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009306
Yan, Zheng76dda932009-09-21 16:00:26 -04009307 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009308 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009309}
9310
Chris Mason39279cc2007-06-12 06:35:45 -04009311struct inode *btrfs_alloc_inode(struct super_block *sb)
9312{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009313 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009314 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009316
David Sterba712e36c2017-10-31 17:08:27 +01009317 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009318 if (!ei)
9319 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320
9321 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009322 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009323 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009324 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009325 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009327 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009328 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009329 ei->disk_i_size = 0;
9330 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009331 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009332 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009333 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009334 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009335 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009336 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009337
Josef Bacik9e0baf62011-07-15 15:16:44 +00009338 spin_lock_init(&ei->lock);
9339 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009340 if (sb->s_magic != BTRFS_TEST_MAGIC)
9341 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9342 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009343 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009344 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009345 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009346
Miao Xie16cdcec2011-04-22 18:12:22 +08009347 ei->delayed_node = NULL;
9348
chandan r9cc97d62012-07-04 12:48:07 +05309349 ei->i_otime.tv_sec = 0;
9350 ei->i_otime.tv_nsec = 0;
9351
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009352 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009353 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009354 extent_io_tree_init(&ei->io_tree, inode);
9355 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009356 ei->io_tree.track_uptodate = 1;
9357 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009358 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009359 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009360 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009361 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009362 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009363 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009364 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009365 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009366
9367 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009368}
9369
Josef Bacikaaedb552013-10-11 14:44:09 -04009370#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9371void btrfs_test_destroy_inode(struct inode *inode)
9372{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009373 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009374 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9375}
9376#endif
9377
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009378static void btrfs_i_callback(struct rcu_head *head)
9379{
9380 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009381 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9382}
9383
Chris Mason39279cc2007-06-12 06:35:45 -04009384void btrfs_destroy_inode(struct inode *inode)
9385{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009386 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009387 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009388 struct btrfs_root *root = BTRFS_I(inode)->root;
9389
Al Virob3d9b7a2012-06-09 13:51:19 -04009390 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009391 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009392 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9393 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009394 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009395 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009396 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009397 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009398 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009399
Chris Mason5a3f23d2009-03-31 13:27:11 -04009400 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009401 * This can happen where we create an inode, but somebody else also
9402 * created the same inode and we need to destroy the one we already
9403 * created.
9404 */
9405 if (!root)
9406 goto free;
9407
Josef Bacik8a35d952012-05-23 14:26:42 -04009408 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9409 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009410 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009411 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009412 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009413 }
Josef Bacik7b128762008-07-24 12:17:14 -04009414
Chris Masond3977122009-01-05 21:25:51 -05009415 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009416 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9417 if (!ordered)
9418 break;
9419 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009420 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009421 "found ordered extent %llu %llu on inode cleanup",
9422 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009423 btrfs_remove_ordered_extent(inode, ordered);
9424 btrfs_put_ordered_extent(ordered);
9425 btrfs_put_ordered_extent(ordered);
9426 }
9427 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009428 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009429 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009430 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009431free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009432 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009433}
9434
Al Viro45321ac2010-06-07 13:43:19 -04009435int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009436{
9437 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009438
Naohiro Aota6379ef92013-06-06 09:56:34 +00009439 if (root == NULL)
9440 return 1;
9441
Liu Bofa6ac872013-02-20 14:10:23 +00009442 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009443 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009444 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009445 else
Al Viro45321ac2010-06-07 13:43:19 -04009446 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009447}
9448
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009449static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009450{
9451 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9452
9453 inode_init_once(&ei->vfs_inode);
9454}
9455
9456void btrfs_destroy_cachep(void)
9457{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009458 /*
9459 * Make sure all delayed rcu free inodes are flushed before we
9460 * destroy cache.
9461 */
9462 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009463 kmem_cache_destroy(btrfs_inode_cachep);
9464 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009465 kmem_cache_destroy(btrfs_path_cachep);
9466 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009467}
9468
Liu Bof5c29bd2017-11-02 17:21:50 -06009469int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009470{
David Sterba837e1972012-09-07 03:00:48 -06009471 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009472 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009473 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9474 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009475 if (!btrfs_inode_cachep)
9476 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009477
David Sterba837e1972012-09-07 03:00:48 -06009478 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009479 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009480 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009481 if (!btrfs_trans_handle_cachep)
9482 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009483
David Sterba837e1972012-09-07 03:00:48 -06009484 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009485 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009486 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009487 if (!btrfs_path_cachep)
9488 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009489
David Sterba837e1972012-09-07 03:00:48 -06009490 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009491 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009492 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009493 if (!btrfs_free_space_cachep)
9494 goto fail;
9495
Chris Mason39279cc2007-06-12 06:35:45 -04009496 return 0;
9497fail:
9498 btrfs_destroy_cachep();
9499 return -ENOMEM;
9500}
9501
David Howellsa528d352017-01-31 16:46:22 +00009502static int btrfs_getattr(const struct path *path, struct kstat *stat,
9503 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009504{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009505 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009506 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009507 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009508 u32 bi_flags = BTRFS_I(inode)->flags;
9509
9510 stat->result_mask |= STATX_BTIME;
9511 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9512 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9513 if (bi_flags & BTRFS_INODE_APPEND)
9514 stat->attributes |= STATX_ATTR_APPEND;
9515 if (bi_flags & BTRFS_INODE_COMPRESS)
9516 stat->attributes |= STATX_ATTR_COMPRESSED;
9517 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9518 stat->attributes |= STATX_ATTR_IMMUTABLE;
9519 if (bi_flags & BTRFS_INODE_NODUMP)
9520 stat->attributes |= STATX_ATTR_NODUMP;
9521
9522 stat->attributes_mask |= (STATX_ATTR_APPEND |
9523 STATX_ATTR_COMPRESSED |
9524 STATX_ATTR_IMMUTABLE |
9525 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009526
Chris Mason39279cc2007-06-12 06:35:45 -04009527 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009528 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009529
9530 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009531 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009532 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009533 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009534 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009535 return 0;
9536}
9537
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538static int btrfs_rename_exchange(struct inode *old_dir,
9539 struct dentry *old_dentry,
9540 struct inode *new_dir,
9541 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009542{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009543 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009544 struct btrfs_trans_handle *trans;
9545 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009546 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009547 struct inode *new_inode = new_dentry->d_inode;
9548 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009549 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009551 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9552 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 u64 old_idx = 0;
9554 u64 new_idx = 0;
9555 u64 root_objectid;
9556 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009557 bool root_log_pinned = false;
9558 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009559
9560 /* we only allow rename subvolume link between subvolumes */
9561 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9562 return -EXDEV;
9563
9564 /* close the race window with snapshot create/destroy ioctl */
9565 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009566 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009568 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569
9570 /*
9571 * We want to reserve the absolute worst case amount of items. So if
9572 * both inodes are subvols and we need to unlink them then that would
9573 * require 4 item modifications, but if they are both normal inodes it
9574 * would require 5 item modifications, so we'll assume their normal
9575 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9576 * should cover the worst case number of items we'll modify.
9577 */
9578 trans = btrfs_start_transaction(root, 12);
9579 if (IS_ERR(trans)) {
9580 ret = PTR_ERR(trans);
9581 goto out_notrans;
9582 }
9583
9584 /*
9585 * We need to find a free sequence number both in the source and
9586 * in the destination directory for the exchange.
9587 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009588 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 if (ret)
9590 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009591 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 if (ret)
9593 goto out_fail;
9594
9595 BTRFS_I(old_inode)->dir_index = 0ULL;
9596 BTRFS_I(new_inode)->dir_index = 0ULL;
9597
9598 /* Reference for the source. */
9599 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9600 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009601 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009603 btrfs_pin_log_trans(root);
9604 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 ret = btrfs_insert_inode_ref(trans, dest,
9606 new_dentry->d_name.name,
9607 new_dentry->d_name.len,
9608 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009609 btrfs_ino(BTRFS_I(new_dir)),
9610 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 if (ret)
9612 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 }
9614
9615 /* And now for the dest. */
9616 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9617 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009618 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009620 btrfs_pin_log_trans(dest);
9621 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622 ret = btrfs_insert_inode_ref(trans, root,
9623 old_dentry->d_name.name,
9624 old_dentry->d_name.len,
9625 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009626 btrfs_ino(BTRFS_I(old_dir)),
9627 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 if (ret)
9629 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 }
9631
9632 /* Update inode version and ctime/mtime. */
9633 inode_inc_iversion(old_dir);
9634 inode_inc_iversion(new_dir);
9635 inode_inc_iversion(old_inode);
9636 inode_inc_iversion(new_inode);
9637 old_dir->i_ctime = old_dir->i_mtime = ctime;
9638 new_dir->i_ctime = new_dir->i_mtime = ctime;
9639 old_inode->i_ctime = ctime;
9640 new_inode->i_ctime = ctime;
9641
9642 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009643 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9644 BTRFS_I(old_inode), 1);
9645 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9646 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 }
9648
9649 /* src is a subvolume */
9650 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9651 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9652 ret = btrfs_unlink_subvol(trans, root, old_dir,
9653 root_objectid,
9654 old_dentry->d_name.name,
9655 old_dentry->d_name.len);
9656 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009657 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9658 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 old_dentry->d_name.name,
9660 old_dentry->d_name.len);
9661 if (!ret)
9662 ret = btrfs_update_inode(trans, root, old_inode);
9663 }
9664 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009665 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666 goto out_fail;
9667 }
9668
9669 /* dest is a subvolume */
9670 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9671 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9672 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9673 root_objectid,
9674 new_dentry->d_name.name,
9675 new_dentry->d_name.len);
9676 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009677 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9678 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 new_dentry->d_name.name,
9680 new_dentry->d_name.len);
9681 if (!ret)
9682 ret = btrfs_update_inode(trans, dest, new_inode);
9683 }
9684 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009685 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 goto out_fail;
9687 }
9688
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009689 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690 new_dentry->d_name.name,
9691 new_dentry->d_name.len, 0, old_idx);
9692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009693 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 goto out_fail;
9695 }
9696
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009697 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 old_dentry->d_name.name,
9699 old_dentry->d_name.len, 0, new_idx);
9700 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009701 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 goto out_fail;
9703 }
9704
9705 if (old_inode->i_nlink == 1)
9706 BTRFS_I(old_inode)->dir_index = old_idx;
9707 if (new_inode->i_nlink == 1)
9708 BTRFS_I(new_inode)->dir_index = new_idx;
9709
Filipe Manana86e8aa02016-05-05 02:02:27 +01009710 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009712 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9713 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009715 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009717 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009719 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9720 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009721 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009722 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009723 }
9724out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009725 /*
9726 * If we have pinned a log and an error happened, we unpin tasks
9727 * trying to sync the log and force them to fallback to a transaction
9728 * commit if the log currently contains any of the inodes involved in
9729 * this rename operation (to ensure we do not persist a log with an
9730 * inconsistent state for any of these inodes or leading to any
9731 * inconsistencies when replayed). If the transaction was aborted, the
9732 * abortion reason is propagated to userspace when attempting to commit
9733 * the transaction. If the log does not contain any of these inodes, we
9734 * allow the tasks to sync it.
9735 */
9736 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009737 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9738 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9739 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009740 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009741 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009742 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009743
9744 if (root_log_pinned) {
9745 btrfs_end_log_trans(root);
9746 root_log_pinned = false;
9747 }
9748 if (dest_log_pinned) {
9749 btrfs_end_log_trans(dest);
9750 dest_log_pinned = false;
9751 }
9752 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009753 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754out_notrans:
9755 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009756 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009758 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759
9760 return ret;
9761}
9762
9763static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9764 struct btrfs_root *root,
9765 struct inode *dir,
9766 struct dentry *dentry)
9767{
9768 int ret;
9769 struct inode *inode;
9770 u64 objectid;
9771 u64 index;
9772
9773 ret = btrfs_find_free_ino(root, &objectid);
9774 if (ret)
9775 return ret;
9776
9777 inode = btrfs_new_inode(trans, root, dir,
9778 dentry->d_name.name,
9779 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009780 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 objectid,
9782 S_IFCHR | WHITEOUT_MODE,
9783 &index);
9784
9785 if (IS_ERR(inode)) {
9786 ret = PTR_ERR(inode);
9787 return ret;
9788 }
9789
9790 inode->i_op = &btrfs_special_inode_operations;
9791 init_special_inode(inode, inode->i_mode,
9792 WHITEOUT_DEV);
9793
9794 ret = btrfs_init_inode_security(trans, inode, dir,
9795 &dentry->d_name);
9796 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009797 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009799 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9800 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009802 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803
9804 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009805out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009807 if (ret)
9808 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809 iput(inode);
9810
Filipe Mananac9901612016-05-05 01:41:57 +01009811 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812}
9813
Chris Mason39279cc2007-06-12 06:35:45 -04009814static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009815 struct inode *new_dir, struct dentry *new_dentry,
9816 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009818 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009819 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009820 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009821 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9822 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009823 struct inode *new_inode = d_inode(new_dentry);
9824 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009825 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009826 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009827 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009828 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009829 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009830
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009831 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009832 return -EPERM;
9833
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009834 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009835 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009836 return -EXDEV;
9837
Li Zefan33345d012011-04-20 10:31:50 +08009838 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009839 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009840 return -ENOTEMPTY;
9841
Chris Mason39279cc2007-06-12 06:35:45 -04009842 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009843 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009844 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009845
9846
9847 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009848 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009849 new_dentry->d_name.name,
9850 new_dentry->d_name.len);
9851
9852 if (ret) {
9853 if (ret == -EEXIST) {
9854 /* we shouldn't get
9855 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309856 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009857 return ret;
9858 }
9859 } else {
9860 /* maybe -EOVERFLOW */
9861 return ret;
9862 }
9863 }
9864 ret = 0;
9865
Chris Mason5a3f23d2009-03-31 13:27:11 -04009866 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009867 * we're using rename to replace one file with another. Start IO on it
9868 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009869 */
Chris Mason8d875f92014-08-12 10:47:42 -07009870 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009871 filemap_flush(old_inode->i_mapping);
9872
Yan, Zheng76dda932009-09-21 16:00:26 -04009873 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009874 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009875 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009876 /*
9877 * We want to reserve the absolute worst case amount of items. So if
9878 * both inodes are subvols and we need to unlink them then that would
9879 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009880 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009881 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9882 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009883 * If our rename has the whiteout flag, we need more 5 units for the
9884 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9885 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009886 */
Filipe Manana5062af32016-05-05 10:26:26 +01009887 trans_num_items = 11;
9888 if (flags & RENAME_WHITEOUT)
9889 trans_num_items += 5;
9890 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009891 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009892 ret = PTR_ERR(trans);
9893 goto out_notrans;
9894 }
Chris Mason5f39d392007-10-15 16:14:19 -04009895
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009896 if (dest != root)
9897 btrfs_record_root_in_trans(trans, dest);
9898
Nikolay Borisov877574e2017-02-20 13:50:33 +02009899 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009900 if (ret)
9901 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009902
Miao Xie67de1172013-12-26 13:07:06 +08009903 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009904 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009905 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009906 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009907 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009908 btrfs_pin_log_trans(root);
9909 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009910 ret = btrfs_insert_inode_ref(trans, dest,
9911 new_dentry->d_name.name,
9912 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009913 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009914 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009915 if (ret)
9916 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009917 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009918
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009919 inode_inc_iversion(old_dir);
9920 inode_inc_iversion(new_dir);
9921 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009922 old_dir->i_ctime = old_dir->i_mtime =
9923 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009924 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009925
Chris Mason12fcfd22009-03-24 10:24:20 -04009926 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009927 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9928 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009929
Li Zefan33345d012011-04-20 10:31:50 +08009930 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009931 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9932 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9933 old_dentry->d_name.name,
9934 old_dentry->d_name.len);
9935 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009936 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9937 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009938 old_dentry->d_name.name,
9939 old_dentry->d_name.len);
9940 if (!ret)
9941 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009942 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009943 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009944 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009945 goto out_fail;
9946 }
Chris Mason39279cc2007-06-12 06:35:45 -04009947
9948 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009949 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009950 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009951 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009952 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9953 root_objectid = BTRFS_I(new_inode)->location.objectid;
9954 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9955 root_objectid,
9956 new_dentry->d_name.name,
9957 new_dentry->d_name.len);
9958 BUG_ON(new_inode->i_nlink == 0);
9959 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009960 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9961 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009962 new_dentry->d_name.name,
9963 new_dentry->d_name.len);
9964 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009965 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009966 ret = btrfs_orphan_add(trans,
9967 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009968 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009969 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009970 goto out_fail;
9971 }
Chris Mason39279cc2007-06-12 06:35:45 -04009972 }
Josef Bacikaec74772008-07-24 12:12:38 -04009973
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009974 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009975 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009976 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009977 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009978 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009979 goto out_fail;
9980 }
Chris Mason39279cc2007-06-12 06:35:45 -04009981
Miao Xie67de1172013-12-26 13:07:06 +08009982 if (old_inode->i_nlink == 1)
9983 BTRFS_I(old_inode)->dir_index = index;
9984
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009985 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009986 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009987
David Sterbaf85b7372017-01-20 14:54:07 +01009988 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9989 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009990 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009991 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009992 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009993
9994 if (flags & RENAME_WHITEOUT) {
9995 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9996 old_dentry);
9997
9998 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009999 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010000 goto out_fail;
10001 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010002 }
Chris Mason39279cc2007-06-12 06:35:45 -040010003out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010004 /*
10005 * If we have pinned the log and an error happened, we unpin tasks
10006 * trying to sync the log and force them to fallback to a transaction
10007 * commit if the log currently contains any of the inodes involved in
10008 * this rename operation (to ensure we do not persist a log with an
10009 * inconsistent state for any of these inodes or leading to any
10010 * inconsistencies when replayed). If the transaction was aborted, the
10011 * abortion reason is propagated to userspace when attempting to commit
10012 * the transaction. If the log does not contain any of these inodes, we
10013 * allow the tasks to sync it.
10014 */
10015 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010016 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10017 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10018 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010019 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010020 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010021 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010022
10023 btrfs_end_log_trans(root);
10024 log_pinned = false;
10025 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010026 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010027out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010028 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010029 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010030
Chris Mason39279cc2007-06-12 06:35:45 -040010031 return ret;
10032}
10033
Miklos Szeredi80ace852014-07-23 15:15:32 +020010034static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10035 struct inode *new_dir, struct dentry *new_dentry,
10036 unsigned int flags)
10037{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010038 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010039 return -EINVAL;
10040
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010041 if (flags & RENAME_EXCHANGE)
10042 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10043 new_dentry);
10044
10045 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010046}
10047
Miao Xie8ccf6f192012-10-25 09:28:04 +000010048static void btrfs_run_delalloc_work(struct btrfs_work *work)
10049{
10050 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010051 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010052
10053 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10054 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010055 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010056 filemap_flush(inode->i_mapping);
10057 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10058 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010059 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010060
10061 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010062 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010063 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010064 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010065 complete(&delalloc_work->completion);
10066}
10067
10068struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010069 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010070{
10071 struct btrfs_delalloc_work *work;
10072
David Sterba100d5702015-12-08 14:39:32 +010010073 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010074 if (!work)
10075 return NULL;
10076
10077 init_completion(&work->completion);
10078 INIT_LIST_HEAD(&work->list);
10079 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010080 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010081 WARN_ON_ONCE(!inode);
10082 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10083 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010084
10085 return work;
10086}
10087
10088void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10089{
10090 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010091 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010092}
10093
Chris Masond352ac62008-09-29 15:18:18 -040010094/*
10095 * some fairly slow code that needs optimization. This walks the list
10096 * of all the inodes with pending delalloc and forces them to disk.
10097 */
Miao Xie6c255e62014-03-06 13:55:01 +080010098static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10099 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010100{
Chris Masonea8c2812008-08-04 23:17:27 -040010101 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010102 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010103 struct btrfs_delalloc_work *work, *next;
10104 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010105 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010106 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010107
Miao Xie8ccf6f192012-10-25 09:28:04 +000010108 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010109 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010110
Miao Xie573bfb72014-03-06 13:55:03 +080010111 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 spin_lock(&root->delalloc_lock);
10113 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010114 while (!list_empty(&splice)) {
10115 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010116 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010117
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 list_move_tail(&binode->delalloc_inodes,
10119 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010120 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010121 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010122 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010123 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010124 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010125 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010126
David Sterba651d4942015-11-27 19:24:16 +010010127 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010128 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010129 if (delay_iput)
10130 btrfs_add_delayed_iput(inode);
10131 else
10132 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010133 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010134 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010135 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010136 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010137 btrfs_queue_work(root->fs_info->flush_workers,
10138 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010139 ret++;
10140 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010141 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010142 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010144 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010145 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010146
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010147out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010148 list_for_each_entry_safe(work, next, &works, list) {
10149 list_del_init(&work->list);
10150 btrfs_wait_and_free_delalloc_work(work);
10151 }
10152
Miao Xieeb73c1b2013-05-15 07:48:22 +000010153 if (!list_empty_careful(&splice)) {
10154 spin_lock(&root->delalloc_lock);
10155 list_splice_tail(&splice, &root->delalloc_inodes);
10156 spin_unlock(&root->delalloc_lock);
10157 }
Miao Xie573bfb72014-03-06 13:55:03 +080010158 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 return ret;
10160}
10161
10162int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10163{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010164 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010165 int ret;
10166
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010167 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010168 return -EROFS;
10169
Miao Xie6c255e62014-03-06 13:55:01 +080010170 ret = __start_delalloc_inodes(root, delay_iput, -1);
10171 if (ret > 0)
10172 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010173 return ret;
10174}
10175
Miao Xie6c255e62014-03-06 13:55:01 +080010176int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10177 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010178{
10179 struct btrfs_root *root;
10180 struct list_head splice;
10181 int ret;
10182
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010183 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010184 return -EROFS;
10185
10186 INIT_LIST_HEAD(&splice);
10187
Miao Xie573bfb72014-03-06 13:55:03 +080010188 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010189 spin_lock(&fs_info->delalloc_root_lock);
10190 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010191 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010192 root = list_first_entry(&splice, struct btrfs_root,
10193 delalloc_root);
10194 root = btrfs_grab_fs_root(root);
10195 BUG_ON(!root);
10196 list_move_tail(&root->delalloc_root,
10197 &fs_info->delalloc_roots);
10198 spin_unlock(&fs_info->delalloc_root_lock);
10199
Miao Xie6c255e62014-03-06 13:55:01 +080010200 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010201 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010202 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 goto out;
10204
Miao Xie6c255e62014-03-06 13:55:01 +080010205 if (nr != -1) {
10206 nr -= ret;
10207 WARN_ON(nr < 0);
10208 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 spin_lock(&fs_info->delalloc_root_lock);
10210 }
10211 spin_unlock(&fs_info->delalloc_root_lock);
10212
Miao Xie6c255e62014-03-06 13:55:01 +080010213 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010214out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010215 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010216 spin_lock(&fs_info->delalloc_root_lock);
10217 list_splice_tail(&splice, &fs_info->delalloc_roots);
10218 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010219 }
Miao Xie573bfb72014-03-06 13:55:03 +080010220 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010221 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010222}
10223
Chris Mason39279cc2007-06-12 06:35:45 -040010224static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10225 const char *symname)
10226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010227 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010228 struct btrfs_trans_handle *trans;
10229 struct btrfs_root *root = BTRFS_I(dir)->root;
10230 struct btrfs_path *path;
10231 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010232 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010233 int err;
10234 int drop_inode = 0;
10235 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010236 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010237 int name_len;
10238 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010239 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010240 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010241 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010242
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010243 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010244 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010245 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010246
Josef Bacik9ed74f22009-09-11 16:12:44 -040010247 /*
10248 * 2 items for inode item and ref
10249 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010250 * 1 item for updating parent inode item
10251 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010252 * 1 item for xattr if selinux is on
10253 */
Filipe Manana9269d122015-12-31 18:16:29 +000010254 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010255 if (IS_ERR(trans))
10256 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010257
Li Zefan581bb052011-04-20 10:06:11 +080010258 err = btrfs_find_free_ino(root, &objectid);
10259 if (err)
10260 goto out_unlock;
10261
Josef Bacikaec74772008-07-24 12:12:38 -040010262 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010263 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10264 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010265 if (IS_ERR(inode)) {
10266 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010267 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010268 }
Chris Mason39279cc2007-06-12 06:35:45 -040010269
Casey Schauflerad19db72011-12-15 10:09:07 -050010270 /*
10271 * If the active LSM wants to access the inode during
10272 * d_instantiate it needs these. Smack checks to see
10273 * if the filesystem supports xattrs by looking at the
10274 * ops vector.
10275 */
10276 inode->i_fop = &btrfs_file_operations;
10277 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010278 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010279 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10280
10281 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10282 if (err)
10283 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010284
Chris Mason39279cc2007-06-12 06:35:45 -040010285 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010286 if (!path) {
10287 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010288 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010289 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010290 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010291 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010292 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010293 datasize = btrfs_file_extent_calc_inline_size(name_len);
10294 err = btrfs_insert_empty_item(trans, root, path, &key,
10295 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010296 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010297 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010298 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010299 }
Chris Mason5f39d392007-10-15 16:14:19 -040010300 leaf = path->nodes[0];
10301 ei = btrfs_item_ptr(leaf, path->slots[0],
10302 struct btrfs_file_extent_item);
10303 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10304 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010305 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010306 btrfs_set_file_extent_encryption(leaf, ei, 0);
10307 btrfs_set_file_extent_compression(leaf, ei, 0);
10308 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10309 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10310
Chris Mason39279cc2007-06-12 06:35:45 -040010311 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010312 write_extent_buffer(leaf, symname, ptr, name_len);
10313 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010314 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010315
Chris Mason39279cc2007-06-12 06:35:45 -040010316 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010317 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010318 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010319 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010320 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010321 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010322 /*
10323 * Last step, add directory indexes for our symlink inode. This is the
10324 * last step to avoid extra cleanup of these indexes if an error happens
10325 * elsewhere above.
10326 */
10327 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010328 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10329 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010330 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010331 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010332 goto out_unlock_inode;
10333 }
10334
10335 unlock_new_inode(inode);
10336 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010337
10338out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010339 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010340 if (drop_inode) {
10341 inode_dec_link_count(inode);
10342 iput(inode);
10343 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010344 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010345 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010346
10347out_unlock_inode:
10348 drop_inode = 1;
10349 unlock_new_inode(inode);
10350 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010351}
Chris Mason16432982008-04-10 10:23:21 -040010352
Josef Bacik0af3d002010-06-21 14:48:16 -040010353static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10354 u64 start, u64 num_bytes, u64 min_size,
10355 loff_t actual_len, u64 *alloc_hint,
10356 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010359 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10360 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010361 struct btrfs_root *root = BTRFS_I(inode)->root;
10362 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010363 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010364 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010365 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010366 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010367 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010368 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010369 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010370
Josef Bacik0af3d002010-06-21 14:48:16 -040010371 if (trans)
10372 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010373 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010374 if (own_trans) {
10375 trans = btrfs_start_transaction(root, 3);
10376 if (IS_ERR(trans)) {
10377 ret = PTR_ERR(trans);
10378 break;
10379 }
Yan Zhengd899e052008-10-30 14:25:28 -040010380 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010381
Byongho Leeee221842015-12-15 01:42:10 +090010382 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010383 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010384 /*
10385 * If we are severely fragmented we could end up with really
10386 * small allocations, so if the allocator is returning small
10387 * chunks lets make its job easier by only searching for those
10388 * sized chunks.
10389 */
10390 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010391 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10392 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010393 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010394 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010395 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010396 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010397 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010398 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010399
Josef Bacik0b670dc2015-09-23 17:11:16 -040010400 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010401 ret = insert_reserved_file_extent(trans, inode,
10402 cur_offset, ins.objectid,
10403 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010404 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010405 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010406 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010407 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010408 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010409 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010410 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010411 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010412 break;
10413 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010414
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010415 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010416 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010417
Josef Bacik5dc562c2012-08-17 13:14:17 -040010418 em = alloc_extent_map();
10419 if (!em) {
10420 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10421 &BTRFS_I(inode)->runtime_flags);
10422 goto next;
10423 }
10424
10425 em->start = cur_offset;
10426 em->orig_start = cur_offset;
10427 em->len = ins.offset;
10428 em->block_start = ins.objectid;
10429 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010430 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010431 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010432 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010433 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10434 em->generation = trans->transid;
10435
10436 while (1) {
10437 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010438 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010439 write_unlock(&em_tree->lock);
10440 if (ret != -EEXIST)
10441 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010442 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010443 cur_offset + ins.offset - 1,
10444 0);
10445 }
10446 free_extent_map(em);
10447next:
Yan Zhengd899e052008-10-30 14:25:28 -040010448 num_bytes -= ins.offset;
10449 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010450 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010451
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010452 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010453 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010454 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010455 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010456 (actual_len > inode->i_size) &&
10457 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010458 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010459 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010460 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010461 i_size = cur_offset;
10462 i_size_write(inode, i_size);
10463 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010464 }
10465
Yan Zhengd899e052008-10-30 14:25:28 -040010466 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010467
10468 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010469 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010470 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010471 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010472 break;
10473 }
Yan Zhengd899e052008-10-30 14:25:28 -040010474
Josef Bacik0af3d002010-06-21 14:48:16 -040010475 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010476 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010477 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010478 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010479 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010480 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010481 return ret;
10482}
10483
Josef Bacik0af3d002010-06-21 14:48:16 -040010484int btrfs_prealloc_file_range(struct inode *inode, int mode,
10485 u64 start, u64 num_bytes, u64 min_size,
10486 loff_t actual_len, u64 *alloc_hint)
10487{
10488 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10489 min_size, actual_len, alloc_hint,
10490 NULL);
10491}
10492
10493int btrfs_prealloc_file_range_trans(struct inode *inode,
10494 struct btrfs_trans_handle *trans, int mode,
10495 u64 start, u64 num_bytes, u64 min_size,
10496 loff_t actual_len, u64 *alloc_hint)
10497{
10498 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10499 min_size, actual_len, alloc_hint, trans);
10500}
10501
Chris Masone6dcd2d2008-07-17 12:53:50 -040010502static int btrfs_set_page_dirty(struct page *page)
10503{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010504 return __set_page_dirty_nobuffers(page);
10505}
10506
Al Viro10556cb2011-06-20 19:28:19 -040010507static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010508{
Li Zefanb83cc962010-12-20 16:04:08 +080010509 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010510 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010511
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010512 if (mask & MAY_WRITE &&
10513 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10514 if (btrfs_root_readonly(root))
10515 return -EROFS;
10516 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10517 return -EACCES;
10518 }
Al Viro2830ba72011-06-20 19:16:29 -040010519 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010520}
Chris Mason39279cc2007-06-12 06:35:45 -040010521
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010522static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10523{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010524 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010525 struct btrfs_trans_handle *trans;
10526 struct btrfs_root *root = BTRFS_I(dir)->root;
10527 struct inode *inode = NULL;
10528 u64 objectid;
10529 u64 index;
10530 int ret = 0;
10531
10532 /*
10533 * 5 units required for adding orphan entry
10534 */
10535 trans = btrfs_start_transaction(root, 5);
10536 if (IS_ERR(trans))
10537 return PTR_ERR(trans);
10538
10539 ret = btrfs_find_free_ino(root, &objectid);
10540 if (ret)
10541 goto out;
10542
10543 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010544 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010545 if (IS_ERR(inode)) {
10546 ret = PTR_ERR(inode);
10547 inode = NULL;
10548 goto out;
10549 }
10550
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010551 inode->i_fop = &btrfs_file_operations;
10552 inode->i_op = &btrfs_file_inode_operations;
10553
10554 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010555 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10556
Chris Masonb0d5d102014-09-08 13:08:51 -070010557 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10558 if (ret)
10559 goto out_inode;
10560
10561 ret = btrfs_update_inode(trans, root, inode);
10562 if (ret)
10563 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010564 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010565 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010566 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010567
Filipe Manana5762b5c2014-08-01 00:10:32 +010010568 /*
10569 * We set number of links to 0 in btrfs_new_inode(), and here we set
10570 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10571 * through:
10572 *
10573 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10574 */
10575 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010576 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010577 d_tmpfile(dentry, inode);
10578 mark_inode_dirty(inode);
10579
10580out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010581 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010582 if (ret)
10583 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010584 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010585 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010586
10587out_inode:
10588 unlock_new_inode(inode);
10589 goto out;
10590
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010591}
10592
David Sterba20a7db82017-02-17 16:24:29 +010010593__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010594static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010595{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010596 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010597}
10598
Josef Bacikc6100a42017-05-05 11:57:13 -040010599static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10600{
10601 struct inode *inode = private_data;
10602 return btrfs_sb(inode->i_sb);
10603}
10604
10605static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10606 u64 start, u64 end)
10607{
10608 struct inode *inode = private_data;
10609 u64 isize;
10610
10611 isize = i_size_read(inode);
10612 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10613 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10614 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10615 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10616 }
10617}
10618
10619void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10620{
10621 struct inode *inode = private_data;
10622 unsigned long index = start >> PAGE_SHIFT;
10623 unsigned long end_index = end >> PAGE_SHIFT;
10624 struct page *page;
10625
10626 while (index <= end_index) {
10627 page = find_get_page(inode->i_mapping, index);
10628 ASSERT(page); /* Pages should be in the extent_io_tree */
10629 set_page_writeback(page);
10630 put_page(page);
10631 index++;
10632 }
10633}
10634
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010635static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010636 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010637 .lookup = btrfs_lookup,
10638 .create = btrfs_create,
10639 .unlink = btrfs_unlink,
10640 .link = btrfs_link,
10641 .mkdir = btrfs_mkdir,
10642 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010643 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010644 .symlink = btrfs_symlink,
10645 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010646 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010647 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010648 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010649 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010650 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010651 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010652 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010653};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010654static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010655 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010656 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010657 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010658};
Yan, Zheng76dda932009-09-21 16:00:26 -040010659
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010660static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010661 .llseek = generic_file_llseek,
10662 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010663 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010664 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010665 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010666#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010667 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010668#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010669 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010670 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010671};
10672
David Sterba20e55062015-11-19 11:42:28 +010010673static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010674 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010675 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010676 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010677 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010678 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010679 .tree_fs_info = iotree_fs_info,
10680 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010681
10682 /* optional callbacks */
10683 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010684 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010685 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010686 .set_bit_hook = btrfs_set_bit_hook,
10687 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010688 .merge_extent_hook = btrfs_merge_extent_hook,
10689 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010690 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010691};
10692
Chris Mason35054392009-01-21 13:11:13 -050010693/*
10694 * btrfs doesn't support the bmap operation because swapfiles
10695 * use bmap to make a mapping of extents in the file. They assume
10696 * these extents won't change over the life of the file and they
10697 * use the bmap result to do IO directly to the drive.
10698 *
10699 * the btrfs bmap call would return logical addresses that aren't
10700 * suitable for IO and they also will change frequently as COW
10701 * operations happen. So, swapfile + btrfs == corruption.
10702 *
10703 * For now we're avoiding this by dropping bmap.
10704 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010705static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010706 .readpage = btrfs_readpage,
10707 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010708 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010709 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010710 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010711 .invalidatepage = btrfs_invalidatepage,
10712 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010713 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010714 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010715};
10716
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010717static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010718 .readpage = btrfs_readpage,
10719 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010720 .invalidatepage = btrfs_invalidatepage,
10721 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010722};
10723
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010724static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010725 .getattr = btrfs_getattr,
10726 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010727 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010728 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010729 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010730 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010731 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010732 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010733};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010734static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010735 .getattr = btrfs_getattr,
10736 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010737 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010738 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010739 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010740 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010741 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010742};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010743static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010744 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010745 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010746 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010747 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010748 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010749 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010750};
Yan, Zheng76dda932009-09-21 16:00:26 -040010751
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010752const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010753 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010754 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010755};