blob: 1b1a9e35e0829eef005da6c5221f2b3a79b84213 [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>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
71 u64 outstanding_extents;
72 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
75};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_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);
Josef Bacik70c8a912012-10-11 16:54:30 -0400112static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
113 u64 len, u64 orig_start,
114 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400115 u64 orig_block_len, u64 ram_bytes,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
170 key.objectid = btrfs_ino(inode);
171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Josef Bacik00361582013-08-14 14:02:47 -0400253 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400254 u64 isize = i_size_read(inode);
255 u64 actual_end = min(end + 1, isize);
256 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000257 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 u64 data_len = inline_len;
259 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000260 struct btrfs_path *path;
261 int extent_inserted = 0;
262 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400263
264 if (compressed_size)
265 data_len = compressed_size;
266
267 if (start > 0 ||
Chandan Rajendra0c29ba92016-01-21 15:56:01 +0530268 actual_end > root->sectorsize ||
Wang Shilong354877b2014-07-17 11:44:11 +0800269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400270 (!compressed_size &&
271 (actual_end & (root->sectorsize - 1)) == 0) ||
272 end + 1 < isize ||
273 data_len > root->fs_info->max_inline) {
274 return 1;
275 }
276
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 path = btrfs_alloc_path();
278 if (!path)
279 return -ENOMEM;
280
Josef Bacik00361582013-08-14 14:02:47 -0400281 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 if (IS_ERR(trans)) {
283 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400284 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 }
Josef Bacik00361582013-08-14 14:02:47 -0400286 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
287
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000288 if (compressed_size && compressed_pages)
289 extent_item_size = btrfs_file_extent_calc_inline_size(
290 compressed_size);
291 else
292 extent_item_size = btrfs_file_extent_calc_inline_size(
293 inline_len);
294
295 ret = __btrfs_drop_extents(trans, root, inode, path,
296 start, aligned_end, NULL,
297 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400298 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400299 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400300 goto out;
301 }
Chris Masonc8b97812008-10-29 14:49:59 -0400302
303 if (isize > actual_end)
304 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 ret = insert_inline_extent(trans, path, extent_inserted,
306 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400307 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000308 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400309 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400310 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400311 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400312 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400313 ret = 1;
314 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100315 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400316
Josef Bacikbdc20e62013-02-28 13:23:38 -0500317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400319 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400320out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800321 /*
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
326 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000328 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400329 btrfs_end_transaction(trans, root);
330 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400331}
332
Chris Mason771ed682008-11-06 22:02:51 -0500333struct async_extent {
334 u64 start;
335 u64 ram_size;
336 u64 compressed_size;
337 struct page **pages;
338 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800339 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500340 struct list_head list;
341};
342
343struct async_cow {
344 struct inode *inode;
345 struct btrfs_root *root;
346 struct page *locked_page;
347 u64 start;
348 u64 end;
349 struct list_head extents;
350 struct btrfs_work work;
351};
352
353static noinline int add_async_extent(struct async_cow *cow,
354 u64 start, u64 ram_size,
355 u64 compressed_size,
356 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800357 unsigned long nr_pages,
358 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500359{
360 struct async_extent *async_extent;
361
362 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500364 async_extent->start = start;
365 async_extent->ram_size = ram_size;
366 async_extent->compressed_size = compressed_size;
367 async_extent->pages = pages;
368 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 list_add_tail(&async_extent->list, &cow->extents);
371 return 0;
372}
373
Wang Shilongf79707b2014-07-17 11:44:09 +0800374static inline int inode_need_compress(struct inode *inode)
375{
376 struct btrfs_root *root = BTRFS_I(inode)->root;
377
378 /* force compress */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400379 if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800380 return 1;
381 /* bad compression ratios */
382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 return 0;
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400384 if (btrfs_test_opt(root->fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
386 BTRFS_I(inode)->force_compress)
387 return 1;
388 return 0;
389}
390
Chris Masonc8b97812008-10-29 14:49:59 -0400391/*
Chris Mason771ed682008-11-06 22:02:51 -0500392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400395 *
Chris Mason771ed682008-11-06 22:02:51 -0500396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 *
Chris Mason771ed682008-11-06 22:02:51 -0500402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300405 * are written in the same order that the flusher thread sent them
406 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400407 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100408static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400413{
414 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400416 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400417 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500418 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400419 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400420 struct page **pages = NULL;
421 unsigned long nr_pages;
422 unsigned long nr_pages_ret = 0;
423 unsigned long total_compressed = 0;
424 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900425 unsigned long max_compressed = SZ_128K;
426 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400427 int i;
428 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800429 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400430 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400431
Liu Bo4cb13e52012-03-29 09:57:45 -0400432 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900433 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400434 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400435 btrfs_add_inode_defrag(NULL, inode);
436
Chris Mason42dc7ba2008-12-15 11:44:56 -0500437 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400438again:
439 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300440 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
441 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400442
Chris Masonf03d9302009-02-04 09:31:06 -0500443 /*
444 * we don't want to send crud past the end of i_size through
445 * compression, that's just a waste of CPU time. So, if the
446 * end of the file is before the start of our current
447 * requested range of bytes, we bail out to the uncompressed
448 * cleanup code that can deal with all of this.
449 *
450 * It isn't really the fastest way to fix things, but this is a
451 * very uncommon corner.
452 */
453 if (actual_end <= start)
454 goto cleanup_and_bail_uncompressed;
455
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_compressed = actual_end - start;
457
Shilong Wang4bcbb332014-10-07 18:44:35 -0400458 /*
459 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400460 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400461 */
462 if (total_compressed <= blocksize &&
463 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
464 goto cleanup_and_bail_uncompressed;
465
Chris Masonc8b97812008-10-29 14:49:59 -0400466 /* we want to make sure that amount of ram required to uncompress
467 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500468 * of a compressed extent to 128k. This is a crucial number
469 * because it also controls how easily we can spread reads across
470 * cpus for decompression.
471 *
472 * We also want to make sure the amount of IO required to do
473 * a random read is reasonably small, so we limit the size of
474 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 */
476 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000477 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500478 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 total_in = 0;
480 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400481
Chris Mason771ed682008-11-06 22:02:51 -0500482 /*
483 * we do compression for mount -o compress and when the
484 * inode has not been flagged as nocompress. This flag can
485 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400486 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800487 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400488 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100489 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800490 if (!pages) {
491 /* just bail out to the uncompressed code */
Filipe Mananaa0880352018-10-13 00:37:25 +0100492 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800493 goto cont;
494 }
Chris Mason179e29e2007-11-01 11:28:41 -0400495
Li Zefan261507a02010-12-17 14:21:50 +0800496 if (BTRFS_I(inode)->force_compress)
497 compress_type = BTRFS_I(inode)->force_compress;
498
Chris Mason4adaa612013-03-26 13:07:00 -0400499 /*
500 * we need to call clear_page_dirty_for_io on each
501 * page in the range. Otherwise applications with the file
502 * mmap'd can wander in and change the page contents while
503 * we are compressing them.
504 *
505 * If the compression fails for any reason, we set the pages
506 * dirty again later on.
507 */
508 extent_range_clear_dirty_for_io(inode, start, end);
509 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800510 ret = btrfs_compress_pages(compress_type,
511 inode->i_mapping, start,
512 total_compressed, pages,
513 nr_pages, &nr_pages_ret,
514 &total_in,
515 &total_compressed,
516 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400517
518 if (!ret) {
519 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 struct page *page = pages[nr_pages_ret - 1];
522 char *kaddr;
523
524 /* zero the tail end of the last page, we might be
525 * sending it down to disk
526 */
527 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800528 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300530 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800531 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400532 }
533 will_compress = 1;
534 }
535 }
Li Zefan560f7d72011-09-08 10:22:01 +0800536cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400537 if (start == 0) {
538 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500539 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400540 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500541 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400542 */
Josef Bacik00361582013-08-14 14:02:47 -0400543 ret = cow_file_range_inline(root, inode, start, end,
544 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500546 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400547 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000548 total_compressed,
549 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100551 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400552 unsigned long clear_flags = EXTENT_DELALLOC |
553 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100554 unsigned long page_error_op;
555
Josef Bacik151a41b2013-07-29 13:22:24 -0400556 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100557 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400558
Chris Mason771ed682008-11-06 22:02:51 -0500559 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100560 * inline extent creation worked or returned error,
561 * we don't need to create any more async work items.
562 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500563 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800564 extent_clear_unlock_delalloc(inode, start, end, end,
565 NULL, clear_flags,
566 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400567 PAGE_CLEAR_DIRTY |
568 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100569 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400570 PAGE_END_WRITEBACK);
Filipe Mananafd35ded2017-03-08 16:43:49 +0000571 if (ret == 0)
572 btrfs_free_reserved_data_space_noquota(inode,
573 start,
574 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400575 goto free_pages_out;
576 }
577 }
578
579 if (will_compress) {
580 /*
581 * we aren't doing an inline extent round the compressed size
582 * up to a block size boundary so the allocator does sane
583 * things
584 */
Qu Wenruofda28322013-02-26 08:10:22 +0000585 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400586
587 /*
588 * one last check to make sure the compression is really a
589 * win, compare the page count read with the blocks on disk
590 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300591 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400592 if (total_compressed >= total_in) {
593 will_compress = 0;
594 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400595 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700596 *num_added += 1;
597
598 /*
599 * The async work queues will take care of doing actual
600 * allocation on disk for these compressed pages, and
601 * will submit them to the elevator.
602 */
603 add_async_extent(async_cow, start, num_bytes,
604 total_compressed, pages, nr_pages_ret,
605 compress_type);
606
607 if (start + num_bytes < end) {
608 start += num_bytes;
609 pages = NULL;
610 cond_resched();
611 goto again;
612 }
613 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400614 }
615 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700616 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400617 /*
618 * the compression code ran but failed to make things smaller,
619 * free any pages it allocated and our page pointer array
620 */
621 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400622 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300623 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400624 }
625 kfree(pages);
626 pages = NULL;
627 total_compressed = 0;
628 nr_pages_ret = 0;
629
630 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400631 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500632 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500633 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500634 }
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
Chris Masonf03d9302009-02-04 09:31:06 -0500636cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 /*
638 * No compression, but we still need to write the pages in the file
639 * we've been given so far. redirty the locked page if it corresponds
640 * to our extent and set things up for the async work queue to run
641 * cow_file_range to do the normal delalloc dance.
642 */
643 if (page_offset(locked_page) >= start &&
644 page_offset(locked_page) <= end)
645 __set_page_dirty_nobuffers(locked_page);
646 /* unlocked later on in the async handlers */
647
648 if (redirty)
649 extent_range_redirty_for_io(inode, start, end);
650 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
651 BTRFS_COMPRESS_NONE);
652 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500653
Filipe Mananac44f6492014-10-09 21:15:44 +0100654 return;
Chris Mason771ed682008-11-06 22:02:51 -0500655
656free_pages_out:
657 for (i = 0; i < nr_pages_ret; i++) {
658 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300659 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500660 }
Chris Masond3977122009-01-05 21:25:51 -0500661 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500662}
Chris Mason771ed682008-11-06 22:02:51 -0500663
Filipe Manana40ae8372014-10-06 22:14:24 +0100664static void free_async_extent_pages(struct async_extent *async_extent)
665{
666 int i;
667
668 if (!async_extent->pages)
669 return;
670
671 for (i = 0; i < async_extent->nr_pages; i++) {
672 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300673 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100674 }
675 kfree(async_extent->pages);
676 async_extent->nr_pages = 0;
677 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500678}
679
680/*
681 * phase two of compressed writeback. This is the ordered portion
682 * of the code, which only gets called in the order the work was
683 * queued. We walk all the async extents created by compress_file_range
684 * and send them down to the disk.
685 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100686static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500687 struct async_cow *async_cow)
688{
689 struct async_extent *async_extent;
690 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500691 struct btrfs_key ins;
692 struct extent_map *em;
693 struct btrfs_root *root = BTRFS_I(inode)->root;
694 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
695 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500696 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500697
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500698again:
Chris Masond3977122009-01-05 21:25:51 -0500699 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500700 async_extent = list_entry(async_cow->extents.next,
701 struct async_extent, list);
702 list_del(&async_extent->list);
703
704 io_tree = &BTRFS_I(inode)->io_tree;
705
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706retry:
Chris Mason771ed682008-11-06 22:02:51 -0500707 /* did the compression code fall back to uncompressed IO? */
708 if (!async_extent->pages) {
709 int page_started = 0;
710 unsigned long nr_written = 0;
711
712 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000713 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100714 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500715
716 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500717 ret = cow_file_range(inode, async_cow->locked_page,
718 async_extent->start,
719 async_extent->start +
720 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800721 async_extent->start +
722 async_extent->ram_size - 1,
723 &page_started, &nr_written, 0,
724 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500725
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 /* JDM XXX */
727
Chris Mason771ed682008-11-06 22:02:51 -0500728 /*
729 * if page_started, cow_file_range inserted an
730 * inline extent and took care of all the unlocking
731 * and IO for us. Otherwise, we need to submit
732 * all those pages down to the drive.
733 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500735 extent_write_locked_range(io_tree,
736 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500737 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent->ram_size - 1,
739 btrfs_get_extent,
740 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500741 else if (ret)
742 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500743 kfree(async_extent);
744 cond_resched();
745 continue;
746 }
747
748 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100749 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500750
Wang Xiaoguang18513092016-07-25 15:51:40 +0800751 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500752 async_extent->compressed_size,
753 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800754 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100756 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400758 if (ret == -ENOSPC) {
759 unlock_extent(io_tree, async_extent->start,
760 async_extent->start +
761 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800762
763 /*
764 * we need to redirty the pages if we decide to
765 * fallback to uncompressed IO, otherwise we
766 * will not submit these pages down to lower
767 * layers.
768 */
769 extent_range_redirty_for_io(inode,
770 async_extent->start,
771 async_extent->start +
772 async_extent->ram_size - 1);
773
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100774 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400775 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500776 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500777 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000778 /*
779 * here we're doing allocation and writeback of the
780 * compressed pages
781 */
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
785
David Sterba172ddd62011-04-21 00:48:27 +0200786 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000787 if (!em) {
788 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000790 }
Chris Mason771ed682008-11-06 22:02:51 -0500791 em->start = async_extent->start;
792 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500793 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400794 em->mod_start = em->start;
795 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500796
797 em->block_start = ins.objectid;
798 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500799 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400800 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500801 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800802 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500803 set_bit(EXTENT_FLAG_PINNED, &em->flags);
804 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400805 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500806
Chris Masond3977122009-01-05 21:25:51 -0500807 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400808 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400809 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400810 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500811 if (ret != -EEXIST) {
812 free_extent_map(em);
813 break;
814 }
815 btrfs_drop_extent_cache(inode, async_extent->start,
816 async_extent->start +
817 async_extent->ram_size - 1, 0);
818 }
819
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500820 if (ret)
821 goto out_free_reserve;
822
Li Zefan261507a02010-12-17 14:21:50 +0800823 ret = btrfs_add_ordered_extent_compress(inode,
824 async_extent->start,
825 ins.objectid,
826 async_extent->ram_size,
827 ins.offset,
828 BTRFS_ORDERED_COMPRESSED,
829 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 if (ret) {
831 btrfs_drop_extent_cache(inode, async_extent->start,
832 async_extent->start +
833 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500834 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100836 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500837
Chris Mason771ed682008-11-06 22:02:51 -0500838 /*
839 * clear dirty, set writeback and unlock the pages.
840 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400841 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400842 async_extent->start +
843 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800844 async_extent->start +
845 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400846 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
847 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400848 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500849 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500850 async_extent->start,
851 async_extent->ram_size,
852 ins.objectid,
853 ins.offset, async_extent->pages,
854 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100855 if (ret) {
856 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
857 struct page *p = async_extent->pages[0];
858 const u64 start = async_extent->start;
859 const u64 end = start + async_extent->ram_size - 1;
860
861 p->mapping = inode->i_mapping;
862 tree->ops->writepage_end_io_hook(p, start, end,
863 NULL, 0);
864 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800865 extent_clear_unlock_delalloc(inode, start, end, end,
866 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 PAGE_END_WRITEBACK |
868 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100869 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100870 }
Chris Mason771ed682008-11-06 22:02:51 -0500871 alloc_hint = ins.objectid + ins.offset;
872 kfree(async_extent);
873 cond_resched();
874 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100875 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100877 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800878 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100879out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400880 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881 async_extent->start +
882 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800883 async_extent->start +
884 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400886 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
887 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100888 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
889 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100890 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500892 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500893}
894
Josef Bacik4b46fce2010-05-23 11:00:55 -0400895static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
896 u64 num_bytes)
897{
898 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
899 struct extent_map *em;
900 u64 alloc_hint = 0;
901
902 read_lock(&em_tree->lock);
903 em = search_extent_mapping(em_tree, start, num_bytes);
904 if (em) {
905 /*
906 * if block start isn't an actual block number then find the
907 * first block in this inode and use that as a hint. If that
908 * block is also bogus then just don't worry about it.
909 */
910 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
911 free_extent_map(em);
912 em = search_extent_mapping(em_tree, 0, 0);
913 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
914 alloc_hint = em->block_start;
915 if (em)
916 free_extent_map(em);
917 } else {
918 alloc_hint = em->block_start;
919 free_extent_map(em);
920 }
921 }
922 read_unlock(&em_tree->lock);
923
924 return alloc_hint;
925}
926
Chris Mason771ed682008-11-06 22:02:51 -0500927/*
928 * when extent_io.c finds a delayed allocation range in the file,
929 * the call backs end up in this code. The basic idea is to
930 * allocate extents on disk for the range, and create ordered data structs
931 * in ram to track those extents.
932 *
933 * locked_page is the page that writepage had locked already. We use
934 * it to make sure we don't do extra locks or unlocks.
935 *
936 * *page_started is set to one if we unlock locked_page and do everything
937 * required to start IO on it. It may be clean and already done with
938 * IO when we return.
939 */
Josef Bacik00361582013-08-14 14:02:47 -0400940static noinline int cow_file_range(struct inode *inode,
941 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800942 u64 start, u64 end, u64 delalloc_end,
943 int *page_started, unsigned long *nr_written,
944 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500945{
Josef Bacik00361582013-08-14 14:02:47 -0400946 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500947 u64 alloc_hint = 0;
948 u64 num_bytes;
949 unsigned long ram_size;
950 u64 disk_num_bytes;
951 u64 cur_alloc_size;
952 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500953 struct btrfs_key ins;
954 struct extent_map *em;
955 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
956 int ret = 0;
957
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400958 if (btrfs_is_free_space_inode(inode)) {
959 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500960 ret = -EINVAL;
961 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400962 }
Chris Mason771ed682008-11-06 22:02:51 -0500963
Qu Wenruofda28322013-02-26 08:10:22 +0000964 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500965 num_bytes = max(blocksize, num_bytes);
966 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500967
Chris Mason4cb53002011-05-24 15:35:30 -0400968 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900969 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400970 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400971 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400972
Chris Mason771ed682008-11-06 22:02:51 -0500973 if (start == 0) {
974 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
976 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500977 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400981 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
983 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800984 btrfs_free_reserved_data_space_noquota(inode, start,
985 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500986 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300987 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500988 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500989 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500992 }
993 }
Chris Masonc8b97812008-10-29 14:49:59 -0400994
995 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200996 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Josef Bacik4b46fce2010-05-23 11:00:55 -0400998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400999 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001002 unsigned long op;
1003
Josef Bacik287a0ab2010-03-19 18:07:23 +00001004 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001005 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -05001006 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001007 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001008 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001010
David Sterba172ddd62011-04-21 00:48:27 +02001011 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001012 if (!em) {
1013 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001014 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001017 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001018 ram_size = ins.offset;
1019 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001020 em->mod_start = em->start;
1021 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001024 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001025 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001026 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001027 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001028 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001029 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001030
Chris Masond3977122009-01-05 21:25:51 -05001031 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001032 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001033 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001034 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035 if (ret != -EEXIST) {
1036 free_extent_map(em);
1037 break;
1038 }
1039 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001040 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041 }
Liu Boace68ba2013-04-22 10:53:47 +00001042 if (ret)
1043 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001044
Chris Mason98d20f62008-04-14 09:46:10 -04001045 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001046 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001047 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001049 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Yan Zheng17d217f2008-12-12 10:03:38 -05001051 if (root->root_key.objectid ==
1052 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1053 ret = btrfs_reloc_clone_csums(inode, start,
1054 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001055 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001056 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 }
1058
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001059 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1060
Chris Masond3977122009-01-05 21:25:51 -05001061 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001062 break;
Chris Masond3977122009-01-05 21:25:51 -05001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 op = unlock ? PAGE_UNLOCK : 0;
1072 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
1078 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001079 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001080 num_bytes -= cur_alloc_size;
1081 alloc_hint = ins.objectid + ins.offset;
1082 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001083 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001085 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001086
Filipe Mananad9f85962014-08-25 10:43:00 +01001087out_drop_extent_cache:
1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001090 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001091 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1094 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001095 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1096 EXTENT_DELALLOC | EXTENT_DEFRAG,
1097 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1098 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001100}
Chris Masonc8b97812008-10-29 14:49:59 -04001101
Chris Mason771ed682008-11-06 22:02:51 -05001102/*
1103 * work queue call back to started compression on a file and pages
1104 */
1105static noinline void async_cow_start(struct btrfs_work *work)
1106{
1107 struct async_cow *async_cow;
1108 int num_added = 0;
1109 async_cow = container_of(work, struct async_cow, work);
1110
1111 compress_file_range(async_cow->inode, async_cow->locked_page,
1112 async_cow->start, async_cow->end, async_cow,
1113 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001114 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001115 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001116 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 }
Chris Mason771ed682008-11-06 22:02:51 -05001118}
1119
1120/*
1121 * work queue call back to submit previously compressed pages
1122 */
1123static noinline void async_cow_submit(struct btrfs_work *work)
1124{
1125 struct async_cow *async_cow;
1126 struct btrfs_root *root;
1127 unsigned long nr_pages;
1128
1129 async_cow = container_of(work, struct async_cow, work);
1130
1131 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001132 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1133 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001134
David Sterbaee863952015-02-16 19:41:40 +01001135 /*
1136 * atomic_sub_return implies a barrier for waitqueue_active
1137 */
Josef Bacik66657b32012-08-01 15:36:24 -04001138 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001139 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001140 waitqueue_active(&root->fs_info->async_submit_wait))
1141 wake_up(&root->fs_info->async_submit_wait);
1142
Chris Masond3977122009-01-05 21:25:51 -05001143 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001144 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001145}
Chris Masonc8b97812008-10-29 14:49:59 -04001146
Chris Mason771ed682008-11-06 22:02:51 -05001147static noinline void async_cow_free(struct btrfs_work *work)
1148{
1149 struct async_cow *async_cow;
1150 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001151 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001152 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001153 kfree(async_cow);
1154}
1155
1156static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1157 u64 start, u64 end, int *page_started,
1158 unsigned long *nr_written)
1159{
1160 struct async_cow *async_cow;
1161 struct btrfs_root *root = BTRFS_I(inode)->root;
1162 unsigned long nr_pages;
1163 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001164 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001165
Chris Masona3429ab2009-10-08 12:30:20 -04001166 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1167 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001168 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001169 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001170 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001171 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001172 async_cow->root = root;
1173 async_cow->locked_page = locked_page;
1174 async_cow->start = start;
1175
Wang Shilongf79707b2014-07-17 11:44:09 +08001176 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001177 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001178 cur_end = end;
1179 else
Byongho Leeee221842015-12-15 01:42:10 +09001180 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001181
1182 async_cow->end = cur_end;
1183 INIT_LIST_HEAD(&async_cow->extents);
1184
Liu Bo9e0af232014-08-15 23:36:53 +08001185 btrfs_init_work(&async_cow->work,
1186 btrfs_delalloc_helper,
1187 async_cow_start, async_cow_submit,
1188 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001189
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001190 nr_pages = (cur_end - start + PAGE_SIZE) >>
1191 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001192 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1193
Qu Wenruoafe3d242014-02-28 10:46:07 +08001194 btrfs_queue_work(root->fs_info->delalloc_workers,
1195 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001196
1197 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1198 wait_event(root->fs_info->async_submit_wait,
1199 (atomic_read(&root->fs_info->async_delalloc_pages) <
1200 limit));
1201 }
1202
Chris Masond3977122009-01-05 21:25:51 -05001203 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001204 atomic_read(&root->fs_info->async_delalloc_pages)) {
1205 wait_event(root->fs_info->async_submit_wait,
1206 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1207 0));
1208 }
1209
1210 *nr_written += nr_pages;
1211 start = cur_end + 1;
1212 }
1213 *page_started = 1;
1214 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215}
1216
Chris Masond3977122009-01-05 21:25:51 -05001217static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001218 u64 bytenr, u64 num_bytes)
1219{
1220 int ret;
1221 struct btrfs_ordered_sum *sums;
1222 LIST_HEAD(list);
1223
Yan Zheng07d400a2009-01-06 11:42:00 -05001224 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001225 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001226 if (ret == 0 && list_empty(&list))
1227 return 0;
1228
1229 while (!list_empty(&list)) {
1230 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1231 list_del(&sums->list);
1232 kfree(sums);
1233 }
Liu Bo3fd6a732018-01-31 17:09:13 -07001234 if (ret < 0)
1235 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001236 return 1;
1237}
1238
Chris Masond352ac62008-09-29 15:18:18 -04001239/*
1240 * when nowcow writeback call back. This checks for snapshots or COW copies
1241 * of the extents that exist in the file, and COWs the file as required.
1242 *
1243 * If no cow copies or snapshots exist, we write directly to the existing
1244 * blocks on disk
1245 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001246static noinline int run_delalloc_nocow(struct inode *inode,
1247 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001248 u64 start, u64 end, int *page_started, int force,
1249 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001250{
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001252 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001256 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 u64 cow_start;
1258 u64 cur_offset;
1259 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001260 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 u64 disk_bytenr;
1262 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001263 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001264 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001266 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001267 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 int nocow;
1269 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001270 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001271 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001272
1273 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001274 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001275 extent_clear_unlock_delalloc(inode, start, end, end,
1276 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001278 EXTENT_DO_ACCOUNTING |
1279 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001280 PAGE_CLEAR_DIRTY |
1281 PAGE_SET_WRITEBACK |
1282 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001283 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001284 }
Li Zefan82d59022011-04-20 10:33:24 +08001285
Liu Bo83eea1f2012-07-10 05:28:39 -06001286 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001287
1288 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001289 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001290 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001291 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001292
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001293 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001294 extent_clear_unlock_delalloc(inode, start, end, end,
1295 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001296 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001297 EXTENT_DO_ACCOUNTING |
1298 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001299 PAGE_CLEAR_DIRTY |
1300 PAGE_SET_WRITEBACK |
1301 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 btrfs_free_path(path);
1303 return PTR_ERR(trans);
1304 }
1305
Josef Bacik74b21072011-04-13 12:02:53 -04001306 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001307
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 cow_start = (u64)-1;
1309 cur_offset = start;
1310 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001311 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001313 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001314 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1316 leaf = path->nodes[0];
1317 btrfs_item_key_to_cpu(leaf, &found_key,
1318 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001319 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 found_key.type == BTRFS_EXTENT_DATA_KEY)
1321 path->slots[0]--;
1322 }
1323 check_prev = 0;
1324next_slot:
1325 leaf = path->nodes[0];
1326 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1327 ret = btrfs_next_leaf(root, path);
Liu Boecd72fd2018-01-25 11:02:50 -07001328 if (ret < 0) {
1329 if (cow_start != (u64)-1)
1330 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001331 goto error;
Liu Boecd72fd2018-01-25 11:02:50 -07001332 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 if (ret > 0)
1334 break;
1335 leaf = path->nodes[0];
1336 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 nocow = 0;
1339 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001340 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
Filipe Manana1d512cb2015-11-09 00:33:58 +00001343 if (found_key.objectid > ino)
1344 break;
1345 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1346 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1347 path->slots[0]++;
1348 goto next_slot;
1349 }
1350 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 found_key.offset > end)
1352 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001353
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 if (found_key.offset > cur_offset) {
1355 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001356 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 goto out_check;
1358 }
Chris Masonc31f8832008-01-08 15:46:31 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 fi = btrfs_item_ptr(leaf, path->slots[0],
1361 struct btrfs_file_extent_item);
1362 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001363
Josef Bacikcc95bef2013-04-04 14:31:27 -04001364 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001365 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1366 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001368 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 extent_end = found_key.offset +
1370 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001371 disk_num_bytes =
1372 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 if (extent_end <= start) {
1374 path->slots[0]++;
1375 goto next_slot;
1376 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001377 if (disk_bytenr == 0)
1378 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (btrfs_file_extent_compression(leaf, fi) ||
1380 btrfs_file_extent_encryption(leaf, fi) ||
1381 btrfs_file_extent_other_encoding(leaf, fi))
1382 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001383 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1384 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001385 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 goto out_check;
Liu Bo3fd6a732018-01-31 17:09:13 -07001387 ret = btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001388 found_key.offset -
Liu Bo3fd6a732018-01-31 17:09:13 -07001389 extent_offset, disk_bytenr);
1390 if (ret) {
1391 /*
1392 * ret could be -EIO if the above fails to read
1393 * metadata.
1394 */
1395 if (ret < 0) {
1396 if (cow_start != (u64)-1)
1397 cur_offset = cow_start;
1398 goto error;
1399 }
1400
1401 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 goto out_check;
Liu Bo3fd6a732018-01-31 17:09:13 -07001403 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001404 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 disk_bytenr += cur_offset - found_key.offset;
1406 num_bytes = min(end + 1, extent_end) - cur_offset;
1407 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001408 * if there are pending snapshots for this root,
1409 * we fall into common COW way.
1410 */
1411 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001412 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001413 if (!err)
1414 goto out_check;
1415 }
1416 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001417 * force cow if csum exists in the range.
1418 * this ensure that csum for a given extent are
1419 * either valid or do not exist.
1420 */
Liu Bo3fd6a732018-01-31 17:09:13 -07001421 ret = csum_exist_in_range(root, disk_bytenr, num_bytes);
1422 if (ret) {
1423 /*
1424 * ret could be -EIO if the above fails to read
1425 * metadata.
1426 */
1427 if (ret < 0) {
1428 if (cow_start != (u64)-1)
1429 cur_offset = cow_start;
1430 goto error;
1431 }
1432 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001433 goto out_check;
Liu Bo3fd6a732018-01-31 17:09:13 -07001434 }
Filipe Mananaf78c4362016-05-09 13:15:41 +01001435 if (!btrfs_inc_nocow_writers(root->fs_info,
1436 disk_bytenr))
1437 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 nocow = 1;
1439 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1440 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001441 btrfs_file_extent_inline_len(leaf,
1442 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 extent_end = ALIGN(extent_end, root->sectorsize);
1444 } else {
1445 BUG_ON(1);
1446 }
1447out_check:
1448 if (extent_end <= start) {
1449 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001450 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001451 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001452 if (nocow)
1453 btrfs_dec_nocow_writers(root->fs_info,
1454 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 goto next_slot;
1456 }
1457 if (!nocow) {
1458 if (cow_start == (u64)-1)
1459 cow_start = cur_offset;
1460 cur_offset = extent_end;
1461 if (cur_offset > end)
1462 break;
1463 path->slots[0]++;
1464 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001466
David Sterbab3b4aa72011-04-21 01:20:15 +02001467 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001469 ret = cow_file_range(inode, locked_page,
1470 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001471 end, page_started, nr_written, 1,
1472 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001473 if (ret) {
1474 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001475 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001476 if (nocow)
1477 btrfs_dec_nocow_writers(root->fs_info,
1478 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001480 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001481 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001482 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001483
Yan Zhengd899e052008-10-30 14:25:28 -04001484 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1485 struct extent_map *em;
1486 struct extent_map_tree *em_tree;
1487 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001488 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001490 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001491 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001492 em->len = num_bytes;
1493 em->block_len = num_bytes;
1494 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001495 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001496 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001497 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001498 em->mod_start = em->start;
1499 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001500 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001501 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001502 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001503 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001504 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001505 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001506 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001507 if (ret != -EEXIST) {
1508 free_extent_map(em);
1509 break;
1510 }
1511 btrfs_drop_extent_cache(inode, em->start,
1512 em->start + em->len - 1, 0);
1513 }
1514 type = BTRFS_ORDERED_PREALLOC;
1515 } else {
1516 type = BTRFS_ORDERED_NOCOW;
1517 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001518
1519 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001520 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001521 if (nocow)
1522 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001523 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001524
Yan, Zhengefa56462010-05-16 10:49:59 -04001525 if (root->root_key.objectid ==
1526 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1527 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1528 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001529 if (ret) {
1530 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001531 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001533 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001534 }
1535
Josef Bacikc2790a22013-07-29 11:20:47 -04001536 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001537 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001538 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001539 EXTENT_DELALLOC |
1540 EXTENT_CLEAR_DATA_RESV,
1541 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1542
Wang Shilonge9894fd2014-03-27 11:12:25 +08001543 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001544 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 cur_offset = extent_end;
1546 if (cur_offset > end)
1547 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001548 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001549 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001550
Robbie Ko3fe6b9a2018-10-30 18:04:04 +08001551 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001553
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 if (cow_start != (u64)-1) {
Robbie Ko3fe6b9a2018-10-30 18:04:04 +08001555 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001556 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1557 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001558 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 }
1561
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562error:
Miao Xiea698d0752012-09-20 01:51:59 -06001563 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 if (!ret)
1565 ret = err;
1566
Josef Bacik17ca04a2012-05-31 15:58:55 -04001567 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001568 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001569 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001570 EXTENT_DELALLOC | EXTENT_DEFRAG |
1571 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1572 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001573 PAGE_SET_WRITEBACK |
1574 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001575 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001576 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001577}
1578
Wang Shilong47059d92014-07-03 18:22:07 +08001579static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1580{
1581
1582 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1583 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1584 return 0;
1585
1586 /*
1587 * @defrag_bytes is a hint value, no spinlock held here,
1588 * if is not zero, it means the file is defragging.
1589 * Force cow if given extent needs to be defragged.
1590 */
1591 if (BTRFS_I(inode)->defrag_bytes &&
1592 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1593 EXTENT_DEFRAG, 0, NULL))
1594 return 1;
1595
1596 return 0;
1597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
1600 * extent_io.c call back to do delayed allocation processing
1601 */
Chris Masonc8b97812008-10-29 14:49:59 -04001602static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001603 u64 start, u64 end, int *page_started,
1604 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001605{
Chris Masonbe20aa92007-12-17 20:14:01 -05001606 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001607 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001608
Wang Shilong47059d92014-07-03 18:22:07 +08001609 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001610 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001611 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001612 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001613 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001614 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001615 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001616 ret = cow_file_range(inode, locked_page, start, end, end,
1617 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001618 } else {
1619 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1620 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001621 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001622 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001623 }
Chris Masonb888db22007-08-27 16:49:44 -04001624 return ret;
1625}
1626
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001627static void btrfs_split_extent_hook(struct inode *inode,
1628 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 size;
1631
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001632 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001634 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 size = orig->end - orig->start + 1;
1637 if (size > BTRFS_MAX_EXTENT_SIZE) {
1638 u64 num_extents;
1639 u64 new_size;
1640
1641 /*
Josef Bacikba117212015-03-13 15:01:24 -04001642 * See the explanation in btrfs_merge_extent_hook, the same
1643 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001644 */
1645 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001646 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001647 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001648 new_size = split - orig->start;
1649 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1650 BTRFS_MAX_EXTENT_SIZE);
1651 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1652 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653 return;
1654 }
1655
Josef Bacik9e0baf62011-07-15 15:16:44 +00001656 spin_lock(&BTRFS_I(inode)->lock);
1657 BTRFS_I(inode)->outstanding_extents++;
1658 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659}
1660
1661/*
1662 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1663 * extents so we can keep track of new extents that are just merged onto old
1664 * extents, such as when we are doing sequential writes, so we can properly
1665 * account for the metadata space we'll need.
1666 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001667static void btrfs_merge_extent_hook(struct inode *inode,
1668 struct extent_state *new,
1669 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001670{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671 u64 new_size, old_size;
1672 u64 num_extents;
1673
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674 /* not delalloc, ignore it */
1675 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001676 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001677
Josef Bacik8461a3d2015-03-13 15:12:08 -04001678 if (new->start > other->start)
1679 new_size = new->end - other->start + 1;
1680 else
1681 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001682
1683 /* we're not bigger than the max, unreserve the space and go */
1684 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1685 spin_lock(&BTRFS_I(inode)->lock);
1686 BTRFS_I(inode)->outstanding_extents--;
1687 spin_unlock(&BTRFS_I(inode)->lock);
1688 return;
1689 }
1690
1691 /*
Josef Bacikba117212015-03-13 15:01:24 -04001692 * We have to add up either side to figure out how many extents were
1693 * accounted for before we merged into one big extent. If the number of
1694 * extents we accounted for is <= the amount we need for the new range
1695 * then we can return, otherwise drop. Think of it like this
1696 *
1697 * [ 4k][MAX_SIZE]
1698 *
1699 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1700 * need 2 outstanding extents, on one side we have 1 and the other side
1701 * we have 1 so they are == and we can return. But in this case
1702 *
1703 * [MAX_SIZE+4k][MAX_SIZE+4k]
1704 *
1705 * Each range on their own accounts for 2 extents, but merged together
1706 * they are only 3 extents worth of accounting, so we need to drop in
1707 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001708 */
Josef Bacikba117212015-03-13 15:01:24 -04001709 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001710 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1711 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001712 old_size = new->end - new->start + 1;
1713 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1714 BTRFS_MAX_EXTENT_SIZE);
1715
Josef Bacikdcab6a32015-02-11 15:08:59 -05001716 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001717 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001718 return;
1719
Josef Bacik9e0baf62011-07-15 15:16:44 +00001720 spin_lock(&BTRFS_I(inode)->lock);
1721 BTRFS_I(inode)->outstanding_extents--;
1722 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001723}
1724
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1726 struct inode *inode)
1727{
1728 spin_lock(&root->delalloc_lock);
1729 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1730 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1731 &root->delalloc_inodes);
1732 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1733 &BTRFS_I(inode)->runtime_flags);
1734 root->nr_delalloc_inodes++;
1735 if (root->nr_delalloc_inodes == 1) {
1736 spin_lock(&root->fs_info->delalloc_root_lock);
1737 BUG_ON(!list_empty(&root->delalloc_root));
1738 list_add_tail(&root->delalloc_root,
1739 &root->fs_info->delalloc_roots);
1740 spin_unlock(&root->fs_info->delalloc_root_lock);
1741 }
1742 }
1743 spin_unlock(&root->delalloc_lock);
1744}
1745
1746static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1747 struct inode *inode)
1748{
1749 spin_lock(&root->delalloc_lock);
1750 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1751 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1752 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1753 &BTRFS_I(inode)->runtime_flags);
1754 root->nr_delalloc_inodes--;
1755 if (!root->nr_delalloc_inodes) {
1756 spin_lock(&root->fs_info->delalloc_root_lock);
1757 BUG_ON(list_empty(&root->delalloc_root));
1758 list_del_init(&root->delalloc_root);
1759 spin_unlock(&root->fs_info->delalloc_root_lock);
1760 }
1761 }
1762 spin_unlock(&root->delalloc_lock);
1763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
1766 * extent_io.c set_bit_hook, used to track delayed allocation
1767 * bytes in this file, and to maintain the list of inodes that
1768 * have pending delalloc work to be done.
1769 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001770static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001771 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001772{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773
Wang Shilong47059d92014-07-03 18:22:07 +08001774 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1775 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001776 /*
1777 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001778 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001779 * bit, which is only set or cleared with irqs on
1780 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001781 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001782 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001783 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001784 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001785
Josef Bacik9e0baf62011-07-15 15:16:44 +00001786 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001787 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001788 } else {
1789 spin_lock(&BTRFS_I(inode)->lock);
1790 BTRFS_I(inode)->outstanding_extents++;
1791 spin_unlock(&BTRFS_I(inode)->lock);
1792 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001793
Josef Bacik6a3891c2015-03-16 17:38:52 -04001794 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001795 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001796 return;
1797
Miao Xie963d6782013-01-29 10:10:51 +00001798 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1799 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001800 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001801 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001802 if (*bits & EXTENT_DEFRAG)
1803 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001804 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001805 &BTRFS_I(inode)->runtime_flags))
1806 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001807 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001808 }
Chris Mason291d6732008-01-29 15:55:23 -05001809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * extent_io.c clear_bit_hook, see set_bit_hook for why
1813 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001814static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001815 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001816 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001817{
Wang Shilong47059d92014-07-03 18:22:07 +08001818 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001819 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1820 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001821
1822 spin_lock(&BTRFS_I(inode)->lock);
1823 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1824 BTRFS_I(inode)->defrag_bytes -= len;
1825 spin_unlock(&BTRFS_I(inode)->lock);
1826
Chris Mason75eff682008-12-15 15:54:40 -05001827 /*
1828 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001829 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001830 * bit, which is only set or cleared with irqs on
1831 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001832 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001833 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001834 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001835
Josef Bacik9e0baf62011-07-15 15:16:44 +00001836 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001837 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001838 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1839 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001840 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001841 spin_unlock(&BTRFS_I(inode)->lock);
1842 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001843
Josef Bacikb6d08f02013-09-27 14:57:43 -04001844 /*
1845 * We don't reserve metadata space for space cache inodes so we
1846 * don't need to call dellalloc_release_metadata if there is an
1847 * error.
1848 */
1849 if (*bits & EXTENT_DO_ACCOUNTING &&
1850 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001851 btrfs_delalloc_release_metadata(inode, len);
1852
Josef Bacik6a3891c2015-03-16 17:38:52 -04001853 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001854 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001855 return;
1856
Josef Bacik0cb59c92010-07-02 12:14:14 -04001857 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001858 && do_list && !(state->state & EXTENT_NORESERVE)
1859 && (*bits & (EXTENT_DO_ACCOUNTING |
1860 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001861 btrfs_free_reserved_data_space_noquota(inode,
1862 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001863
Miao Xie963d6782013-01-29 10:10:51 +00001864 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1865 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001866 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001867 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001868 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001869 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001870 &BTRFS_I(inode)->runtime_flags))
1871 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001872 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001873 }
Chris Mason291d6732008-01-29 15:55:23 -05001874}
1875
Chris Masond352ac62008-09-29 15:18:18 -04001876/*
1877 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1878 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001879 *
1880 * return 1 if page cannot be merged to bio
1881 * return 0 if page can be merged to bio
1882 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001883 */
Mike Christie81a75f62016-06-05 14:31:54 -05001884int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001885 size_t size, struct bio *bio,
1886 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001887{
1888 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 u64 length = 0;
1891 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 int ret;
1893
Chris Mason771ed682008-11-06 22:02:51 -05001894 if (bio_flags & EXTENT_BIO_COMPRESSED)
1895 return 0;
1896
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 map_length = length;
Mike Christieb3d3fa52016-06-05 14:31:53 -05001899 ret = btrfs_map_block(root->fs_info, bio_op(bio), logical,
Chris Masonf1885912008-04-09 16:28:12 -04001900 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001901 if (ret < 0)
1902 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001903 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001904 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001905 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001906}
1907
Chris Masond352ac62008-09-29 15:18:18 -04001908/*
1909 * in order to insert checksums into the metadata in large chunks,
1910 * we wait until bio submission time. All the pages in the bio are
1911 * checksummed and sums are attached onto the ordered extent record.
1912 *
1913 * At IO completion time the cums attached on the ordered extent record
1914 * are inserted into the btree
1915 */
Mike Christie81a75f62016-06-05 14:31:54 -05001916static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1917 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001919{
Chris Mason065631f2008-02-20 12:07:25 -05001920 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001921 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001922
Chris Masond20f7042008-12-08 16:58:54 -05001923 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001925 return 0;
1926}
Chris Masone0156402008-04-16 11:15:20 -04001927
Chris Mason4a69a412008-11-06 22:03:00 -05001928/*
1929 * in order to insert checksums into the metadata in large chunks,
1930 * we wait until bio submission time. All the pages in the bio are
1931 * checksummed and sums are attached onto the ordered extent record.
1932 *
1933 * At IO completion time the cums attached on the ordered extent record
1934 * are inserted into the btree
1935 */
Mike Christie81a75f62016-06-05 14:31:54 -05001936static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001939{
1940 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001941 int ret;
1942
Mike Christie81a75f62016-06-05 14:31:54 -05001943 ret = btrfs_map_bio(root, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001944 if (ret) {
1945 bio->bi_error = ret;
1946 bio_endio(bio);
1947 }
Stefan Behrens61891922012-11-05 18:51:52 +01001948 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001949}
1950
Chris Masond352ac62008-09-29 15:18:18 -04001951/*
Chris Masoncad321a2008-12-17 14:51:42 -05001952 * extent_io.c submission hook. This does the right thing for csum calculation
1953 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001954 */
Mike Christie81a75f62016-06-05 14:31:54 -05001955static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001956 int mirror_num, unsigned long bio_flags,
1957 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001958{
1959 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301960 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001961 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001962 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001963 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001964
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001965 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001966
Liu Bo83eea1f2012-07-10 05:28:39 -06001967 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301968 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001969
Mike Christie37226b22016-06-05 14:31:52 -05001970 if (bio_op(bio) != REQ_OP_WRITE) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001971 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1972 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001973 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001974
Chris Masond20f7042008-12-08 16:58:54 -05001975 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001976 ret = btrfs_submit_compressed_read(inode, bio,
1977 mirror_num,
1978 bio_flags);
1979 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001980 } else if (!skip_sum) {
1981 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1982 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001983 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001984 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001985 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001986 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001987 /* csum items have already been cloned */
1988 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1989 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001990 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001991 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -05001992 inode, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001993 bio_flags, bio_offset,
1994 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001995 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001996 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001997 } else if (!skip_sum) {
1998 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1999 if (ret)
2000 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002001 }
2002
Chris Mason0b86a832008-03-24 15:01:56 -04002003mapit:
Mike Christie81a75f62016-06-05 14:31:54 -05002004 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002005
2006out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002007 if (ret < 0) {
2008 bio->bi_error = ret;
2009 bio_endio(bio);
2010 }
Stefan Behrens61891922012-11-05 18:51:52 +01002011 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002012}
Chris Mason6885f302008-02-20 16:11:05 -05002013
Chris Masond352ac62008-09-29 15:18:18 -04002014/*
2015 * given a list of ordered sums record them in the inode. This happens
2016 * at IO completion time based on sums calculated at bio submission time.
2017 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002018static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002019 struct inode *inode, u64 file_offset,
2020 struct list_head *list)
2021{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002022 struct btrfs_ordered_sum *sum;
2023
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002024 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002025 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002026 btrfs_csum_file_blocks(trans,
2027 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002028 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002029 }
2030 return 0;
2031}
2032
Josef Bacik2ac55d42010-02-03 19:33:23 +00002033int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002034 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002035{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002036 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002037 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002038 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002039}
2040
Chris Masond352ac62008-09-29 15:18:18 -04002041/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002042struct btrfs_writepage_fixup {
2043 struct page *page;
2044 struct btrfs_work work;
2045};
2046
Christoph Hellwigb2950862008-12-02 09:54:17 -05002047static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002048{
2049 struct btrfs_writepage_fixup *fixup;
2050 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002051 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002052 struct page *page;
2053 struct inode *inode;
2054 u64 page_start;
2055 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002056 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002057
2058 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2059 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002060again:
Chris Mason247e7432008-07-17 12:53:51 -04002061 lock_page(page);
2062 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2063 ClearPageChecked(page);
2064 goto out_page;
2065 }
2066
2067 inode = page->mapping->host;
2068 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002069 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002070
David Sterbaff13db42015-12-03 14:30:40 +01002071 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002072 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002073
2074 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002075 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002076 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002077
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302078 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002079 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002080 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002081 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2082 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002083 unlock_page(page);
2084 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002085 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002086 goto again;
2087 }
Chris Mason247e7432008-07-17 12:53:51 -04002088
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002089 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002091 if (ret) {
2092 mapping_set_error(page->mapping, ret);
2093 end_extent_writepage(page, ret, page_start, page_end);
2094 ClearPageChecked(page);
2095 goto out;
2096 }
2097
Nikolay Borisov8fe7cea2017-12-05 09:29:19 +02002098 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
2099 &cached_state, 0);
2100 if (ret) {
2101 mapping_set_error(page->mapping, ret);
2102 end_extent_writepage(page, ret, page_start, page_end);
2103 ClearPageChecked(page);
2104 goto out;
2105 }
2106
Chris Mason247e7432008-07-17 12:53:51 -04002107 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002108 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002109out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002110 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2111 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002112out_page:
2113 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002114 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002115 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002116}
2117
2118/*
2119 * There are a few paths in the higher layers of the kernel that directly
2120 * set the page dirty bit without asking the filesystem if it is a
2121 * good idea. This causes problems because we want to make sure COW
2122 * properly happens and the data=ordered rules are followed.
2123 *
Chris Masonc8b97812008-10-29 14:49:59 -04002124 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002125 * hasn't been properly setup for IO. We kick off an async process
2126 * to fix it up. The async helper will wait for ordered extents, set
2127 * the delalloc bit and make it safe to write the page.
2128 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002129static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002130{
2131 struct inode *inode = page->mapping->host;
2132 struct btrfs_writepage_fixup *fixup;
2133 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002134
Chris Mason8b62b722009-09-02 16:53:46 -04002135 /* this page is properly in the ordered list */
2136 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002137 return 0;
2138
2139 if (PageChecked(page))
2140 return -EAGAIN;
2141
2142 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2143 if (!fixup)
2144 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002145
Chris Mason247e7432008-07-17 12:53:51 -04002146 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002148 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2149 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002150 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002151 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002152 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002153}
2154
Yan Zhengd899e052008-10-30 14:25:28 -04002155static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2156 struct inode *inode, u64 file_pos,
2157 u64 disk_bytenr, u64 disk_num_bytes,
2158 u64 num_bytes, u64 ram_bytes,
2159 u8 compression, u8 encryption,
2160 u16 other_encoding, int extent_type)
2161{
2162 struct btrfs_root *root = BTRFS_I(inode)->root;
2163 struct btrfs_file_extent_item *fi;
2164 struct btrfs_path *path;
2165 struct extent_buffer *leaf;
2166 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002167 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002168 int ret;
2169
2170 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002171 if (!path)
2172 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002173
Chris Masona1ed8352009-09-11 12:27:37 -04002174 /*
2175 * we may be replacing one extent in the tree with another.
2176 * The new extent is pinned in the extent map, and we don't want
2177 * to drop it from the cache until it is completely in the btree.
2178 *
2179 * So, tell btrfs_drop_extents to leave this extent in the cache.
2180 * the caller is expected to unpin it and allow it to be merged
2181 * with the others.
2182 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002183 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2184 file_pos + num_bytes, NULL, 0,
2185 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002186 if (ret)
2187 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002188
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002189 if (!extent_inserted) {
2190 ins.objectid = btrfs_ino(inode);
2191 ins.offset = file_pos;
2192 ins.type = BTRFS_EXTENT_DATA_KEY;
2193
2194 path->leave_spinning = 1;
2195 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2196 sizeof(*fi));
2197 if (ret)
2198 goto out;
2199 }
Yan Zhengd899e052008-10-30 14:25:28 -04002200 leaf = path->nodes[0];
2201 fi = btrfs_item_ptr(leaf, path->slots[0],
2202 struct btrfs_file_extent_item);
2203 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2204 btrfs_set_file_extent_type(leaf, fi, extent_type);
2205 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2206 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2207 btrfs_set_file_extent_offset(leaf, fi, 0);
2208 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2209 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2210 btrfs_set_file_extent_compression(leaf, fi, compression);
2211 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2212 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002213
Yan Zhengd899e052008-10-30 14:25:28 -04002214 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002215 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002216
2217 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002218
2219 ins.objectid = disk_bytenr;
2220 ins.offset = disk_num_bytes;
2221 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222 ret = btrfs_alloc_reserved_file_extent(trans, root,
2223 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002224 btrfs_ino(inode), file_pos,
2225 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002226 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002227 * Release the reserved range from inode dirty range map, as it is
2228 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002229 */
2230 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231out:
Yan Zhengd899e052008-10-30 14:25:28 -04002232 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002233
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002235}
2236
Liu Bo38c227d2013-01-29 03:18:40 +00002237/* snapshot-aware defrag */
2238struct sa_defrag_extent_backref {
2239 struct rb_node node;
2240 struct old_sa_defrag_extent *old;
2241 u64 root_id;
2242 u64 inum;
2243 u64 file_pos;
2244 u64 extent_offset;
2245 u64 num_bytes;
2246 u64 generation;
2247};
2248
2249struct old_sa_defrag_extent {
2250 struct list_head list;
2251 struct new_sa_defrag_extent *new;
2252
2253 u64 extent_offset;
2254 u64 bytenr;
2255 u64 offset;
2256 u64 len;
2257 int count;
2258};
2259
2260struct new_sa_defrag_extent {
2261 struct rb_root root;
2262 struct list_head head;
2263 struct btrfs_path *path;
2264 struct inode *inode;
2265 u64 file_pos;
2266 u64 len;
2267 u64 bytenr;
2268 u64 disk_len;
2269 u8 compress_type;
2270};
2271
2272static int backref_comp(struct sa_defrag_extent_backref *b1,
2273 struct sa_defrag_extent_backref *b2)
2274{
2275 if (b1->root_id < b2->root_id)
2276 return -1;
2277 else if (b1->root_id > b2->root_id)
2278 return 1;
2279
2280 if (b1->inum < b2->inum)
2281 return -1;
2282 else if (b1->inum > b2->inum)
2283 return 1;
2284
2285 if (b1->file_pos < b2->file_pos)
2286 return -1;
2287 else if (b1->file_pos > b2->file_pos)
2288 return 1;
2289
2290 /*
2291 * [------------------------------] ===> (a range of space)
2292 * |<--->| |<---->| =============> (fs/file tree A)
2293 * |<---------------------------->| ===> (fs/file tree B)
2294 *
2295 * A range of space can refer to two file extents in one tree while
2296 * refer to only one file extent in another tree.
2297 *
2298 * So we may process a disk offset more than one time(two extents in A)
2299 * and locate at the same extent(one extent in B), then insert two same
2300 * backrefs(both refer to the extent in B).
2301 */
2302 return 0;
2303}
2304
2305static void backref_insert(struct rb_root *root,
2306 struct sa_defrag_extent_backref *backref)
2307{
2308 struct rb_node **p = &root->rb_node;
2309 struct rb_node *parent = NULL;
2310 struct sa_defrag_extent_backref *entry;
2311 int ret;
2312
2313 while (*p) {
2314 parent = *p;
2315 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2316
2317 ret = backref_comp(backref, entry);
2318 if (ret < 0)
2319 p = &(*p)->rb_left;
2320 else
2321 p = &(*p)->rb_right;
2322 }
2323
2324 rb_link_node(&backref->node, parent, p);
2325 rb_insert_color(&backref->node, root);
2326}
2327
2328/*
2329 * Note the backref might has changed, and in this case we just return 0.
2330 */
2331static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2332 void *ctx)
2333{
2334 struct btrfs_file_extent_item *extent;
2335 struct btrfs_fs_info *fs_info;
2336 struct old_sa_defrag_extent *old = ctx;
2337 struct new_sa_defrag_extent *new = old->new;
2338 struct btrfs_path *path = new->path;
2339 struct btrfs_key key;
2340 struct btrfs_root *root;
2341 struct sa_defrag_extent_backref *backref;
2342 struct extent_buffer *leaf;
2343 struct inode *inode = new->inode;
2344 int slot;
2345 int ret;
2346 u64 extent_offset;
2347 u64 num_bytes;
2348
2349 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2350 inum == btrfs_ino(inode))
2351 return 0;
2352
2353 key.objectid = root_id;
2354 key.type = BTRFS_ROOT_ITEM_KEY;
2355 key.offset = (u64)-1;
2356
2357 fs_info = BTRFS_I(inode)->root->fs_info;
2358 root = btrfs_read_fs_root_no_name(fs_info, &key);
2359 if (IS_ERR(root)) {
2360 if (PTR_ERR(root) == -ENOENT)
2361 return 0;
2362 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002363 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002364 inum, offset, root_id);
2365 return PTR_ERR(root);
2366 }
2367
2368 key.objectid = inum;
2369 key.type = BTRFS_EXTENT_DATA_KEY;
2370 if (offset > (u64)-1 << 32)
2371 key.offset = 0;
2372 else
2373 key.offset = offset;
2374
2375 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302376 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002377 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002378 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002379
2380 while (1) {
2381 cond_resched();
2382
2383 leaf = path->nodes[0];
2384 slot = path->slots[0];
2385
2386 if (slot >= btrfs_header_nritems(leaf)) {
2387 ret = btrfs_next_leaf(root, path);
2388 if (ret < 0) {
2389 goto out;
2390 } else if (ret > 0) {
2391 ret = 0;
2392 goto out;
2393 }
2394 continue;
2395 }
2396
2397 path->slots[0]++;
2398
2399 btrfs_item_key_to_cpu(leaf, &key, slot);
2400
2401 if (key.objectid > inum)
2402 goto out;
2403
2404 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2405 continue;
2406
2407 extent = btrfs_item_ptr(leaf, slot,
2408 struct btrfs_file_extent_item);
2409
2410 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2411 continue;
2412
Liu Boe68afa42013-07-01 22:13:26 +08002413 /*
2414 * 'offset' refers to the exact key.offset,
2415 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2416 * (key.offset - extent_offset).
2417 */
2418 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002419 continue;
2420
Liu Boe68afa42013-07-01 22:13:26 +08002421 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002422 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002423
Liu Bo38c227d2013-01-29 03:18:40 +00002424 if (extent_offset >= old->extent_offset + old->offset +
2425 old->len || extent_offset + num_bytes <=
2426 old->extent_offset + old->offset)
2427 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002428 break;
2429 }
2430
2431 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2432 if (!backref) {
2433 ret = -ENOENT;
2434 goto out;
2435 }
2436
2437 backref->root_id = root_id;
2438 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002439 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002440 backref->num_bytes = num_bytes;
2441 backref->extent_offset = extent_offset;
2442 backref->generation = btrfs_file_extent_generation(leaf, extent);
2443 backref->old = old;
2444 backref_insert(&new->root, backref);
2445 old->count++;
2446out:
2447 btrfs_release_path(path);
2448 WARN_ON(ret);
2449 return ret;
2450}
2451
2452static noinline bool record_extent_backrefs(struct btrfs_path *path,
2453 struct new_sa_defrag_extent *new)
2454{
2455 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2456 struct old_sa_defrag_extent *old, *tmp;
2457 int ret;
2458
2459 new->path = path;
2460
2461 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002462 ret = iterate_inodes_from_logical(old->bytenr +
2463 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002464 path, record_one_backref,
2465 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002466 if (ret < 0 && ret != -ENOENT)
2467 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002468
2469 /* no backref to be processed for this extent */
2470 if (!old->count) {
2471 list_del(&old->list);
2472 kfree(old);
2473 }
2474 }
2475
2476 if (list_empty(&new->head))
2477 return false;
2478
2479 return true;
2480}
2481
2482static int relink_is_mergable(struct extent_buffer *leaf,
2483 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002484 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002485{
Liu Bo116e0022013-08-02 16:30:40 +08002486 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002487 return 0;
2488
2489 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2490 return 0;
2491
Liu Bo116e0022013-08-02 16:30:40 +08002492 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2493 return 0;
2494
2495 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002496 btrfs_file_extent_other_encoding(leaf, fi))
2497 return 0;
2498
2499 return 1;
2500}
2501
2502/*
2503 * Note the backref might has changed, and in this case we just return 0.
2504 */
2505static noinline int relink_extent_backref(struct btrfs_path *path,
2506 struct sa_defrag_extent_backref *prev,
2507 struct sa_defrag_extent_backref *backref)
2508{
2509 struct btrfs_file_extent_item *extent;
2510 struct btrfs_file_extent_item *item;
2511 struct btrfs_ordered_extent *ordered;
2512 struct btrfs_trans_handle *trans;
2513 struct btrfs_fs_info *fs_info;
2514 struct btrfs_root *root;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2517 struct old_sa_defrag_extent *old = backref->old;
2518 struct new_sa_defrag_extent *new = old->new;
2519 struct inode *src_inode = new->inode;
2520 struct inode *inode;
2521 struct extent_state *cached = NULL;
2522 int ret = 0;
2523 u64 start;
2524 u64 len;
2525 u64 lock_start;
2526 u64 lock_end;
2527 bool merge = false;
2528 int index;
2529
2530 if (prev && prev->root_id == backref->root_id &&
2531 prev->inum == backref->inum &&
2532 prev->file_pos + prev->num_bytes == backref->file_pos)
2533 merge = true;
2534
2535 /* step 1: get root */
2536 key.objectid = backref->root_id;
2537 key.type = BTRFS_ROOT_ITEM_KEY;
2538 key.offset = (u64)-1;
2539
2540 fs_info = BTRFS_I(src_inode)->root->fs_info;
2541 index = srcu_read_lock(&fs_info->subvol_srcu);
2542
2543 root = btrfs_read_fs_root_no_name(fs_info, &key);
2544 if (IS_ERR(root)) {
2545 srcu_read_unlock(&fs_info->subvol_srcu, index);
2546 if (PTR_ERR(root) == -ENOENT)
2547 return 0;
2548 return PTR_ERR(root);
2549 }
Liu Bo38c227d2013-01-29 03:18:40 +00002550
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002551 if (btrfs_root_readonly(root)) {
2552 srcu_read_unlock(&fs_info->subvol_srcu, index);
2553 return 0;
2554 }
2555
Liu Bo38c227d2013-01-29 03:18:40 +00002556 /* step 2: get inode */
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_INODE_ITEM_KEY;
2559 key.offset = 0;
2560
2561 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2562 if (IS_ERR(inode)) {
2563 srcu_read_unlock(&fs_info->subvol_srcu, index);
2564 return 0;
2565 }
2566
2567 srcu_read_unlock(&fs_info->subvol_srcu, index);
2568
2569 /* step 3: relink backref */
2570 lock_start = backref->file_pos;
2571 lock_end = backref->file_pos + backref->num_bytes - 1;
2572 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002573 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002574
2575 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2576 if (ordered) {
2577 btrfs_put_ordered_extent(ordered);
2578 goto out_unlock;
2579 }
2580
2581 trans = btrfs_join_transaction(root);
2582 if (IS_ERR(trans)) {
2583 ret = PTR_ERR(trans);
2584 goto out_unlock;
2585 }
2586
2587 key.objectid = backref->inum;
2588 key.type = BTRFS_EXTENT_DATA_KEY;
2589 key.offset = backref->file_pos;
2590
2591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2592 if (ret < 0) {
2593 goto out_free_path;
2594 } else if (ret > 0) {
2595 ret = 0;
2596 goto out_free_path;
2597 }
2598
2599 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2600 struct btrfs_file_extent_item);
2601
2602 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2603 backref->generation)
2604 goto out_free_path;
2605
2606 btrfs_release_path(path);
2607
2608 start = backref->file_pos;
2609 if (backref->extent_offset < old->extent_offset + old->offset)
2610 start += old->extent_offset + old->offset -
2611 backref->extent_offset;
2612
2613 len = min(backref->extent_offset + backref->num_bytes,
2614 old->extent_offset + old->offset + old->len);
2615 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2616
2617 ret = btrfs_drop_extents(trans, root, inode, start,
2618 start + len, 1);
2619 if (ret)
2620 goto out_free_path;
2621again:
2622 key.objectid = btrfs_ino(inode);
2623 key.type = BTRFS_EXTENT_DATA_KEY;
2624 key.offset = start;
2625
Liu Boa09a0a72013-03-11 09:20:58 +00002626 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002627 if (merge) {
2628 struct btrfs_file_extent_item *fi;
2629 u64 extent_len;
2630 struct btrfs_key found_key;
2631
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002632 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002633 if (ret < 0)
2634 goto out_free_path;
2635
2636 path->slots[0]--;
2637 leaf = path->nodes[0];
2638 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2639
2640 fi = btrfs_item_ptr(leaf, path->slots[0],
2641 struct btrfs_file_extent_item);
2642 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2643
Liu Bo116e0022013-08-02 16:30:40 +08002644 if (extent_len + found_key.offset == start &&
2645 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002646 btrfs_set_file_extent_num_bytes(leaf, fi,
2647 extent_len + len);
2648 btrfs_mark_buffer_dirty(leaf);
2649 inode_add_bytes(inode, len);
2650
2651 ret = 1;
2652 goto out_free_path;
2653 } else {
2654 merge = false;
2655 btrfs_release_path(path);
2656 goto again;
2657 }
2658 }
2659
2660 ret = btrfs_insert_empty_item(trans, root, path, &key,
2661 sizeof(*extent));
2662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002663 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002664 goto out_free_path;
2665 }
2666
2667 leaf = path->nodes[0];
2668 item = btrfs_item_ptr(leaf, path->slots[0],
2669 struct btrfs_file_extent_item);
2670 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2671 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2672 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2673 btrfs_set_file_extent_num_bytes(leaf, item, len);
2674 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2675 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2676 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2677 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2678 btrfs_set_file_extent_encryption(leaf, item, 0);
2679 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2680
2681 btrfs_mark_buffer_dirty(leaf);
2682 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002683 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002684
2685 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2686 new->disk_len, 0,
2687 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002688 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002691 goto out_free_path;
2692 }
2693
2694 ret = 1;
2695out_free_path:
2696 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002697 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002698 btrfs_end_transaction(trans, root);
2699out_unlock:
2700 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2701 &cached, GFP_NOFS);
2702 iput(inode);
2703 return ret;
2704}
2705
Liu Bo6f519562013-10-29 10:45:05 +08002706static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2707{
2708 struct old_sa_defrag_extent *old, *tmp;
2709
2710 if (!new)
2711 return;
2712
2713 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002714 kfree(old);
2715 }
2716 kfree(new);
2717}
2718
Liu Bo38c227d2013-01-29 03:18:40 +00002719static void relink_file_extents(struct new_sa_defrag_extent *new)
2720{
2721 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct sa_defrag_extent_backref *backref;
2723 struct sa_defrag_extent_backref *prev = NULL;
2724 struct inode *inode;
2725 struct btrfs_root *root;
2726 struct rb_node *node;
2727 int ret;
2728
2729 inode = new->inode;
2730 root = BTRFS_I(inode)->root;
2731
2732 path = btrfs_alloc_path();
2733 if (!path)
2734 return;
2735
2736 if (!record_extent_backrefs(path, new)) {
2737 btrfs_free_path(path);
2738 goto out;
2739 }
2740 btrfs_release_path(path);
2741
2742 while (1) {
2743 node = rb_first(&new->root);
2744 if (!node)
2745 break;
2746 rb_erase(node, &new->root);
2747
2748 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2749
2750 ret = relink_extent_backref(path, prev, backref);
2751 WARN_ON(ret < 0);
2752
2753 kfree(prev);
2754
2755 if (ret == 1)
2756 prev = backref;
2757 else
2758 prev = NULL;
2759 cond_resched();
2760 }
2761 kfree(prev);
2762
2763 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002764out:
Liu Bo6f519562013-10-29 10:45:05 +08002765 free_sa_defrag_extent(new);
2766
Liu Bo38c227d2013-01-29 03:18:40 +00002767 atomic_dec(&root->fs_info->defrag_running);
2768 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002769}
2770
2771static struct new_sa_defrag_extent *
2772record_old_file_extents(struct inode *inode,
2773 struct btrfs_ordered_extent *ordered)
2774{
2775 struct btrfs_root *root = BTRFS_I(inode)->root;
2776 struct btrfs_path *path;
2777 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002778 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002779 struct new_sa_defrag_extent *new;
2780 int ret;
2781
2782 new = kmalloc(sizeof(*new), GFP_NOFS);
2783 if (!new)
2784 return NULL;
2785
2786 new->inode = inode;
2787 new->file_pos = ordered->file_offset;
2788 new->len = ordered->len;
2789 new->bytenr = ordered->start;
2790 new->disk_len = ordered->disk_len;
2791 new->compress_type = ordered->compress_type;
2792 new->root = RB_ROOT;
2793 INIT_LIST_HEAD(&new->head);
2794
2795 path = btrfs_alloc_path();
2796 if (!path)
2797 goto out_kfree;
2798
2799 key.objectid = btrfs_ino(inode);
2800 key.type = BTRFS_EXTENT_DATA_KEY;
2801 key.offset = new->file_pos;
2802
2803 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2804 if (ret < 0)
2805 goto out_free_path;
2806 if (ret > 0 && path->slots[0] > 0)
2807 path->slots[0]--;
2808
2809 /* find out all the old extents for the file range */
2810 while (1) {
2811 struct btrfs_file_extent_item *extent;
2812 struct extent_buffer *l;
2813 int slot;
2814 u64 num_bytes;
2815 u64 offset;
2816 u64 end;
2817 u64 disk_bytenr;
2818 u64 extent_offset;
2819
2820 l = path->nodes[0];
2821 slot = path->slots[0];
2822
2823 if (slot >= btrfs_header_nritems(l)) {
2824 ret = btrfs_next_leaf(root, path);
2825 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002826 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002827 else if (ret > 0)
2828 break;
2829 continue;
2830 }
2831
2832 btrfs_item_key_to_cpu(l, &key, slot);
2833
2834 if (key.objectid != btrfs_ino(inode))
2835 break;
2836 if (key.type != BTRFS_EXTENT_DATA_KEY)
2837 break;
2838 if (key.offset >= new->file_pos + new->len)
2839 break;
2840
2841 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2842
2843 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2844 if (key.offset + num_bytes < new->file_pos)
2845 goto next;
2846
2847 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2848 if (!disk_bytenr)
2849 goto next;
2850
2851 extent_offset = btrfs_file_extent_offset(l, extent);
2852
2853 old = kmalloc(sizeof(*old), GFP_NOFS);
2854 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002855 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002856
2857 offset = max(new->file_pos, key.offset);
2858 end = min(new->file_pos + new->len, key.offset + num_bytes);
2859
2860 old->bytenr = disk_bytenr;
2861 old->extent_offset = extent_offset;
2862 old->offset = offset - key.offset;
2863 old->len = end - offset;
2864 old->new = new;
2865 old->count = 0;
2866 list_add_tail(&old->list, &new->head);
2867next:
2868 path->slots[0]++;
2869 cond_resched();
2870 }
2871
2872 btrfs_free_path(path);
2873 atomic_inc(&root->fs_info->defrag_running);
2874
2875 return new;
2876
Liu Bo38c227d2013-01-29 03:18:40 +00002877out_free_path:
2878 btrfs_free_path(path);
2879out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002880 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002881 return NULL;
2882}
2883
Miao Xiee570fd22014-06-19 10:42:50 +08002884static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2885 u64 start, u64 len)
2886{
2887 struct btrfs_block_group_cache *cache;
2888
2889 cache = btrfs_lookup_block_group(root->fs_info, start);
2890 ASSERT(cache);
2891
2892 spin_lock(&cache->lock);
2893 cache->delalloc_bytes -= len;
2894 spin_unlock(&cache->lock);
2895
2896 btrfs_put_block_group(cache);
2897}
2898
Chris Masond352ac62008-09-29 15:18:18 -04002899/* as ordered data IO finishes, this gets called so we can finish
2900 * an ordered extent if the range of bytes in the file it covers are
2901 * fully written.
2902 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002903static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002904{
Josef Bacik5fd02042012-05-02 14:00:54 -04002905 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002906 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002907 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002909 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002910 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002911 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002912 int ret = 0;
2913 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002914 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002915 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916
Liu Bo83eea1f2012-07-10 05:28:39 -06002917 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002918
Josef Bacik5fd02042012-05-02 14:00:54 -04002919 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2920 ret = -EIO;
2921 goto out;
2922 }
2923
Miao Xief6124962014-09-12 18:44:04 +08002924 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2925 ordered_extent->file_offset +
2926 ordered_extent->len - 1);
2927
Josef Bacik77cef2e2013-08-29 13:57:21 -04002928 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2929 truncated = true;
2930 logical_len = ordered_extent->truncated_len;
2931 /* Truncated the entire extent, don't bother adding */
2932 if (!logical_len)
2933 goto out;
2934 }
2935
Yan, Zhengc2167752009-11-12 09:34:21 +00002936 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002937 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002938
2939 /*
2940 * For mwrite(mmap + memset to write) case, we still reserve
2941 * space for NOCOW range.
2942 * As NOCOW won't cause a new delayed ref, just free the space
2943 */
2944 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2945 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002946 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2947 if (nolock)
2948 trans = btrfs_join_transaction_nolock(root);
2949 else
2950 trans = btrfs_join_transaction(root);
2951 if (IS_ERR(trans)) {
2952 ret = PTR_ERR(trans);
2953 trans = NULL;
2954 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002955 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002956 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2957 ret = btrfs_update_inode_fallback(trans, root, inode);
2958 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002959 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002960 goto out;
2961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002962
Josef Bacik2ac55d42010-02-03 19:33:23 +00002963 lock_extent_bits(io_tree, ordered_extent->file_offset,
2964 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002965 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966
Liu Bo38c227d2013-01-29 03:18:40 +00002967 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2968 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bof33ed842017-05-26 17:44:23 -06002969 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002970 if (ret) {
2971 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002972 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002973 /* the inode is shared */
2974 new = record_old_file_extents(inode, ordered_extent);
2975
2976 clear_extent_bit(io_tree, ordered_extent->file_offset,
2977 ordered_extent->file_offset + ordered_extent->len - 1,
2978 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2979 }
2980
Josef Bacik0cb59c92010-07-02 12:14:14 -04002981 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002982 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002983 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002984 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002985 if (IS_ERR(trans)) {
2986 ret = PTR_ERR(trans);
2987 trans = NULL;
2988 goto out_unlock;
2989 }
Chris Masona79b7d42014-05-22 16:18:52 -07002990
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002991 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002992
Chris Masonc8b97812008-10-29 14:49:59 -04002993 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002994 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002995 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002996 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002997 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002998 ordered_extent->file_offset,
2999 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003000 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003001 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04003002 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003003 ret = insert_reserved_file_extent(trans, inode,
3004 ordered_extent->file_offset,
3005 ordered_extent->start,
3006 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003007 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003008 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003009 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003010 if (!ret)
3011 btrfs_release_delalloc_bytes(root,
3012 ordered_extent->start,
3013 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003014 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003015 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3016 ordered_extent->file_offset, ordered_extent->len,
3017 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003019 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04003020 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003022
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023 add_pending_csums(trans, inode, ordered_extent->file_offset,
3024 &ordered_extent->list);
3025
Josef Bacik6c760c02012-11-09 10:53:21 -05003026 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3027 ret = btrfs_update_inode_fallback(trans, root, inode);
3028 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003029 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05003030 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003031 }
3032 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04003033out_unlock:
3034 unlock_extent_cached(io_tree, ordered_extent->file_offset,
3035 ordered_extent->file_offset +
3036 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00003037out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003038 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003039 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003040 if (trans)
3041 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003042
Josef Bacik77cef2e2013-08-29 13:57:21 -04003043 if (ret || truncated) {
3044 u64 start, end;
3045
3046 if (truncated)
3047 start = ordered_extent->file_offset + logical_len;
3048 else
3049 start = ordered_extent->file_offset;
3050 end = ordered_extent->file_offset + ordered_extent->len - 1;
3051 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3052
3053 /* Drop the cache for the part of the extent we didn't write. */
3054 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003055
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003056 /*
3057 * If the ordered extent had an IOERR or something else went
3058 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003059 * back to the allocator. We only free the extent in the
3060 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003061 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003062 if ((ret || !logical_len) &&
3063 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003064 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3065 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003066 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003067 }
3068
3069
Josef Bacik5fd02042012-05-02 14:00:54 -04003070 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003071 * This needs to be done to make sure anybody waiting knows we are done
3072 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003073 */
3074 btrfs_remove_ordered_extent(inode, ordered_extent);
3075
Liu Bo38c227d2013-01-29 03:18:40 +00003076 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003077 if (new) {
3078 if (ret) {
3079 free_sa_defrag_extent(new);
3080 atomic_dec(&root->fs_info->defrag_running);
3081 } else {
3082 relink_file_extents(new);
3083 }
3084 }
Liu Bo38c227d2013-01-29 03:18:40 +00003085
Chris Masone6dcd2d2008-07-17 12:53:50 -04003086 /* once for us */
3087 btrfs_put_ordered_extent(ordered_extent);
3088 /* once for the tree */
3089 btrfs_put_ordered_extent(ordered_extent);
3090
Josef Bacik5fd02042012-05-02 14:00:54 -04003091 return ret;
3092}
3093
3094static void finish_ordered_fn(struct btrfs_work *work)
3095{
3096 struct btrfs_ordered_extent *ordered_extent;
3097 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3098 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003099}
3100
Christoph Hellwigb2950862008-12-02 09:54:17 -05003101static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003102 struct extent_state *state, int uptodate)
3103{
Josef Bacik5fd02042012-05-02 14:00:54 -04003104 struct inode *inode = page->mapping->host;
3105 struct btrfs_root *root = BTRFS_I(inode)->root;
3106 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003107 struct btrfs_workqueue *wq;
3108 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003109
liubo1abe9b82011-03-24 11:18:59 +00003110 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3111
Chris Mason8b62b722009-09-02 16:53:46 -04003112 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003113 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3114 end - start + 1, uptodate))
3115 return 0;
3116
Liu Bo9e0af232014-08-15 23:36:53 +08003117 if (btrfs_is_free_space_inode(inode)) {
3118 wq = root->fs_info->endio_freespace_worker;
3119 func = btrfs_freespace_write_helper;
3120 } else {
3121 wq = root->fs_info->endio_write_workers;
3122 func = btrfs_endio_write_helper;
3123 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003124
Liu Bo9e0af232014-08-15 23:36:53 +08003125 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3126 NULL);
3127 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003128
3129 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003130}
3131
Miao Xiedc380ae2014-09-12 18:43:55 +08003132static int __readpage_endio_check(struct inode *inode,
3133 struct btrfs_io_bio *io_bio,
3134 int icsum, struct page *page,
3135 int pgoff, u64 start, size_t len)
3136{
3137 char *kaddr;
3138 u32 csum_expected;
3139 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003140
3141 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3142
3143 kaddr = kmap_atomic(page);
3144 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3145 btrfs_csum_final(csum, (char *)&csum);
3146 if (csum != csum_expected)
3147 goto zeroit;
3148
3149 kunmap_atomic(kaddr);
3150 return 0;
3151zeroit:
David Sterba94647322015-10-08 11:01:36 +02003152 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3153 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003154 btrfs_ino(inode), start, csum, csum_expected);
3155 memset(kaddr + pgoff, 1, len);
3156 flush_dcache_page(page);
3157 kunmap_atomic(kaddr);
3158 if (csum_expected == 0)
3159 return 0;
3160 return -EIO;
3161}
3162
Chris Masond352ac62008-09-29 15:18:18 -04003163/*
Chris Masond352ac62008-09-29 15:18:18 -04003164 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003165 * if there's a match, we allow the bio to finish. If not, the code in
3166 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003167 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003168static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3169 u64 phy_offset, struct page *page,
3170 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003171{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003172 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003173 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003174 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003175 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003176
Chris Masond20f7042008-12-08 16:58:54 -05003177 if (PageChecked(page)) {
3178 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003179 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003180 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003181
3182 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003183 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003184
Yan Zheng17d217f2008-12-12 10:03:38 -05003185 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003186 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003187 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003188 return 0;
3189 }
3190
Miao Xiefacc8a222013-07-25 19:22:34 +08003191 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003192 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3193 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003194}
Chris Masonb888db22007-08-27 16:49:44 -04003195
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003196void btrfs_add_delayed_iput(struct inode *inode)
3197{
3198 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003199 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003200
3201 if (atomic_add_unless(&inode->i_count, -1, 1))
3202 return;
3203
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003204 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003205 if (binode->delayed_iput_count == 0) {
3206 ASSERT(list_empty(&binode->delayed_iput));
3207 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3208 } else {
3209 binode->delayed_iput_count++;
3210 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003211 spin_unlock(&fs_info->delayed_iput_lock);
3212}
3213
3214void btrfs_run_delayed_iputs(struct btrfs_root *root)
3215{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003216 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003217
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003219 while (!list_empty(&fs_info->delayed_iputs)) {
3220 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003221
David Sterba8089fe62015-11-19 14:15:51 +01003222 inode = list_first_entry(&fs_info->delayed_iputs,
3223 struct btrfs_inode, delayed_iput);
3224 if (inode->delayed_iput_count) {
3225 inode->delayed_iput_count--;
3226 list_move_tail(&inode->delayed_iput,
3227 &fs_info->delayed_iputs);
3228 } else {
3229 list_del_init(&inode->delayed_iput);
3230 }
3231 spin_unlock(&fs_info->delayed_iput_lock);
3232 iput(&inode->vfs_inode);
3233 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003234 }
David Sterba8089fe62015-11-19 14:15:51 +01003235 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236}
3237
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003239 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240 * files in the subvolume, it removes orphan item and frees block_rsv
3241 * structure.
3242 */
3243void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3244 struct btrfs_root *root)
3245{
Josef Bacik90290e12011-12-02 15:44:12 -05003246 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 int ret;
3248
Josef Bacik8a35d952012-05-23 14:26:42 -04003249 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3251 return;
3252
Josef Bacik90290e12011-12-02 15:44:12 -05003253 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003254 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003255 spin_unlock(&root->orphan_lock);
3256 return;
3257 }
3258
3259 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3260 spin_unlock(&root->orphan_lock);
3261 return;
3262 }
3263
3264 block_rsv = root->orphan_block_rsv;
3265 root->orphan_block_rsv = NULL;
3266 spin_unlock(&root->orphan_lock);
3267
Miao Xie27cdeb72014-04-02 19:51:05 +08003268 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 btrfs_root_refs(&root->root_item) > 0) {
3270 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3271 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003272 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003273 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003274 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003275 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3276 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 }
3278
Josef Bacik90290e12011-12-02 15:44:12 -05003279 if (block_rsv) {
3280 WARN_ON(block_rsv->size > 0);
3281 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 }
3283}
3284
3285/*
Josef Bacik7b128762008-07-24 12:17:14 -04003286 * This creates an orphan entry for the given inode in case something goes
3287 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 *
3289 * NOTE: caller of this function should reserve 5 units of metadata for
3290 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003291 */
3292int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3293{
3294 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 struct btrfs_block_rsv *block_rsv = NULL;
3296 int reserve = 0;
3297 int insert = 0;
3298 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003299
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003301 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003302 if (!block_rsv)
3303 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003304 }
3305
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 spin_lock(&root->orphan_lock);
3307 if (!root->orphan_block_rsv) {
3308 root->orphan_block_rsv = block_rsv;
3309 } else if (block_rsv) {
3310 btrfs_free_block_rsv(root, block_rsv);
3311 block_rsv = NULL;
3312 }
Josef Bacik7b128762008-07-24 12:17:14 -04003313
Josef Bacik8a35d952012-05-23 14:26:42 -04003314 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3315 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316#if 0
3317 /*
3318 * For proper ENOSPC handling, we should do orphan
3319 * cleanup when mounting. But this introduces backward
3320 * compatibility issue.
3321 */
3322 if (!xchg(&root->orphan_item_inserted, 1))
3323 insert = 2;
3324 else
3325 insert = 1;
3326#endif
3327 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003328 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 }
Josef Bacik7b128762008-07-24 12:17:14 -04003330
Josef Bacik72ac3c02012-05-23 14:13:11 -04003331 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3332 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003335
Yan, Zhengd68fc572010-05-16 10:49:58 -04003336 /* grab metadata reservation from transaction handle */
3337 if (reserve) {
3338 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003339 ASSERT(!ret);
3340 if (ret) {
3341 atomic_dec(&root->orphan_inodes);
3342 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3343 &BTRFS_I(inode)->runtime_flags);
3344 if (insert)
3345 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3346 &BTRFS_I(inode)->runtime_flags);
3347 return ret;
3348 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003349 }
3350
3351 /* insert an orphan item to track this unlinked/truncated file */
3352 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003353 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003354 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003355 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003356 if (reserve) {
3357 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3358 &BTRFS_I(inode)->runtime_flags);
3359 btrfs_orphan_release_metadata(inode);
3360 }
3361 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003362 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3363 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003364 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003365 return ret;
3366 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003367 }
3368 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003369 }
3370
3371 /* insert an orphan item to track subvolume contains orphan files */
3372 if (insert >= 2) {
3373 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3374 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003375 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003376 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003377 return ret;
3378 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 }
3380 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003381}
3382
3383/*
3384 * We have done the truncate/delete so we can go ahead and remove the orphan
3385 * item for this particular inode.
3386 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003387static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3388 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003389{
3390 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003391 int delete_item = 0;
3392 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003393 int ret = 0;
3394
Yan, Zhengd68fc572010-05-16 10:49:58 -04003395 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003396 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3397 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003398 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003399
Josef Bacik72ac3c02012-05-23 14:13:11 -04003400 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3401 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003403 spin_unlock(&root->orphan_lock);
3404
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003405 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003406 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003407 if (trans)
3408 ret = btrfs_del_orphan_item(trans, root,
3409 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003410 }
Josef Bacik7b128762008-07-24 12:17:14 -04003411
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003412 if (release_rsv)
3413 btrfs_orphan_release_metadata(inode);
3414
Josef Bacik4ef31a42013-08-13 14:10:08 -04003415 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003416}
3417
3418/*
3419 * this cleans up any orphans that may be left on the list from the last use
3420 * of this root.
3421 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003422int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003423{
3424 struct btrfs_path *path;
3425 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003426 struct btrfs_key key, found_key;
3427 struct btrfs_trans_handle *trans;
3428 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003429 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003430 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3431
Yan, Zhengd68fc572010-05-16 10:49:58 -04003432 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003433 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003434
3435 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003436 if (!path) {
3437 ret = -ENOMEM;
3438 goto out;
3439 }
David Sterbae4058b52015-11-27 16:31:35 +01003440 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003441
3442 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003443 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003444 key.offset = (u64)-1;
3445
Josef Bacik7b128762008-07-24 12:17:14 -04003446 while (1) {
3447 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003448 if (ret < 0)
3449 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003450
3451 /*
3452 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003453 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003454 * find the key and see if we have stuff that matches
3455 */
3456 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003458 if (path->slots[0] == 0)
3459 break;
3460 path->slots[0]--;
3461 }
3462
3463 /* pull out the item */
3464 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003465 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3466
3467 /* make sure the item matches what we want */
3468 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3469 break;
David Sterba962a2982014-06-04 18:41:45 +02003470 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003471 break;
3472
3473 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003474 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003475
3476 /*
3477 * this is where we are basically btrfs_lookup, without the
3478 * crossing root thing. we store the inode number in the
3479 * offset of the orphan item.
3480 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003481
3482 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003483 btrfs_err(root->fs_info,
3484 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003485 ret = -EINVAL;
3486 goto out;
3487 }
3488
3489 last_objectid = found_key.offset;
3490
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003491 found_key.objectid = found_key.offset;
3492 found_key.type = BTRFS_INODE_ITEM_KEY;
3493 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003494 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303495 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003496 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003498
Filipe Manana67710892016-06-06 11:51:25 +01003499 if (ret == -ENOENT && root == root->fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003500 struct btrfs_root *dead_root;
3501 struct btrfs_fs_info *fs_info = root->fs_info;
3502 int is_dead_root = 0;
3503
3504 /*
3505 * this is an orphan in the tree root. Currently these
3506 * could come from 2 sources:
3507 * a) a snapshot deletion in progress
3508 * b) a free space cache inode
3509 * We need to distinguish those two, as the snapshot
3510 * orphan must not get deleted.
3511 * find_dead_roots already ran before us, so if this
3512 * is a snapshot deletion, we should find the root
3513 * in the dead_roots list
3514 */
3515 spin_lock(&fs_info->trans_lock);
3516 list_for_each_entry(dead_root, &fs_info->dead_roots,
3517 root_list) {
3518 if (dead_root->root_key.objectid ==
3519 found_key.objectid) {
3520 is_dead_root = 1;
3521 break;
3522 }
3523 }
3524 spin_unlock(&fs_info->trans_lock);
3525 if (is_dead_root) {
3526 /* prevent this orphan from being found again */
3527 key.offset = found_key.objectid - 1;
3528 continue;
3529 }
3530 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003531 /*
3532 * Inode is already gone but the orphan item is still there,
3533 * kill the orphan item.
3534 */
Filipe Manana67710892016-06-06 11:51:25 +01003535 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003536 trans = btrfs_start_transaction(root, 1);
3537 if (IS_ERR(trans)) {
3538 ret = PTR_ERR(trans);
3539 goto out;
3540 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003541 btrfs_debug(root->fs_info, "auto deleting %Lu",
3542 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003543 ret = btrfs_del_orphan_item(trans, root,
3544 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003545 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003546 if (ret)
3547 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003548 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003549 }
Josef Bacik7b128762008-07-24 12:17:14 -04003550
Josef Bacik7b128762008-07-24 12:17:14 -04003551 /*
3552 * add this inode to the orphan list so btrfs_orphan_del does
3553 * the proper thing when we hit it
3554 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003555 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3556 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003557 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003558
Josef Bacik7b128762008-07-24 12:17:14 -04003559 /* if we have links, this was a truncate, lets do that */
3560 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303561 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003562 iput(inode);
3563 continue;
3564 }
Josef Bacik7b128762008-07-24 12:17:14 -04003565 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003566
3567 /* 1 for the orphan item deletion. */
3568 trans = btrfs_start_transaction(root, 1);
3569 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003570 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003571 ret = PTR_ERR(trans);
3572 goto out;
3573 }
3574 ret = btrfs_orphan_add(trans, inode);
3575 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003576 if (ret) {
3577 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003578 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003579 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003580
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003581 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003582 if (ret)
3583 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003584 } else {
3585 nr_unlink++;
3586 }
3587
3588 /* this will do delete_inode and everything for us */
3589 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003590 if (ret)
3591 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003592 }
Miao Xie3254c872011-11-10 20:45:05 -05003593 /* release the path since we're done with it */
3594 btrfs_release_path(path);
3595
Yan, Zhengd68fc572010-05-16 10:49:58 -04003596 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3597
3598 if (root->orphan_block_rsv)
3599 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3600 (u64)-1);
3601
Miao Xie27cdeb72014-04-02 19:51:05 +08003602 if (root->orphan_block_rsv ||
3603 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003604 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003605 if (!IS_ERR(trans))
3606 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003607 }
Josef Bacik7b128762008-07-24 12:17:14 -04003608
3609 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003610 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003611 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003612 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003613
3614out:
3615 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003616 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003617 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003618 btrfs_free_path(path);
3619 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003620}
3621
Chris Masond352ac62008-09-29 15:18:18 -04003622/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003623 * very simple check to peek ahead in the leaf looking for xattrs. If we
3624 * don't find any xattrs, we know there can't be any acls.
3625 *
3626 * slot is the slot the inode is in, objectid is the objectid of the inode
3627 */
3628static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003629 int slot, u64 objectid,
3630 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003631{
3632 u32 nritems = btrfs_header_nritems(leaf);
3633 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003634 static u64 xattr_access = 0;
3635 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003636 int scanned = 0;
3637
Josef Bacikf23b5a52013-06-19 10:16:26 -04003638 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003639 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3640 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3641 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3642 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003643 }
3644
Chris Mason46a53cc2009-04-27 11:47:50 -04003645 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003646 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003647 while (slot < nritems) {
3648 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3649
3650 /* we found a different objectid, there must not be acls */
3651 if (found_key.objectid != objectid)
3652 return 0;
3653
3654 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003655 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003656 if (*first_xattr_slot == -1)
3657 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003658 if (found_key.offset == xattr_access ||
3659 found_key.offset == xattr_default)
3660 return 1;
3661 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003662
3663 /*
3664 * we found a key greater than an xattr key, there can't
3665 * be any acls later on
3666 */
3667 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3668 return 0;
3669
3670 slot++;
3671 scanned++;
3672
3673 /*
3674 * it goes inode, inode backrefs, xattrs, extents,
3675 * so if there are a ton of hard links to an inode there can
3676 * be a lot of backrefs. Don't waste time searching too hard,
3677 * this is just an optimization
3678 */
3679 if (scanned >= 8)
3680 break;
3681 }
3682 /* we hit the end of the leaf before we found an xattr or
3683 * something larger than an xattr. We have to assume the inode
3684 * has acls
3685 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 if (*first_xattr_slot == -1)
3687 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003688 return 1;
3689}
3690
3691/*
Chris Masond352ac62008-09-29 15:18:18 -04003692 * read an inode from the btree into the in-memory inode
3693 */
Filipe Manana67710892016-06-06 11:51:25 +01003694static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003695{
3696 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003697 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003698 struct btrfs_inode_item *inode_item;
3699 struct btrfs_root *root = BTRFS_I(inode)->root;
3700 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003701 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003702 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003703 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003704 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003705 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003706 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003707
3708 ret = btrfs_fill_inode(inode, &rdev);
3709 if (!ret)
3710 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003711
3712 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003713 if (!path) {
3714 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003715 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003716 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003717
Chris Mason39279cc2007-06-12 06:35:45 -04003718 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003719
Chris Mason39279cc2007-06-12 06:35:45 -04003720 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003721 if (ret) {
3722 if (ret > 0)
3723 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003724 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003725 }
Chris Mason39279cc2007-06-12 06:35:45 -04003726
Chris Mason5f39d392007-10-15 16:14:19 -04003727 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003728
3729 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003730 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003731
Chris Mason5f39d392007-10-15 16:14:19 -04003732 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3733 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003734 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003735 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003736 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3737 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003738 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003739
David Sterbaa937b972014-12-12 17:39:12 +01003740 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3741 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003742
David Sterbaa937b972014-12-12 17:39:12 +01003743 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3744 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003745
David Sterbaa937b972014-12-12 17:39:12 +01003746 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3747 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
chandan r9cc97d62012-07-04 12:48:07 +05303749 BTRFS_I(inode)->i_otime.tv_sec =
3750 btrfs_timespec_sec(leaf, &inode_item->otime);
3751 BTRFS_I(inode)->i_otime.tv_nsec =
3752 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003753
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003754 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003755 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003756 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3757
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003758 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003759 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003760 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003761 rdev = btrfs_inode_rdev(leaf, inode_item);
3762
Josef Bacikaec74772008-07-24 12:12:38 -04003763 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003764 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003765
3766cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003767 /*
3768 * If we were modified in the current generation and evicted from memory
3769 * and then re-read we need to do a full sync since we don't have any
3770 * idea about which extents were modified before we were evicted from
3771 * cache.
3772 *
3773 * This is required for both inode re-read from disk and delayed inode
3774 * in delayed_nodes_tree.
3775 */
3776 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3777 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3778 &BTRFS_I(inode)->runtime_flags);
3779
Filipe Mananabde6c242015-07-24 00:00:19 +01003780 /*
3781 * We don't persist the id of the transaction where an unlink operation
3782 * against the inode was last made. So here we assume the inode might
3783 * have been evicted, and therefore the exact value of last_unlink_trans
3784 * lost, and set it to last_trans to avoid metadata inconsistencies
3785 * between the inode and its parent if the inode is fsync'ed and the log
3786 * replayed. For example, in the scenario:
3787 *
3788 * touch mydir/foo
3789 * ln mydir/foo mydir/bar
3790 * sync
3791 * unlink mydir/bar
3792 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3793 * xfs_io -c fsync mydir/foo
3794 * <power failure>
3795 * mount fs, triggers fsync log replay
3796 *
3797 * We must make sure that when we fsync our inode foo we also log its
3798 * parent inode, otherwise after log replay the parent still has the
3799 * dentry with the "bar" name but our inode foo has a link count of 1
3800 * and doesn't have an inode ref with the name "bar" anymore.
3801 *
3802 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003803 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003804 * transaction commits on fsync if our inode is a directory, or if our
3805 * inode is not a directory, logging its parent unnecessarily.
3806 */
3807 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3808
Miao Xie67de1172013-12-26 13:07:06 +08003809 path->slots[0]++;
3810 if (inode->i_nlink != 1 ||
3811 path->slots[0] >= btrfs_header_nritems(leaf))
3812 goto cache_acl;
3813
3814 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3815 if (location.objectid != btrfs_ino(inode))
3816 goto cache_acl;
3817
3818 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3819 if (location.type == BTRFS_INODE_REF_KEY) {
3820 struct btrfs_inode_ref *ref;
3821
3822 ref = (struct btrfs_inode_ref *)ptr;
3823 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3824 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3825 struct btrfs_inode_extref *extref;
3826
3827 extref = (struct btrfs_inode_extref *)ptr;
3828 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3829 extref);
3830 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003831cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003832 /*
3833 * try to precache a NULL acl entry for files that don't have
3834 * any xattrs or acls
3835 */
Li Zefan33345d012011-04-20 10:31:50 +08003836 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003837 btrfs_ino(inode), &first_xattr_slot);
3838 if (first_xattr_slot != -1) {
3839 path->slots[0] = first_xattr_slot;
3840 ret = btrfs_load_inode_props(inode, path);
3841 if (ret)
3842 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003843 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003844 btrfs_ino(inode),
3845 root->root_key.objectid, ret);
3846 }
3847 btrfs_free_path(path);
3848
Al Viro72c04902009-06-24 16:58:48 -04003849 if (!maybe_acls)
3850 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003851
Chris Mason39279cc2007-06-12 06:35:45 -04003852 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003853 case S_IFREG:
3854 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003855 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003856 inode->i_fop = &btrfs_file_operations;
3857 inode->i_op = &btrfs_file_inode_operations;
3858 break;
3859 case S_IFDIR:
3860 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval79babd42017-01-25 17:06:38 -08003861 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 break;
3863 case S_IFLNK:
3864 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003865 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003866 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3867 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003868 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003869 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003870 init_special_inode(inode, inode->i_mode, rdev);
3871 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003873
3874 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003875 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003876
3877make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003878 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003879 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003880 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003881}
3882
Chris Masond352ac62008-09-29 15:18:18 -04003883/*
3884 * given a leaf and an inode, copy the inode fields into the leaf
3885 */
Chris Masone02119d2008-09-05 16:13:11 -04003886static void fill_inode_item(struct btrfs_trans_handle *trans,
3887 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003888 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003889 struct inode *inode)
3890{
Liu Bo51fab692012-12-27 09:01:21 +00003891 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003892
Liu Bo51fab692012-12-27 09:01:21 +00003893 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003894
Liu Bo51fab692012-12-27 09:01:21 +00003895 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3896 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3897 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3898 &token);
3899 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3900 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003901
David Sterbaa937b972014-12-12 17:39:12 +01003902 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003903 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003904 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003905 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
David Sterbaa937b972014-12-12 17:39:12 +01003907 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003908 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003909 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003910 inode->i_mtime.tv_nsec, &token);
3911
David Sterbaa937b972014-12-12 17:39:12 +01003912 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003913 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_ctime.tv_nsec, &token);
3916
chandan r9cc97d62012-07-04 12:48:07 +05303917 btrfs_set_token_timespec_sec(leaf, &item->otime,
3918 BTRFS_I(inode)->i_otime.tv_sec, &token);
3919 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3920 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3921
Liu Bo51fab692012-12-27 09:01:21 +00003922 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3923 &token);
3924 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3925 &token);
3926 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3927 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3928 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3929 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3930 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003931}
3932
Chris Masond352ac62008-09-29 15:18:18 -04003933/*
3934 * copy everything in the in-memory inode into the btree.
3935 */
Chris Mason21151332011-11-10 20:39:08 -05003936static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003937 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003938{
3939 struct btrfs_inode_item *inode_item;
3940 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003941 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003942 int ret;
3943
3944 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003945 if (!path)
3946 return -ENOMEM;
3947
Chris Masonb9473432009-03-13 11:00:37 -04003948 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003949 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3950 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003951 if (ret) {
3952 if (ret > 0)
3953 ret = -ENOENT;
3954 goto failed;
3955 }
3956
Chris Mason5f39d392007-10-15 16:14:19 -04003957 leaf = path->nodes[0];
3958 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003959 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003960
Chris Masone02119d2008-09-05 16:13:11 -04003961 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003962 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003963 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003964 ret = 0;
3965failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003966 btrfs_free_path(path);
3967 return ret;
3968}
3969
Chris Masond352ac62008-09-29 15:18:18 -04003970/*
Chris Mason21151332011-11-10 20:39:08 -05003971 * copy everything in the in-memory inode into the btree.
3972 */
3973noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3974 struct btrfs_root *root, struct inode *inode)
3975{
3976 int ret;
3977
3978 /*
3979 * If the inode is a free space inode, we can deadlock during commit
3980 * if we put it into the delayed code.
3981 *
3982 * The data relocation inode should also be directly updated
3983 * without delay
3984 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003985 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003986 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacikafcdd122016-09-02 15:40:02 -04003987 && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003988 btrfs_update_root_times(trans, root);
3989
Chris Mason21151332011-11-10 20:39:08 -05003990 ret = btrfs_delayed_update_inode(trans, root, inode);
3991 if (!ret)
3992 btrfs_set_inode_last_trans(trans, inode);
3993 return ret;
3994 }
3995
3996 return btrfs_update_inode_item(trans, root, inode);
3997}
3998
Josef Bacikbe6aef62012-10-22 15:43:12 -04003999noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4000 struct btrfs_root *root,
4001 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004002{
4003 int ret;
4004
4005 ret = btrfs_update_inode(trans, root, inode);
4006 if (ret == -ENOSPC)
4007 return btrfs_update_inode_item(trans, root, inode);
4008 return ret;
4009}
4010
4011/*
Chris Masond352ac62008-09-29 15:18:18 -04004012 * unlink helper that gets used here in inode.c and in the tree logging
4013 * recovery code. It remove a link in a directory with a given name, and
4014 * also drops the back refs in the inode to the directory
4015 */
Al Viro92986792011-03-04 17:14:37 +00004016static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4017 struct btrfs_root *root,
4018 struct inode *dir, struct inode *inode,
4019 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004020{
4021 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004022 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004023 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004025 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004026 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004027 u64 ino = btrfs_ino(inode);
4028 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004029
4030 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004031 if (!path) {
4032 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004033 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004034 }
4035
Chris Masonb9473432009-03-13 11:00:37 -04004036 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004037 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004038 name, name_len, -1);
4039 if (IS_ERR(di)) {
4040 ret = PTR_ERR(di);
4041 goto err;
4042 }
4043 if (!di) {
4044 ret = -ENOENT;
4045 goto err;
4046 }
Chris Mason5f39d392007-10-15 16:14:19 -04004047 leaf = path->nodes[0];
4048 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004049 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004050 if (ret)
4051 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004052 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004053
Miao Xie67de1172013-12-26 13:07:06 +08004054 /*
4055 * If we don't have dir index, we have to get it by looking up
4056 * the inode ref, since we get the inode ref, remove it directly,
4057 * it is unnecessary to do delayed deletion.
4058 *
4059 * But if we have dir index, needn't search inode ref to get it.
4060 * Since the inode ref is close to the inode item, it is better
4061 * that we delay to delete it, and just do this deletion when
4062 * we update the inode item.
4063 */
4064 if (BTRFS_I(inode)->dir_index) {
4065 ret = btrfs_delayed_delete_inode_ref(inode);
4066 if (!ret) {
4067 index = BTRFS_I(inode)->dir_index;
4068 goto skip_backref;
4069 }
4070 }
4071
Li Zefan33345d012011-04-20 10:31:50 +08004072 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4073 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004074 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004075 btrfs_info(root->fs_info,
4076 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004077 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004078 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004079 goto err;
4080 }
Miao Xie67de1172013-12-26 13:07:06 +08004081skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004082 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004083 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004084 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004085 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 }
Chris Mason39279cc2007-06-12 06:35:45 -04004087
Chris Masone02119d2008-09-05 16:13:11 -04004088 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004089 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004090 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004091 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 goto err;
4093 }
Chris Masone02119d2008-09-05 16:13:11 -04004094
4095 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4096 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004097 if (ret == -ENOENT)
4098 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004099 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004100 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004101err:
4102 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004103 if (ret)
4104 goto out;
4105
4106 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004107 inode_inc_iversion(inode);
4108 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004109 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004110 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004111 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004112out:
Chris Mason39279cc2007-06-12 06:35:45 -04004113 return ret;
4114}
4115
Al Viro92986792011-03-04 17:14:37 +00004116int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4117 struct btrfs_root *root,
4118 struct inode *dir, struct inode *inode,
4119 const char *name, int name_len)
4120{
4121 int ret;
4122 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4123 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004124 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004125 ret = btrfs_update_inode(trans, root, inode);
4126 }
4127 return ret;
4128}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004129
4130/*
4131 * helper to start transaction for unlink and rmdir.
4132 *
Josef Bacikd52be812013-05-29 14:54:47 -04004133 * unlink and rmdir are special in btrfs, they do not always free space, so
4134 * if we cannot make our reservations the normal way try and see if there is
4135 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4136 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004137 */
Josef Bacikd52be812013-05-29 14:54:47 -04004138static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004140 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004141
Josef Bacike70bea52011-10-11 14:18:24 -04004142 /*
4143 * 1 for the possible orphan item
4144 * 1 for the dir item
4145 * 1 for the dir index
4146 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004147 * 1 for the inode
4148 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004149 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004150}
4151
Chris Mason39279cc2007-06-12 06:35:45 -04004152static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4153{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004156 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004157 int ret;
4158
Josef Bacikd52be812013-05-29 14:54:47 -04004159 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004160 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004161 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004162
David Howells2b0143b2015-03-17 22:25:59 +00004163 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004164
David Howells2b0143b2015-03-17 22:25:59 +00004165 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004166 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004167 if (ret)
4168 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004169
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004171 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004172 if (ret)
4173 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174 }
Josef Bacik7b128762008-07-24 12:17:14 -04004175
Tsutomu Itohb5324022011-07-19 07:27:20 +00004176out:
Josef Bacikd52be812013-05-29 14:54:47 -04004177 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004178 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004179 return ret;
4180}
4181
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4183 struct btrfs_root *root,
4184 struct inode *dir, u64 objectid,
4185 const char *name, int name_len)
4186{
4187 struct btrfs_path *path;
4188 struct extent_buffer *leaf;
4189 struct btrfs_dir_item *di;
4190 struct btrfs_key key;
4191 u64 index;
4192 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004193 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194
4195 path = btrfs_alloc_path();
4196 if (!path)
4197 return -ENOMEM;
4198
Li Zefan33345d012011-04-20 10:31:50 +08004199 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 if (IS_ERR_OR_NULL(di)) {
4202 if (!di)
4203 ret = -ENOENT;
4204 else
4205 ret = PTR_ERR(di);
4206 goto out;
4207 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208
4209 leaf = path->nodes[0];
4210 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4211 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4212 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004213 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004214 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004215 goto out;
4216 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004217 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218
4219 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4220 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004221 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004223 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004224 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004225 goto out;
4226 }
Li Zefan33345d012011-04-20 10:31:50 +08004227 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004228 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229 if (IS_ERR_OR_NULL(di)) {
4230 if (!di)
4231 ret = -ENOENT;
4232 else
4233 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004234 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004235 goto out;
4236 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237
4238 leaf = path->nodes[0];
4239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004240 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 index = key.offset;
4242 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004243 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004244
Miao Xie16cdcec2011-04-22 18:12:22 +08004245 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004246 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004247 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004248 goto out;
4249 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004250
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004252 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004253 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004254 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004256 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004258 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260}
4261
Chris Mason39279cc2007-06-12 06:35:45 -04004262static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4263{
David Howells2b0143b2015-03-17 22:25:59 +00004264 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004265 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004266 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004267 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004268 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004269
David Sterbab3ae2442012-09-13 16:04:34 -06004270 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004271 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004272 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4273 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004274
Josef Bacikd52be812013-05-29 14:54:47 -04004275 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004276 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004277 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004278
Li Zefan33345d012011-04-20 10:31:50 +08004279 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280 err = btrfs_unlink_subvol(trans, root, dir,
4281 BTRFS_I(inode)->location.objectid,
4282 dentry->d_name.name,
4283 dentry->d_name.len);
4284 goto out;
4285 }
4286
Josef Bacik7b128762008-07-24 12:17:14 -04004287 err = btrfs_orphan_add(trans, inode);
4288 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004289 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004290
Filipe Manana44f714d2016-06-06 16:11:13 +01004291 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4292
Chris Mason39279cc2007-06-12 06:35:45 -04004293 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004294 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004295 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004296 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004297 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004298 /*
4299 * Propagate the last_unlink_trans value of the deleted dir to
4300 * its parent directory. This is to prevent an unrecoverable
4301 * log tree in the case we do something like this:
4302 * 1) create dir foo
4303 * 2) create snapshot under dir foo
4304 * 3) delete the snapshot
4305 * 4) rmdir foo
4306 * 5) mkdir foo
4307 * 6) fsync foo or some file inside foo
4308 */
4309 if (last_unlink_trans >= trans->transid)
4310 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4311 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004312out:
Josef Bacikd52be812013-05-29 14:54:47 -04004313 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004314 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004315
Chris Mason39279cc2007-06-12 06:35:45 -04004316 return err;
4317}
4318
Chris Mason28f75a02015-02-04 06:59:29 -08004319static int truncate_space_check(struct btrfs_trans_handle *trans,
4320 struct btrfs_root *root,
4321 u64 bytes_deleted)
4322{
4323 int ret;
4324
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004325 /*
4326 * This is only used to apply pressure to the enospc system, we don't
4327 * intend to use this reservation at all.
4328 */
Chris Mason28f75a02015-02-04 06:59:29 -08004329 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004330 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004331 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4332 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004333 if (!ret) {
4334 trace_btrfs_space_reservation(root->fs_info, "transaction",
4335 trans->transid,
4336 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004337 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004338 }
Chris Mason28f75a02015-02-04 06:59:29 -08004339 return ret;
4340
4341}
4342
Filipe Manana0305cd52015-10-16 12:34:25 +01004343static int truncate_inline_extent(struct inode *inode,
4344 struct btrfs_path *path,
4345 struct btrfs_key *found_key,
4346 const u64 item_end,
4347 const u64 new_size)
4348{
4349 struct extent_buffer *leaf = path->nodes[0];
4350 int slot = path->slots[0];
4351 struct btrfs_file_extent_item *fi;
4352 u32 size = (u32)(new_size - found_key->offset);
4353 struct btrfs_root *root = BTRFS_I(inode)->root;
4354
4355 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4356
4357 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4358 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004359 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004360
4361 /*
4362 * Zero out the remaining of the last page of our inline extent,
4363 * instead of directly truncating our inline extent here - that
4364 * would be much more complex (decompressing all the data, then
4365 * compressing the truncated data, which might be bigger than
4366 * the size of the inline extent, resize the extent, etc).
4367 * We release the path because to get the page we might need to
4368 * read the extent item from disk (data not in the page cache).
4369 */
4370 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304371 return btrfs_truncate_block(inode, offset, page_end - offset,
4372 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004373 }
4374
4375 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4376 size = btrfs_file_extent_calc_inline_size(size);
4377 btrfs_truncate_item(root, path, size, 1);
4378
4379 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4380 inode_sub_bytes(inode, item_end + 1 - new_size);
4381
4382 return 0;
4383}
4384
Chris Mason323ac952008-10-01 19:05:46 -04004385/*
Chris Mason39279cc2007-06-12 06:35:45 -04004386 * this can truncate away extent items, csum items and directory items.
4387 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004388 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004389 *
4390 * csum items that cross the new i_size are truncated to the new size
4391 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004392 *
4393 * min_type is the minimum key type to truncate down to. If set to 0, this
4394 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004395 */
Yan, Zheng80825102009-11-12 09:35:36 +00004396int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4397 struct btrfs_root *root,
4398 struct inode *inode,
4399 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004400{
Chris Mason39279cc2007-06-12 06:35:45 -04004401 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004402 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004404 struct btrfs_key key;
4405 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004406 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004407 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004408 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004409 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004410 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004411 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 int found_extent;
4413 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004414 int pending_del_nr = 0;
4415 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004416 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004417 int ret;
4418 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004419 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004420 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004421 bool be_nice = 0;
4422 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004423 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004424
4425 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004426
Chris Mason28ed1342014-12-17 09:41:04 -08004427 /*
4428 * for non-free space inodes and ref cows, we want to back off from
4429 * time to time
4430 */
4431 if (!btrfs_is_free_space_inode(inode) &&
4432 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4433 be_nice = 1;
4434
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004435 path = btrfs_alloc_path();
4436 if (!path)
4437 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004438 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004439
Josef Bacik5dc562c2012-08-17 13:14:17 -04004440 /*
4441 * We want to drop from the next block forward in case this new size is
4442 * not block aligned since we will be keeping the last block of the
4443 * extent just the way it is.
4444 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004445 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4446 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004447 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4448 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004449
Miao Xie16cdcec2011-04-22 18:12:22 +08004450 /*
4451 * This function is also used to drop the items in the log tree before
4452 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4453 * it is used to drop the loged items. So we shouldn't kill the delayed
4454 * items.
4455 */
4456 if (min_type == 0 && root == BTRFS_I(inode)->root)
4457 btrfs_kill_delayed_inode_items(inode);
4458
Li Zefan33345d012011-04-20 10:31:50 +08004459 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004460 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004461 key.type = (u8)-1;
4462
Chris Mason85e21ba2008-01-29 15:11:36 -05004463search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004464 /*
4465 * with a 16K leaf size and 128MB extents, you can actually queue
4466 * up a huge file in a single leaf. Most of the time that
4467 * bytes_deleted is > 0, it will be huge by the time we get here
4468 */
Byongho Leeee221842015-12-15 01:42:10 +09004469 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004470 if (btrfs_should_end_transaction(trans, root)) {
4471 err = -EAGAIN;
4472 goto error;
4473 }
4474 }
4475
4476
Chris Masonb9473432009-03-13 11:00:37 -04004477 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004478 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004479 if (ret < 0) {
4480 err = ret;
4481 goto out;
4482 }
Chris Masond3977122009-01-05 21:25:51 -05004483
Chris Mason85e21ba2008-01-29 15:11:36 -05004484 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004485 /* there are no items in the tree for us to truncate, we're
4486 * done
4487 */
Yan, Zheng80825102009-11-12 09:35:36 +00004488 if (path->slots[0] == 0)
4489 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004490 path->slots[0]--;
4491 }
4492
Chris Masond3977122009-01-05 21:25:51 -05004493 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004494 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004495 leaf = path->nodes[0];
4496 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004497 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004498
Li Zefan33345d012011-04-20 10:31:50 +08004499 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004500 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004501
Chris Mason85e21ba2008-01-29 15:11:36 -05004502 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004503 break;
4504
Chris Mason5f39d392007-10-15 16:14:19 -04004505 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004507 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004508 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004509 extent_type = btrfs_file_extent_type(leaf, fi);
4510 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004511 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004512 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004513 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004514 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004515 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004516 }
Yan008630c2007-11-07 13:31:09 -05004517 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004518 }
Yan, Zheng80825102009-11-12 09:35:36 +00004519 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004520 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004521 } else {
Liu Boc3eab852016-12-01 13:43:31 -08004522 if (item_end < new_size) {
4523 /*
4524 * With NO_HOLES mode, for the following mapping
4525 *
4526 * [0-4k][hole][8k-12k]
4527 *
4528 * if truncating isize down to 6k, it ends up
4529 * isize being 8k.
4530 */
4531 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4532 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004533 break;
Liu Boc3eab852016-12-01 13:43:31 -08004534 }
Yan, Zheng80825102009-11-12 09:35:36 +00004535 if (found_key.offset >= new_size)
4536 del_item = 1;
4537 else
4538 del_item = 0;
4539 }
Chris Mason39279cc2007-06-12 06:35:45 -04004540 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004541 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004542 if (found_type != BTRFS_EXTENT_DATA_KEY)
4543 goto delete;
4544
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004545 if (del_item)
4546 last_size = found_key.offset;
4547 else
4548 last_size = new_size;
4549
Chris Mason179e29e2007-11-01 11:28:41 -04004550 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004551 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004552 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004553 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004554 u64 orig_num_bytes =
4555 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004556 extent_num_bytes = ALIGN(new_size -
4557 found_key.offset,
4558 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004559 btrfs_set_file_extent_num_bytes(leaf, fi,
4560 extent_num_bytes);
4561 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004562 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004563 if (test_bit(BTRFS_ROOT_REF_COWS,
4564 &root->state) &&
4565 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004566 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004567 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004568 } else {
Chris Masondb945352007-10-15 16:15:53 -04004569 extent_num_bytes =
4570 btrfs_file_extent_disk_num_bytes(leaf,
4571 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004572 extent_offset = found_key.offset -
4573 btrfs_file_extent_offset(leaf, fi);
4574
Chris Mason39279cc2007-06-12 06:35:45 -04004575 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004576 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004577 if (extent_start != 0) {
4578 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004579 if (test_bit(BTRFS_ROOT_REF_COWS,
4580 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004581 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004582 }
4583 }
Chris Mason90692182008-02-08 13:49:28 -05004584 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004585 /*
4586 * we can't truncate inline items that have had
4587 * special encodings
4588 */
4589 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004590 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4591 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004592
4593 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004594 * Need to release path in order to truncate a
4595 * compressed extent. So delete any accumulated
4596 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004597 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004598 if (btrfs_file_extent_compression(leaf, fi) !=
4599 BTRFS_COMPRESS_NONE && pending_del_nr) {
4600 err = btrfs_del_items(trans, root, path,
4601 pending_del_slot,
4602 pending_del_nr);
4603 if (err) {
4604 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004605 err);
4606 goto error;
4607 }
4608 pending_del_nr = 0;
4609 }
4610
4611 err = truncate_inline_extent(inode, path,
4612 &found_key,
4613 item_end,
4614 new_size);
4615 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004616 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004617 goto error;
4618 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004619 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4620 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004621 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004622 }
Chris Mason39279cc2007-06-12 06:35:45 -04004623 }
Chris Mason179e29e2007-11-01 11:28:41 -04004624delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004625 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004626 if (!pending_del_nr) {
4627 /* no pending yet, add ourselves */
4628 pending_del_slot = path->slots[0];
4629 pending_del_nr = 1;
4630 } else if (pending_del_nr &&
4631 path->slots[0] + 1 == pending_del_slot) {
4632 /* hop on the pending chunk */
4633 pending_del_nr++;
4634 pending_del_slot = path->slots[0];
4635 } else {
Chris Masond3977122009-01-05 21:25:51 -05004636 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004637 }
Chris Mason39279cc2007-06-12 06:35:45 -04004638 } else {
4639 break;
4640 }
Chris Mason28f75a02015-02-04 06:59:29 -08004641 should_throttle = 0;
4642
Miao Xie27cdeb72014-04-02 19:51:05 +08004643 if (found_extent &&
4644 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4645 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004646 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004647 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004648 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004649 extent_num_bytes, 0,
4650 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004651 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004652 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004653 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004654 btrfs_async_run_delayed_refs(root,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004655 trans->delayed_ref_updates * 2,
4656 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004657 if (be_nice) {
4658 if (truncate_space_check(trans, root,
4659 extent_num_bytes)) {
4660 should_end = 1;
4661 }
4662 if (btrfs_should_throttle_delayed_refs(trans,
4663 root)) {
4664 should_throttle = 1;
4665 }
4666 }
Chris Mason39279cc2007-06-12 06:35:45 -04004667 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004668
Yan, Zheng80825102009-11-12 09:35:36 +00004669 if (found_type == BTRFS_INODE_ITEM_KEY)
4670 break;
4671
4672 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004673 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004674 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004675 if (pending_del_nr) {
4676 ret = btrfs_del_items(trans, root, path,
4677 pending_del_slot,
4678 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004679 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004680 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004681 goto error;
4682 }
Yan, Zheng80825102009-11-12 09:35:36 +00004683 pending_del_nr = 0;
4684 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004685 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004686 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004687 unsigned long updates = trans->delayed_ref_updates;
4688 if (updates) {
4689 trans->delayed_ref_updates = 0;
4690 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4691 if (ret && !err)
4692 err = ret;
4693 }
4694 }
Chris Mason28f75a02015-02-04 06:59:29 -08004695 /*
4696 * if we failed to refill our space rsv, bail out
4697 * and let the transaction restart
4698 */
4699 if (should_end) {
4700 err = -EAGAIN;
4701 goto error;
4702 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004703 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 } else {
4705 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004706 }
Chris Mason39279cc2007-06-12 06:35:45 -04004707 }
Yan, Zheng80825102009-11-12 09:35:36 +00004708out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004709 if (pending_del_nr) {
4710 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4711 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004712 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004713 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004714 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004715error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004716 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004717 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004718
Chris Mason39279cc2007-06-12 06:35:45 -04004719 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004720
Byongho Leeee221842015-12-15 01:42:10 +09004721 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004722 unsigned long updates = trans->delayed_ref_updates;
4723 if (updates) {
4724 trans->delayed_ref_updates = 0;
4725 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4726 if (ret && !err)
4727 err = ret;
4728 }
4729 }
Yan, Zheng80825102009-11-12 09:35:36 +00004730 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004731}
4732
Chris Masona52d9a82007-08-27 16:49:44 -04004733/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304734 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004735 * @inode - inode that we're zeroing
4736 * @from - the offset to start zeroing
4737 * @len - the length to zero, 0 to zero the entire range respective to the
4738 * offset
4739 * @front - zero up to the offset instead of from the offset on
4740 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004742 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004743 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004745 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004746{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004747 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004748 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004749 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4750 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004751 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004752 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004753 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004754 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304755 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004756 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004757 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004758 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759 u64 block_start;
4760 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004761
Josef Bacik2aaa6652012-08-29 14:27:18 -04004762 if ((offset & (blocksize - 1)) == 0 &&
4763 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004764 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004766 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004768 if (ret)
4769 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004770
Chris Mason211c17f2008-05-15 09:13:45 -04004771again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004772 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004773 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004774 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775 round_down(from, blocksize),
4776 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004777 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004778 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004779 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004780
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 block_start = round_down(from, blocksize);
4782 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783
Chris Masona52d9a82007-08-27 16:49:44 -04004784 if (!PageUptodate(page)) {
4785 ret = btrfs_readpage(NULL, page);
4786 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004787 if (page->mapping != mapping) {
4788 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004789 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004790 goto again;
4791 }
Chris Masona52d9a82007-08-27 16:49:44 -04004792 if (!PageUptodate(page)) {
4793 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004794 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004795 }
4796 }
Chris Mason211c17f2008-05-15 09:13:45 -04004797 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304799 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004800 set_page_extent_mapped(page);
4801
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004803 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304804 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004805 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004807 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004808 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809 btrfs_put_ordered_extent(ordered);
4810 goto again;
4811 }
4812
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004814 EXTENT_DIRTY | EXTENT_DELALLOC |
4815 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004816 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004817
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004819 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004820 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004822 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004823 goto out_unlock;
4824 }
4825
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004827 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004830 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 memset(kaddr + (block_start - page_offset(page)),
4832 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004833 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304834 memset(kaddr + (block_start - page_offset(page)) + offset,
4835 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004836 flush_dcache_page(page);
4837 kunmap(page);
4838 }
Chris Mason247e7432008-07-17 12:53:51 -04004839 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004840 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004842 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004843
Chris Mason89642222008-07-24 09:41:53 -04004844out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004845 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846 btrfs_delalloc_release_space(inode, block_start,
4847 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004848 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004849 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004850out:
4851 return ret;
4852}
4853
Josef Bacik16e75492013-10-22 12:18:51 -04004854static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4855 u64 offset, u64 len)
4856{
4857 struct btrfs_trans_handle *trans;
4858 int ret;
4859
4860 /*
4861 * Still need to make sure the inode looks like it's been updated so
4862 * that any holes get logged if we fsync.
4863 */
4864 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4865 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4866 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4867 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4868 return 0;
4869 }
4870
4871 /*
4872 * 1 - for the one we're dropping
4873 * 1 - for the one we're adding
4874 * 1 - for updating the inode.
4875 */
4876 trans = btrfs_start_transaction(root, 3);
4877 if (IS_ERR(trans))
4878 return PTR_ERR(trans);
4879
4880 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004882 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004883 btrfs_end_transaction(trans, root);
4884 return ret;
4885 }
4886
4887 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4888 0, 0, len, 0, len, 0, 0, 0);
4889 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004890 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004891 else
4892 btrfs_update_inode(trans, root, inode);
4893 btrfs_end_transaction(trans, root);
4894 return ret;
4895}
4896
Josef Bacik695a0d02011-03-04 15:46:53 -05004897/*
4898 * This function puts in dummy file extents for the area we're creating a hole
4899 * for. So if we are truncating this file to a larger size we need to insert
4900 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4901 * the range between oldsize and size
4902 */
Josef Bacika41ad392011-01-31 15:30:16 -05004903int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004904{
Yan Zheng9036c102008-10-30 14:19:41 -04004905 struct btrfs_root *root = BTRFS_I(inode)->root;
4906 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004907 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004908 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004910 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4911 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004912 u64 last_byte;
4913 u64 cur_offset;
4914 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004915 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004916
Josef Bacika71754f2013-06-17 17:14:39 -04004917 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304918 * If our size started in the middle of a block we need to zero out the
4919 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004920 * expose stale data.
4921 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304922 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004923 if (err)
4924 return err;
4925
Yan Zheng9036c102008-10-30 14:19:41 -04004926 if (size <= hole_start)
4927 return 0;
4928
Yan Zheng9036c102008-10-30 14:19:41 -04004929 while (1) {
4930 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004931
David Sterbaff13db42015-12-03 14:30:40 +01004932 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004933 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004934 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4935 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004936 if (!ordered)
4937 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004938 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4939 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004940 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004941 btrfs_put_ordered_extent(ordered);
4942 }
4943
Yan Zheng9036c102008-10-30 14:19:41 -04004944 cur_offset = hole_start;
4945 while (1) {
4946 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4947 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004948 if (IS_ERR(em)) {
4949 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004950 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004951 break;
4952 }
Yan Zheng9036c102008-10-30 14:19:41 -04004953 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004954 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004955 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004956 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004957 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004958
Josef Bacik16e75492013-10-22 12:18:51 -04004959 err = maybe_insert_hole(root, inode, cur_offset,
4960 hole_size);
4961 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004962 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004963 btrfs_drop_extent_cache(inode, cur_offset,
4964 cur_offset + hole_size - 1, 0);
4965 hole_em = alloc_extent_map();
4966 if (!hole_em) {
4967 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4968 &BTRFS_I(inode)->runtime_flags);
4969 goto next;
4970 }
4971 hole_em->start = cur_offset;
4972 hole_em->len = hole_size;
4973 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004974
Josef Bacik5dc562c2012-08-17 13:14:17 -04004975 hole_em->block_start = EXTENT_MAP_HOLE;
4976 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004977 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004978 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004979 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4980 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004981 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004982
4983 while (1) {
4984 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004985 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 write_unlock(&em_tree->lock);
4987 if (err != -EEXIST)
4988 break;
4989 btrfs_drop_extent_cache(inode, cur_offset,
4990 cur_offset +
4991 hole_size - 1, 0);
4992 }
4993 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004994 }
Josef Bacik16e75492013-10-22 12:18:51 -04004995next:
Yan Zheng9036c102008-10-30 14:19:41 -04004996 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004997 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004998 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004999 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005000 break;
5001 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005002 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005003 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5004 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005005 return err;
5006}
5007
Eric Sandeen3972f262013-01-12 02:57:22 +00005008static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005009{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005010 struct btrfs_root *root = BTRFS_I(inode)->root;
5011 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005012 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005013 loff_t newsize = attr->ia_size;
5014 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005015 int ret;
5016
Eric Sandeen3972f262013-01-12 02:57:22 +00005017 /*
5018 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5019 * special case where we need to update the times despite not having
5020 * these flags set. For all other operations the VFS set these flags
5021 * explicitly if it wants a timestamp update.
5022 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005023 if (newsize != oldsize) {
5024 inode_inc_iversion(inode);
5025 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5026 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005027 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005028 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005029
Josef Bacika41ad392011-01-31 15:30:16 -05005030 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005031 /*
5032 * Don't do an expanding truncate while snapshoting is ongoing.
5033 * This is to ensure the snapshot captures a fully consistent
5034 * state of this file - if the snapshot captures this expanding
5035 * truncation, it must capture all writes that happened before
5036 * this truncation.
5037 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005038 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005039 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005040 if (ret) {
5041 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005042 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005043 }
Yan, Zheng80825102009-11-12 09:35:36 +00005044
Miao Xief4a2f4c2011-12-14 20:12:01 -05005045 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005046 if (IS_ERR(trans)) {
5047 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005048 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005049 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005050
5051 i_size_write(inode, newsize);
5052 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305053 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005054 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005055 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005056 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05005057 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005058
Josef Bacika41ad392011-01-31 15:30:16 -05005059 /*
5060 * We're truncating a file that used to have good data down to
5061 * zero. Make sure it gets into the ordered flush list so that
5062 * any new writes get down to disk quickly.
5063 */
5064 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005065 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5066 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005067
Josef Bacikf3fe8202013-01-07 17:03:21 -05005068 /*
5069 * 1 for the orphan item we're going to add
5070 * 1 for the orphan item deletion.
5071 */
5072 trans = btrfs_start_transaction(root, 2);
5073 if (IS_ERR(trans))
5074 return PTR_ERR(trans);
5075
5076 /*
5077 * We need to do this in case we fail at _any_ point during the
5078 * actual truncate. Once we do the truncate_setsize we could
5079 * invalidate pages which forces any outstanding ordered io to
5080 * be instantly completed which will give us extents that need
5081 * to be truncated. If we fail to get an orphan inode down we
5082 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005083 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005084 * will be consistent.
5085 */
5086 ret = btrfs_orphan_add(trans, inode);
5087 btrfs_end_transaction(trans, root);
5088 if (ret)
5089 return ret;
5090
Josef Bacika41ad392011-01-31 15:30:16 -05005091 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5092 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005093
5094 /* Disable nonlocked read DIO to avoid the end less truncate */
5095 btrfs_inode_block_unlocked_dio(inode);
5096 inode_dio_wait(inode);
5097 btrfs_inode_resume_unlocked_dio(inode);
5098
Josef Bacika41ad392011-01-31 15:30:16 -05005099 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005100 if (ret && inode->i_nlink) {
5101 int err;
5102
5103 /*
5104 * failed to truncate, disk_i_size is only adjusted down
5105 * as we remove extents, so it should represent the true
5106 * size of the inode, so reset the in memory size and
5107 * delete our orphan entry.
5108 */
5109 trans = btrfs_join_transaction(root);
5110 if (IS_ERR(trans)) {
5111 btrfs_orphan_del(NULL, inode);
5112 return ret;
5113 }
5114 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5115 err = btrfs_orphan_del(trans, inode);
5116 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005117 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005118 btrfs_end_transaction(trans, root);
5119 }
Yan, Zheng80825102009-11-12 09:35:36 +00005120 }
5121
Josef Bacika41ad392011-01-31 15:30:16 -05005122 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005123}
5124
Chris Mason39279cc2007-06-12 06:35:45 -04005125static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5126{
David Howells2b0143b2015-03-17 22:25:59 +00005127 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005128 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 int err;
5130
Li Zefanb83cc962010-12-20 16:04:08 +08005131 if (btrfs_root_readonly(root))
5132 return -EROFS;
5133
Jan Kara31051c82016-05-26 16:55:18 +02005134 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005135 if (err)
5136 return err;
5137
Chris Mason5a3f23d2009-03-31 13:27:11 -04005138 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005139 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005140 if (err)
5141 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005142 }
Yan Zheng9036c102008-10-30 14:19:41 -04005143
Christoph Hellwig10257742010-06-04 11:30:02 +02005144 if (attr->ia_valid) {
5145 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005146 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005147 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005148
Josef Bacik22c44fe2011-11-30 10:45:38 -05005149 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005150 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005151 }
5152
Chris Mason39279cc2007-06-12 06:35:45 -04005153 return err;
5154}
Chris Mason61295eb2008-01-14 16:24:38 -05005155
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005156/*
5157 * While truncating the inode pages during eviction, we get the VFS calling
5158 * btrfs_invalidatepage() against each page of the inode. This is slow because
5159 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5160 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5161 * extent_state structures over and over, wasting lots of time.
5162 *
5163 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5164 * those expensive operations on a per page basis and do only the ordered io
5165 * finishing, while we release here the extent_map and extent_state structures,
5166 * without the excessive merging and splitting.
5167 */
5168static void evict_inode_truncate_pages(struct inode *inode)
5169{
5170 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5171 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5172 struct rb_node *node;
5173
5174 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005175 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005176
5177 write_lock(&map_tree->lock);
5178 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5179 struct extent_map *em;
5180
5181 node = rb_first(&map_tree->map);
5182 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005183 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5184 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005185 remove_extent_mapping(map_tree, em);
5186 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005187 if (need_resched()) {
5188 write_unlock(&map_tree->lock);
5189 cond_resched();
5190 write_lock(&map_tree->lock);
5191 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192 }
5193 write_unlock(&map_tree->lock);
5194
Filipe Manana6ca07092015-05-26 00:55:42 +01005195 /*
5196 * Keep looping until we have no more ranges in the io tree.
5197 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005198 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5199 * still in progress (unlocked the pages in the bio but did not yet
5200 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005201 * ranges can still be locked and eviction started because before
5202 * submitting those bios, which are executed by a separate task (work
5203 * queue kthread), inode references (inode->i_count) were not taken
5204 * (which would be dropped in the end io callback of each bio).
5205 * Therefore here we effectively end up waiting for those bios and
5206 * anyone else holding locked ranges without having bumped the inode's
5207 * reference count - if we don't do it, when they access the inode's
5208 * io_tree to unlock a range it may be too late, leading to an
5209 * use-after-free issue.
5210 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005211 spin_lock(&io_tree->lock);
5212 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5213 struct extent_state *state;
5214 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005215 u64 start;
5216 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217
5218 node = rb_first(&io_tree->state);
5219 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 start = state->start;
5221 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 spin_unlock(&io_tree->lock);
5223
David Sterbaff13db42015-12-03 14:30:40 +01005224 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005225
5226 /*
5227 * If still has DELALLOC flag, the extent didn't reach disk,
5228 * and its reserved space won't be freed by delayed_ref.
5229 * So we need to free its reserved space here.
5230 * (Refer to comment in btrfs_invalidatepage, case 2)
5231 *
5232 * Note, end is the bytenr of last byte, so we need + 1 here.
5233 */
5234 if (state->state & EXTENT_DELALLOC)
5235 btrfs_qgroup_free_data(inode, start, end - start + 1);
5236
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005238 EXTENT_LOCKED | EXTENT_DIRTY |
5239 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5240 EXTENT_DEFRAG, 1, 1,
5241 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242
Filipe Manana7064dd52014-08-08 02:47:05 +01005243 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005244 spin_lock(&io_tree->lock);
5245 }
5246 spin_unlock(&io_tree->lock);
5247}
5248
Al Virobd555972010-06-07 11:35:40 -04005249void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005250{
5251 struct btrfs_trans_handle *trans;
5252 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005253 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005254 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005255 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005256 int ret;
5257
liubo1abe9b82011-03-24 11:18:59 +00005258 trace_btrfs_inode_evict(inode);
5259
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005260 if (!root) {
Liu Bob48edd62018-01-25 11:02:53 -07005261 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005262 return;
5263 }
5264
5265 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5266
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005267 evict_inode_truncate_pages(inode);
5268
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005269 if (inode->i_nlink &&
5270 ((btrfs_root_refs(&root->root_item) != 0 &&
5271 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5272 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005273 goto no_delete;
5274
Chris Mason39279cc2007-06-12 06:35:45 -04005275 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005276 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005277 goto no_delete;
5278 }
Al Virobd555972010-06-07 11:35:40 -04005279 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005280 if (!special_file(inode->i_mode))
5281 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005282
Miao Xief6124962014-09-12 18:44:04 +08005283 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5284
Josef Bacikafcdd122016-09-02 15:40:02 -04005285 if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005286 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005287 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005288 goto no_delete;
5289 }
5290
Yan, Zheng76dda932009-09-21 16:00:26 -04005291 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005292 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5293 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005294 goto no_delete;
5295 }
5296
Miao Xie0e8c36a2012-12-19 06:59:51 +00005297 ret = btrfs_commit_inode_delayed_inode(inode);
5298 if (ret) {
5299 btrfs_orphan_del(NULL, inode);
5300 goto no_delete;
5301 }
5302
Miao Xie66d8f3d2012-09-06 04:02:28 -06005303 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005304 if (!rsv) {
5305 btrfs_orphan_del(NULL, inode);
5306 goto no_delete;
5307 }
Josef Bacik4a338542011-08-29 11:01:31 -04005308 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005309 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005310 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005311
Chris Masondbe674a2008-07-17 12:54:05 -04005312 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005313
Josef Bacik4289a662011-08-05 13:22:24 -04005314 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005315 * This is a bit simpler than btrfs_truncate since we've already
5316 * reserved our space for our orphan item in the unlink, so we just
5317 * need to reserve some slack space in case we add bytes and update
5318 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005319 */
Yan, Zheng80825102009-11-12 09:35:36 +00005320 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005321 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5322 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005323
Josef Bacik726c35f2011-09-26 15:46:06 -04005324 /*
5325 * Try and steal from the global reserve since we will
5326 * likely not use this space anyway, we want to try as
5327 * hard as possible to get this to work.
5328 */
5329 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005330 steal_from_global++;
5331 else
5332 steal_from_global = 0;
5333 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005334
Josef Bacik3bce8762015-02-24 12:35:51 -08005335 /*
5336 * steal_from_global == 0: we reserved stuff, hooray!
5337 * steal_from_global == 1: we didn't reserve stuff, boo!
5338 * steal_from_global == 2: we've committed, still not a lot of
5339 * room but maybe we'll have room in the global reserve this
5340 * time.
5341 * steal_from_global == 3: abandon all hope!
5342 */
5343 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005344 btrfs_warn(root->fs_info,
5345 "Could not get space for a delete, will truncate on mount %d",
5346 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005347 btrfs_orphan_del(NULL, inode);
5348 btrfs_free_block_rsv(root, rsv);
5349 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005350 }
5351
Miao Xie0e8c36a2012-12-19 06:59:51 +00005352 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005353 if (IS_ERR(trans)) {
5354 btrfs_orphan_del(NULL, inode);
5355 btrfs_free_block_rsv(root, rsv);
5356 goto no_delete;
5357 }
5358
Josef Bacik3bce8762015-02-24 12:35:51 -08005359 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005360 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005361 * sure there is room to do it, if not we need to commit and try
5362 * again.
5363 */
5364 if (steal_from_global) {
5365 if (!btrfs_check_space_for_delayed_refs(trans, root))
5366 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005367 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005368 else
5369 ret = -ENOSPC;
5370 }
5371
5372 /*
5373 * Couldn't steal from the global reserve, we have too much
5374 * pending stuff built up, commit the transaction and try it
5375 * again.
5376 */
5377 if (ret) {
5378 ret = btrfs_commit_transaction(trans, root);
5379 if (ret) {
5380 btrfs_orphan_del(NULL, inode);
5381 btrfs_free_block_rsv(root, rsv);
5382 goto no_delete;
5383 }
5384 continue;
5385 } else {
5386 steal_from_global = 0;
5387 }
5388
Josef Bacik4289a662011-08-05 13:22:24 -04005389 trans->block_rsv = rsv;
5390
Yan, Zhengd68fc572010-05-16 10:49:58 -04005391 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005392 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005393 break;
5394
Miao Xie8407aa42012-09-07 01:43:32 -06005395 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005396 btrfs_end_transaction(trans, root);
5397 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005398 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005399 }
5400
Josef Bacik4289a662011-08-05 13:22:24 -04005401 btrfs_free_block_rsv(root, rsv);
5402
Josef Bacik4ef31a42013-08-13 14:10:08 -04005403 /*
5404 * Errors here aren't a big deal, it just means we leave orphan items
5405 * in the tree. They will be cleaned up on the next mount.
5406 */
Yan, Zheng80825102009-11-12 09:35:36 +00005407 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005408 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005409 btrfs_orphan_del(trans, inode);
5410 } else {
5411 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005412 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005413
Josef Bacik4289a662011-08-05 13:22:24 -04005414 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005415 if (!(root == root->fs_info->tree_root ||
5416 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005417 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005418
Chris Mason54aa1f42007-06-22 14:16:25 -04005419 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005420 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005421no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005422 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005423 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005424}
5425
5426/*
5427 * this returns the key found in the dir entry in the location pointer.
5428 * If no dir entries were found, location->objectid is 0.
5429 */
5430static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5431 struct btrfs_key *location)
5432{
5433 const char *name = dentry->d_name.name;
5434 int namelen = dentry->d_name.len;
5435 struct btrfs_dir_item *di;
5436 struct btrfs_path *path;
5437 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005438 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005439
5440 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005441 if (!path)
5442 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005443
Li Zefan33345d012011-04-20 10:31:50 +08005444 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005445 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005446 if (IS_ERR(di))
5447 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005448
David Sterbac7040052011-04-19 18:00:01 +02005449 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005450 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005451
Chris Mason5f39d392007-10-15 16:14:19 -04005452 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005453out:
Chris Mason39279cc2007-06-12 06:35:45 -04005454 btrfs_free_path(path);
5455 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005456out_err:
5457 location->objectid = 0;
5458 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005459}
5460
5461/*
5462 * when we hit a tree root in a directory, the btrfs part of the inode
5463 * needs to be changed to reflect the root directory of the tree root. This
5464 * is kind of like crossing a mount point.
5465 */
5466static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005467 struct inode *dir,
5468 struct dentry *dentry,
5469 struct btrfs_key *location,
5470 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005471{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 struct btrfs_path *path;
5473 struct btrfs_root *new_root;
5474 struct btrfs_root_ref *ref;
5475 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005476 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005477 int ret;
5478 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005479
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 path = btrfs_alloc_path();
5481 if (!path) {
5482 err = -ENOMEM;
5483 goto out;
5484 }
Chris Mason39279cc2007-06-12 06:35:45 -04005485
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005487 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5488 key.type = BTRFS_ROOT_REF_KEY;
5489 key.offset = location->objectid;
5490
5491 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5492 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 if (ret) {
5494 if (ret < 0)
5495 err = ret;
5496 goto out;
5497 }
Chris Mason39279cc2007-06-12 06:35:45 -04005498
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 leaf = path->nodes[0];
5500 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005501 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5503 goto out;
5504
5505 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5506 (unsigned long)(ref + 1),
5507 dentry->d_name.len);
5508 if (ret)
5509 goto out;
5510
David Sterbab3b4aa72011-04-21 01:20:15 +02005511 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512
5513 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5514 if (IS_ERR(new_root)) {
5515 err = PTR_ERR(new_root);
5516 goto out;
5517 }
5518
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 *sub_root = new_root;
5520 location->objectid = btrfs_root_dirid(&new_root->root_item);
5521 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005522 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 err = 0;
5524out:
5525 btrfs_free_path(path);
5526 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005527}
5528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529static void inode_tree_add(struct inode *inode)
5530{
5531 struct btrfs_root *root = BTRFS_I(inode)->root;
5532 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005533 struct rb_node **p;
5534 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005535 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005536 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537
Al Viro1d3382cb2010-10-23 15:19:20 -04005538 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005540 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005542 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 while (*p) {
5544 parent = *p;
5545 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5546
Li Zefan33345d012011-04-20 10:31:50 +08005547 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005548 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005549 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 else {
5552 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005553 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005554 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005555 RB_CLEAR_NODE(parent);
5556 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005557 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 }
5559 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005560 rb_link_node(new, parent, p);
5561 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005562 spin_unlock(&root->inode_lock);
5563}
5564
5565static void inode_tree_del(struct inode *inode)
5566{
5567 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005570 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005571 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005574 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005576 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005577
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005578 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 synchronize_srcu(&root->fs_info->subvol_srcu);
5580 spin_lock(&root->inode_lock);
5581 empty = RB_EMPTY_ROOT(&root->inode_tree);
5582 spin_unlock(&root->inode_lock);
5583 if (empty)
5584 btrfs_add_dead_root(root);
5585 }
5586}
5587
Jeff Mahoney143bede2012-03-01 14:56:26 +01005588void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005589{
5590 struct rb_node *node;
5591 struct rb_node *prev;
5592 struct btrfs_inode *entry;
5593 struct inode *inode;
5594 u64 objectid = 0;
5595
Liu Bo7813b3d2014-02-10 17:37:25 +08005596 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5597 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005598
5599 spin_lock(&root->inode_lock);
5600again:
5601 node = root->inode_tree.rb_node;
5602 prev = NULL;
5603 while (node) {
5604 prev = node;
5605 entry = rb_entry(node, struct btrfs_inode, rb_node);
5606
Li Zefan33345d012011-04-20 10:31:50 +08005607 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005608 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005609 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005610 node = node->rb_right;
5611 else
5612 break;
5613 }
5614 if (!node) {
5615 while (prev) {
5616 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005617 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 node = prev;
5619 break;
5620 }
5621 prev = rb_next(prev);
5622 }
5623 }
5624 while (node) {
5625 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005626 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005627 inode = igrab(&entry->vfs_inode);
5628 if (inode) {
5629 spin_unlock(&root->inode_lock);
5630 if (atomic_read(&inode->i_count) > 1)
5631 d_prune_aliases(inode);
5632 /*
Al Viro45321ac2010-06-07 13:43:19 -04005633 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005634 * the inode cache when its usage count
5635 * hits zero.
5636 */
5637 iput(inode);
5638 cond_resched();
5639 spin_lock(&root->inode_lock);
5640 goto again;
5641 }
5642
5643 if (cond_resched_lock(&root->inode_lock))
5644 goto again;
5645
5646 node = rb_next(node);
5647 }
5648 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005649}
5650
Chris Masone02119d2008-09-05 16:13:11 -04005651static int btrfs_init_locked_inode(struct inode *inode, void *p)
5652{
5653 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005654 inode->i_ino = args->location->objectid;
5655 memcpy(&BTRFS_I(inode)->location, args->location,
5656 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005657 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005658 return 0;
5659}
5660
5661static int btrfs_find_actor(struct inode *inode, void *opaque)
5662{
5663 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005664 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005665 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005666}
5667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005669 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005670 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005671{
5672 struct inode *inode;
5673 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005674 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005675
Chris Mason90d3e592014-01-09 17:28:00 -08005676 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005677 args.root = root;
5678
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005679 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005680 btrfs_init_locked_inode,
5681 (void *)&args);
5682 return inode;
5683}
5684
Balaji Rao1a54ef82008-07-21 02:01:04 +05305685/* Get an inode object given its location and corresponding root.
5686 * Returns in *is_new if the inode was read from disk
5687 */
5688struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005689 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305690{
5691 struct inode *inode;
5692
Chris Mason90d3e592014-01-09 17:28:00 -08005693 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305694 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005695 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305696
5697 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005698 int ret;
5699
5700 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005701 if (!is_bad_inode(inode)) {
5702 inode_tree_add(inode);
5703 unlock_new_inode(inode);
5704 if (new)
5705 *new = 1;
5706 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005707 unlock_new_inode(inode);
5708 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005709 ASSERT(ret < 0);
5710 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005711 }
5712 }
5713
Balaji Rao1a54ef82008-07-21 02:01:04 +05305714 return inode;
5715}
5716
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005717static struct inode *new_simple_dir(struct super_block *s,
5718 struct btrfs_key *key,
5719 struct btrfs_root *root)
5720{
5721 struct inode *inode = new_inode(s);
5722
5723 if (!inode)
5724 return ERR_PTR(-ENOMEM);
5725
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726 BTRFS_I(inode)->root = root;
5727 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005728 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005729
5730 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005731 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandovalad80fad2017-01-25 17:06:39 -08005732 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005733 inode->i_fop = &simple_dir_operations;
5734 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005735 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305736 inode->i_atime = inode->i_mtime;
5737 inode->i_ctime = inode->i_mtime;
5738 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005739
5740 return inode;
5741}
5742
Chris Mason3de45862008-11-17 21:02:50 -05005743struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005744{
Chris Masond3977122009-01-05 21:25:51 -05005745 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005747 struct btrfs_root *sub_root = root;
5748 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005749 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005750 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005751
5752 if (dentry->d_name.len > BTRFS_NAME_LEN)
5753 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005754
Jeff Layton39e3c952012-11-28 11:30:53 -05005755 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005756 if (ret < 0)
5757 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005758
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005759 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005760 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005761
5762 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005763 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005765 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005766
5767 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5768
Yan, Zheng76dda932009-09-21 16:00:26 -04005769 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005770 ret = fixup_tree_root_location(root, dir, dentry,
5771 &location, &sub_root);
5772 if (ret < 0) {
5773 if (ret != -ENOENT)
5774 inode = ERR_PTR(ret);
5775 else
5776 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5777 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005778 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005779 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005780 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5781
Julia Lawall34d19ba2011-01-24 19:55:19 +00005782 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005783 down_read(&root->fs_info->cleanup_work_sem);
5784 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005785 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005786 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005787 if (ret) {
5788 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005789 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005790 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005791 }
5792
Chris Mason3de45862008-11-17 21:02:50 -05005793 return inode;
5794}
5795
Nick Pigginfe15ce42011-01-07 17:49:23 +11005796static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005797{
5798 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005799 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005800
Li Zefan848cce02012-02-21 17:04:28 +08005801 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005802 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005803
Li Zefan848cce02012-02-21 17:04:28 +08005804 if (inode) {
5805 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005806 if (btrfs_root_refs(&root->root_item) == 0)
5807 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005808
5809 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5810 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005811 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005812 return 0;
5813}
5814
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005815static void btrfs_dentry_release(struct dentry *dentry)
5816{
Daeseok Youn944a4512014-04-14 15:37:02 +09005817 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005818}
5819
Chris Mason3de45862008-11-17 21:02:50 -05005820static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005821 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005822{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005823 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005824
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005825 inode = btrfs_lookup_dentry(dir, dentry);
5826 if (IS_ERR(inode)) {
5827 if (PTR_ERR(inode) == -ENOENT)
5828 inode = NULL;
5829 else
5830 return ERR_CAST(inode);
5831 }
5832
Al Viro41d28bc2014-10-12 22:24:21 -04005833 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005834}
5835
Miao Xie16cdcec2011-04-22 18:12:22 +08005836unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005837 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5838};
5839
Al Viro9cdda8d2013-05-22 16:48:09 -04005840static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005841{
Al Viro9cdda8d2013-05-22 16:48:09 -04005842 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005843 struct btrfs_root *root = BTRFS_I(inode)->root;
5844 struct btrfs_item *item;
5845 struct btrfs_dir_item *di;
5846 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005847 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005848 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005849 struct list_head ins_list;
5850 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005851 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005852 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005853 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005854 unsigned char d_type;
5855 int over = 0;
5856 u32 di_cur;
5857 u32 di_total;
5858 u32 di_len;
5859 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005860 char tmp_name[32];
5861 char *name_ptr;
5862 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005863 int is_curr = 0; /* ctx->pos points to the current index? */
David Sterbabc4ef752015-11-13 13:44:28 +01005864 bool emitted;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005865 bool put = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005866
5867 /* FIXME, use a real flag for deciding about the key type */
5868 if (root->fs_info->tree_root == root)
5869 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005870
Al Viro9cdda8d2013-05-22 16:48:09 -04005871 if (!dir_emit_dots(file, ctx))
5872 return 0;
5873
David Woodhouse49593bf2008-08-17 17:08:36 +01005874 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005875 if (!path)
5876 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005877
David Sterbae4058b52015-11-27 16:31:35 +01005878 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005879
Miao Xie16cdcec2011-04-22 18:12:22 +08005880 if (key_type == BTRFS_DIR_INDEX_KEY) {
5881 INIT_LIST_HEAD(&ins_list);
5882 INIT_LIST_HEAD(&del_list);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005883 put = btrfs_readdir_get_delayed_items(inode, &ins_list,
5884 &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005885 }
5886
David Sterba962a2982014-06-04 18:41:45 +02005887 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005888 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005889 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005890
Chris Mason39279cc2007-06-12 06:35:45 -04005891 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5892 if (ret < 0)
5893 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005894
David Sterbabc4ef752015-11-13 13:44:28 +01005895 emitted = false;
David Woodhouse49593bf2008-08-17 17:08:36 +01005896 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005897 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005898 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005899 if (slot >= btrfs_header_nritems(leaf)) {
5900 ret = btrfs_next_leaf(root, path);
5901 if (ret < 0)
5902 goto err;
5903 else if (ret > 0)
5904 break;
5905 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005906 }
Chris Mason3de45862008-11-17 21:02:50 -05005907
Ross Kirkdd3cc162013-09-16 15:58:09 +01005908 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005909 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5910
5911 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005912 break;
David Sterba962a2982014-06-04 18:41:45 +02005913 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005914 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005915 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005916 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005917 if (key_type == BTRFS_DIR_INDEX_KEY &&
5918 btrfs_should_delete_dir_index(&del_list,
5919 found_key.offset))
5920 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005921
Al Viro9cdda8d2013-05-22 16:48:09 -04005922 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005923 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005924
Chris Mason39279cc2007-06-12 06:35:45 -04005925 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5926 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005927 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005928
5929 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005930 struct btrfs_key location;
5931
Josef Bacik22a94d42011-03-16 16:47:17 -04005932 if (verify_dir_item(root, leaf, di))
5933 break;
5934
Chris Mason5f39d392007-10-15 16:14:19 -04005935 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005936 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005937 name_ptr = tmp_name;
5938 } else {
David Sterba49e350a2016-02-11 14:25:38 +01005939 name_ptr = kmalloc(name_len, GFP_KERNEL);
David Woodhouse49593bf2008-08-17 17:08:36 +01005940 if (!name_ptr) {
5941 ret = -ENOMEM;
5942 goto err;
5943 }
Chris Mason5f39d392007-10-15 16:14:19 -04005944 }
5945 read_extent_buffer(leaf, name_ptr,
5946 (unsigned long)(di + 1), name_len);
5947
5948 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5949 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005950
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005951
Chris Mason3de45862008-11-17 21:02:50 -05005952 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005953 * skip it.
5954 *
5955 * In contrast to old kernels, we insert the snapshot's
5956 * dir item and dir index after it has been created, so
5957 * we won't find a reference to our own snapshot. We
5958 * still keep the following code for backward
5959 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005960 */
5961 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5962 location.objectid == root->root_key.objectid) {
5963 over = 0;
5964 goto skip;
5965 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005966 over = !dir_emit(ctx, name_ptr, name_len,
5967 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005968
Chris Mason3de45862008-11-17 21:02:50 -05005969skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005970 if (name_ptr != tmp_name)
5971 kfree(name_ptr);
5972
Chris Mason39279cc2007-06-12 06:35:45 -04005973 if (over)
5974 goto nopos;
David Sterbabc4ef752015-11-13 13:44:28 +01005975 emitted = true;
Josef Bacik5103e942007-11-16 11:45:54 -05005976 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005977 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005978 di_cur += di_len;
5979 di = (struct btrfs_dir_item *)((char *)di + di_len);
5980 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005981next:
5982 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005983 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005984
Miao Xie16cdcec2011-04-22 18:12:22 +08005985 if (key_type == BTRFS_DIR_INDEX_KEY) {
5986 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005987 ctx->pos++;
David Sterbabc4ef752015-11-13 13:44:28 +01005988 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
Miao Xie16cdcec2011-04-22 18:12:22 +08005989 if (ret)
5990 goto nopos;
5991 }
5992
David Sterbabc4ef752015-11-13 13:44:28 +01005993 /*
5994 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5995 * it was was set to the termination value in previous call. We assume
5996 * that "." and ".." were emitted if we reach this point and set the
5997 * termination value as well for an empty directory.
5998 */
5999 if (ctx->pos > 2 && !emitted)
6000 goto nopos;
6001
David Woodhouse49593bf2008-08-17 17:08:36 +01006002 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07006003 ctx->pos++;
6004
6005 /*
6006 * Stop new entries from being returned after we return the last
6007 * entry.
6008 *
6009 * New directory entries are assigned a strictly increasing
6010 * offset. This means that new entries created during readdir
6011 * are *guaranteed* to be seen in the future by that readdir.
6012 * This has broken buggy programs which operate on names as
6013 * they're returned by readdir. Until we re-use freed offsets
6014 * we have this hack to stop new entries from being returned
6015 * under the assumption that they'll never reach this huge
6016 * offset.
6017 *
6018 * This is being careful not to overflow 32bit loff_t unless the
6019 * last entry requires it because doing so has broken 32bit apps
6020 * in the past.
6021 */
6022 if (key_type == BTRFS_DIR_INDEX_KEY) {
6023 if (ctx->pos >= INT_MAX)
6024 ctx->pos = LLONG_MAX;
6025 else
6026 ctx->pos = INT_MAX;
6027 }
Chris Mason39279cc2007-06-12 06:35:45 -04006028nopos:
6029 ret = 0;
6030err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006031 if (put)
6032 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006033 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006034 return ret;
6035}
6036
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006037int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006038{
6039 struct btrfs_root *root = BTRFS_I(inode)->root;
6040 struct btrfs_trans_handle *trans;
6041 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006042 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006043
Josef Bacik72ac3c02012-05-23 14:13:11 -04006044 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006045 return 0;
6046
Liu Bo83eea1f2012-07-10 05:28:39 -06006047 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08006048 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006049
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006050 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006051 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006052 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006053 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006054 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006055 if (IS_ERR(trans))
6056 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06006057 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006058 }
6059 return ret;
6060}
6061
6062/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006063 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006064 * inode changes. But, it is most likely to find the inode in cache.
6065 * FIXME, needs more benchmarking...there are no reasons other than performance
6066 * to keep or drop this code.
6067 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006068static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006069{
6070 struct btrfs_root *root = BTRFS_I(inode)->root;
6071 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006072 int ret;
6073
Josef Bacik72ac3c02012-05-23 14:13:11 -04006074 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006075 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006076
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006077 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006078 if (IS_ERR(trans))
6079 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006080
6081 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006082 if (ret && ret == -ENOSPC) {
6083 /* whoops, lets try again with the full transaction */
6084 btrfs_end_transaction(trans, root);
6085 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006086 if (IS_ERR(trans))
6087 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006088
Chris Mason94b60442010-05-26 11:02:00 -04006089 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006090 }
Chris Mason39279cc2007-06-12 06:35:45 -04006091 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08006092 if (BTRFS_I(inode)->delayed_node)
6093 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006094
6095 return ret;
6096}
6097
6098/*
6099 * This is a copy of file_update_time. We need this so we can return error on
6100 * ENOSPC for updating the inode in the case of file write and mmap writes.
6101 */
Josef Bacike41f9412012-03-26 09:46:47 -04006102static int btrfs_update_time(struct inode *inode, struct timespec *now,
6103 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006104{
Alexander Block2bc5565282012-06-15 09:49:33 +02006105 struct btrfs_root *root = BTRFS_I(inode)->root;
6106
6107 if (btrfs_root_readonly(root))
6108 return -EROFS;
6109
Josef Bacike41f9412012-03-26 09:46:47 -04006110 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006111 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006112 if (flags & S_CTIME)
6113 inode->i_ctime = *now;
6114 if (flags & S_MTIME)
6115 inode->i_mtime = *now;
6116 if (flags & S_ATIME)
6117 inode->i_atime = *now;
6118 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006119}
6120
Chris Masond352ac62008-09-29 15:18:18 -04006121/*
6122 * find the highest existing sequence number in a directory
6123 * and then set the in-memory index_cnt variable to reflect
6124 * free sequence numbers
6125 */
Josef Bacikaec74772008-07-24 12:12:38 -04006126static int btrfs_set_inode_index_count(struct inode *inode)
6127{
6128 struct btrfs_root *root = BTRFS_I(inode)->root;
6129 struct btrfs_key key, found_key;
6130 struct btrfs_path *path;
6131 struct extent_buffer *leaf;
6132 int ret;
6133
Li Zefan33345d012011-04-20 10:31:50 +08006134 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006135 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006136 key.offset = (u64)-1;
6137
6138 path = btrfs_alloc_path();
6139 if (!path)
6140 return -ENOMEM;
6141
6142 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6143 if (ret < 0)
6144 goto out;
6145 /* FIXME: we should be able to handle this */
6146 if (ret == 0)
6147 goto out;
6148 ret = 0;
6149
6150 /*
6151 * MAGIC NUMBER EXPLANATION:
6152 * since we search a directory based on f_pos we have to start at 2
6153 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6154 * else has to start at 2
6155 */
6156 if (path->slots[0] == 0) {
6157 BTRFS_I(inode)->index_cnt = 2;
6158 goto out;
6159 }
6160
6161 path->slots[0]--;
6162
6163 leaf = path->nodes[0];
6164 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6165
Li Zefan33345d012011-04-20 10:31:50 +08006166 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006167 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006168 BTRFS_I(inode)->index_cnt = 2;
6169 goto out;
6170 }
6171
6172 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6173out:
6174 btrfs_free_path(path);
6175 return ret;
6176}
6177
Chris Masond352ac62008-09-29 15:18:18 -04006178/*
6179 * helper to find a free sequence number in a given directory. This current
6180 * code is very simple, later versions will do smarter things in the btree
6181 */
Chris Mason3de45862008-11-17 21:02:50 -05006182int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006183{
6184 int ret = 0;
6185
6186 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006187 ret = btrfs_inode_delayed_dir_index_count(dir);
6188 if (ret) {
6189 ret = btrfs_set_inode_index_count(dir);
6190 if (ret)
6191 return ret;
6192 }
Josef Bacikaec74772008-07-24 12:12:38 -04006193 }
6194
Chris Mason00e4e6b2008-08-05 11:18:09 -04006195 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006196 BTRFS_I(dir)->index_cnt++;
6197
6198 return ret;
6199}
6200
Chris Masonb0d5d102014-09-08 13:08:51 -07006201static int btrfs_insert_inode_locked(struct inode *inode)
6202{
6203 struct btrfs_iget_args args;
6204 args.location = &BTRFS_I(inode)->location;
6205 args.root = BTRFS_I(inode)->root;
6206
6207 return insert_inode_locked4(inode,
6208 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6209 btrfs_find_actor, &args);
6210}
6211
Chris Mason39279cc2007-06-12 06:35:45 -04006212static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6213 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006214 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006215 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006216 u64 ref_objectid, u64 objectid,
6217 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006218{
6219 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006220 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006221 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006222 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006223 struct btrfs_inode_ref *ref;
6224 struct btrfs_key key[2];
6225 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006226 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006227 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006228 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006229
Chris Mason5f39d392007-10-15 16:14:19 -04006230 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006231 if (!path)
6232 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006233
Chris Mason39279cc2007-06-12 06:35:45 -04006234 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006235 if (!inode) {
6236 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006237 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006238 }
Chris Mason39279cc2007-06-12 06:35:45 -04006239
Li Zefan581bb052011-04-20 10:06:11 +08006240 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006241 * O_TMPFILE, set link count to 0, so that after this point,
6242 * we fill in an inode item with the correct link count.
6243 */
6244 if (!name)
6245 set_nlink(inode, 0);
6246
6247 /*
Li Zefan581bb052011-04-20 10:06:11 +08006248 * we have to initialize this early, so we can reclaim the inode
6249 * number if we fail afterwards in this function.
6250 */
6251 inode->i_ino = objectid;
6252
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006253 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006254 trace_btrfs_inode_request(dir);
6255
Chris Mason3de45862008-11-17 21:02:50 -05006256 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006257 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006258 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006259 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006260 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006261 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006262 } else if (dir) {
6263 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006264 }
6265 /*
6266 * index_cnt is ignored for everything but a dir,
6267 * btrfs_get_inode_index_count has an explanation for the magic
6268 * number
6269 */
6270 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006271 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006272 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006273 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006274 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006275
Josef Bacik5dc562c2012-08-17 13:14:17 -04006276 /*
6277 * We could have gotten an inode number from somebody who was fsynced
6278 * and then removed in this same transaction, so let's just set full
6279 * sync since it will be a full sync anyway and this will blow away the
6280 * old info in the log.
6281 */
6282 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6283
Chris Mason9c583092008-01-29 15:15:18 -05006284 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006285 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006286 key[0].offset = 0;
6287
Chris Mason9c583092008-01-29 15:15:18 -05006288 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006289
6290 if (name) {
6291 /*
6292 * Start new inodes with an inode_ref. This is slightly more
6293 * efficient for small numbers of hard links since they will
6294 * be packed into one item. Extended refs will kick in if we
6295 * add more hard links than can fit in the ref item.
6296 */
6297 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006298 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 key[1].offset = ref_objectid;
6300
6301 sizes[1] = name_len + sizeof(*ref);
6302 }
Chris Mason9c583092008-01-29 15:15:18 -05006303
Chris Masonb0d5d102014-09-08 13:08:51 -07006304 location = &BTRFS_I(inode)->location;
6305 location->objectid = objectid;
6306 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006307 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006308
6309 ret = btrfs_insert_inode_locked(inode);
6310 if (ret < 0)
6311 goto fail;
6312
Chris Masonb9473432009-03-13 11:00:37 -04006313 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006314 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006315 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006316 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006317
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006318 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006319 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306320
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006321 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306322 inode->i_atime = inode->i_mtime;
6323 inode->i_ctime = inode->i_mtime;
6324 BTRFS_I(inode)->i_otime = inode->i_mtime;
6325
Chris Mason5f39d392007-10-15 16:14:19 -04006326 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6327 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006328 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6329 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006330 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006331
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006332 if (name) {
6333 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6334 struct btrfs_inode_ref);
6335 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6336 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6337 ptr = (unsigned long)(ref + 1);
6338 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6339 }
Chris Mason9c583092008-01-29 15:15:18 -05006340
Chris Mason5f39d392007-10-15 16:14:19 -04006341 btrfs_mark_buffer_dirty(path->nodes[0]);
6342 btrfs_free_path(path);
6343
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006344 btrfs_inherit_iflags(inode, dir);
6345
Al Viro569254b2011-07-24 17:08:40 -04006346 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006347 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006348 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006349 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006350 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6351 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006352 }
6353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006354 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006355
6356 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006357 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006358
Alexander Block8ea05e32012-07-25 17:35:53 +02006359 btrfs_update_root_times(trans, root);
6360
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006361 ret = btrfs_inode_inherit_props(trans, inode, dir);
6362 if (ret)
6363 btrfs_err(root->fs_info,
6364 "error inheriting props for ino %llu (root %llu): %d",
6365 btrfs_ino(inode), root->root_key.objectid, ret);
6366
Chris Mason39279cc2007-06-12 06:35:45 -04006367 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006368
6369fail_unlock:
6370 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006371fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006372 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006373 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006374 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006375 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006376 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006377}
6378
6379static inline u8 btrfs_inode_type(struct inode *inode)
6380{
6381 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6382}
6383
Chris Masond352ac62008-09-29 15:18:18 -04006384/*
6385 * utility function to add 'inode' into 'parent_inode' with
6386 * a give name and a given sequence number.
6387 * if 'add_backref' is true, also insert a backref from the
6388 * inode to the parent directory.
6389 */
Chris Masone02119d2008-09-05 16:13:11 -04006390int btrfs_add_link(struct btrfs_trans_handle *trans,
6391 struct inode *parent_inode, struct inode *inode,
6392 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006393{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006394 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006395 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006396 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006397 u64 ino = btrfs_ino(inode);
6398 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006399
Li Zefan33345d012011-04-20 10:31:50 +08006400 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006401 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6402 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006403 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006404 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006405 key.offset = 0;
6406 }
Chris Mason39279cc2007-06-12 06:35:45 -04006407
Li Zefan33345d012011-04-20 10:31:50 +08006408 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006409 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6410 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006411 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006412 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006413 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6414 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006415 }
6416
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006417 /* Nothing to clean up yet */
6418 if (ret)
6419 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006420
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006421 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6422 parent_inode, &key,
6423 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006424 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006425 goto fail_dir_item;
6426 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006427 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006428 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006429 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006430
6431 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6432 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006433 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006434 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006435 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006436 ret = btrfs_update_inode(trans, root, parent_inode);
6437 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006438 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006439 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006440
6441fail_dir_item:
6442 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6443 u64 local_index;
6444 int err;
6445 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6446 key.objectid, root->root_key.objectid,
6447 parent_ino, &local_index, name, name_len);
6448
6449 } else if (add_backref) {
6450 u64 local_index;
6451 int err;
6452
6453 err = btrfs_del_inode_ref(trans, root, name, name_len,
6454 ino, parent_ino, &local_index);
6455 }
6456 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006457}
6458
6459static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006460 struct inode *dir, struct dentry *dentry,
6461 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006462{
Josef Bacika1b075d2010-11-19 20:36:11 +00006463 int err = btrfs_add_link(trans, dir, inode,
6464 dentry->d_name.name, dentry->d_name.len,
6465 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 if (err > 0)
6467 err = -EEXIST;
6468 return err;
6469}
6470
Josef Bacik618e21d2007-07-11 10:18:17 -04006471static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006472 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006473{
6474 struct btrfs_trans_handle *trans;
6475 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006476 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006477 int err;
6478 int drop_inode = 0;
6479 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006480 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006481
Josef Bacik9ed74f22009-09-11 16:12:44 -04006482 /*
6483 * 2 for inode item and ref
6484 * 2 for dir items
6485 * 1 for xattr if selinux is on
6486 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006487 trans = btrfs_start_transaction(root, 5);
6488 if (IS_ERR(trans))
6489 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006490
Li Zefan581bb052011-04-20 10:06:11 +08006491 err = btrfs_find_free_ino(root, &objectid);
6492 if (err)
6493 goto out_unlock;
6494
Josef Bacikaec74772008-07-24 12:12:38 -04006495 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006496 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006497 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006498 if (IS_ERR(inode)) {
6499 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006500 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006501 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006502
Casey Schauflerad19db72011-12-15 10:09:07 -05006503 /*
6504 * If the active LSM wants to access the inode during
6505 * d_instantiate it needs these. Smack checks to see
6506 * if the filesystem supports xattrs by looking at the
6507 * ops vector.
6508 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006509 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006510 init_special_inode(inode, inode->i_mode, rdev);
6511
6512 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006513 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006514 goto out_unlock_inode;
6515
6516 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6517 if (err) {
6518 goto out_unlock_inode;
6519 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006520 btrfs_update_inode(trans, root, inode);
Al Viro2d2d3f12018-05-04 08:23:01 -04006521 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006522 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006523
Josef Bacik618e21d2007-07-11 10:18:17 -04006524out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006525 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006526 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006527 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006528 if (drop_inode) {
6529 inode_dec_link_count(inode);
6530 iput(inode);
6531 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006532 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006533
6534out_unlock_inode:
6535 drop_inode = 1;
6536 unlock_new_inode(inode);
6537 goto out_unlock;
6538
Josef Bacik618e21d2007-07-11 10:18:17 -04006539}
6540
Chris Mason39279cc2007-06-12 06:35:45 -04006541static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006542 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006543{
6544 struct btrfs_trans_handle *trans;
6545 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006546 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006547 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006548 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006549 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006550 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006551
Josef Bacik9ed74f22009-09-11 16:12:44 -04006552 /*
6553 * 2 for inode item and ref
6554 * 2 for dir items
6555 * 1 for xattr if selinux is on
6556 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006557 trans = btrfs_start_transaction(root, 5);
6558 if (IS_ERR(trans))
6559 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006560
Li Zefan581bb052011-04-20 10:06:11 +08006561 err = btrfs_find_free_ino(root, &objectid);
6562 if (err)
6563 goto out_unlock;
6564
Josef Bacikaec74772008-07-24 12:12:38 -04006565 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006566 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006567 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006568 if (IS_ERR(inode)) {
6569 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006570 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006571 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006572 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006573 /*
6574 * If the active LSM wants to access the inode during
6575 * d_instantiate it needs these. Smack checks to see
6576 * if the filesystem supports xattrs by looking at the
6577 * ops vector.
6578 */
6579 inode->i_fop = &btrfs_file_operations;
6580 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006581 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006582
6583 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6584 if (err)
6585 goto out_unlock_inode;
6586
6587 err = btrfs_update_inode(trans, root, inode);
6588 if (err)
6589 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006590
Josef Bacika1b075d2010-11-19 20:36:11 +00006591 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006592 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006593 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006594
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006595 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro2d2d3f12018-05-04 08:23:01 -04006596 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006597
Chris Mason39279cc2007-06-12 06:35:45 -04006598out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006599 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006600 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006601 inode_dec_link_count(inode);
6602 iput(inode);
6603 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006604 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006605 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006607
6608out_unlock_inode:
6609 unlock_new_inode(inode);
6610 goto out_unlock;
6611
Chris Mason39279cc2007-06-12 06:35:45 -04006612}
6613
6614static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6615 struct dentry *dentry)
6616{
Filipe Manana271dba42016-01-05 16:24:05 +00006617 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006618 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006619 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006620 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006621 int err;
6622 int drop_inode = 0;
6623
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006624 /* do not allow sys_link's with other subvols of the same device */
6625 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006626 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006627
Mark Fashehf1863732012-08-08 11:32:27 -07006628 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006629 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006630
Chris Mason3de45862008-11-17 21:02:50 -05006631 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006632 if (err)
6633 goto fail;
6634
Yan, Zhenga22285a2010-05-16 10:48:46 -04006635 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006636 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006637 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006638 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006639 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006640 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006641 if (IS_ERR(trans)) {
6642 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006643 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006644 goto fail;
6645 }
Chris Mason5f39d392007-10-15 16:14:19 -04006646
Miao Xie67de1172013-12-26 13:07:06 +08006647 /* There are several dir indexes for this inode, clear the cache. */
6648 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006649 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006650 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006651 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006652 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006653 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006654
Josef Bacika1b075d2010-11-19 20:36:11 +00006655 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006656
Yan, Zhenga5719522009-09-24 09:17:31 -04006657 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006658 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006659 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006660 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006661 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006662 if (err)
6663 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006664 if (inode->i_nlink == 1) {
6665 /*
6666 * If new hard link count is 1, it's a file created
6667 * with open(2) O_TMPFILE flag.
6668 */
6669 err = btrfs_orphan_del(trans, inode);
6670 if (err)
6671 goto fail;
6672 }
Al Viro08c422c2011-12-23 07:58:13 -05006673 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006674 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006675 }
Chris Mason39279cc2007-06-12 06:35:45 -04006676
Josef Bacikc581afc2014-02-06 16:06:06 -05006677 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006678fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006679 if (trans)
6680 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006681 if (drop_inode) {
6682 inode_dec_link_count(inode);
6683 iput(inode);
6684 }
Liu Bob53d3f52012-11-14 14:34:34 +00006685 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006686 return err;
6687}
6688
Al Viro18bb1db2011-07-26 01:41:39 -04006689static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006690{
Chris Masonb9d86662008-05-02 16:13:49 -04006691 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006692 struct btrfs_trans_handle *trans;
6693 struct btrfs_root *root = BTRFS_I(dir)->root;
6694 int err = 0;
6695 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006696 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006697 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006698
Josef Bacik9ed74f22009-09-11 16:12:44 -04006699 /*
6700 * 2 items for inode and ref
6701 * 2 items for dir items
6702 * 1 for xattr if selinux is on
6703 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006704 trans = btrfs_start_transaction(root, 5);
6705 if (IS_ERR(trans))
6706 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006707
Li Zefan581bb052011-04-20 10:06:11 +08006708 err = btrfs_find_free_ino(root, &objectid);
6709 if (err)
6710 goto out_fail;
6711
Josef Bacikaec74772008-07-24 12:12:38 -04006712 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006713 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006714 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006715 if (IS_ERR(inode)) {
6716 err = PTR_ERR(inode);
6717 goto out_fail;
6718 }
Chris Mason5f39d392007-10-15 16:14:19 -04006719
Chris Mason39279cc2007-06-12 06:35:45 -04006720 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006721 /* these must be set before we unlock the inode */
6722 inode->i_op = &btrfs_dir_inode_operations;
6723 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006724
Eric Paris2a7dba32011-02-01 11:05:39 -05006725 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006726 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006727 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006728
Chris Masondbe674a2008-07-17 12:54:05 -04006729 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006730 err = btrfs_update_inode(trans, root, inode);
6731 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006732 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006733
Josef Bacika1b075d2010-11-19 20:36:11 +00006734 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6735 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006736 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006737 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006738
Al Viro2d2d3f12018-05-04 08:23:01 -04006739 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006740 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006741
6742out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006743 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006744 if (drop_on_err) {
6745 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006746 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006747 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006748 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006749 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006750 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006751
6752out_fail_inode:
6753 unlock_new_inode(inode);
6754 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006755}
6756
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006757/* Find next extent map of a given extent map, caller needs to ensure locks */
6758static struct extent_map *next_extent_map(struct extent_map *em)
6759{
6760 struct rb_node *next;
6761
6762 next = rb_next(&em->rb_node);
6763 if (!next)
6764 return NULL;
6765 return container_of(next, struct extent_map, rb_node);
6766}
6767
6768static struct extent_map *prev_extent_map(struct extent_map *em)
6769{
6770 struct rb_node *prev;
6771
6772 prev = rb_prev(&em->rb_node);
6773 if (!prev)
6774 return NULL;
6775 return container_of(prev, struct extent_map, rb_node);
6776}
6777
Chris Masond352ac62008-09-29 15:18:18 -04006778/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006779 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006780 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006781 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006782 */
Chris Mason3b951512008-04-17 11:29:12 -04006783static int merge_extent_mapping(struct extent_map_tree *em_tree,
6784 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006785 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006786 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006787{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006788 struct extent_map *prev;
6789 struct extent_map *next;
6790 u64 start;
6791 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006792 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006793
Chris Masone6dcd2d2008-07-17 12:53:50 -04006794 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006795
6796 if (existing->start > map_start) {
6797 next = existing;
6798 prev = prev_extent_map(next);
6799 } else {
6800 prev = existing;
6801 next = next_extent_map(prev);
6802 }
6803
6804 start = prev ? extent_map_end(prev) : em->start;
6805 start = max_t(u64, start, em->start);
6806 end = next ? next->start : extent_map_end(em);
6807 end = min_t(u64, end, extent_map_end(em));
6808 start_diff = start - em->start;
6809 em->start = start;
6810 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006811 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6812 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006813 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006814 em->block_len -= start_diff;
6815 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006816 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006817}
6818
Chris Masonc8b97812008-10-29 14:49:59 -04006819static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006820 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006821 size_t pg_offset, u64 extent_offset,
6822 struct btrfs_file_extent_item *item)
6823{
6824 int ret;
6825 struct extent_buffer *leaf = path->nodes[0];
6826 char *tmp;
6827 size_t max_size;
6828 unsigned long inline_size;
6829 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006830 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006831
6832 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006833 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006834 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6835 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006836 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006837 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006838 if (!tmp)
6839 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006840 ptr = btrfs_file_extent_inline_start(item);
6841
6842 read_extent_buffer(leaf, tmp, ptr, inline_size);
6843
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006844 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006845 ret = btrfs_decompress(compress_type, tmp, page,
6846 extent_offset, inline_size, max_size);
Zygo Blaxell8f60ef92017-03-10 16:45:44 -05006847
6848 /*
6849 * decompression code contains a memset to fill in any space between the end
6850 * of the uncompressed data and the end of max_size in case the decompressed
6851 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6852 * the end of an inline extent and the beginning of the next block, so we
6853 * cover that region here.
6854 */
6855
6856 if (max_size + pg_offset < PAGE_SIZE) {
6857 char *map = kmap(page);
6858 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6859 kunmap(page);
6860 }
Chris Masonc8b97812008-10-29 14:49:59 -04006861 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006862 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006863}
6864
Chris Masond352ac62008-09-29 15:18:18 -04006865/*
6866 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006867 * the ugly parts come from merging extents from the disk with the in-ram
6868 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006869 * where the in-ram extents might be locked pending data=ordered completion.
6870 *
6871 * This also copies inline extents directly into the page.
6872 */
Chris Masond3977122009-01-05 21:25:51 -05006873
Chris Masona52d9a82007-08-27 16:49:44 -04006874struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006875 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006876 int create)
6877{
6878 int ret;
6879 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006880 u64 extent_start = 0;
6881 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006882 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006883 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006884 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006885 struct btrfs_root *root = BTRFS_I(inode)->root;
6886 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006887 struct extent_buffer *leaf;
6888 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006889 struct extent_map *em = NULL;
6890 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006891 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006892 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006893 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006894
Chris Masona52d9a82007-08-27 16:49:44 -04006895again:
Chris Mason890871b2009-09-02 16:24:52 -04006896 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006897 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006898 if (em)
6899 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006900 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006901
Chris Masona52d9a82007-08-27 16:49:44 -04006902 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006903 if (em->start > start || em->start + em->len <= start)
6904 free_extent_map(em);
6905 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006906 free_extent_map(em);
6907 else
6908 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 }
David Sterba172ddd62011-04-21 00:48:27 +02006910 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006911 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006912 err = -ENOMEM;
6913 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006914 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006915 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006916 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006917 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006918 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006919 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006920
6921 if (!path) {
6922 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006923 if (!path) {
6924 err = -ENOMEM;
6925 goto out;
6926 }
6927 /*
6928 * Chances are we'll be called again, so go ahead and do
6929 * readahead
6930 */
David Sterbae4058b52015-11-27 16:31:35 +01006931 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006932 }
6933
Chris Mason179e29e2007-11-01 11:28:41 -04006934 ret = btrfs_lookup_file_extent(trans, root, path,
6935 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006936 if (ret < 0) {
6937 err = ret;
6938 goto out;
6939 }
6940
6941 if (ret != 0) {
6942 if (path->slots[0] == 0)
6943 goto not_found;
6944 path->slots[0]--;
6945 }
6946
Chris Mason5f39d392007-10-15 16:14:19 -04006947 leaf = path->nodes[0];
6948 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006949 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006950 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006951 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006952 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006953 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006954 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006955 /*
6956 * If we backup past the first extent we want to move forward
6957 * and see if there is an extent in front of us, otherwise we'll
6958 * say there is a hole for our whole search range which can
6959 * cause problems.
6960 */
6961 extent_end = start;
6962 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006963 }
6964
Chris Mason5f39d392007-10-15 16:14:19 -04006965 found_type = btrfs_file_extent_type(leaf, item);
6966 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006967 if (found_type == BTRFS_FILE_EXTENT_REG ||
6968 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006969 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006970 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006971 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6972 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006973 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006974 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006975 }
Josef Bacik25a50342013-10-14 12:08:38 -04006976next:
Yan Zheng9036c102008-10-30 14:19:41 -04006977 if (start >= extent_end) {
6978 path->slots[0]++;
6979 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6980 ret = btrfs_next_leaf(root, path);
6981 if (ret < 0) {
6982 err = ret;
6983 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006984 }
Yan Zheng9036c102008-10-30 14:19:41 -04006985 if (ret > 0)
6986 goto not_found;
6987 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006988 }
Yan Zheng9036c102008-10-30 14:19:41 -04006989 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6990 if (found_key.objectid != objectid ||
6991 found_key.type != BTRFS_EXTENT_DATA_KEY)
6992 goto not_found;
6993 if (start + len <= found_key.offset)
6994 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006995 if (start > found_key.offset)
6996 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006997 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006998 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006999 em->len = found_key.offset - start;
7000 goto not_found_em;
7001 }
7002
Filipe Manana7ffbb592014-06-09 03:48:05 +01007003 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
7004
Yan Zhengd899e052008-10-30 14:25:28 -04007005 if (found_type == BTRFS_FILE_EXTENT_REG ||
7006 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007007 goto insert;
7008 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007009 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007010 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007011 size_t size;
7012 size_t extent_offset;
7013 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007014
Filipe Manana7ffbb592014-06-09 03:48:05 +01007015 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007016 goto out;
Yan689f9342007-10-29 11:41:07 -04007017
Chris Mason514ac8a2014-01-03 21:07:00 -08007018 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007019 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007020 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7021 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007022 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00007023 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007024 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007025 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007026 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007027 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007028 if (btrfs_file_extent_compression(leaf, item) !=
7029 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007030 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007031 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007032 if (ret) {
7033 err = ret;
7034 goto out;
7035 }
Chris Masonc8b97812008-10-29 14:49:59 -04007036 } else {
7037 map = kmap(page);
7038 read_extent_buffer(leaf, map + pg_offset, ptr,
7039 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007040 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007041 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007042 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007043 copy_size);
7044 }
Chris Masonc8b97812008-10-29 14:49:59 -04007045 kunmap(page);
7046 }
Chris Mason179e29e2007-11-01 11:28:41 -04007047 flush_dcache_page(page);
7048 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007049 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007050 if (!trans) {
7051 kunmap(page);
7052 free_extent_map(em);
7053 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007054
David Sterbab3b4aa72011-04-21 01:20:15 +02007055 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007056 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007057
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007058 if (IS_ERR(trans))
7059 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007060 goto again;
7061 }
Chris Masonc8b97812008-10-29 14:49:59 -04007062 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007063 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007064 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007065 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007066 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007067 }
Chris Masond1310b22008-01-24 16:13:08 -05007068 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007069 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007070 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007071 }
7072not_found:
7073 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007074 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007075 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007076not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007077 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007078 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007079insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007080 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007081 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007082 btrfs_err(root->fs_info,
7083 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7084 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007085 err = -EIO;
7086 goto out;
7087 }
Chris Masond1310b22008-01-24 16:13:08 -05007088
7089 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007090 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007091 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007092 /* it is possible that someone inserted the extent into the tree
7093 * while we had the lock dropped. It is also possible that
7094 * an overlapping map exists in the tree
7095 */
Chris Masona52d9a82007-08-27 16:49:44 -04007096 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007097 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007098
7099 ret = 0;
7100
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007101 existing = search_extent_mapping(em_tree, start, len);
7102 /*
7103 * existing will always be non-NULL, since there must be
7104 * extent causing the -EEXIST.
7105 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007106 if (existing->start == em->start &&
7107 extent_map_end(existing) == extent_map_end(em) &&
7108 em->block_start == existing->block_start) {
7109 /*
7110 * these two extents are the same, it happens
7111 * with inlines especially
7112 */
7113 free_extent_map(em);
7114 em = existing;
7115 err = 0;
7116
7117 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007118 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007119 /*
7120 * The existing extent map is the one nearest to
7121 * the [start, start + len) range which overlaps
7122 */
7123 err = merge_extent_mapping(em_tree, existing,
7124 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007125 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007126 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007127 free_extent_map(em);
7128 em = NULL;
7129 }
7130 } else {
7131 free_extent_map(em);
7132 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007133 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
Chris Masona52d9a82007-08-27 16:49:44 -04007135 }
Chris Mason890871b2009-09-02 16:24:52 -04007136 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007137out:
liubo1abe9b82011-03-24 11:18:59 +00007138
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05007139 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007140
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007141 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007142 if (trans) {
7143 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007144 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007145 err = ret;
7146 }
Chris Masona52d9a82007-08-27 16:49:44 -04007147 if (err) {
7148 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007149 return ERR_PTR(err);
7150 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007151 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007152 return em;
7153}
7154
Chris Masonec29ed52011-02-23 16:23:20 -05007155struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7156 size_t pg_offset, u64 start, u64 len,
7157 int create)
7158{
7159 struct extent_map *em;
7160 struct extent_map *hole_em = NULL;
7161 u64 range_start = start;
7162 u64 end;
7163 u64 found;
7164 u64 found_end;
7165 int err = 0;
7166
7167 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7168 if (IS_ERR(em))
7169 return em;
7170 if (em) {
7171 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007172 * if our em maps to
7173 * - a hole or
7174 * - a pre-alloc extent,
7175 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007176 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007177 if (em->block_start != EXTENT_MAP_HOLE &&
7178 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007179 return em;
7180 else
7181 hole_em = em;
7182 }
7183
7184 /* check to see if we've wrapped (len == -1 or similar) */
7185 end = start + len;
7186 if (end < start)
7187 end = (u64)-1;
7188 else
7189 end -= 1;
7190
7191 em = NULL;
7192
7193 /* ok, we didn't find anything, lets look for delalloc */
7194 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7195 end, len, EXTENT_DELALLOC, 1);
7196 found_end = range_start + found;
7197 if (found_end < range_start)
7198 found_end = (u64)-1;
7199
7200 /*
7201 * we didn't find anything useful, return
7202 * the original results from get_extent()
7203 */
7204 if (range_start > end || found_end <= start) {
7205 em = hole_em;
7206 hole_em = NULL;
7207 goto out;
7208 }
7209
7210 /* adjust the range_start to make sure it doesn't
7211 * go backwards from the start they passed in
7212 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307213 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007214 found = found_end - range_start;
7215
7216 if (found > 0) {
7217 u64 hole_start = start;
7218 u64 hole_len = len;
7219
David Sterba172ddd62011-04-21 00:48:27 +02007220 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007221 if (!em) {
7222 err = -ENOMEM;
7223 goto out;
7224 }
7225 /*
7226 * when btrfs_get_extent can't find anything it
7227 * returns one huge hole
7228 *
7229 * make sure what it found really fits our range, and
7230 * adjust to make sure it is based on the start from
7231 * the caller
7232 */
7233 if (hole_em) {
7234 u64 calc_end = extent_map_end(hole_em);
7235
7236 if (calc_end <= start || (hole_em->start > end)) {
7237 free_extent_map(hole_em);
7238 hole_em = NULL;
7239 } else {
7240 hole_start = max(hole_em->start, start);
7241 hole_len = calc_end - hole_start;
7242 }
7243 }
7244 em->bdev = NULL;
7245 if (hole_em && range_start > hole_start) {
7246 /* our hole starts before our delalloc, so we
7247 * have to return just the parts of the hole
7248 * that go until the delalloc starts
7249 */
7250 em->len = min(hole_len,
7251 range_start - hole_start);
7252 em->start = hole_start;
7253 em->orig_start = hole_start;
7254 /*
7255 * don't adjust block start at all,
7256 * it is fixed at EXTENT_MAP_HOLE
7257 */
7258 em->block_start = hole_em->block_start;
7259 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007260 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7261 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007262 } else {
7263 em->start = range_start;
7264 em->len = found;
7265 em->orig_start = range_start;
7266 em->block_start = EXTENT_MAP_DELALLOC;
7267 em->block_len = found;
7268 }
7269 } else if (hole_em) {
7270 return hole_em;
7271 }
7272out:
7273
7274 free_extent_map(hole_em);
7275 if (err) {
7276 free_extent_map(em);
7277 return ERR_PTR(err);
7278 }
7279 return em;
7280}
7281
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007282static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7283 const u64 start,
7284 const u64 len,
7285 const u64 orig_start,
7286 const u64 block_start,
7287 const u64 block_len,
7288 const u64 orig_block_len,
7289 const u64 ram_bytes,
7290 const int type)
7291{
7292 struct extent_map *em = NULL;
7293 int ret;
7294
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295 if (type != BTRFS_ORDERED_NOCOW) {
7296 em = create_pinned_em(inode, start, len, orig_start,
7297 block_start, block_len, orig_block_len,
7298 ram_bytes, type);
7299 if (IS_ERR(em))
7300 goto out;
7301 }
7302 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7303 len, block_len, type);
7304 if (ret) {
7305 if (em) {
7306 free_extent_map(em);
7307 btrfs_drop_extent_cache(inode, start,
7308 start + len - 1, 0);
7309 }
7310 em = ERR_PTR(ret);
7311 }
7312 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007313
7314 return em;
7315}
7316
Josef Bacik4b46fce2010-05-23 11:00:55 -04007317static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7318 u64 start, u64 len)
7319{
7320 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007321 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322 struct btrfs_key ins;
7323 u64 alloc_hint;
7324 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326 alloc_hint = get_extent_allocation_hint(inode, start, len);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007327 ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007328 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007329 if (ret)
7330 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007331
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007332 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7333 ins.objectid, ins.offset, ins.offset,
7334 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007335 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007336 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007337 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007338
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339 return em;
7340}
7341
Chris Mason46bfbb52010-05-26 11:04:10 -04007342/*
7343 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7344 * block must be cow'd
7345 */
Josef Bacik00361582013-08-14 14:02:47 -04007346noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007347 u64 *orig_start, u64 *orig_block_len,
7348 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007349{
Josef Bacik00361582013-08-14 14:02:47 -04007350 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007351 struct btrfs_path *path;
7352 int ret;
7353 struct extent_buffer *leaf;
7354 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007355 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 struct btrfs_file_extent_item *fi;
7357 struct btrfs_key key;
7358 u64 disk_bytenr;
7359 u64 backref_offset;
7360 u64 extent_end;
7361 u64 num_bytes;
7362 int slot;
7363 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007364 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007365
Chris Mason46bfbb52010-05-26 11:04:10 -04007366 path = btrfs_alloc_path();
7367 if (!path)
7368 return -ENOMEM;
7369
Josef Bacik00361582013-08-14 14:02:47 -04007370 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007371 offset, 0);
7372 if (ret < 0)
7373 goto out;
7374
7375 slot = path->slots[0];
7376 if (ret == 1) {
7377 if (slot == 0) {
7378 /* can't find the item, must cow */
7379 ret = 0;
7380 goto out;
7381 }
7382 slot--;
7383 }
7384 ret = 0;
7385 leaf = path->nodes[0];
7386 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007387 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007388 key.type != BTRFS_EXTENT_DATA_KEY) {
7389 /* not our file or wrong item type, must cow */
7390 goto out;
7391 }
7392
7393 if (key.offset > offset) {
7394 /* Wrong offset, must cow */
7395 goto out;
7396 }
7397
7398 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7399 found_type = btrfs_file_extent_type(leaf, fi);
7400 if (found_type != BTRFS_FILE_EXTENT_REG &&
7401 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7402 /* not a regular extent, must cow */
7403 goto out;
7404 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007405
7406 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7407 goto out;
7408
Miao Xiee77751a2013-12-27 21:11:50 +08007409 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7410 if (extent_end <= offset)
7411 goto out;
7412
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007414 if (disk_bytenr == 0)
7415 goto out;
7416
7417 if (btrfs_file_extent_compression(leaf, fi) ||
7418 btrfs_file_extent_encryption(leaf, fi) ||
7419 btrfs_file_extent_other_encoding(leaf, fi))
7420 goto out;
7421
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 backref_offset = btrfs_file_extent_offset(leaf, fi);
7423
Josef Bacik7ee9e442013-06-21 16:37:03 -04007424 if (orig_start) {
7425 *orig_start = key.offset - backref_offset;
7426 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7427 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7428 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007429
Chris Mason46bfbb52010-05-26 11:04:10 -04007430 if (btrfs_extent_readonly(root, disk_bytenr))
7431 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007432
7433 num_bytes = min(offset + *len, extent_end) - offset;
7434 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7435 u64 range_end;
7436
7437 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7438 ret = test_range_bit(io_tree, offset, range_end,
7439 EXTENT_DELALLOC, 0, NULL);
7440 if (ret) {
7441 ret = -EAGAIN;
7442 goto out;
7443 }
7444 }
7445
Josef Bacik1bda19e2013-10-18 12:10:36 -04007446 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007447
7448 /*
7449 * look for other files referencing this extent, if we
7450 * find any we must cow
7451 */
Josef Bacik00361582013-08-14 14:02:47 -04007452 trans = btrfs_join_transaction(root);
7453 if (IS_ERR(trans)) {
7454 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007455 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007456 }
7457
7458 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7459 key.offset - backref_offset, disk_bytenr);
7460 btrfs_end_transaction(trans, root);
7461 if (ret) {
7462 ret = 0;
7463 goto out;
7464 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007465
7466 /*
7467 * adjust disk_bytenr and num_bytes to cover just the bytes
7468 * in this extent we are about to write. If there
7469 * are any csums in that range we have to cow in order
7470 * to keep the csums correct
7471 */
7472 disk_bytenr += backref_offset;
7473 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7475 goto out;
7476 /*
7477 * all of the above have passed, it is safe to overwrite this extent
7478 * without cow
7479 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007480 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007481 ret = 1;
7482out:
7483 btrfs_free_path(path);
7484 return ret;
7485}
7486
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007487bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7488{
7489 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7490 int found = false;
7491 void **pagep = NULL;
7492 struct page *page = NULL;
David Sterba4d15ab92017-05-12 01:03:52 +02007493 unsigned long start_idx;
7494 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007495
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007496 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007497
7498 /*
7499 * end is the last byte in the last page. end == start is legal
7500 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007501 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007502
7503 rcu_read_lock();
7504
7505 /* Most of the code in this while loop is lifted from
7506 * find_get_page. It's been modified to begin searching from a
7507 * page and return just the first page found in that range. If the
7508 * found idx is less than or equal to the end idx then we know that
7509 * a page exists. If no pages are found or if those pages are
7510 * outside of the range then we're fine (yay!) */
7511 while (page == NULL &&
7512 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7513 page = radix_tree_deref_slot(pagep);
7514 if (unlikely(!page))
7515 break;
7516
7517 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007518 if (radix_tree_deref_retry(page)) {
7519 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007520 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007521 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007522 /*
7523 * Otherwise, shmem/tmpfs must be storing a swap entry
7524 * here as an exceptional entry: so return it without
7525 * attempting to raise page count.
7526 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007527 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007528 break; /* TODO: Is this relevant for this use case? */
7529 }
7530
Filipe Manana91405152014-06-05 13:22:24 +01007531 if (!page_cache_get_speculative(page)) {
7532 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007533 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007534 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007535
7536 /*
7537 * Has the page moved?
7538 * This is part of the lockless pagecache protocol. See
7539 * include/linux/pagemap.h for details.
7540 */
7541 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007542 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007543 page = NULL;
7544 }
7545 }
7546
7547 if (page) {
7548 if (page->index <= end_idx)
7549 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007550 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007551 }
7552
7553 rcu_read_unlock();
7554 return found;
7555}
7556
Josef Bacikeb838e72012-07-31 16:28:48 -04007557static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7558 struct extent_state **cached_state, int writing)
7559{
7560 struct btrfs_ordered_extent *ordered;
7561 int ret = 0;
7562
7563 while (1) {
7564 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007565 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 /*
7567 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007568 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007569 * extents in this range.
7570 */
7571 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7572 lockend - lockstart + 1);
7573
7574 /*
7575 * We need to make sure there are no buffered pages in this
7576 * range either, we could have raced between the invalidate in
7577 * generic_file_direct_write and locking the extent. The
7578 * invalidate needs to happen so that reads after a write do not
7579 * get stale data.
7580 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007581 if (!ordered &&
7582 (!writing ||
7583 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007584 break;
7585
7586 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7587 cached_state, GFP_NOFS);
7588
7589 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007590 /*
7591 * If we are doing a DIO read and the ordered extent we
7592 * found is for a buffered write, we can not wait for it
7593 * to complete and retry, because if we do so we can
7594 * deadlock with concurrent buffered writes on page
7595 * locks. This happens only if our DIO read covers more
7596 * than one extent map, if at this point has already
7597 * created an ordered extent for a previous extent map
7598 * and locked its range in the inode's io tree, and a
7599 * concurrent write against that previous extent map's
7600 * range and this range started (we unlock the ranges
7601 * in the io tree only when the bios complete and
7602 * buffered writes always lock pages before attempting
7603 * to lock range in the io tree).
7604 */
7605 if (writing ||
7606 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7607 btrfs_start_ordered_extent(inode, ordered, 1);
7608 else
7609 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 btrfs_put_ordered_extent(ordered);
7611 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007612 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007613 * We could trigger writeback for this range (and wait
7614 * for it to complete) and then invalidate the pages for
7615 * this range (through invalidate_inode_pages2_range()),
7616 * but that can lead us to a deadlock with a concurrent
7617 * call to readpages() (a buffered read or a defrag call
7618 * triggered a readahead) on a page lock due to an
7619 * ordered dio extent we created before but did not have
7620 * yet a corresponding bio submitted (whence it can not
7621 * complete), which makes readpages() wait for that
7622 * ordered extent to complete while holding a lock on
7623 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007625 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007626 }
7627
Filipe Mananaade77022016-02-18 14:28:55 +00007628 if (ret)
7629 break;
7630
Josef Bacikeb838e72012-07-31 16:28:48 -04007631 cond_resched();
7632 }
7633
7634 return ret;
7635}
7636
Josef Bacik69ffb542012-09-11 15:40:07 -04007637static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7638 u64 len, u64 orig_start,
7639 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007640 u64 orig_block_len, u64 ram_bytes,
7641 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007642{
7643 struct extent_map_tree *em_tree;
7644 struct extent_map *em;
7645 struct btrfs_root *root = BTRFS_I(inode)->root;
7646 int ret;
7647
7648 em_tree = &BTRFS_I(inode)->extent_tree;
7649 em = alloc_extent_map();
7650 if (!em)
7651 return ERR_PTR(-ENOMEM);
7652
7653 em->start = start;
7654 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007655 em->mod_start = start;
7656 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007657 em->len = len;
7658 em->block_len = block_len;
7659 em->block_start = block_start;
7660 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007661 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007662 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007663 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007664 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7665 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007666 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007667
7668 do {
7669 btrfs_drop_extent_cache(inode, em->start,
7670 em->start + em->len - 1, 0);
7671 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007672 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007673 write_unlock(&em_tree->lock);
7674 } while (ret == -EEXIST);
7675
7676 if (ret) {
7677 free_extent_map(em);
7678 return ERR_PTR(ret);
7679 }
7680
7681 return em;
7682}
7683
Filipe Manana9c9464c2015-11-04 09:52:04 +00007684static void adjust_dio_outstanding_extents(struct inode *inode,
7685 struct btrfs_dio_data *dio_data,
7686 const u64 len)
7687{
7688 unsigned num_extents;
7689
7690 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7691 BTRFS_MAX_EXTENT_SIZE);
7692 /*
7693 * If we have an outstanding_extents count still set then we're
7694 * within our reservation, otherwise we need to adjust our inode
7695 * counter appropriately.
7696 */
Liu Bof76ddff2016-12-22 17:13:54 -08007697 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007698 dio_data->outstanding_extents -= num_extents;
7699 } else {
Liu Bof76ddff2016-12-22 17:13:54 -08007700 /*
7701 * If dio write length has been split due to no large enough
7702 * contiguous space, we need to compensate our inode counter
7703 * appropriately.
7704 */
7705 u64 num_needed = num_extents - dio_data->outstanding_extents;
7706
Filipe Manana9c9464c2015-11-04 09:52:04 +00007707 spin_lock(&BTRFS_I(inode)->lock);
Liu Bof76ddff2016-12-22 17:13:54 -08007708 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007709 spin_unlock(&BTRFS_I(inode)->lock);
7710 }
7711}
7712
Josef Bacik4b46fce2010-05-23 11:00:55 -04007713static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7714 struct buffer_head *bh_result, int create)
7715{
7716 struct extent_map *em;
7717 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007718 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307719 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007721 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007723 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007724 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007725
Miao Xie172a5042013-02-21 02:48:22 -07007726 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007727 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007728 else
Josef Bacikc3298612012-08-03 16:49:19 -04007729 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007730
Josef Bacikc3298612012-08-03 16:49:19 -04007731 lockstart = start;
7732 lockend = start + len - 1;
7733
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007734 if (current->journal_info) {
7735 /*
7736 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007737 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007738 * confused.
7739 */
chandan50745b02015-08-28 21:10:13 +05307740 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007741 current->journal_info = NULL;
7742 }
7743
Josef Bacikeb838e72012-07-31 16:28:48 -04007744 /*
7745 * If this errors out it's because we couldn't invalidate pagecache for
7746 * this range and we need to fallback to buffered.
7747 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007748 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7749 create)) {
7750 ret = -ENOTBLK;
7751 goto err;
7752 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007753
Josef Bacik4b46fce2010-05-23 11:00:55 -04007754 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007755 if (IS_ERR(em)) {
7756 ret = PTR_ERR(em);
7757 goto unlock_err;
7758 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007759
7760 /*
7761 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7762 * io. INLINE is special, and we could probably kludge it in here, but
7763 * it's still buffered so for safety lets just fall back to the generic
7764 * buffered path.
7765 *
7766 * For COMPRESSED we _have_ to read the entire extent in so we can
7767 * decompress it, so there will be buffering required no matter what we
7768 * do, so go ahead and fallback to buffered.
7769 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007770 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771 * to buffered IO. Don't blame me, this is the price we pay for using
7772 * the generic code.
7773 */
7774 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7775 em->block_start == EXTENT_MAP_INLINE) {
7776 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007777 ret = -ENOTBLK;
7778 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779 }
7780
7781 /* Just a good old fashioned hole, return */
7782 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7783 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7784 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007785 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007786 }
7787
7788 /*
7789 * We don't allocate a new extent in the following cases
7790 *
7791 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7792 * existing extent.
7793 * 2) The extent is marked as PREALLOC. We're good to go here and can
7794 * just use the extent.
7795 *
7796 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007797 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007798 len = min(len, em->len - (start - em->start));
7799 lockstart = start + len;
7800 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007801 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802
7803 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7804 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7805 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007806 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007807 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007808
7809 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7810 type = BTRFS_ORDERED_PREALLOC;
7811 else
7812 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007813 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007815
Josef Bacik00361582013-08-14 14:02:47 -04007816 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007817 &orig_block_len, &ram_bytes) == 1 &&
7818 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007819 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007820
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007821 em2 = btrfs_create_dio_extent(inode, start, len,
7822 orig_start, block_start,
7823 len, orig_block_len,
7824 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007825 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007826 if (type == BTRFS_ORDERED_PREALLOC) {
7827 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007828 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007829 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007830 if (em2 && IS_ERR(em2)) {
7831 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007832 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007833 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007834 /*
7835 * For inode marked NODATACOW or extent marked PREALLOC,
7836 * use the existing or preallocated extent, so does not
7837 * need to adjust btrfs_space_info's bytes_may_use.
7838 */
7839 btrfs_free_reserved_data_space_noquota(inode,
7840 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007841 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843 }
Josef Bacik00361582013-08-14 14:02:47 -04007844
Chris Mason46bfbb52010-05-26 11:04:10 -04007845 /*
7846 * this will cow the extent, reset the len in case we changed
7847 * it above
7848 */
7849 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007850 free_extent_map(em);
7851 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007852 if (IS_ERR(em)) {
7853 ret = PTR_ERR(em);
7854 goto unlock_err;
7855 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007856 len = min(len, em->len - (start - em->start));
7857unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007858 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7859 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007860 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007861 bh_result->b_bdev = em->bdev;
7862 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007863 if (create) {
7864 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7865 set_buffer_new(bh_result);
7866
7867 /*
7868 * Need to update the i_size under the extent lock so buffered
7869 * readers will get the updated i_size when we unlock.
7870 */
7871 if (start + len > i_size_read(inode))
7872 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007873
Filipe Manana9c9464c2015-11-04 09:52:04 +00007874 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307875 WARN_ON(dio_data->reserve < len);
7876 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007877 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307878 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007879 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880
Josef Bacikeb838e72012-07-31 16:28:48 -04007881 /*
7882 * In the case of write we need to clear and unlock the entire range,
7883 * in the case of read we need to unlock only the end area that we
7884 * aren't using if there is any left over space.
7885 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007886 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007887 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7888 lockend, unlock_bits, 1, 0,
7889 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007890 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007891 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007892 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007893
Josef Bacik4b46fce2010-05-23 11:00:55 -04007894 free_extent_map(em);
7895
7896 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007897
7898unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007899 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7900 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007901err:
chandan50745b02015-08-28 21:10:13 +05307902 if (dio_data)
7903 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007904 /*
7905 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7906 * write less data then expected, so that we don't underflow our inode's
7907 * outstanding extents counter.
7908 */
7909 if (create && dio_data)
7910 adjust_dio_outstanding_extents(inode, dio_data, len);
7911
Josef Bacikeb838e72012-07-31 16:28:48 -04007912 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007913}
7914
Miao Xie8b110e32014-09-12 18:44:03 +08007915static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f62016-06-05 14:31:54 -05007916 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007917{
7918 struct btrfs_root *root = BTRFS_I(inode)->root;
7919 int ret;
7920
Mike Christie37226b22016-06-05 14:31:52 -05007921 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007922
7923 bio_get(bio);
7924
7925 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7926 BTRFS_WQ_ENDIO_DIO_REPAIR);
7927 if (ret)
7928 goto err;
7929
Mike Christie81a75f62016-06-05 14:31:54 -05007930 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007931err:
7932 bio_put(bio);
7933 return ret;
7934}
7935
7936static int btrfs_check_dio_repairable(struct inode *inode,
7937 struct bio *failed_bio,
7938 struct io_failure_record *failrec,
7939 int failed_mirror)
7940{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007942 int num_copies;
7943
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007944 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007945 if (num_copies == 1) {
7946 /*
7947 * we only have a single copy of the data, so don't bother with
7948 * all the retry and error correction code that follows. no
7949 * matter what the error is, it is very likely to persist.
7950 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007951 btrfs_debug(fs_info,
7952 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7953 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007954 return 0;
7955 }
7956
7957 failrec->failed_mirror = failed_mirror;
7958 failrec->this_mirror++;
7959 if (failrec->this_mirror == failed_mirror)
7960 failrec->this_mirror++;
7961
7962 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007963 btrfs_debug(fs_info,
7964 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7965 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007966 return 0;
7967 }
7968
7969 return 1;
7970}
7971
7972static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307973 struct page *page, unsigned int pgoff,
7974 u64 start, u64 end, int failed_mirror,
7975 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007976{
7977 struct io_failure_record *failrec;
7978 struct bio *bio;
7979 int isector;
7980 int read_mode;
7981 int ret;
7982
Mike Christie37226b22016-06-05 14:31:52 -05007983 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007984
7985 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7986 if (ret)
7987 return ret;
7988
7989 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7990 failed_mirror);
7991 if (!ret) {
7992 free_io_failure(inode, failrec);
7993 return -EIO;
7994 }
7995
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 if ((failed_bio->bi_vcnt > 1)
7997 || (failed_bio->bi_io_vec->bv_len
7998 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007999 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
8000 else
8001 read_mode = READ_SYNC;
8002
8003 isector = start - btrfs_io_bio(failed_bio)->logical;
8004 isector >>= inode->i_sb->s_blocksize_bits;
8005 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008007 if (!bio) {
8008 free_io_failure(inode, failrec);
8009 return -EIO;
8010 }
Mike Christie37226b22016-06-05 14:31:52 -05008011 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008012
8013 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8014 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8015 read_mode, failrec->this_mirror, failrec->in_validation);
8016
Mike Christie81a75f62016-06-05 14:31:54 -05008017 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008018 if (ret) {
8019 free_io_failure(inode, failrec);
8020 bio_put(bio);
8021 }
8022
8023 return ret;
8024}
8025
8026struct btrfs_retry_complete {
8027 struct completion done;
8028 struct inode *inode;
8029 u64 start;
8030 int uptodate;
8031};
8032
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008033static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008034{
8035 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008037 struct bio_vec *bvec;
8038 int i;
8039
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008040 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008041 goto end;
8042
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 ASSERT(bio->bi_vcnt == 1);
8044 inode = bio->bi_io_vec->bv_page->mapping->host;
8045 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8046
Miao Xie8b110e32014-09-12 18:44:03 +08008047 done->uptodate = 1;
8048 bio_for_each_segment_all(bvec, bio, i)
8049 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
8050end:
8051 complete(&done->done);
8052 bio_put(bio);
8053}
8054
8055static int __btrfs_correct_data_nocsum(struct inode *inode,
8056 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008057{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008059 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08008060 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008061 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 unsigned int pgoff;
8063 u32 sectorsize;
8064 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008065 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08008066 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008067
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308068 fs_info = BTRFS_I(inode)->root->fs_info;
8069 sectorsize = BTRFS_I(inode)->root->sectorsize;
8070
Miao Xiec1dc0892014-09-12 18:43:56 +08008071 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008072 done.inode = inode;
8073
8074 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8076 pgoff = bvec->bv_offset;
8077
8078next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008079 done.uptodate = 0;
8080 done.start = start;
8081 init_completion(&done.done);
8082
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8084 pgoff, start, start + sectorsize - 1,
8085 io_bio->mirror_num,
8086 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008087 if (ret)
8088 return ret;
8089
8090 wait_for_completion(&done.done);
8091
8092 if (!done.uptodate) {
8093 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308094 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008095 }
8096
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 start += sectorsize;
8098
Liu Boc17acd22017-04-07 13:11:10 -07008099 nr_sectors--;
8100 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 pgoff += sectorsize;
Liu Boc17acd22017-04-07 13:11:10 -07008102 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103 goto next_block_or_try_again;
8104 }
Miao Xie8b110e32014-09-12 18:44:03 +08008105 }
8106
8107 return 0;
8108}
8109
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008110static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008111{
8112 struct btrfs_retry_complete *done = bio->bi_private;
8113 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008115 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008117 int uptodate;
8118 int ret;
8119 int i;
8120
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008121 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008122 goto end;
8123
8124 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125
8126 start = done->start;
8127
8128 ASSERT(bio->bi_vcnt == 1);
8129 inode = bio->bi_io_vec->bv_page->mapping->host;
8130 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8131
Miao Xie8b110e32014-09-12 18:44:03 +08008132 bio_for_each_segment_all(bvec, bio, i) {
8133 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308134 bvec->bv_page, bvec->bv_offset,
8135 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008136 if (!ret)
8137 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308138 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008139 else
8140 uptodate = 0;
8141 }
8142
8143 done->uptodate = uptodate;
8144end:
8145 complete(&done->done);
8146 bio_put(bio);
8147}
8148
8149static int __btrfs_subio_endio_read(struct inode *inode,
8150 struct btrfs_io_bio *io_bio, int err)
8151{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008153 struct bio_vec *bvec;
8154 struct btrfs_retry_complete done;
8155 u64 start;
8156 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308157 u32 sectorsize;
8158 int nr_sectors;
8159 unsigned int pgoff;
8160 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008161 int i;
8162 int ret;
8163
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308164 fs_info = BTRFS_I(inode)->root->fs_info;
8165 sectorsize = BTRFS_I(inode)->root->sectorsize;
8166
Miao Xie8b110e32014-09-12 18:44:03 +08008167 err = 0;
8168 start = io_bio->logical;
8169 done.inode = inode;
8170
Miao Xiec1dc0892014-09-12 18:43:56 +08008171 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8173
8174 pgoff = bvec->bv_offset;
8175next_block:
8176 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8177 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8178 bvec->bv_page, pgoff, start,
8179 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008180 if (likely(!ret))
8181 goto next;
8182try_again:
8183 done.uptodate = 0;
8184 done.start = start;
8185 init_completion(&done.done);
8186
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308187 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8188 pgoff, start, start + sectorsize - 1,
8189 io_bio->mirror_num,
8190 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008191 if (ret) {
8192 err = ret;
8193 goto next;
8194 }
8195
8196 wait_for_completion(&done.done);
8197
8198 if (!done.uptodate) {
8199 /* We might have another mirror, so try again */
8200 goto try_again;
8201 }
8202next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308203 offset += sectorsize;
8204 start += sectorsize;
8205
8206 ASSERT(nr_sectors);
8207
Liu Boc17acd22017-04-07 13:11:10 -07008208 nr_sectors--;
8209 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308210 pgoff += sectorsize;
Liu Boc17acd22017-04-07 13:11:10 -07008211 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308212 goto next_block;
8213 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008214 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008215
8216 return err;
8217}
8218
Miao Xie8b110e32014-09-12 18:44:03 +08008219static int btrfs_subio_endio_read(struct inode *inode,
8220 struct btrfs_io_bio *io_bio, int err)
8221{
8222 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8223
8224 if (skip_csum) {
8225 if (unlikely(err))
8226 return __btrfs_correct_data_nocsum(inode, io_bio);
8227 else
8228 return 0;
8229 } else {
8230 return __btrfs_subio_endio_read(inode, io_bio, err);
8231 }
8232}
8233
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008234static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008235{
8236 struct btrfs_dio_private *dip = bio->bi_private;
8237 struct inode *inode = dip->inode;
8238 struct bio *dio_bio;
8239 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008240 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008241
Miao Xie8b110e32014-09-12 18:44:03 +08008242 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8243 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008244
Josef Bacik4b46fce2010-05-23 11:00:55 -04008245 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008246 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008247 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008248
Josef Bacik4b46fce2010-05-23 11:00:55 -04008249 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008250
Filipe Manana1636d1d2016-02-15 16:20:26 +00008251 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008252 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008253
8254 if (io_bio->end_io)
8255 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008256 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008257}
8258
Filipe Manana14543772015-11-24 16:23:54 +00008259static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8260 const u64 offset,
8261 const u64 bytes,
8262 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008263{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008264 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008265 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008266 u64 ordered_offset = offset;
8267 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008268 int ret;
8269
Chris Mason163cf092010-11-28 19:56:33 -05008270again:
8271 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8272 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008273 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008274 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008275 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008276 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008277
Liu Bo9e0af232014-08-15 23:36:53 +08008278 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8279 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008280 btrfs_queue_work(root->fs_info->endio_write_workers,
8281 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008282out_test:
8283 /*
8284 * our bio might span multiple ordered extents. If we haven't
8285 * completed the accounting for the whole dio, go back and try again
8286 */
Filipe Manana14543772015-11-24 16:23:54 +00008287 if (ordered_offset < offset + bytes) {
8288 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008289 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008290 goto again;
8291 }
Filipe Manana14543772015-11-24 16:23:54 +00008292}
8293
8294static void btrfs_endio_direct_write(struct bio *bio)
8295{
8296 struct btrfs_dio_private *dip = bio->bi_private;
8297 struct bio *dio_bio = dip->dio_bio;
8298
8299 btrfs_endio_direct_write_update_ordered(dip->inode,
8300 dip->logical_offset,
8301 dip->bytes,
8302 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008303
Josef Bacik4b46fce2010-05-23 11:00:55 -04008304 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008305
Filipe Manana1636d1d2016-02-15 16:20:26 +00008306 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008307 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008308 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008309}
8310
Mike Christie81a75f62016-06-05 14:31:54 -05008311static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008312 struct bio *bio, int mirror_num,
8313 unsigned long bio_flags, u64 offset)
8314{
8315 int ret;
8316 struct btrfs_root *root = BTRFS_I(inode)->root;
8317 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008318 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008319 return 0;
8320}
8321
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008322static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008323{
8324 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008325 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008326
Miao Xie8b110e32014-09-12 18:44:03 +08008327 if (err)
8328 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008329 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008330 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008331 (unsigned long long)bio->bi_iter.bi_sector,
8332 bio->bi_iter.bi_size, err);
8333
8334 if (dip->subio_endio)
8335 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008336
8337 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008338 dip->errors = 1;
8339
8340 /*
8341 * before atomic variable goto zero, we must make sure
8342 * dip->errors is perceived to be set.
8343 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008344 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008345 }
8346
8347 /* if there are more bios still pending for this dio, just exit */
8348 if (!atomic_dec_and_test(&dip->pending_bios))
8349 goto out;
8350
Chris Mason9be33952013-05-17 18:30:14 -04008351 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008352 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008353 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008354 dip->dio_bio->bi_error = 0;
8355 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008356 }
8357out:
8358 bio_put(bio);
8359}
8360
8361static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8362 u64 first_sector, gfp_t gfp_flags)
8363{
Chris Masonda2f0f72015-07-02 13:57:22 -07008364 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008365 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008366 if (bio)
8367 bio_associate_current(bio);
8368 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008369}
8370
Miao Xiec1dc0892014-09-12 18:43:56 +08008371static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8372 struct inode *inode,
8373 struct btrfs_dio_private *dip,
8374 struct bio *bio,
8375 u64 file_offset)
8376{
8377 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8378 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8379 int ret;
8380
8381 /*
8382 * We load all the csum data we need when we submit
8383 * the first bio to reduce the csum tree search and
8384 * contention.
8385 */
8386 if (dip->logical_offset == file_offset) {
8387 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8388 file_offset);
8389 if (ret)
8390 return ret;
8391 }
8392
8393 if (bio == dip->orig_bio)
8394 return 0;
8395
8396 file_offset -= dip->logical_offset;
8397 file_offset >>= inode->i_sb->s_blocksize_bits;
8398 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8399
8400 return 0;
8401}
8402
Miao Xiee65e1532010-11-22 03:04:43 +00008403static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f62016-06-05 14:31:54 -05008404 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008405 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008406{
Miao Xiefacc8a222013-07-25 19:22:34 +08008407 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008408 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008409 struct btrfs_root *root = BTRFS_I(inode)->root;
8410 int ret;
8411
Josef Bacikb812ce22012-11-16 13:56:32 -05008412 if (async_submit)
8413 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8414
Miao Xiee65e1532010-11-22 03:04:43 +00008415 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008416
8417 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008418 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8419 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008420 if (ret)
8421 goto err;
8422 }
Miao Xiee65e1532010-11-22 03:04:43 +00008423
Josef Bacik1ae39932011-04-06 14:41:34 -04008424 if (skip_sum)
8425 goto map;
8426
8427 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008428 ret = btrfs_wq_submit_bio(root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -05008429 inode, bio, 0, 0, file_offset,
Miao Xiee65e1532010-11-22 03:04:43 +00008430 __btrfs_submit_bio_start_direct_io,
8431 __btrfs_submit_bio_done);
8432 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008433 } else if (write) {
8434 /*
8435 * If we aren't doing async submit, calculate the csum of the
8436 * bio now.
8437 */
8438 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8439 if (ret)
8440 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008441 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008442 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8443 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008444 if (ret)
8445 goto err;
8446 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008447map:
Mike Christie81a75f62016-06-05 14:31:54 -05008448 ret = btrfs_map_bio(root, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008449err:
8450 bio_put(bio);
8451 return ret;
8452}
8453
Mike Christie81a75f62016-06-05 14:31:54 -05008454static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008455 int skip_sum)
8456{
8457 struct inode *inode = dip->inode;
8458 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008459 struct bio *bio;
8460 struct bio *orig_bio = dip->orig_bio;
8461 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008462 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008463 u64 file_offset = dip->logical_offset;
8464 u64 submit_len = 0;
8465 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308466 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008467 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308468 int nr_sectors;
8469 int ret;
8470 int i;
Miao Xiee65e1532010-11-22 03:04:43 +00008471
Kent Overstreet4f024f32013-10-11 15:44:27 -07008472 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008473 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
8474 start_sector << 9, &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008475 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008476 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008477
Kent Overstreet4f024f32013-10-11 15:44:27 -07008478 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008479 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008480 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008481 goto submit;
8482 }
8483
David Woodhouse53b381b2013-01-29 18:40:14 -05008484 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008485 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008486 async_submit = 0;
8487 else
8488 async_submit = 1;
8489
Josef Bacik02f57c72011-04-06 14:25:44 -04008490 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8491 if (!bio)
8492 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008493
Bart Van Assche4382e332016-09-14 10:45:36 +02008494 bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
Josef Bacik02f57c72011-04-06 14:25:44 -04008495 bio->bi_private = dip;
8496 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008497 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008498 atomic_inc(&dip->pending_bios);
8499
Miao Xiee65e1532010-11-22 03:04:43 +00008500 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308501 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8502 i = 0;
8503next_block:
8504 if (unlikely(map_length < submit_len + blocksize ||
8505 bio_add_page(bio, bvec->bv_page, blocksize,
8506 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008507 /*
8508 * inc the count before we submit the bio so
8509 * we know the end IO handler won't happen before
8510 * we inc the count. Otherwise, the dip might get freed
8511 * before we're done setting it up
8512 */
8513 atomic_inc(&dip->pending_bios);
Mike Christie81a75f62016-06-05 14:31:54 -05008514 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008515 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008516 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008517 if (ret) {
8518 bio_put(bio);
8519 atomic_dec(&dip->pending_bios);
8520 goto out_err;
8521 }
8522
Miao Xiee65e1532010-11-22 03:04:43 +00008523 start_sector += submit_len >> 9;
8524 file_offset += submit_len;
8525
8526 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008527
8528 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8529 start_sector, GFP_NOFS);
8530 if (!bio)
8531 goto out_err;
Bart Van Assche4382e332016-09-14 10:45:36 +02008532 bio_set_op_attrs(bio, bio_op(orig_bio),
8533 bio_flags(orig_bio));
Miao Xiee65e1532010-11-22 03:04:43 +00008534 bio->bi_private = dip;
8535 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008536 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008537
Kent Overstreet4f024f32013-10-11 15:44:27 -07008538 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008539 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008540 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008541 &map_length, NULL, 0);
8542 if (ret) {
8543 bio_put(bio);
8544 goto out_err;
8545 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308546
8547 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008548 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308549 submit_len += blocksize;
8550 if (--nr_sectors) {
8551 i++;
8552 goto next_block;
8553 }
Miao Xiee65e1532010-11-22 03:04:43 +00008554 bvec++;
8555 }
8556 }
8557
Josef Bacik02f57c72011-04-06 14:25:44 -04008558submit:
Mike Christie81a75f62016-06-05 14:31:54 -05008559 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008560 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008561 if (!ret)
8562 return 0;
8563
8564 bio_put(bio);
8565out_err:
8566 dip->errors = 1;
8567 /*
8568 * before atomic variable goto zero, we must
8569 * make sure dip->errors is perceived to be set.
8570 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008571 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008572 if (atomic_dec_and_test(&dip->pending_bios))
8573 bio_io_error(dip->orig_bio);
8574
8575 /* bio_end_io() will handle error, so we needn't return it */
8576 return 0;
8577}
8578
Mike Christie8a4c1e42016-06-05 14:31:50 -05008579static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8580 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008581{
Filipe Manana61de7182015-07-01 12:13:10 +01008582 struct btrfs_dio_private *dip = NULL;
8583 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008584 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008585 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008586 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008587 int ret = 0;
8588
8589 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8590
Chris Mason9be33952013-05-17 18:30:14 -04008591 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008592 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008593 ret = -ENOMEM;
8594 goto free_ordered;
8595 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008596
Miao Xiec1dc0892014-09-12 18:43:56 +08008597 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008598 if (!dip) {
8599 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008600 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008601 }
8602
8603 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008604 dip->inode = inode;
8605 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008606 dip->bytes = dio_bio->bi_iter.bi_size;
8607 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008608 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008609 dip->orig_bio = io_bio;
8610 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008611 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008612 btrfs_bio = btrfs_io_bio(io_bio);
8613 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008614
Miao Xiec1dc0892014-09-12 18:43:56 +08008615 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008616 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008617 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008618 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008619 dip->subio_endio = btrfs_subio_endio_read;
8620 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621
Filipe Mananaf28a4922015-12-08 19:23:20 +00008622 /*
8623 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8624 * even if we fail to submit a bio, because in such case we do the
8625 * corresponding error handling below and it must not be done a second
8626 * time by btrfs_direct_IO().
8627 */
8628 if (write) {
8629 struct btrfs_dio_data *dio_data = current->journal_info;
8630
8631 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8632 dip->bytes;
8633 dio_data->unsubmitted_oe_range_start =
8634 dio_data->unsubmitted_oe_range_end;
8635 }
8636
Mike Christie81a75f62016-06-05 14:31:54 -05008637 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008638 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008639 return;
Chris Mason9be33952013-05-17 18:30:14 -04008640
Miao Xie23ea8e52014-09-12 18:43:54 +08008641 if (btrfs_bio->end_io)
8642 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008643
Josef Bacik4b46fce2010-05-23 11:00:55 -04008644free_ordered:
8645 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008646 * If we arrived here it means either we failed to submit the dip
8647 * or we either failed to clone the dio_bio or failed to allocate the
8648 * dip. If we cloned the dio_bio and allocated the dip, we can just
8649 * call bio_endio against our io_bio so that we get proper resource
8650 * cleanup if we fail to submit the dip, otherwise, we must do the
8651 * same as btrfs_endio_direct_[write|read] because we can't call these
8652 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008653 */
Filipe Manana61de7182015-07-01 12:13:10 +01008654 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008655 io_bio->bi_error = -EIO;
8656 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008657 /*
8658 * The end io callbacks free our dip, do the final put on io_bio
8659 * and all the cleanup and final put for dio_bio (through
8660 * dio_end_io()).
8661 */
8662 dip = NULL;
8663 io_bio = NULL;
8664 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008665 if (write)
8666 btrfs_endio_direct_write_update_ordered(inode,
8667 file_offset,
8668 dio_bio->bi_iter.bi_size,
8669 0);
8670 else
Filipe Manana61de7182015-07-01 12:13:10 +01008671 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8672 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008673
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008674 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008675 /*
8676 * Releases and cleans up our dio_bio, no need to bio_put()
8677 * nor bio_endio()/bio_io_error() against dio_bio.
8678 */
8679 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008680 }
Filipe Manana61de7182015-07-01 12:13:10 +01008681 if (io_bio)
8682 bio_put(io_bio);
8683 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008684}
8685
Omar Sandoval6f673762015-03-16 04:33:52 -07008686static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008687 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008688{
8689 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008690 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008691 unsigned blocksize_mask = root->sectorsize - 1;
8692 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008693
8694 if (offset & blocksize_mask)
8695 goto out;
8696
Al Viro28060d52014-03-22 05:15:17 -04008697 if (iov_iter_alignment(iter) & blocksize_mask)
8698 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008699
Al Viro28060d52014-03-22 05:15:17 -04008700 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008701 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008702 return 0;
8703 /*
8704 * Check to make sure we don't have duplicate iov_base's in this
8705 * iovec, if so return EINVAL, otherwise we'll get csum errors
8706 * when reading back.
8707 */
8708 for (seg = 0; seg < iter->nr_segs; seg++) {
8709 for (i = seg + 1; i < iter->nr_segs; i++) {
8710 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008711 goto out;
8712 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008713 }
8714 retval = 0;
8715out:
8716 return retval;
8717}
Josef Bacikeb838e72012-07-31 16:28:48 -04008718
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008719static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008720{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008721 struct file *file = iocb->ki_filp;
8722 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308723 struct btrfs_root *root = BTRFS_I(inode)->root;
8724 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008725 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008726 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008727 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008728 bool wakeup = true;
8729 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008730 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008731
Omar Sandoval6f673762015-03-16 04:33:52 -07008732 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008733 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008734
Jens Axboefe0f07d2015-04-15 17:05:48 -06008735 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008736 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008737
Josef Bacik0e267c42013-07-02 10:38:02 -04008738 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008739 * The generic stuff only does filemap_write_and_wait_range, which
8740 * isn't enough if we've written compressed pages to this area, so
8741 * we need to flush the dirty pages again to make absolutely sure
8742 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008743 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008744 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008745 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8746 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008747 filemap_fdatawrite_range(inode->i_mapping, offset,
8748 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008749
Omar Sandoval6f673762015-03-16 04:33:52 -07008750 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008751 /*
8752 * If the write DIO is beyond the EOF, we need update
8753 * the isize, but it is protected by i_mutex. So we can
8754 * not unlock the i_mutex at this case.
8755 */
8756 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008757 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008758 relock = true;
8759 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008760 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008761 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008762 goto out;
chandan50745b02015-08-28 21:10:13 +05308763 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008764 BTRFS_MAX_EXTENT_SIZE - 1,
8765 BTRFS_MAX_EXTENT_SIZE);
8766
8767 /*
8768 * We need to know how many extents we reserved so that we can
8769 * do the accounting properly if we go over the number we
8770 * originally calculated. Abuse current->journal_info for this.
8771 */
chandan50745b02015-08-28 21:10:13 +05308772 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008773 dio_data.unsubmitted_oe_range_start = (u64)offset;
8774 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308775 current->journal_info = &dio_data;
Chandan Rajendrae8b50682016-12-23 15:00:18 +05308776 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008777 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8778 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008779 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008780 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8781 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008782 }
8783
Omar Sandoval17f8c842015-03-16 04:33:50 -07008784 ret = __blockdev_direct_IO(iocb, inode,
8785 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008786 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008787 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008788 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendrae8b50682016-12-23 15:00:18 +05308789 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008790 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008791 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308792 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008793 btrfs_delalloc_release_space(inode, offset,
8794 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008795 /*
8796 * On error we might have left some ordered extents
8797 * without submitting corresponding bios for them, so
8798 * cleanup them up to avoid other tasks getting them
8799 * and waiting for them to complete forever.
8800 */
8801 if (dio_data.unsubmitted_oe_range_start <
8802 dio_data.unsubmitted_oe_range_end)
8803 btrfs_endio_direct_write_update_ordered(inode,
8804 dio_data.unsubmitted_oe_range_start,
8805 dio_data.unsubmitted_oe_range_end -
8806 dio_data.unsubmitted_oe_range_start,
8807 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008808 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008809 btrfs_delalloc_release_space(inode, offset,
8810 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008811 }
Miao Xie38851cc2013-02-08 07:04:11 +00008812out:
Miao Xie2e60a512013-02-08 07:01:08 +00008813 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008814 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008815 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008816 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008817
8818 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008819}
8820
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008821#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8822
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008823static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8824 __u64 start, __u64 len)
8825{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008826 int ret;
8827
8828 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8829 if (ret)
8830 return ret;
8831
Chris Masonec29ed52011-02-23 16:23:20 -05008832 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008833}
8834
Chris Mason9ebefb182007-06-15 13:50:00 -04008835int btrfs_readpage(struct file *file, struct page *page)
8836{
Chris Masond1310b22008-01-24 16:13:08 -05008837 struct extent_io_tree *tree;
8838 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008839 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008840}
Chris Mason1832a6d2007-12-21 16:27:21 -05008841
Chris Mason39279cc2007-06-12 06:35:45 -04008842static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8843{
Chris Masond1310b22008-01-24 16:13:08 -05008844 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008845 struct inode *inode = page->mapping->host;
8846 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008847
8848 if (current->flags & PF_MEMALLOC) {
8849 redirty_page_for_writepage(wbc, page);
8850 unlock_page(page);
8851 return 0;
8852 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008853
8854 /*
8855 * If we are under memory pressure we will call this directly from the
8856 * VM, we need to make sure we have the inode referenced for the ordered
8857 * extent. If not just return like we didn't do anything.
8858 */
8859 if (!igrab(inode)) {
8860 redirty_page_for_writepage(wbc, page);
8861 return AOP_WRITEPAGE_ACTIVATE;
8862 }
Chris Masond1310b22008-01-24 16:13:08 -05008863 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008864 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8865 btrfs_add_delayed_iput(inode);
8866 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008867}
Chris Mason39279cc2007-06-12 06:35:45 -04008868
Eric Sandeen48a3b632013-04-25 20:41:01 +00008869static int btrfs_writepages(struct address_space *mapping,
8870 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008871{
Chris Masond1310b22008-01-24 16:13:08 -05008872 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008873
Chris Masond1310b22008-01-24 16:13:08 -05008874 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008875 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8876}
8877
Chris Mason3ab2fb52007-11-08 10:59:22 -05008878static int
8879btrfs_readpages(struct file *file, struct address_space *mapping,
8880 struct list_head *pages, unsigned nr_pages)
8881{
Chris Masond1310b22008-01-24 16:13:08 -05008882 struct extent_io_tree *tree;
8883 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008884 return extent_readpages(tree, mapping, pages, nr_pages,
8885 btrfs_get_extent);
8886}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008887static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008888{
Chris Masond1310b22008-01-24 16:13:08 -05008889 struct extent_io_tree *tree;
8890 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008891 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008892
Chris Masond1310b22008-01-24 16:13:08 -05008893 tree = &BTRFS_I(page->mapping->host)->io_tree;
8894 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008895 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008896 if (ret == 1) {
8897 ClearPagePrivate(page);
8898 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008899 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008900 }
8901 return ret;
8902}
Chris Mason39279cc2007-06-12 06:35:45 -04008903
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8905{
Chris Mason98509cf2008-09-11 15:51:43 -04008906 if (PageWriteback(page) || PageDirty(page))
8907 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008908 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008909}
8910
Lukas Czernerd47992f2013-05-21 23:17:23 -04008911static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8912 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008913{
Josef Bacik5fd02042012-05-02 14:00:54 -04008914 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008915 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008917 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008918 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008919 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308920 u64 start;
8921 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008922 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008923
Chris Mason8b62b722009-09-02 16:53:46 -04008924 /*
8925 * we have the page locked, so new writeback can't start,
8926 * and the dirty bit won't be cleared while we are here.
8927 *
8928 * Wait for IO on this page so that we can safely clear
8929 * the PagePrivate2 bit and do ordered accounting
8930 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008931 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008932
Josef Bacik5fd02042012-05-02 14:00:54 -04008933 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934 if (offset) {
8935 btrfs_releasepage(page, GFP_NOFS);
8936 return;
8937 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008938
8939 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008940 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308941again:
8942 start = page_start;
8943 ordered = btrfs_lookup_ordered_range(inode, start,
8944 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308946 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008947 /*
8948 * IO on this page will never be started, so we need
8949 * to account for any ordered extents now
8950 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008951 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308952 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008953 EXTENT_DIRTY | EXTENT_DELALLOC |
8954 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8955 EXTENT_DEFRAG, 1, 0, &cached_state,
8956 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008957 /*
8958 * whoever cleared the private bit is responsible
8959 * for the finish_ordered_io
8960 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008961 if (TestClearPagePrivate2(page)) {
8962 struct btrfs_ordered_inode_tree *tree;
8963 u64 new_len;
8964
8965 tree = &BTRFS_I(inode)->ordered_tree;
8966
8967 spin_lock_irq(&tree->lock);
8968 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308969 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008970 if (new_len < ordered->truncated_len)
8971 ordered->truncated_len = new_len;
8972 spin_unlock_irq(&tree->lock);
8973
8974 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308975 start,
8976 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008977 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008978 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008979 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008980 if (!inode_evicting) {
8981 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308982 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008983 &cached_state);
8984 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308985
8986 start = end + 1;
8987 if (start < page_end)
8988 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008989 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008990
Qu Wenruob9d0b382015-09-29 10:35:16 +08008991 /*
8992 * Qgroup reserved space handler
8993 * Page here will be either
8994 * 1) Already written to disk
8995 * In this case, its reserved space is released from data rsv map
8996 * and will be freed by delayed_ref handler finally.
8997 * So even we call qgroup_free_data(), it won't decrease reserved
8998 * space.
8999 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009000 * This means the reserved space should be freed here. However,
9001 * if a truncate invalidates the page (by clearing PageDirty)
9002 * and the page is accounted for while allocating extent
9003 * in btrfs_check_data_free_space() we let delayed_ref to
9004 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009005 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009006 if (PageDirty(page))
9007 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009008 if (!inode_evicting) {
9009 clear_extent_bit(tree, page_start, page_end,
9010 EXTENT_LOCKED | EXTENT_DIRTY |
9011 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9012 EXTENT_DEFRAG, 1, 1,
9013 &cached_state, GFP_NOFS);
9014
9015 __btrfs_releasepage(page, GFP_NOFS);
9016 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017
Chris Mason4a096752008-07-21 10:29:44 -04009018 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009019 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009020 ClearPagePrivate(page);
9021 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009022 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009023 }
Chris Mason39279cc2007-06-12 06:35:45 -04009024}
9025
Chris Mason9ebefb182007-06-15 13:50:00 -04009026/*
9027 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9028 * called from a page fault handler when a page is first dirtied. Hence we must
9029 * be careful to check for EOF conditions here. We set the page up correctly
9030 * for a written page which means we get ENOSPC checking when writing into
9031 * holes and correct delalloc and unwritten extent mapping on filesystems that
9032 * support these features.
9033 *
9034 * We are not allowed to take the i_mutex here so we have to play games to
9035 * protect against truncate races as the page could now be beyond EOF. Because
9036 * vmtruncate() writes the inode size before removing pages, once we have the
9037 * page lock we can determine safely if the page is beyond EOF. If it is not
9038 * beyond EOF, then the page is guaranteed safe against truncation until we
9039 * unlock the page.
9040 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07009041int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009042{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009043 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05009044 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05009045 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009046 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9047 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009048 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009049 char *kaddr;
9050 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009051 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009052 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009053 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309054 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009055 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009056 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309057 u64 end;
9058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009059 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009060
Jan Karab2b5ef52012-06-12 16:20:45 +02009061 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009062 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009063 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309064 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009065
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309066 /*
9067 * Reserving delalloc space after obtaining the page lock can lead to
9068 * deadlock. For example, if a dirty page is locked by this function
9069 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9070 * dirty page write out, then the btrfs_writepage() function could
9071 * end up waiting indefinitely to get a lock on the page currently
9072 * being processed by btrfs_page_mkwrite() function.
9073 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009074 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309075 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009076 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009077 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009078 reserved = 1;
9079 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009080 if (ret) {
9081 if (ret == -ENOMEM)
9082 ret = VM_FAULT_OOM;
9083 else /* -ENOSPC, -EIO, etc */
9084 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009085 if (reserved)
9086 goto out;
9087 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009088 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009089
Nick Piggin56a76f82009-03-31 15:23:23 -07009090 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009091again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009092 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009093 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009094
Chris Mason9ebefb182007-06-15 13:50:00 -04009095 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009096 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009097 /* page got truncated out from underneath us */
9098 goto out_unlock;
9099 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009100 wait_on_page_writeback(page);
9101
David Sterbaff13db42015-12-03 14:30:40 +01009102 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009103 set_page_extent_mapped(page);
9104
Chris Masoneb84ae02008-07-17 13:53:27 -04009105 /*
9106 * we can't set the delalloc bits if there are pending ordered
9107 * extents. Drop our locks and wait for them to finish
9108 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309109 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009110 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009111 unlock_extent_cached(io_tree, page_start, page_end,
9112 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009113 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009114 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009115 btrfs_put_ordered_extent(ordered);
9116 goto again;
9117 }
9118
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009119 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309120 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009121 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309122 end = page_start + reserved_space - 1;
9123 spin_lock(&BTRFS_I(inode)->lock);
9124 BTRFS_I(inode)->outstanding_extents++;
9125 spin_unlock(&BTRFS_I(inode)->lock);
9126 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009127 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309128 }
9129 }
9130
Josef Bacikfbf19082009-10-01 17:10:23 -04009131 /*
9132 * XXX - page_mkwrite gets called every time the page is dirtied, even
9133 * if it was already dirty, so for space accounting reasons we need to
9134 * clear any delalloc bits for the range we are fixing to save. There
9135 * is probably a better way to do this, but for now keep consistent with
9136 * prepare_pages in the normal write path.
9137 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309138 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009139 EXTENT_DIRTY | EXTENT_DELALLOC |
9140 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009141 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009142
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309143 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009144 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009145 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009146 unlock_extent_cached(io_tree, page_start, page_end,
9147 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009148 ret = VM_FAULT_SIGBUS;
9149 goto out_unlock;
9150 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009151 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009152
9153 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009154 if (page_start + PAGE_SIZE > size)
9155 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009156 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009157 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009158
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009159 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009160 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009161 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009162 flush_dcache_page(page);
9163 kunmap(page);
9164 }
Chris Mason247e7432008-07-17 12:53:51 -04009165 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009166 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009167 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009168
Chris Mason257c62e2009-10-13 13:21:08 -04009169 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9170 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009171 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009172
Josef Bacik2ac55d42010-02-03 19:33:23 +00009173 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009174
9175out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009176 if (!ret) {
9177 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009178 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009179 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009180 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009181out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309182 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009183out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009184 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009185 return ret;
9186}
9187
Josef Bacika41ad392011-01-31 15:30:16 -05009188static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009189{
9190 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009191 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009192 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009193 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009194 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009195 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009196 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009197
Josef Bacik0ef8b722013-10-25 16:13:35 -04009198 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9199 (u64)-1);
9200 if (ret)
9201 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009202
Josef Bacikfcb80c22011-05-03 10:40:22 -04009203 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009204 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009205 * 3 things going on here
9206 *
9207 * 1) We need to reserve space for our orphan item and the space to
9208 * delete our orphan item. Lord knows we don't want to have a dangling
9209 * orphan item because we didn't reserve space to remove it.
9210 *
9211 * 2) We need to reserve space to update our inode.
9212 *
9213 * 3) We need to have something to cache all the space that is going to
9214 * be free'd up by the truncate operation, but also have some slack
9215 * space reserved in case it uses space during the truncate (thank you
9216 * very much snapshotting).
9217 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009218 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009220 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009221 * doesn't end up using space reserved for updating the inode or
9222 * removing the orphan item. We also need to be able to stop the
9223 * transaction and start a new one, which means we need to be able to
9224 * update the inode several times, and we have no idea of knowing how
9225 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009226 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009227 * Then there is the orphan item, which does indeed need to be held on
9228 * to for the whole operation, and we need nobody to touch this reserved
9229 * space except the orphan code.
9230 *
9231 * So that leaves us with
9232 *
9233 * 1) root->orphan_block_rsv - for the orphan deletion.
9234 * 2) rsv - for the truncate reservation, which we will steal from the
9235 * transaction reservation.
9236 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9237 * updating the inode.
9238 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009239 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009240 if (!rsv)
9241 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009242 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009243 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009244
Josef Bacik907cbce2011-08-08 13:46:15 -04009245 /*
Josef Bacik07127182011-08-19 10:29:59 -04009246 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009247 * 1 for updating the inode.
9248 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009249 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250 if (IS_ERR(trans)) {
9251 err = PTR_ERR(trans);
9252 goto out;
9253 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009254
Josef Bacik907cbce2011-08-08 13:46:15 -04009255 /* Migrate the slack space for the truncate to our reserve */
9256 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009257 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009258 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009259
Chris Mason5a3f23d2009-03-31 13:27:11 -04009260 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009261 * So if we truncate and then write and fsync we normally would just
9262 * write the extents that changed, which is a problem if we need to
9263 * first truncate that entire inode. So set this flag so we write out
9264 * all of the extents in the inode to the sync log so we're completely
9265 * safe.
9266 */
9267 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009268 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009269
Yan, Zheng80825102009-11-12 09:35:36 +00009270 while (1) {
9271 ret = btrfs_truncate_inode_items(trans, root, inode,
9272 inode->i_size,
9273 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009274 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009275 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009276 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009277 }
Chris Mason39279cc2007-06-12 06:35:45 -04009278
Josef Bacikfcb80c22011-05-03 10:40:22 -04009279 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009280 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009281 if (ret) {
9282 err = ret;
9283 break;
9284 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009285
Yan, Zheng80825102009-11-12 09:35:36 +00009286 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009287 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009288
9289 trans = btrfs_start_transaction(root, 2);
9290 if (IS_ERR(trans)) {
9291 ret = err = PTR_ERR(trans);
9292 trans = NULL;
9293 break;
9294 }
9295
9296 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009297 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009298 BUG_ON(ret); /* shouldn't happen */
9299 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009300 }
9301
9302 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009303 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009304 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009305 if (ret)
9306 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009307 }
9308
Chris Mason917c16b2011-11-08 14:49:59 -05009309 if (trans) {
9310 trans->block_rsv = &root->fs_info->trans_block_rsv;
9311 ret = btrfs_update_inode(trans, root, inode);
9312 if (ret && !err)
9313 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009314
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009315 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009316 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009317 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009318out:
9319 btrfs_free_block_rsv(root, rsv);
9320
Josef Bacik3893e332011-01-31 16:03:11 -05009321 if (ret && !err)
9322 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009323
Josef Bacik3893e332011-01-31 16:03:11 -05009324 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009325}
9326
Sven Wegener3b963622008-06-09 21:57:42 -04009327/*
Chris Masond352ac62008-09-29 15:18:18 -04009328 * create a new subvolume directory/inode (helper for the ioctl).
9329 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009330int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009331 struct btrfs_root *new_root,
9332 struct btrfs_root *parent_root,
9333 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009334{
Chris Mason39279cc2007-06-12 06:35:45 -04009335 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009336 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009337 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009338
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009339 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9340 new_dirid, new_dirid,
9341 S_IFDIR | (~current_umask() & S_IRWXUGO),
9342 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009343 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009344 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009345 inode->i_op = &btrfs_dir_inode_operations;
9346 inode->i_fop = &btrfs_dir_file_operations;
9347
Miklos Szeredibfe86842011-10-28 14:13:29 +02009348 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009349 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009350 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009351
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009352 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9353 if (err)
9354 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009355 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009356 new_root->root_key.objectid, err);
9357
Yan, Zheng76dda932009-09-21 16:00:26 -04009358 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009359
Yan, Zheng76dda932009-09-21 16:00:26 -04009360 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009361 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009362}
9363
Chris Mason39279cc2007-06-12 06:35:45 -04009364struct inode *btrfs_alloc_inode(struct super_block *sb)
9365{
9366 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009367 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009368
9369 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9370 if (!ei)
9371 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009372
9373 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009374 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009375 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009376 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009377 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009378 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009379 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009380 ei->disk_i_size = 0;
9381 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009382 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009383 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009384 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009385 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009386 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009387 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009388
Josef Bacik9e0baf62011-07-15 15:16:44 +00009389 spin_lock_init(&ei->lock);
9390 ei->outstanding_extents = 0;
9391 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009392
Josef Bacik72ac3c02012-05-23 14:13:11 -04009393 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009394 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009395
Miao Xie16cdcec2011-04-22 18:12:22 +08009396 ei->delayed_node = NULL;
9397
chandan r9cc97d62012-07-04 12:48:07 +05309398 ei->i_otime.tv_sec = 0;
9399 ei->i_otime.tv_nsec = 0;
9400
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009401 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009402 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009403 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9404 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009405 ei->io_tree.track_uptodate = 1;
9406 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009407 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009408 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009409 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009410 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009411 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009412 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009414 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009415
9416 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009417}
9418
Josef Bacikaaedb552013-10-11 14:44:09 -04009419#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9420void btrfs_test_destroy_inode(struct inode *inode)
9421{
9422 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9423 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9424}
9425#endif
9426
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009427static void btrfs_i_callback(struct rcu_head *head)
9428{
9429 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009430 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9431}
9432
Chris Mason39279cc2007-06-12 06:35:45 -04009433void btrfs_destroy_inode(struct inode *inode)
9434{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009435 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009436 struct btrfs_root *root = BTRFS_I(inode)->root;
9437
Al Virob3d9b7a2012-06-09 13:51:19 -04009438 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009439 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009440 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9441 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009442 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9443 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009444 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009445
Chris Mason5a3f23d2009-03-31 13:27:11 -04009446 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009447 * This can happen where we create an inode, but somebody else also
9448 * created the same inode and we need to destroy the one we already
9449 * created.
9450 */
9451 if (!root)
9452 goto free;
9453
Josef Bacik8a35d952012-05-23 14:26:42 -04009454 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9455 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009456 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009457 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009458 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009459 }
Josef Bacik7b128762008-07-24 12:17:14 -04009460
Chris Masond3977122009-01-05 21:25:51 -05009461 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009462 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9463 if (!ordered)
9464 break;
9465 else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009466 btrfs_err(root->fs_info,
9467 "found ordered extent %llu %llu on inode cleanup",
9468 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009469 btrfs_remove_ordered_extent(inode, ordered);
9470 btrfs_put_ordered_extent(ordered);
9471 btrfs_put_ordered_extent(ordered);
9472 }
9473 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009474 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009475 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009476 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009477free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009478 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009479}
9480
Al Viro45321ac2010-06-07 13:43:19 -04009481int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009482{
9483 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009484
Naohiro Aota6379ef92013-06-06 09:56:34 +00009485 if (root == NULL)
9486 return 1;
9487
Liu Bofa6ac872013-02-20 14:10:23 +00009488 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009489 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009490 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009491 else
Al Viro45321ac2010-06-07 13:43:19 -04009492 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009493}
9494
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009495static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009496{
9497 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9498
9499 inode_init_once(&ei->vfs_inode);
9500}
9501
9502void btrfs_destroy_cachep(void)
9503{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009504 /*
9505 * Make sure all delayed rcu free inodes are flushed before we
9506 * destroy cache.
9507 */
9508 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009509 kmem_cache_destroy(btrfs_inode_cachep);
9510 kmem_cache_destroy(btrfs_trans_handle_cachep);
9511 kmem_cache_destroy(btrfs_transaction_cachep);
9512 kmem_cache_destroy(btrfs_path_cachep);
9513 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009514}
9515
9516int btrfs_init_cachep(void)
9517{
David Sterba837e1972012-09-07 03:00:48 -06009518 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009519 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009520 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9521 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009522 if (!btrfs_inode_cachep)
9523 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009524
David Sterba837e1972012-09-07 03:00:48 -06009525 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009526 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009527 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009528 if (!btrfs_trans_handle_cachep)
9529 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009530
David Sterba837e1972012-09-07 03:00:48 -06009531 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009532 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009533 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009534 if (!btrfs_transaction_cachep)
9535 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009536
David Sterba837e1972012-09-07 03:00:48 -06009537 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009538 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009539 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009540 if (!btrfs_path_cachep)
9541 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009542
David Sterba837e1972012-09-07 03:00:48 -06009543 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009544 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009545 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009546 if (!btrfs_free_space_cachep)
9547 goto fail;
9548
Chris Mason39279cc2007-06-12 06:35:45 -04009549 return 0;
9550fail:
9551 btrfs_destroy_cachep();
9552 return -ENOMEM;
9553}
9554
9555static int btrfs_getattr(struct vfsmount *mnt,
9556 struct dentry *dentry, struct kstat *stat)
9557{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009558 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009559 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009560 u32 blocksize = inode->i_sb->s_blocksize;
9561
Chris Mason39279cc2007-06-12 06:35:45 -04009562 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009563 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009564
9565 spin_lock(&BTRFS_I(inode)->lock);
9566 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9567 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009568 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009569 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009570 return 0;
9571}
9572
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573static int btrfs_rename_exchange(struct inode *old_dir,
9574 struct dentry *old_dentry,
9575 struct inode *new_dir,
9576 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009577{
9578 struct btrfs_trans_handle *trans;
9579 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009580 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 struct inode *new_inode = new_dentry->d_inode;
9582 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009583 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009584 struct dentry *parent;
9585 u64 old_ino = btrfs_ino(old_inode);
9586 u64 new_ino = btrfs_ino(new_inode);
9587 u64 old_idx = 0;
9588 u64 new_idx = 0;
9589 u64 root_objectid;
9590 int ret;
Filipe Manana77c82912018-06-11 19:24:16 +01009591 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009592 bool root_log_pinned = false;
9593 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594
9595 /* we only allow rename subvolume link between subvolumes */
9596 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9597 return -EXDEV;
9598
9599 /* close the race window with snapshot create/destroy ioctl */
9600 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9601 down_read(&root->fs_info->subvol_sem);
9602 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9603 down_read(&dest->fs_info->subvol_sem);
9604
9605 /*
9606 * We want to reserve the absolute worst case amount of items. So if
9607 * both inodes are subvols and we need to unlink them then that would
9608 * require 4 item modifications, but if they are both normal inodes it
9609 * would require 5 item modifications, so we'll assume their normal
9610 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9611 * should cover the worst case number of items we'll modify.
9612 */
9613 trans = btrfs_start_transaction(root, 12);
9614 if (IS_ERR(trans)) {
9615 ret = PTR_ERR(trans);
9616 goto out_notrans;
9617 }
9618
9619 /*
9620 * We need to find a free sequence number both in the source and
9621 * in the destination directory for the exchange.
9622 */
9623 ret = btrfs_set_inode_index(new_dir, &old_idx);
9624 if (ret)
9625 goto out_fail;
9626 ret = btrfs_set_inode_index(old_dir, &new_idx);
9627 if (ret)
9628 goto out_fail;
9629
9630 BTRFS_I(old_inode)->dir_index = 0ULL;
9631 BTRFS_I(new_inode)->dir_index = 0ULL;
9632
9633 /* Reference for the source. */
9634 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9635 /* force full log commit if subvolume involved. */
9636 btrfs_set_log_full_commit(root->fs_info, trans);
9637 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009638 btrfs_pin_log_trans(root);
9639 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 ret = btrfs_insert_inode_ref(trans, dest,
9641 new_dentry->d_name.name,
9642 new_dentry->d_name.len,
9643 old_ino,
9644 btrfs_ino(new_dir), old_idx);
9645 if (ret)
9646 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 }
9648
9649 /* And now for the dest. */
9650 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9651 /* force full log commit if subvolume involved. */
9652 btrfs_set_log_full_commit(dest->fs_info, trans);
9653 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009654 btrfs_pin_log_trans(dest);
9655 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 ret = btrfs_insert_inode_ref(trans, root,
9657 old_dentry->d_name.name,
9658 old_dentry->d_name.len,
9659 new_ino,
9660 btrfs_ino(old_dir), new_idx);
9661 if (ret)
9662 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 }
9664
9665 /* Update inode version and ctime/mtime. */
9666 inode_inc_iversion(old_dir);
9667 inode_inc_iversion(new_dir);
9668 inode_inc_iversion(old_inode);
9669 inode_inc_iversion(new_inode);
9670 old_dir->i_ctime = old_dir->i_mtime = ctime;
9671 new_dir->i_ctime = new_dir->i_mtime = ctime;
9672 old_inode->i_ctime = ctime;
9673 new_inode->i_ctime = ctime;
9674
9675 if (old_dentry->d_parent != new_dentry->d_parent) {
9676 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9677 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9678 }
9679
9680 /* src is a subvolume */
9681 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9682 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9683 ret = btrfs_unlink_subvol(trans, root, old_dir,
9684 root_objectid,
9685 old_dentry->d_name.name,
9686 old_dentry->d_name.len);
9687 } else { /* src is an inode */
9688 ret = __btrfs_unlink_inode(trans, root, old_dir,
9689 old_dentry->d_inode,
9690 old_dentry->d_name.name,
9691 old_dentry->d_name.len);
9692 if (!ret)
9693 ret = btrfs_update_inode(trans, root, old_inode);
9694 }
9695 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009696 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697 goto out_fail;
9698 }
9699
9700 /* dest is a subvolume */
9701 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9702 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9703 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9704 root_objectid,
9705 new_dentry->d_name.name,
9706 new_dentry->d_name.len);
9707 } else { /* dest is an inode */
9708 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9709 new_dentry->d_inode,
9710 new_dentry->d_name.name,
9711 new_dentry->d_name.len);
9712 if (!ret)
9713 ret = btrfs_update_inode(trans, dest, new_inode);
9714 }
9715 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009716 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 goto out_fail;
9718 }
9719
9720 ret = btrfs_add_link(trans, new_dir, old_inode,
9721 new_dentry->d_name.name,
9722 new_dentry->d_name.len, 0, old_idx);
9723 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009724 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 goto out_fail;
9726 }
9727
9728 ret = btrfs_add_link(trans, old_dir, new_inode,
9729 old_dentry->d_name.name,
9730 old_dentry->d_name.len, 0, new_idx);
9731 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009732 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 goto out_fail;
9734 }
9735
9736 if (old_inode->i_nlink == 1)
9737 BTRFS_I(old_inode)->dir_index = old_idx;
9738 if (new_inode->i_nlink == 1)
9739 BTRFS_I(new_inode)->dir_index = new_idx;
9740
Filipe Manana86e8aa02016-05-05 02:02:27 +01009741 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009742 parent = new_dentry->d_parent;
9743 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9744 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009745 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009747 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 parent = old_dentry->d_parent;
9749 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9750 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009751 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752 }
9753out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009754 /*
9755 * If we have pinned a log and an error happened, we unpin tasks
9756 * trying to sync the log and force them to fallback to a transaction
9757 * commit if the log currently contains any of the inodes involved in
9758 * this rename operation (to ensure we do not persist a log with an
9759 * inconsistent state for any of these inodes or leading to any
9760 * inconsistencies when replayed). If the transaction was aborted, the
9761 * abortion reason is propagated to userspace when attempting to commit
9762 * the transaction. If the log does not contain any of these inodes, we
9763 * allow the tasks to sync it.
9764 */
9765 if (ret && (root_log_pinned || dest_log_pinned)) {
9766 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9767 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9768 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9769 (new_inode &&
9770 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9771 btrfs_set_log_full_commit(root->fs_info, trans);
9772
9773 if (root_log_pinned) {
9774 btrfs_end_log_trans(root);
9775 root_log_pinned = false;
9776 }
9777 if (dest_log_pinned) {
9778 btrfs_end_log_trans(dest);
9779 dest_log_pinned = false;
9780 }
9781 }
Filipe Manana77c82912018-06-11 19:24:16 +01009782 ret2 = btrfs_end_transaction(trans, root);
9783 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009784out_notrans:
9785 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9786 up_read(&dest->fs_info->subvol_sem);
9787 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9788 up_read(&root->fs_info->subvol_sem);
9789
9790 return ret;
9791}
9792
9793static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9794 struct btrfs_root *root,
9795 struct inode *dir,
9796 struct dentry *dentry)
9797{
9798 int ret;
9799 struct inode *inode;
9800 u64 objectid;
9801 u64 index;
9802
9803 ret = btrfs_find_free_ino(root, &objectid);
9804 if (ret)
9805 return ret;
9806
9807 inode = btrfs_new_inode(trans, root, dir,
9808 dentry->d_name.name,
9809 dentry->d_name.len,
9810 btrfs_ino(dir),
9811 objectid,
9812 S_IFCHR | WHITEOUT_MODE,
9813 &index);
9814
9815 if (IS_ERR(inode)) {
9816 ret = PTR_ERR(inode);
9817 return ret;
9818 }
9819
9820 inode->i_op = &btrfs_special_inode_operations;
9821 init_special_inode(inode, inode->i_mode,
9822 WHITEOUT_DEV);
9823
9824 ret = btrfs_init_inode_security(trans, inode, dir,
9825 &dentry->d_name);
9826 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009827 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009828
9829 ret = btrfs_add_nondir(trans, dir, dentry,
9830 inode, 0, index);
9831 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009832 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009833
9834 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009835out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009836 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009837 if (ret)
9838 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009839 iput(inode);
9840
Filipe Mananac9901612016-05-05 01:41:57 +01009841 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009842}
9843
Chris Mason39279cc2007-06-12 06:35:45 -04009844static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845 struct inode *new_dir, struct dentry *new_dentry,
9846 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009847{
9848 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009849 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009850 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9851 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009852 struct inode *new_inode = d_inode(new_dentry);
9853 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009854 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009855 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009856 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009857 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009858 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009859
Li Zefan33345d012011-04-20 10:31:50 +08009860 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009861 return -EPERM;
9862
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009863 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009864 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009865 return -EXDEV;
9866
Li Zefan33345d012011-04-20 10:31:50 +08009867 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9868 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 return -ENOTEMPTY;
9870
Chris Mason39279cc2007-06-12 06:35:45 -04009871 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009873 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009874
9875
9876 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009877 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009878 new_dentry->d_name.name,
9879 new_dentry->d_name.len);
9880
9881 if (ret) {
9882 if (ret == -EEXIST) {
9883 /* we shouldn't get
9884 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309885 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009886 return ret;
9887 }
9888 } else {
9889 /* maybe -EOVERFLOW */
9890 return ret;
9891 }
9892 }
9893 ret = 0;
9894
Chris Mason5a3f23d2009-03-31 13:27:11 -04009895 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009896 * we're using rename to replace one file with another. Start IO on it
9897 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009898 */
Chris Mason8d875f92014-08-12 10:47:42 -07009899 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009900 filemap_flush(old_inode->i_mapping);
9901
Yan, Zheng76dda932009-09-21 16:00:26 -04009902 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009903 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009904 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009905 /*
9906 * We want to reserve the absolute worst case amount of items. So if
9907 * both inodes are subvols and we need to unlink them then that would
9908 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009909 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009910 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9911 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009912 * If our rename has the whiteout flag, we need more 5 units for the
9913 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9914 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009915 */
Filipe Manana5062af32016-05-05 10:26:26 +01009916 trans_num_items = 11;
9917 if (flags & RENAME_WHITEOUT)
9918 trans_num_items += 5;
9919 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009920 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009921 ret = PTR_ERR(trans);
9922 goto out_notrans;
9923 }
Chris Mason5f39d392007-10-15 16:14:19 -04009924
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009925 if (dest != root)
9926 btrfs_record_root_in_trans(trans, dest);
9927
Yan, Zhenga5719522009-09-24 09:17:31 -04009928 ret = btrfs_set_inode_index(new_dir, &index);
9929 if (ret)
9930 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009931
Miao Xie67de1172013-12-26 13:07:06 +08009932 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009933 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009934 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009935 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009937 btrfs_pin_log_trans(root);
9938 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009939 ret = btrfs_insert_inode_ref(trans, dest,
9940 new_dentry->d_name.name,
9941 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009942 old_ino,
9943 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009944 if (ret)
9945 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009947
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009948 inode_inc_iversion(old_dir);
9949 inode_inc_iversion(new_dir);
9950 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009951 old_dir->i_ctime = old_dir->i_mtime =
9952 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009953 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009954
Chris Mason12fcfd22009-03-24 10:24:20 -04009955 if (old_dentry->d_parent != new_dentry->d_parent)
9956 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9957
Li Zefan33345d012011-04-20 10:31:50 +08009958 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009959 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9960 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9961 old_dentry->d_name.name,
9962 old_dentry->d_name.len);
9963 } else {
Al Viro92986792011-03-04 17:14:37 +00009964 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009965 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009966 old_dentry->d_name.name,
9967 old_dentry->d_name.len);
9968 if (!ret)
9969 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009970 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009971 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009972 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009973 goto out_fail;
9974 }
Chris Mason39279cc2007-06-12 06:35:45 -04009975
9976 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009977 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009978 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009979 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009980 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9981 root_objectid = BTRFS_I(new_inode)->location.objectid;
9982 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9983 root_objectid,
9984 new_dentry->d_name.name,
9985 new_dentry->d_name.len);
9986 BUG_ON(new_inode->i_nlink == 0);
9987 } else {
9988 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009989 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009990 new_dentry->d_name.name,
9991 new_dentry->d_name.len);
9992 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009993 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009994 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009995 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009996 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009997 goto out_fail;
9998 }
Chris Mason39279cc2007-06-12 06:35:45 -04009999 }
Josef Bacikaec74772008-07-24 12:12:38 -040010000
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 ret = btrfs_add_link(trans, new_dir, old_inode,
10002 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010003 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010004 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010005 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010006 goto out_fail;
10007 }
Chris Mason39279cc2007-06-12 06:35:45 -040010008
Miao Xie67de1172013-12-26 13:07:06 +080010009 if (old_inode->i_nlink == 1)
10010 BTRFS_I(old_inode)->dir_index = index;
10011
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010012 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010013 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010014
Josef Bacik6a912212010-11-20 09:48:00 +000010015 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010016 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010017 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010018 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010019
10020 if (flags & RENAME_WHITEOUT) {
10021 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10022 old_dentry);
10023
10024 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010025 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010026 goto out_fail;
10027 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010028 }
Chris Mason39279cc2007-06-12 06:35:45 -040010029out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010030 /*
10031 * If we have pinned the log and an error happened, we unpin tasks
10032 * trying to sync the log and force them to fallback to a transaction
10033 * commit if the log currently contains any of the inodes involved in
10034 * this rename operation (to ensure we do not persist a log with an
10035 * inconsistent state for any of these inodes or leading to any
10036 * inconsistencies when replayed). If the transaction was aborted, the
10037 * abortion reason is propagated to userspace when attempting to commit
10038 * the transaction. If the log does not contain any of these inodes, we
10039 * allow the tasks to sync it.
10040 */
10041 if (ret && log_pinned) {
10042 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
10043 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
10044 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
10045 (new_inode &&
10046 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
10047 btrfs_set_log_full_commit(root->fs_info, trans);
10048
10049 btrfs_end_log_trans(root);
10050 log_pinned = false;
10051 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010052 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010053out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010054 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -040010055 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010056
Chris Mason39279cc2007-06-12 06:35:45 -040010057 return ret;
10058}
10059
Miklos Szeredi80ace852014-07-23 15:15:32 +020010060static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10061 struct inode *new_dir, struct dentry *new_dentry,
10062 unsigned int flags)
10063{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010064 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010065 return -EINVAL;
10066
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010067 if (flags & RENAME_EXCHANGE)
10068 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10069 new_dentry);
10070
10071 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010072}
10073
Miao Xie8ccf6f192012-10-25 09:28:04 +000010074static void btrfs_run_delalloc_work(struct btrfs_work *work)
10075{
10076 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010077 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010078
10079 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10080 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010081 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010082 filemap_flush(inode->i_mapping);
10083 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10084 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010085 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010086
10087 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010088 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010089 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010090 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010091 complete(&delalloc_work->completion);
10092}
10093
10094struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010095 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010096{
10097 struct btrfs_delalloc_work *work;
10098
David Sterba100d5702015-12-08 14:39:32 +010010099 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010100 if (!work)
10101 return NULL;
10102
10103 init_completion(&work->completion);
10104 INIT_LIST_HEAD(&work->list);
10105 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010106 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010107 WARN_ON_ONCE(!inode);
10108 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10109 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010110
10111 return work;
10112}
10113
10114void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10115{
10116 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010117 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010118}
10119
Chris Masond352ac62008-09-29 15:18:18 -040010120/*
10121 * some fairly slow code that needs optimization. This walks the list
10122 * of all the inodes with pending delalloc and forces them to disk.
10123 */
Miao Xie6c255e62014-03-06 13:55:01 +080010124static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10125 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010126{
Chris Masonea8c2812008-08-04 23:17:27 -040010127 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010128 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129 struct btrfs_delalloc_work *work, *next;
10130 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010131 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010132 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010133
Miao Xie8ccf6f192012-10-25 09:28:04 +000010134 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010135 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010136
Miao Xie573bfb72014-03-06 13:55:03 +080010137 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 spin_lock(&root->delalloc_lock);
10139 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010140 while (!list_empty(&splice)) {
10141 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010142 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010143
Miao Xieeb73c1b2013-05-15 07:48:22 +000010144 list_move_tail(&binode->delalloc_inodes,
10145 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010146 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010147 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010148 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010149 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010150 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010152
David Sterba651d4942015-11-27 19:24:16 +010010153 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010154 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010155 if (delay_iput)
10156 btrfs_add_delayed_iput(inode);
10157 else
10158 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010159 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010160 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010161 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010162 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010163 btrfs_queue_work(root->fs_info->flush_workers,
10164 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010165 ret++;
10166 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010167 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010168 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010169 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010170 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010171 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010172
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010173out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010174 list_for_each_entry_safe(work, next, &works, list) {
10175 list_del_init(&work->list);
10176 btrfs_wait_and_free_delalloc_work(work);
10177 }
10178
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179 if (!list_empty_careful(&splice)) {
10180 spin_lock(&root->delalloc_lock);
10181 list_splice_tail(&splice, &root->delalloc_inodes);
10182 spin_unlock(&root->delalloc_lock);
10183 }
Miao Xie573bfb72014-03-06 13:55:03 +080010184 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010185 return ret;
10186}
10187
10188int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10189{
10190 int ret;
10191
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010192 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 return -EROFS;
10194
Miao Xie6c255e62014-03-06 13:55:01 +080010195 ret = __start_delalloc_inodes(root, delay_iput, -1);
10196 if (ret > 0)
10197 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 /*
10199 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010200 * we have to make sure the IO is actually started and that
10201 * ordered extents get created before we return
10202 */
10203 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010204 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010205 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -040010206 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010207 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10208 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010209 }
10210 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211 return ret;
10212}
10213
Miao Xie6c255e62014-03-06 13:55:01 +080010214int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10215 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010216{
10217 struct btrfs_root *root;
10218 struct list_head splice;
10219 int ret;
10220
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010221 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222 return -EROFS;
10223
10224 INIT_LIST_HEAD(&splice);
10225
Miao Xie573bfb72014-03-06 13:55:03 +080010226 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010227 spin_lock(&fs_info->delalloc_root_lock);
10228 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010229 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010230 root = list_first_entry(&splice, struct btrfs_root,
10231 delalloc_root);
10232 root = btrfs_grab_fs_root(root);
10233 BUG_ON(!root);
10234 list_move_tail(&root->delalloc_root,
10235 &fs_info->delalloc_roots);
10236 spin_unlock(&fs_info->delalloc_root_lock);
10237
Miao Xie6c255e62014-03-06 13:55:01 +080010238 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010239 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010240 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010241 goto out;
10242
Miao Xie6c255e62014-03-06 13:55:01 +080010243 if (nr != -1) {
10244 nr -= ret;
10245 WARN_ON(nr < 0);
10246 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 spin_lock(&fs_info->delalloc_root_lock);
10248 }
10249 spin_unlock(&fs_info->delalloc_root_lock);
10250
Miao Xie6c255e62014-03-06 13:55:01 +080010251 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010252 atomic_inc(&fs_info->async_submit_draining);
10253 while (atomic_read(&fs_info->nr_async_submits) ||
10254 atomic_read(&fs_info->async_delalloc_pages)) {
10255 wait_event(fs_info->async_submit_wait,
10256 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10257 atomic_read(&fs_info->async_delalloc_pages) == 0));
10258 }
10259 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010260out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010261 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262 spin_lock(&fs_info->delalloc_root_lock);
10263 list_splice_tail(&splice, &fs_info->delalloc_roots);
10264 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010265 }
Miao Xie573bfb72014-03-06 13:55:03 +080010266 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010267 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010268}
10269
Chris Mason39279cc2007-06-12 06:35:45 -040010270static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10271 const char *symname)
10272{
10273 struct btrfs_trans_handle *trans;
10274 struct btrfs_root *root = BTRFS_I(dir)->root;
10275 struct btrfs_path *path;
10276 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010277 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010278 int err;
10279 int drop_inode = 0;
10280 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010281 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010282 int name_len;
10283 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010284 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010285 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010286 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010287
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010288 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010289 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10290 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010291
Josef Bacik9ed74f22009-09-11 16:12:44 -040010292 /*
10293 * 2 items for inode item and ref
10294 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010295 * 1 item for updating parent inode item
10296 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010297 * 1 item for xattr if selinux is on
10298 */
Filipe Manana9269d122015-12-31 18:16:29 +000010299 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010300 if (IS_ERR(trans))
10301 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010302
Li Zefan581bb052011-04-20 10:06:11 +080010303 err = btrfs_find_free_ino(root, &objectid);
10304 if (err)
10305 goto out_unlock;
10306
Josef Bacikaec74772008-07-24 12:12:38 -040010307 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010308 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -040010309 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010310 if (IS_ERR(inode)) {
10311 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010312 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010313 }
Chris Mason39279cc2007-06-12 06:35:45 -040010314
Casey Schauflerad19db72011-12-15 10:09:07 -050010315 /*
10316 * If the active LSM wants to access the inode during
10317 * d_instantiate it needs these. Smack checks to see
10318 * if the filesystem supports xattrs by looking at the
10319 * ops vector.
10320 */
10321 inode->i_fop = &btrfs_file_operations;
10322 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010323 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010324 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10325
10326 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10327 if (err)
10328 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010329
Chris Mason39279cc2007-06-12 06:35:45 -040010330 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010331 if (!path) {
10332 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010333 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010334 }
Li Zefan33345d012011-04-20 10:31:50 +080010335 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010336 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010337 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010338 datasize = btrfs_file_extent_calc_inline_size(name_len);
10339 err = btrfs_insert_empty_item(trans, root, path, &key,
10340 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010341 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010342 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010343 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010344 }
Chris Mason5f39d392007-10-15 16:14:19 -040010345 leaf = path->nodes[0];
10346 ei = btrfs_item_ptr(leaf, path->slots[0],
10347 struct btrfs_file_extent_item);
10348 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10349 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010350 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010351 btrfs_set_file_extent_encryption(leaf, ei, 0);
10352 btrfs_set_file_extent_compression(leaf, ei, 0);
10353 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10354 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10355
Chris Mason39279cc2007-06-12 06:35:45 -040010356 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010357 write_extent_buffer(leaf, symname, ptr, name_len);
10358 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010359 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010360
Chris Mason39279cc2007-06-12 06:35:45 -040010361 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010362 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010363 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010364 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010365 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010366 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010367 /*
10368 * Last step, add directory indexes for our symlink inode. This is the
10369 * last step to avoid extra cleanup of these indexes if an error happens
10370 * elsewhere above.
10371 */
10372 if (!err)
10373 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010374 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010375 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010376 goto out_unlock_inode;
10377 }
10378
Al Viro2d2d3f12018-05-04 08:23:01 -040010379 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010380
10381out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010382 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010383 if (drop_inode) {
10384 inode_dec_link_count(inode);
10385 iput(inode);
10386 }
Liu Bob53d3f52012-11-14 14:34:34 +000010387 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010388 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010389
10390out_unlock_inode:
10391 drop_inode = 1;
10392 unlock_new_inode(inode);
10393 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010394}
Chris Mason16432982008-04-10 10:23:21 -040010395
Josef Bacik0af3d002010-06-21 14:48:16 -040010396static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10397 u64 start, u64 num_bytes, u64 min_size,
10398 loff_t actual_len, u64 *alloc_hint,
10399 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010400{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010401 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10402 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010403 struct btrfs_root *root = BTRFS_I(inode)->root;
10404 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010405 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010406 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010407 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010408 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010409 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010410 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010411 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010412
Josef Bacik0af3d002010-06-21 14:48:16 -040010413 if (trans)
10414 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010415 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010416 if (own_trans) {
10417 trans = btrfs_start_transaction(root, 3);
10418 if (IS_ERR(trans)) {
10419 ret = PTR_ERR(trans);
10420 break;
10421 }
Yan Zhengd899e052008-10-30 14:25:28 -040010422 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010423
Byongho Leeee221842015-12-15 01:42:10 +090010424 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010425 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010426 /*
10427 * If we are severely fragmented we could end up with really
10428 * small allocations, so if the allocator is returning small
10429 * chunks lets make its job easier by only searching for those
10430 * sized chunks.
10431 */
10432 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010433 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10434 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010435 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010436 if (own_trans)
10437 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010438 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010439 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010440 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010441
Josef Bacik0b670dc2015-09-23 17:11:16 -040010442 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010443 ret = insert_reserved_file_extent(trans, inode,
10444 cur_offset, ins.objectid,
10445 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010446 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010447 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010448 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010449 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010450 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010451 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010452 if (own_trans)
10453 btrfs_end_transaction(trans, root);
10454 break;
10455 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010456
Chris Masona1ed8352009-09-11 12:27:37 -040010457 btrfs_drop_extent_cache(inode, cur_offset,
10458 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010459
Josef Bacik5dc562c2012-08-17 13:14:17 -040010460 em = alloc_extent_map();
10461 if (!em) {
10462 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10463 &BTRFS_I(inode)->runtime_flags);
10464 goto next;
10465 }
10466
10467 em->start = cur_offset;
10468 em->orig_start = cur_offset;
10469 em->len = ins.offset;
10470 em->block_start = ins.objectid;
10471 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010472 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010473 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010474 em->bdev = root->fs_info->fs_devices->latest_bdev;
10475 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10476 em->generation = trans->transid;
10477
10478 while (1) {
10479 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010480 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010481 write_unlock(&em_tree->lock);
10482 if (ret != -EEXIST)
10483 break;
10484 btrfs_drop_extent_cache(inode, cur_offset,
10485 cur_offset + ins.offset - 1,
10486 0);
10487 }
10488 free_extent_map(em);
10489next:
Yan Zhengd899e052008-10-30 14:25:28 -040010490 num_bytes -= ins.offset;
10491 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010492 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010493
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010494 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010495 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010496 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010497 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010498 (actual_len > inode->i_size) &&
10499 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010500 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010501 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010502 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010503 i_size = cur_offset;
10504 i_size_write(inode, i_size);
10505 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010506 }
10507
Yan Zhengd899e052008-10-30 14:25:28 -040010508 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010509
10510 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010511 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010512 if (own_trans)
10513 btrfs_end_transaction(trans, root);
10514 break;
10515 }
Yan Zhengd899e052008-10-30 14:25:28 -040010516
Josef Bacik0af3d002010-06-21 14:48:16 -040010517 if (own_trans)
10518 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010519 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010520 if (cur_offset < end)
10521 btrfs_free_reserved_data_space(inode, cur_offset,
10522 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010523 return ret;
10524}
10525
Josef Bacik0af3d002010-06-21 14:48:16 -040010526int btrfs_prealloc_file_range(struct inode *inode, int mode,
10527 u64 start, u64 num_bytes, u64 min_size,
10528 loff_t actual_len, u64 *alloc_hint)
10529{
10530 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10531 min_size, actual_len, alloc_hint,
10532 NULL);
10533}
10534
10535int btrfs_prealloc_file_range_trans(struct inode *inode,
10536 struct btrfs_trans_handle *trans, int mode,
10537 u64 start, u64 num_bytes, u64 min_size,
10538 loff_t actual_len, u64 *alloc_hint)
10539{
10540 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10541 min_size, actual_len, alloc_hint, trans);
10542}
10543
Chris Masone6dcd2d2008-07-17 12:53:50 -040010544static int btrfs_set_page_dirty(struct page *page)
10545{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010546 return __set_page_dirty_nobuffers(page);
10547}
10548
Al Viro10556cb2011-06-20 19:28:19 -040010549static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010550{
Li Zefanb83cc962010-12-20 16:04:08 +080010551 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010552 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010553
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010554 if (mask & MAY_WRITE &&
10555 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10556 if (btrfs_root_readonly(root))
10557 return -EROFS;
10558 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10559 return -EACCES;
10560 }
Al Viro2830ba72011-06-20 19:16:29 -040010561 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010562}
Chris Mason39279cc2007-06-12 06:35:45 -040010563
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10565{
10566 struct btrfs_trans_handle *trans;
10567 struct btrfs_root *root = BTRFS_I(dir)->root;
10568 struct inode *inode = NULL;
10569 u64 objectid;
10570 u64 index;
10571 int ret = 0;
10572
10573 /*
10574 * 5 units required for adding orphan entry
10575 */
10576 trans = btrfs_start_transaction(root, 5);
10577 if (IS_ERR(trans))
10578 return PTR_ERR(trans);
10579
10580 ret = btrfs_find_free_ino(root, &objectid);
10581 if (ret)
10582 goto out;
10583
10584 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10585 btrfs_ino(dir), objectid, mode, &index);
10586 if (IS_ERR(inode)) {
10587 ret = PTR_ERR(inode);
10588 inode = NULL;
10589 goto out;
10590 }
10591
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010592 inode->i_fop = &btrfs_file_operations;
10593 inode->i_op = &btrfs_file_inode_operations;
10594
10595 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010596 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10597
Chris Masonb0d5d102014-09-08 13:08:51 -070010598 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10599 if (ret)
10600 goto out_inode;
10601
10602 ret = btrfs_update_inode(trans, root, inode);
10603 if (ret)
10604 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010605 ret = btrfs_orphan_add(trans, inode);
10606 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010607 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010608
Filipe Manana5762b5c2014-08-01 00:10:32 +010010609 /*
10610 * We set number of links to 0 in btrfs_new_inode(), and here we set
10611 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10612 * through:
10613 *
10614 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10615 */
10616 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010617 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010618 d_tmpfile(dentry, inode);
10619 mark_inode_dirty(inode);
10620
10621out:
10622 btrfs_end_transaction(trans, root);
10623 if (ret)
10624 iput(inode);
10625 btrfs_balance_delayed_items(root);
10626 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010627 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010628
10629out_inode:
10630 unlock_new_inode(inode);
10631 goto out;
10632
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633}
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,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010664 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010665#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010666 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010667#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010668 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010669 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010670};
10671
David Sterba20e55062015-11-19 11:42:28 +010010672static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010673 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010674 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010675 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010676 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010677 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010678 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010679 .set_bit_hook = btrfs_set_bit_hook,
10680 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010681 .merge_extent_hook = btrfs_merge_extent_hook,
10682 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010683};
10684
Chris Mason35054392009-01-21 13:11:13 -050010685/*
10686 * btrfs doesn't support the bmap operation because swapfiles
10687 * use bmap to make a mapping of extents in the file. They assume
10688 * these extents won't change over the life of the file and they
10689 * use the bmap result to do IO directly to the drive.
10690 *
10691 * the btrfs bmap call would return logical addresses that aren't
10692 * suitable for IO and they also will change frequently as COW
10693 * operations happen. So, swapfile + btrfs == corruption.
10694 *
10695 * For now we're avoiding this by dropping bmap.
10696 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010697static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010698 .readpage = btrfs_readpage,
10699 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010700 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010701 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010702 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010703 .invalidatepage = btrfs_invalidatepage,
10704 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010705 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010706 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010707};
10708
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010709static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010710 .readpage = btrfs_readpage,
10711 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010712 .invalidatepage = btrfs_invalidatepage,
10713 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010714};
10715
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010716static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010717 .getattr = btrfs_getattr,
10718 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010719 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010720 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010721 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010722 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010723 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010724 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010725};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010726static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010727 .getattr = btrfs_getattr,
10728 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010729 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010730 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010731 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010732 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010733 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010734};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010735static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010736 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010737 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010738 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010739 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010740 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010741 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010742 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010743};
Yan, Zheng76dda932009-09-21 16:00:26 -040010744
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010745const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010746 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010747 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010748};