blob: 894d56361ea9e4b352fe672d7d3aef169355c762 [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 */
492 goto cont;
493 }
Chris Mason179e29e2007-11-01 11:28:41 -0400494
Li Zefan261507a02010-12-17 14:21:50 +0800495 if (BTRFS_I(inode)->force_compress)
496 compress_type = BTRFS_I(inode)->force_compress;
497
Chris Mason4adaa612013-03-26 13:07:00 -0400498 /*
499 * we need to call clear_page_dirty_for_io on each
500 * page in the range. Otherwise applications with the file
501 * mmap'd can wander in and change the page contents while
502 * we are compressing them.
503 *
504 * If the compression fails for any reason, we set the pages
505 * dirty again later on.
506 */
507 extent_range_clear_dirty_for_io(inode, start, end);
508 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800509 ret = btrfs_compress_pages(compress_type,
510 inode->i_mapping, start,
511 total_compressed, pages,
512 nr_pages, &nr_pages_ret,
513 &total_in,
514 &total_compressed,
515 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400516
517 if (!ret) {
518 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300519 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 struct page *page = pages[nr_pages_ret - 1];
521 char *kaddr;
522
523 /* zero the tail end of the last page, we might be
524 * sending it down to disk
525 */
526 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800527 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300529 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800530 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 }
532 will_compress = 1;
533 }
534 }
Li Zefan560f7d72011-09-08 10:22:01 +0800535cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400536 if (start == 0) {
537 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500538 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400539 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500540 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400541 */
Josef Bacik00361582013-08-14 14:02:47 -0400542 ret = cow_file_range_inline(root, inode, start, end,
543 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400544 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500545 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400546 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000547 total_compressed,
548 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100550 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400551 unsigned long clear_flags = EXTENT_DELALLOC |
552 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100553 unsigned long page_error_op;
554
Josef Bacik151a41b2013-07-29 13:22:24 -0400555 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100556 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400557
Chris Mason771ed682008-11-06 22:02:51 -0500558 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 * inline extent creation worked or returned error,
560 * we don't need to create any more async work items.
561 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500562 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800563 extent_clear_unlock_delalloc(inode, start, end, end,
564 NULL, clear_flags,
565 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400566 PAGE_CLEAR_DIRTY |
567 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100568 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400569 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800570 btrfs_free_reserved_data_space_noquota(inode, start,
571 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 goto free_pages_out;
573 }
574 }
575
576 if (will_compress) {
577 /*
578 * we aren't doing an inline extent round the compressed size
579 * up to a block size boundary so the allocator does sane
580 * things
581 */
Qu Wenruofda28322013-02-26 08:10:22 +0000582 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400583
584 /*
585 * one last check to make sure the compression is really a
586 * win, compare the page count read with the blocks on disk
587 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300588 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400589 if (total_compressed >= total_in) {
590 will_compress = 0;
591 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400592 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700593 *num_added += 1;
594
595 /*
596 * The async work queues will take care of doing actual
597 * allocation on disk for these compressed pages, and
598 * will submit them to the elevator.
599 */
600 add_async_extent(async_cow, start, num_bytes,
601 total_compressed, pages, nr_pages_ret,
602 compress_type);
603
604 if (start + num_bytes < end) {
605 start += num_bytes;
606 pages = NULL;
607 cond_resched();
608 goto again;
609 }
610 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400611 }
612 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700613 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400614 /*
615 * the compression code ran but failed to make things smaller,
616 * free any pages it allocated and our page pointer array
617 */
618 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400619 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300620 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400621 }
622 kfree(pages);
623 pages = NULL;
624 total_compressed = 0;
625 nr_pages_ret = 0;
626
627 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400628 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500629 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500630 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500631 }
Chris Masonc8b97812008-10-29 14:49:59 -0400632 }
Chris Masonf03d9302009-02-04 09:31:06 -0500633cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 /*
635 * No compression, but we still need to write the pages in the file
636 * we've been given so far. redirty the locked page if it corresponds
637 * to our extent and set things up for the async work queue to run
638 * cow_file_range to do the normal delalloc dance.
639 */
640 if (page_offset(locked_page) >= start &&
641 page_offset(locked_page) <= end)
642 __set_page_dirty_nobuffers(locked_page);
643 /* unlocked later on in the async handlers */
644
645 if (redirty)
646 extent_range_redirty_for_io(inode, start, end);
647 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
648 BTRFS_COMPRESS_NONE);
649 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500650
Filipe Mananac44f6492014-10-09 21:15:44 +0100651 return;
Chris Mason771ed682008-11-06 22:02:51 -0500652
653free_pages_out:
654 for (i = 0; i < nr_pages_ret; i++) {
655 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300656 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500657 }
Chris Masond3977122009-01-05 21:25:51 -0500658 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500659}
Chris Mason771ed682008-11-06 22:02:51 -0500660
Filipe Manana40ae8372014-10-06 22:14:24 +0100661static void free_async_extent_pages(struct async_extent *async_extent)
662{
663 int i;
664
665 if (!async_extent->pages)
666 return;
667
668 for (i = 0; i < async_extent->nr_pages; i++) {
669 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300670 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100671 }
672 kfree(async_extent->pages);
673 async_extent->nr_pages = 0;
674 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500675}
676
677/*
678 * phase two of compressed writeback. This is the ordered portion
679 * of the code, which only gets called in the order the work was
680 * queued. We walk all the async extents created by compress_file_range
681 * and send them down to the disk.
682 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100683static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500684 struct async_cow *async_cow)
685{
686 struct async_extent *async_extent;
687 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500688 struct btrfs_key ins;
689 struct extent_map *em;
690 struct btrfs_root *root = BTRFS_I(inode)->root;
691 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
692 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500693 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500694
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500695again:
Chris Masond3977122009-01-05 21:25:51 -0500696 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500697 async_extent = list_entry(async_cow->extents.next,
698 struct async_extent, list);
699 list_del(&async_extent->list);
700
701 io_tree = &BTRFS_I(inode)->io_tree;
702
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500703retry:
Chris Mason771ed682008-11-06 22:02:51 -0500704 /* did the compression code fall back to uncompressed IO? */
705 if (!async_extent->pages) {
706 int page_started = 0;
707 unsigned long nr_written = 0;
708
709 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000710 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100711 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500712
713 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500714 ret = cow_file_range(inode, async_cow->locked_page,
715 async_extent->start,
716 async_extent->start +
717 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800718 async_extent->start +
719 async_extent->ram_size - 1,
720 &page_started, &nr_written, 0,
721 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500722
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100723 /* JDM XXX */
724
Chris Mason771ed682008-11-06 22:02:51 -0500725 /*
726 * if page_started, cow_file_range inserted an
727 * inline extent and took care of all the unlocking
728 * and IO for us. Otherwise, we need to submit
729 * all those pages down to the drive.
730 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500731 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500732 extent_write_locked_range(io_tree,
733 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500734 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500735 async_extent->ram_size - 1,
736 btrfs_get_extent,
737 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500738 else if (ret)
739 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500740 kfree(async_extent);
741 cond_resched();
742 continue;
743 }
744
745 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100746 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Wang Xiaoguang18513092016-07-25 15:51:40 +0800748 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500749 async_extent->compressed_size,
750 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800751 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500752 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100753 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400755 if (ret == -ENOSPC) {
756 unlock_extent(io_tree, async_extent->start,
757 async_extent->start +
758 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800759
760 /*
761 * we need to redirty the pages if we decide to
762 * fallback to uncompressed IO, otherwise we
763 * will not submit these pages down to lower
764 * layers.
765 */
766 extent_range_redirty_for_io(inode,
767 async_extent->start,
768 async_extent->start +
769 async_extent->ram_size - 1);
770
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400772 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500774 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000775 /*
776 * here we're doing allocation and writeback of the
777 * compressed pages
778 */
779 btrfs_drop_extent_cache(inode, async_extent->start,
780 async_extent->start +
781 async_extent->ram_size - 1, 0);
782
David Sterba172ddd62011-04-21 00:48:27 +0200783 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000784 if (!em) {
785 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500786 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000787 }
Chris Mason771ed682008-11-06 22:02:51 -0500788 em->start = async_extent->start;
789 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500790 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400791 em->mod_start = em->start;
792 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500793
794 em->block_start = ins.objectid;
795 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500796 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400797 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500798 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800799 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500800 set_bit(EXTENT_FLAG_PINNED, &em->flags);
801 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400802 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500803
Chris Masond3977122009-01-05 21:25:51 -0500804 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400805 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400806 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400807 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500808 if (ret != -EEXIST) {
809 free_extent_map(em);
810 break;
811 }
812 btrfs_drop_extent_cache(inode, async_extent->start,
813 async_extent->start +
814 async_extent->ram_size - 1, 0);
815 }
816
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500817 if (ret)
818 goto out_free_reserve;
819
Li Zefan261507a02010-12-17 14:21:50 +0800820 ret = btrfs_add_ordered_extent_compress(inode,
821 async_extent->start,
822 ins.objectid,
823 async_extent->ram_size,
824 ins.offset,
825 BTRFS_ORDERED_COMPRESSED,
826 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 if (ret) {
828 btrfs_drop_extent_cache(inode, async_extent->start,
829 async_extent->start +
830 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500831 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100832 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100833 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500834
Chris Mason771ed682008-11-06 22:02:51 -0500835 /*
836 * clear dirty, set writeback and unlock the pages.
837 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400838 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400839 async_extent->start +
840 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800841 async_extent->start +
842 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400843 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
844 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400845 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500846 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500847 async_extent->start,
848 async_extent->ram_size,
849 ins.objectid,
850 ins.offset, async_extent->pages,
851 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100852 if (ret) {
853 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
854 struct page *p = async_extent->pages[0];
855 const u64 start = async_extent->start;
856 const u64 end = start + async_extent->ram_size - 1;
857
858 p->mapping = inode->i_mapping;
859 tree->ops->writepage_end_io_hook(p, start, end,
860 NULL, 0);
861 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800862 extent_clear_unlock_delalloc(inode, start, end, end,
863 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 PAGE_END_WRITEBACK |
865 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100866 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 }
Chris Mason771ed682008-11-06 22:02:51 -0500868 alloc_hint = ins.objectid + ins.offset;
869 kfree(async_extent);
870 cond_resched();
871 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100872 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500873out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100874 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800875 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400877 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500878 async_extent->start +
879 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800880 async_extent->start +
881 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400882 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400883 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
884 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100885 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
886 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100887 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100888 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500889 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500890}
891
Josef Bacik4b46fce2010-05-23 11:00:55 -0400892static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
893 u64 num_bytes)
894{
895 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
896 struct extent_map *em;
897 u64 alloc_hint = 0;
898
899 read_lock(&em_tree->lock);
900 em = search_extent_mapping(em_tree, start, num_bytes);
901 if (em) {
902 /*
903 * if block start isn't an actual block number then find the
904 * first block in this inode and use that as a hint. If that
905 * block is also bogus then just don't worry about it.
906 */
907 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
908 free_extent_map(em);
909 em = search_extent_mapping(em_tree, 0, 0);
910 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
911 alloc_hint = em->block_start;
912 if (em)
913 free_extent_map(em);
914 } else {
915 alloc_hint = em->block_start;
916 free_extent_map(em);
917 }
918 }
919 read_unlock(&em_tree->lock);
920
921 return alloc_hint;
922}
923
Chris Mason771ed682008-11-06 22:02:51 -0500924/*
925 * when extent_io.c finds a delayed allocation range in the file,
926 * the call backs end up in this code. The basic idea is to
927 * allocate extents on disk for the range, and create ordered data structs
928 * in ram to track those extents.
929 *
930 * locked_page is the page that writepage had locked already. We use
931 * it to make sure we don't do extra locks or unlocks.
932 *
933 * *page_started is set to one if we unlock locked_page and do everything
934 * required to start IO on it. It may be clean and already done with
935 * IO when we return.
936 */
Josef Bacik00361582013-08-14 14:02:47 -0400937static noinline int cow_file_range(struct inode *inode,
938 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800939 u64 start, u64 end, u64 delalloc_end,
940 int *page_started, unsigned long *nr_written,
941 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500942{
Josef Bacik00361582013-08-14 14:02:47 -0400943 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500944 u64 alloc_hint = 0;
945 u64 num_bytes;
946 unsigned long ram_size;
947 u64 disk_num_bytes;
948 u64 cur_alloc_size;
949 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500950 struct btrfs_key ins;
951 struct extent_map *em;
952 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
953 int ret = 0;
954
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400955 if (btrfs_is_free_space_inode(inode)) {
956 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500957 ret = -EINVAL;
958 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400959 }
Chris Mason771ed682008-11-06 22:02:51 -0500960
Qu Wenruofda28322013-02-26 08:10:22 +0000961 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500962 num_bytes = max(blocksize, num_bytes);
963 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500964
Chris Mason4cb53002011-05-24 15:35:30 -0400965 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900966 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400967 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400968 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400969
Chris Mason771ed682008-11-06 22:02:51 -0500970 if (start == 0) {
971 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400972 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
973 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500974 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800975 extent_clear_unlock_delalloc(inode, start, end,
976 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400977 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400978 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400979 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
980 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800981 btrfs_free_reserved_data_space_noquota(inode, start,
982 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500983 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300984 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500985 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500986 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100988 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500989 }
990 }
Chris Masonc8b97812008-10-29 14:49:59 -0400991
992 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200993 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400994
Josef Bacik4b46fce2010-05-23 11:00:55 -0400995 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400996 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400997
Chris Masond3977122009-01-05 21:25:51 -0500998 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400999 unsigned long op;
1000
Josef Bacik287a0ab2010-03-19 18:07:23 +00001001 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001002 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -05001003 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001004 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001005 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001007
David Sterba172ddd62011-04-21 00:48:27 +02001008 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001009 if (!em) {
1010 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001011 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001013 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001014 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001015 ram_size = ins.offset;
1016 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001017 em->mod_start = em->start;
1018 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001021 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001022 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001023 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001024 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001025 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001026 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001027
Chris Masond3977122009-01-05 21:25:51 -05001028 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001029 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001030 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001031 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001032 if (ret != -EEXIST) {
1033 free_extent_map(em);
1034 break;
1035 }
1036 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001037 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 }
Liu Boace68ba2013-04-22 10:53:47 +00001039 if (ret)
1040 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041
Chris Mason98d20f62008-04-14 09:46:10 -04001042 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001043 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001044 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001045 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001046 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001047
Yan Zheng17d217f2008-12-12 10:03:38 -05001048 if (root->root_key.objectid ==
1049 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1050 ret = btrfs_reloc_clone_csums(inode, start,
1051 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001052 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001053 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001054 }
1055
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001056 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1057
Chris Masond3977122009-01-05 21:25:51 -05001058 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001059 break;
Chris Masond3977122009-01-05 21:25:51 -05001060
Chris Masonc8b97812008-10-29 14:49:59 -04001061 /* we're not doing compressed IO, don't unlock the first
1062 * page (which the caller expects to stay locked), don't
1063 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001064 *
1065 * Do set the Private2 bit so we know this page was properly
1066 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001067 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001068 op = unlock ? PAGE_UNLOCK : 0;
1069 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001070
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001072 start + ram_size - 1,
1073 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 EXTENT_LOCKED | EXTENT_DELALLOC,
1075 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001076 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001077 num_bytes -= cur_alloc_size;
1078 alloc_hint = ins.objectid + ins.offset;
1079 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001080 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001082 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001083
Filipe Mananad9f85962014-08-25 10:43:00 +01001084out_drop_extent_cache:
1085 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001086out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001087 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001088 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001090 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1091 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001092 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1093 EXTENT_DELALLOC | EXTENT_DEFRAG,
1094 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1095 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001097}
Chris Masonc8b97812008-10-29 14:49:59 -04001098
Chris Mason771ed682008-11-06 22:02:51 -05001099/*
1100 * work queue call back to started compression on a file and pages
1101 */
1102static noinline void async_cow_start(struct btrfs_work *work)
1103{
1104 struct async_cow *async_cow;
1105 int num_added = 0;
1106 async_cow = container_of(work, struct async_cow, work);
1107
1108 compress_file_range(async_cow->inode, async_cow->locked_page,
1109 async_cow->start, async_cow->end, async_cow,
1110 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001111 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001112 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001113 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001114 }
Chris Mason771ed682008-11-06 22:02:51 -05001115}
1116
1117/*
1118 * work queue call back to submit previously compressed pages
1119 */
1120static noinline void async_cow_submit(struct btrfs_work *work)
1121{
1122 struct async_cow *async_cow;
1123 struct btrfs_root *root;
1124 unsigned long nr_pages;
1125
1126 async_cow = container_of(work, struct async_cow, work);
1127
1128 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001129 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1130 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001131
David Sterbaee863952015-02-16 19:41:40 +01001132 /*
1133 * atomic_sub_return implies a barrier for waitqueue_active
1134 */
Josef Bacik66657b32012-08-01 15:36:24 -04001135 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001136 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001137 waitqueue_active(&root->fs_info->async_submit_wait))
1138 wake_up(&root->fs_info->async_submit_wait);
1139
Chris Masond3977122009-01-05 21:25:51 -05001140 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001141 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001142}
Chris Masonc8b97812008-10-29 14:49:59 -04001143
Chris Mason771ed682008-11-06 22:02:51 -05001144static noinline void async_cow_free(struct btrfs_work *work)
1145{
1146 struct async_cow *async_cow;
1147 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001148 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001149 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001150 kfree(async_cow);
1151}
1152
1153static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1154 u64 start, u64 end, int *page_started,
1155 unsigned long *nr_written)
1156{
1157 struct async_cow *async_cow;
1158 struct btrfs_root *root = BTRFS_I(inode)->root;
1159 unsigned long nr_pages;
1160 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001161 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001162
Chris Masona3429ab2009-10-08 12:30:20 -04001163 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1164 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001165 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001166 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001167 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001168 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001169 async_cow->root = root;
1170 async_cow->locked_page = locked_page;
1171 async_cow->start = start;
1172
Wang Shilongf79707b2014-07-17 11:44:09 +08001173 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001174 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001175 cur_end = end;
1176 else
Byongho Leeee221842015-12-15 01:42:10 +09001177 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001178
1179 async_cow->end = cur_end;
1180 INIT_LIST_HEAD(&async_cow->extents);
1181
Liu Bo9e0af232014-08-15 23:36:53 +08001182 btrfs_init_work(&async_cow->work,
1183 btrfs_delalloc_helper,
1184 async_cow_start, async_cow_submit,
1185 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001186
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001187 nr_pages = (cur_end - start + PAGE_SIZE) >>
1188 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001189 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1190
Qu Wenruoafe3d242014-02-28 10:46:07 +08001191 btrfs_queue_work(root->fs_info->delalloc_workers,
1192 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001193
1194 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1195 wait_event(root->fs_info->async_submit_wait,
1196 (atomic_read(&root->fs_info->async_delalloc_pages) <
1197 limit));
1198 }
1199
Chris Masond3977122009-01-05 21:25:51 -05001200 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001201 atomic_read(&root->fs_info->async_delalloc_pages)) {
1202 wait_event(root->fs_info->async_submit_wait,
1203 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1204 0));
1205 }
1206
1207 *nr_written += nr_pages;
1208 start = cur_end + 1;
1209 }
1210 *page_started = 1;
1211 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001212}
1213
Chris Masond3977122009-01-05 21:25:51 -05001214static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001215 u64 bytenr, u64 num_bytes)
1216{
1217 int ret;
1218 struct btrfs_ordered_sum *sums;
1219 LIST_HEAD(list);
1220
Yan Zheng07d400a2009-01-06 11:42:00 -05001221 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001222 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001223 if (ret == 0 && list_empty(&list))
1224 return 0;
1225
1226 while (!list_empty(&list)) {
1227 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1228 list_del(&sums->list);
1229 kfree(sums);
1230 }
1231 return 1;
1232}
1233
Chris Masond352ac62008-09-29 15:18:18 -04001234/*
1235 * when nowcow writeback call back. This checks for snapshots or COW copies
1236 * of the extents that exist in the file, and COWs the file as required.
1237 *
1238 * If no cow copies or snapshots exist, we write directly to the existing
1239 * blocks on disk
1240 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001241static noinline int run_delalloc_nocow(struct inode *inode,
1242 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001243 u64 start, u64 end, int *page_started, int force,
1244 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001245{
Chris Masonbe20aa92007-12-17 20:14:01 -05001246 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001247 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001248 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001249 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 u64 cow_start;
1253 u64 cur_offset;
1254 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001255 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 u64 disk_bytenr;
1257 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001258 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001259 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001261 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001262 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 int nocow;
1264 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001265 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001266 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
1268 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001269 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001270 extent_clear_unlock_delalloc(inode, start, end, end,
1271 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001272 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001273 EXTENT_DO_ACCOUNTING |
1274 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001275 PAGE_CLEAR_DIRTY |
1276 PAGE_SET_WRITEBACK |
1277 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001278 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001279 }
Li Zefan82d59022011-04-20 10:33:24 +08001280
Liu Bo83eea1f2012-07-10 05:28:39 -06001281 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001282
1283 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001284 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001285 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001286 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001287
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001288 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001289 extent_clear_unlock_delalloc(inode, start, end, end,
1290 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001291 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001292 EXTENT_DO_ACCOUNTING |
1293 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001294 PAGE_CLEAR_DIRTY |
1295 PAGE_SET_WRITEBACK |
1296 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001297 btrfs_free_path(path);
1298 return PTR_ERR(trans);
1299 }
1300
Josef Bacik74b21072011-04-13 12:02:53 -04001301 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 cow_start = (u64)-1;
1304 cur_offset = start;
1305 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001306 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001308 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001309 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1311 leaf = path->nodes[0];
1312 btrfs_item_key_to_cpu(leaf, &found_key,
1313 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001314 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 found_key.type == BTRFS_EXTENT_DATA_KEY)
1316 path->slots[0]--;
1317 }
1318 check_prev = 0;
1319next_slot:
1320 leaf = path->nodes[0];
1321 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1322 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001323 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001324 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 if (ret > 0)
1326 break;
1327 leaf = path->nodes[0];
1328 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001329
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 nocow = 0;
1331 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
Filipe Manana1d512cb2015-11-09 00:33:58 +00001335 if (found_key.objectid > ino)
1336 break;
1337 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1338 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1339 path->slots[0]++;
1340 goto next_slot;
1341 }
1342 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 found_key.offset > end)
1344 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001345
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 if (found_key.offset > cur_offset) {
1347 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001348 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 goto out_check;
1350 }
Chris Masonc31f8832008-01-08 15:46:31 -05001351
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 fi = btrfs_item_ptr(leaf, path->slots[0],
1353 struct btrfs_file_extent_item);
1354 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001355
Josef Bacikcc95bef2013-04-04 14:31:27 -04001356 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1358 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 extent_end = found_key.offset +
1362 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001363 disk_num_bytes =
1364 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 if (extent_end <= start) {
1366 path->slots[0]++;
1367 goto next_slot;
1368 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001369 if (disk_bytenr == 0)
1370 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (btrfs_file_extent_compression(leaf, fi) ||
1372 btrfs_file_extent_encryption(leaf, fi) ||
1373 btrfs_file_extent_other_encoding(leaf, fi))
1374 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1376 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001377 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001379 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001380 found_key.offset -
1381 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001383 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001384 disk_bytenr += cur_offset - found_key.offset;
1385 num_bytes = min(end + 1, extent_end) - cur_offset;
1386 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001387 * if there are pending snapshots for this root,
1388 * we fall into common COW way.
1389 */
1390 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001391 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001392 if (!err)
1393 goto out_check;
1394 }
1395 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001396 * force cow if csum exists in the range.
1397 * this ensure that csum for a given extent are
1398 * either valid or do not exist.
1399 */
1400 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1401 goto out_check;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001402 if (!btrfs_inc_nocow_writers(root->fs_info,
1403 disk_bytenr))
1404 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 nocow = 1;
1406 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1407 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001408 btrfs_file_extent_inline_len(leaf,
1409 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001410 extent_end = ALIGN(extent_end, root->sectorsize);
1411 } else {
1412 BUG_ON(1);
1413 }
1414out_check:
1415 if (extent_end <= start) {
1416 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001417 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001418 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001419 if (nocow)
1420 btrfs_dec_nocow_writers(root->fs_info,
1421 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001422 goto next_slot;
1423 }
1424 if (!nocow) {
1425 if (cow_start == (u64)-1)
1426 cow_start = cur_offset;
1427 cur_offset = extent_end;
1428 if (cur_offset > end)
1429 break;
1430 path->slots[0]++;
1431 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001432 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001433
David Sterbab3b4aa72011-04-21 01:20:15 +02001434 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001436 ret = cow_file_range(inode, locked_page,
1437 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001438 end, page_started, nr_written, 1,
1439 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001440 if (ret) {
1441 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001442 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001443 if (nocow)
1444 btrfs_dec_nocow_writers(root->fs_info,
1445 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001447 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001449 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001450
Yan Zhengd899e052008-10-30 14:25:28 -04001451 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1452 struct extent_map *em;
1453 struct extent_map_tree *em_tree;
1454 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001455 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001457 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001458 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001459 em->len = num_bytes;
1460 em->block_len = num_bytes;
1461 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001462 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001463 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001464 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001465 em->mod_start = em->start;
1466 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001467 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001468 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001469 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001470 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001471 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001472 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001473 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001474 if (ret != -EEXIST) {
1475 free_extent_map(em);
1476 break;
1477 }
1478 btrfs_drop_extent_cache(inode, em->start,
1479 em->start + em->len - 1, 0);
1480 }
1481 type = BTRFS_ORDERED_PREALLOC;
1482 } else {
1483 type = BTRFS_ORDERED_NOCOW;
1484 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001485
1486 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001487 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001488 if (nocow)
1489 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001490 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001491
Yan, Zhengefa56462010-05-16 10:49:59 -04001492 if (root->root_key.objectid ==
1493 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1494 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1495 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001496 if (ret) {
1497 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001498 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001499 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001500 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001501 }
1502
Josef Bacikc2790a22013-07-29 11:20:47 -04001503 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001504 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001505 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001506 EXTENT_DELALLOC |
1507 EXTENT_CLEAR_DATA_RESV,
1508 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1509
Wang Shilonge9894fd2014-03-27 11:12:25 +08001510 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001511 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 cur_offset = extent_end;
1513 if (cur_offset > end)
1514 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001515 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001516 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001517
Josef Bacik17ca04a2012-05-31 15:58:55 -04001518 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001520 cur_offset = end;
1521 }
1522
Yan Zheng80ff3852008-10-30 14:20:02 -04001523 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001524 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1525 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001526 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001527 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 }
1529
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001530error:
Miao Xiea698d0752012-09-20 01:51:59 -06001531 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532 if (!ret)
1533 ret = err;
1534
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001536 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001537 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001538 EXTENT_DELALLOC | EXTENT_DEFRAG |
1539 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1540 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 PAGE_SET_WRITEBACK |
1542 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001543 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001545}
1546
Wang Shilong47059d92014-07-03 18:22:07 +08001547static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1548{
1549
1550 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1551 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1552 return 0;
1553
1554 /*
1555 * @defrag_bytes is a hint value, no spinlock held here,
1556 * if is not zero, it means the file is defragging.
1557 * Force cow if given extent needs to be defragged.
1558 */
1559 if (BTRFS_I(inode)->defrag_bytes &&
1560 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1561 EXTENT_DEFRAG, 0, NULL))
1562 return 1;
1563
1564 return 0;
1565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * extent_io.c call back to do delayed allocation processing
1569 */
Chris Masonc8b97812008-10-29 14:49:59 -04001570static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001571 u64 start, u64 end, int *page_started,
1572 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001573{
Chris Masonbe20aa92007-12-17 20:14:01 -05001574 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001575 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001576
Wang Shilong47059d92014-07-03 18:22:07 +08001577 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001578 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001579 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001580 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001581 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001582 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001583 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001584 ret = cow_file_range(inode, locked_page, start, end, end,
1585 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001586 } else {
1587 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1588 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001589 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001590 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001591 }
Chris Masonb888db22007-08-27 16:49:44 -04001592 return ret;
1593}
1594
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001595static void btrfs_split_extent_hook(struct inode *inode,
1596 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001597{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001598 u64 size;
1599
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001601 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001602 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 size = orig->end - orig->start + 1;
1605 if (size > BTRFS_MAX_EXTENT_SIZE) {
1606 u64 num_extents;
1607 u64 new_size;
1608
1609 /*
Josef Bacikba117212015-03-13 15:01:24 -04001610 * See the explanation in btrfs_merge_extent_hook, the same
1611 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612 */
1613 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001614 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001615 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001616 new_size = split - orig->start;
1617 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1618 BTRFS_MAX_EXTENT_SIZE);
1619 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1620 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 return;
1622 }
1623
Josef Bacik9e0baf62011-07-15 15:16:44 +00001624 spin_lock(&BTRFS_I(inode)->lock);
1625 BTRFS_I(inode)->outstanding_extents++;
1626 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001627}
1628
1629/*
1630 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1631 * extents so we can keep track of new extents that are just merged onto old
1632 * extents, such as when we are doing sequential writes, so we can properly
1633 * account for the metadata space we'll need.
1634 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001635static void btrfs_merge_extent_hook(struct inode *inode,
1636 struct extent_state *new,
1637 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001638{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639 u64 new_size, old_size;
1640 u64 num_extents;
1641
Josef Bacik9ed74f22009-09-11 16:12:44 -04001642 /* not delalloc, ignore it */
1643 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001644 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001645
Josef Bacik8461a3d2015-03-13 15:12:08 -04001646 if (new->start > other->start)
1647 new_size = new->end - other->start + 1;
1648 else
1649 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001650
1651 /* we're not bigger than the max, unreserve the space and go */
1652 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1653 spin_lock(&BTRFS_I(inode)->lock);
1654 BTRFS_I(inode)->outstanding_extents--;
1655 spin_unlock(&BTRFS_I(inode)->lock);
1656 return;
1657 }
1658
1659 /*
Josef Bacikba117212015-03-13 15:01:24 -04001660 * We have to add up either side to figure out how many extents were
1661 * accounted for before we merged into one big extent. If the number of
1662 * extents we accounted for is <= the amount we need for the new range
1663 * then we can return, otherwise drop. Think of it like this
1664 *
1665 * [ 4k][MAX_SIZE]
1666 *
1667 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1668 * need 2 outstanding extents, on one side we have 1 and the other side
1669 * we have 1 so they are == and we can return. But in this case
1670 *
1671 * [MAX_SIZE+4k][MAX_SIZE+4k]
1672 *
1673 * Each range on their own accounts for 2 extents, but merged together
1674 * they are only 3 extents worth of accounting, so we need to drop in
1675 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 */
Josef Bacikba117212015-03-13 15:01:24 -04001677 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1679 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001680 old_size = new->end - new->start + 1;
1681 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1682 BTRFS_MAX_EXTENT_SIZE);
1683
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001685 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001686 return;
1687
Josef Bacik9e0baf62011-07-15 15:16:44 +00001688 spin_lock(&BTRFS_I(inode)->lock);
1689 BTRFS_I(inode)->outstanding_extents--;
1690 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001691}
1692
Miao Xieeb73c1b2013-05-15 07:48:22 +00001693static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1694 struct inode *inode)
1695{
1696 spin_lock(&root->delalloc_lock);
1697 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1698 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1699 &root->delalloc_inodes);
1700 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1701 &BTRFS_I(inode)->runtime_flags);
1702 root->nr_delalloc_inodes++;
1703 if (root->nr_delalloc_inodes == 1) {
1704 spin_lock(&root->fs_info->delalloc_root_lock);
1705 BUG_ON(!list_empty(&root->delalloc_root));
1706 list_add_tail(&root->delalloc_root,
1707 &root->fs_info->delalloc_roots);
1708 spin_unlock(&root->fs_info->delalloc_root_lock);
1709 }
1710 }
1711 spin_unlock(&root->delalloc_lock);
1712}
1713
1714static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1715 struct inode *inode)
1716{
1717 spin_lock(&root->delalloc_lock);
1718 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1719 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1720 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1721 &BTRFS_I(inode)->runtime_flags);
1722 root->nr_delalloc_inodes--;
1723 if (!root->nr_delalloc_inodes) {
1724 spin_lock(&root->fs_info->delalloc_root_lock);
1725 BUG_ON(list_empty(&root->delalloc_root));
1726 list_del_init(&root->delalloc_root);
1727 spin_unlock(&root->fs_info->delalloc_root_lock);
1728 }
1729 }
1730 spin_unlock(&root->delalloc_lock);
1731}
1732
Chris Masond352ac62008-09-29 15:18:18 -04001733/*
1734 * extent_io.c set_bit_hook, used to track delayed allocation
1735 * bytes in this file, and to maintain the list of inodes that
1736 * have pending delalloc work to be done.
1737 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001738static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001739 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001740{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001741
Wang Shilong47059d92014-07-03 18:22:07 +08001742 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1743 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001744 /*
1745 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001746 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001747 * bit, which is only set or cleared with irqs on
1748 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001749 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001750 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001751 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001752 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001753
Josef Bacik9e0baf62011-07-15 15:16:44 +00001754 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001755 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001756 } else {
1757 spin_lock(&BTRFS_I(inode)->lock);
1758 BTRFS_I(inode)->outstanding_extents++;
1759 spin_unlock(&BTRFS_I(inode)->lock);
1760 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001761
Josef Bacik6a3891c2015-03-16 17:38:52 -04001762 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001763 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001764 return;
1765
Miao Xie963d6782013-01-29 10:10:51 +00001766 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1767 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001768 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001770 if (*bits & EXTENT_DEFRAG)
1771 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001772 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001773 &BTRFS_I(inode)->runtime_flags))
1774 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001775 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001776 }
Chris Mason291d6732008-01-29 15:55:23 -05001777}
1778
Chris Masond352ac62008-09-29 15:18:18 -04001779/*
1780 * extent_io.c clear_bit_hook, see set_bit_hook for why
1781 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001782static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001783 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001784 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001785{
Wang Shilong47059d92014-07-03 18:22:07 +08001786 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001787 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1788 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001789
1790 spin_lock(&BTRFS_I(inode)->lock);
1791 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1792 BTRFS_I(inode)->defrag_bytes -= len;
1793 spin_unlock(&BTRFS_I(inode)->lock);
1794
Chris Mason75eff682008-12-15 15:54:40 -05001795 /*
1796 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001797 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001798 * bit, which is only set or cleared with irqs on
1799 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001800 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001801 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001802 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001803
Josef Bacik9e0baf62011-07-15 15:16:44 +00001804 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001805 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001806 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1807 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001808 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001809 spin_unlock(&BTRFS_I(inode)->lock);
1810 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001811
Josef Bacikb6d08f02013-09-27 14:57:43 -04001812 /*
1813 * We don't reserve metadata space for space cache inodes so we
1814 * don't need to call dellalloc_release_metadata if there is an
1815 * error.
1816 */
1817 if (*bits & EXTENT_DO_ACCOUNTING &&
1818 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001819 btrfs_delalloc_release_metadata(inode, len);
1820
Josef Bacik6a3891c2015-03-16 17:38:52 -04001821 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001822 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001823 return;
1824
Josef Bacik0cb59c92010-07-02 12:14:14 -04001825 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001826 && do_list && !(state->state & EXTENT_NORESERVE)
1827 && (*bits & (EXTENT_DO_ACCOUNTING |
1828 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001829 btrfs_free_reserved_data_space_noquota(inode,
1830 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001831
Miao Xie963d6782013-01-29 10:10:51 +00001832 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1833 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001834 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001835 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001836 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001837 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001838 &BTRFS_I(inode)->runtime_flags))
1839 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001840 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001841 }
Chris Mason291d6732008-01-29 15:55:23 -05001842}
1843
Chris Masond352ac62008-09-29 15:18:18 -04001844/*
1845 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1846 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001847 *
1848 * return 1 if page cannot be merged to bio
1849 * return 0 if page can be merged to bio
1850 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001851 */
Mike Christie81a75f62016-06-05 14:31:54 -05001852int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001853 size_t size, struct bio *bio,
1854 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001855{
1856 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001857 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001858 u64 length = 0;
1859 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001860 int ret;
1861
Chris Mason771ed682008-11-06 22:02:51 -05001862 if (bio_flags & EXTENT_BIO_COMPRESSED)
1863 return 0;
1864
Kent Overstreet4f024f32013-10-11 15:44:27 -07001865 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001866 map_length = length;
Mike Christieb3d3fa52016-06-05 14:31:53 -05001867 ret = btrfs_map_block(root->fs_info, bio_op(bio), logical,
Chris Masonf1885912008-04-09 16:28:12 -04001868 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001869 if (ret < 0)
1870 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001871 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001872 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001873 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001874}
1875
Chris Masond352ac62008-09-29 15:18:18 -04001876/*
1877 * in order to insert checksums into the metadata in large chunks,
1878 * we wait until bio submission time. All the pages in the bio are
1879 * checksummed and sums are attached onto the ordered extent record.
1880 *
1881 * At IO completion time the cums attached on the ordered extent record
1882 * are inserted into the btree
1883 */
Mike Christie81a75f62016-06-05 14:31:54 -05001884static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1885 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001886 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001887{
Chris Mason065631f2008-02-20 12:07:25 -05001888 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001889 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001890
Chris Masond20f7042008-12-08 16:58:54 -05001891 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001892 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001893 return 0;
1894}
Chris Masone0156402008-04-16 11:15:20 -04001895
Chris Mason4a69a412008-11-06 22:03:00 -05001896/*
1897 * in order to insert checksums into the metadata in large chunks,
1898 * we wait until bio submission time. All the pages in the bio are
1899 * checksummed and sums are attached onto the ordered extent record.
1900 *
1901 * At IO completion time the cums attached on the ordered extent record
1902 * are inserted into the btree
1903 */
Mike Christie81a75f62016-06-05 14:31:54 -05001904static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001905 int mirror_num, unsigned long bio_flags,
1906 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001907{
1908 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001909 int ret;
1910
Mike Christie81a75f62016-06-05 14:31:54 -05001911 ret = btrfs_map_bio(root, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001912 if (ret) {
1913 bio->bi_error = ret;
1914 bio_endio(bio);
1915 }
Stefan Behrens61891922012-11-05 18:51:52 +01001916 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001917}
1918
Chris Masond352ac62008-09-29 15:18:18 -04001919/*
Chris Masoncad321a2008-12-17 14:51:42 -05001920 * extent_io.c submission hook. This does the right thing for csum calculation
1921 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001922 */
Mike Christie81a75f62016-06-05 14:31:54 -05001923static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001924 int mirror_num, unsigned long bio_flags,
1925 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001926{
1927 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301928 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001929 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001930 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001931 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001932
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001933 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001934
Liu Bo83eea1f2012-07-10 05:28:39 -06001935 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301936 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001937
Mike Christie37226b22016-06-05 14:31:52 -05001938 if (bio_op(bio) != REQ_OP_WRITE) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001939 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1940 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001941 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001942
Chris Masond20f7042008-12-08 16:58:54 -05001943 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001944 ret = btrfs_submit_compressed_read(inode, bio,
1945 mirror_num,
1946 bio_flags);
1947 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001948 } else if (!skip_sum) {
1949 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1950 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001951 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001952 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001953 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001954 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001955 /* csum items have already been cloned */
1956 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1957 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001958 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001959 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -05001960 inode, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001961 bio_flags, bio_offset,
1962 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001963 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001964 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001965 } else if (!skip_sum) {
1966 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1967 if (ret)
1968 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001969 }
1970
Chris Mason0b86a832008-03-24 15:01:56 -04001971mapit:
Mike Christie81a75f62016-06-05 14:31:54 -05001972 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001973
1974out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001975 if (ret < 0) {
1976 bio->bi_error = ret;
1977 bio_endio(bio);
1978 }
Stefan Behrens61891922012-11-05 18:51:52 +01001979 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001980}
Chris Mason6885f302008-02-20 16:11:05 -05001981
Chris Masond352ac62008-09-29 15:18:18 -04001982/*
1983 * given a list of ordered sums record them in the inode. This happens
1984 * at IO completion time based on sums calculated at bio submission time.
1985 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001986static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001987 struct inode *inode, u64 file_offset,
1988 struct list_head *list)
1989{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001990 struct btrfs_ordered_sum *sum;
1991
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001992 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001993 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001994 btrfs_csum_file_blocks(trans,
1995 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001996 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001997 }
1998 return 0;
1999}
2000
Josef Bacik2ac55d42010-02-03 19:33:23 +00002001int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002002 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002003{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002004 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002005 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002006 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002007}
2008
Chris Masond352ac62008-09-29 15:18:18 -04002009/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002010struct btrfs_writepage_fixup {
2011 struct page *page;
2012 struct btrfs_work work;
2013};
2014
Christoph Hellwigb2950862008-12-02 09:54:17 -05002015static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002016{
2017 struct btrfs_writepage_fixup *fixup;
2018 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002019 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002020 struct page *page;
2021 struct inode *inode;
2022 u64 page_start;
2023 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002024 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002025
2026 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2027 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002028again:
Chris Mason247e7432008-07-17 12:53:51 -04002029 lock_page(page);
2030 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2031 ClearPageChecked(page);
2032 goto out_page;
2033 }
2034
2035 inode = page->mapping->host;
2036 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002037 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002038
David Sterbaff13db42015-12-03 14:30:40 +01002039 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002040 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002041
2042 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002043 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002044 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002045
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302046 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002047 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002048 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002049 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2050 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002051 unlock_page(page);
2052 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002053 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002054 goto again;
2055 }
Chris Mason247e7432008-07-17 12:53:51 -04002056
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002057 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002058 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002059 if (ret) {
2060 mapping_set_error(page->mapping, ret);
2061 end_extent_writepage(page, ret, page_start, page_end);
2062 ClearPageChecked(page);
2063 goto out;
2064 }
2065
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002066 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2067 0);
Chris Mason247e7432008-07-17 12:53:51 -04002068 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002069 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002070out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002071 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2072 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002073out_page:
2074 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002076 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002077}
2078
2079/*
2080 * There are a few paths in the higher layers of the kernel that directly
2081 * set the page dirty bit without asking the filesystem if it is a
2082 * good idea. This causes problems because we want to make sure COW
2083 * properly happens and the data=ordered rules are followed.
2084 *
Chris Masonc8b97812008-10-29 14:49:59 -04002085 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002086 * hasn't been properly setup for IO. We kick off an async process
2087 * to fix it up. The async helper will wait for ordered extents, set
2088 * the delalloc bit and make it safe to write the page.
2089 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002090static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002091{
2092 struct inode *inode = page->mapping->host;
2093 struct btrfs_writepage_fixup *fixup;
2094 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002095
Chris Mason8b62b722009-09-02 16:53:46 -04002096 /* this page is properly in the ordered list */
2097 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002098 return 0;
2099
2100 if (PageChecked(page))
2101 return -EAGAIN;
2102
2103 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2104 if (!fixup)
2105 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002106
Chris Mason247e7432008-07-17 12:53:51 -04002107 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002108 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002109 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2110 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002111 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002112 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002113 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002114}
2115
Yan Zhengd899e052008-10-30 14:25:28 -04002116static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2117 struct inode *inode, u64 file_pos,
2118 u64 disk_bytenr, u64 disk_num_bytes,
2119 u64 num_bytes, u64 ram_bytes,
2120 u8 compression, u8 encryption,
2121 u16 other_encoding, int extent_type)
2122{
2123 struct btrfs_root *root = BTRFS_I(inode)->root;
2124 struct btrfs_file_extent_item *fi;
2125 struct btrfs_path *path;
2126 struct extent_buffer *leaf;
2127 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002128 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002129 int ret;
2130
2131 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002132 if (!path)
2133 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002134
Chris Masona1ed8352009-09-11 12:27:37 -04002135 /*
2136 * we may be replacing one extent in the tree with another.
2137 * The new extent is pinned in the extent map, and we don't want
2138 * to drop it from the cache until it is completely in the btree.
2139 *
2140 * So, tell btrfs_drop_extents to leave this extent in the cache.
2141 * the caller is expected to unpin it and allow it to be merged
2142 * with the others.
2143 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002144 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2145 file_pos + num_bytes, NULL, 0,
2146 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002147 if (ret)
2148 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002149
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002150 if (!extent_inserted) {
2151 ins.objectid = btrfs_ino(inode);
2152 ins.offset = file_pos;
2153 ins.type = BTRFS_EXTENT_DATA_KEY;
2154
2155 path->leave_spinning = 1;
2156 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2157 sizeof(*fi));
2158 if (ret)
2159 goto out;
2160 }
Yan Zhengd899e052008-10-30 14:25:28 -04002161 leaf = path->nodes[0];
2162 fi = btrfs_item_ptr(leaf, path->slots[0],
2163 struct btrfs_file_extent_item);
2164 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2165 btrfs_set_file_extent_type(leaf, fi, extent_type);
2166 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2167 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2168 btrfs_set_file_extent_offset(leaf, fi, 0);
2169 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2170 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2171 btrfs_set_file_extent_compression(leaf, fi, compression);
2172 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2173 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002174
Yan Zhengd899e052008-10-30 14:25:28 -04002175 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002176 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002177
2178 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002179
2180 ins.objectid = disk_bytenr;
2181 ins.offset = disk_num_bytes;
2182 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183 ret = btrfs_alloc_reserved_file_extent(trans, root,
2184 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002185 btrfs_ino(inode), file_pos,
2186 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002187 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002188 * Release the reserved range from inode dirty range map, as it is
2189 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002190 */
2191 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002192out:
Yan Zhengd899e052008-10-30 14:25:28 -04002193 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002194
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002195 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002196}
2197
Liu Bo38c227d2013-01-29 03:18:40 +00002198/* snapshot-aware defrag */
2199struct sa_defrag_extent_backref {
2200 struct rb_node node;
2201 struct old_sa_defrag_extent *old;
2202 u64 root_id;
2203 u64 inum;
2204 u64 file_pos;
2205 u64 extent_offset;
2206 u64 num_bytes;
2207 u64 generation;
2208};
2209
2210struct old_sa_defrag_extent {
2211 struct list_head list;
2212 struct new_sa_defrag_extent *new;
2213
2214 u64 extent_offset;
2215 u64 bytenr;
2216 u64 offset;
2217 u64 len;
2218 int count;
2219};
2220
2221struct new_sa_defrag_extent {
2222 struct rb_root root;
2223 struct list_head head;
2224 struct btrfs_path *path;
2225 struct inode *inode;
2226 u64 file_pos;
2227 u64 len;
2228 u64 bytenr;
2229 u64 disk_len;
2230 u8 compress_type;
2231};
2232
2233static int backref_comp(struct sa_defrag_extent_backref *b1,
2234 struct sa_defrag_extent_backref *b2)
2235{
2236 if (b1->root_id < b2->root_id)
2237 return -1;
2238 else if (b1->root_id > b2->root_id)
2239 return 1;
2240
2241 if (b1->inum < b2->inum)
2242 return -1;
2243 else if (b1->inum > b2->inum)
2244 return 1;
2245
2246 if (b1->file_pos < b2->file_pos)
2247 return -1;
2248 else if (b1->file_pos > b2->file_pos)
2249 return 1;
2250
2251 /*
2252 * [------------------------------] ===> (a range of space)
2253 * |<--->| |<---->| =============> (fs/file tree A)
2254 * |<---------------------------->| ===> (fs/file tree B)
2255 *
2256 * A range of space can refer to two file extents in one tree while
2257 * refer to only one file extent in another tree.
2258 *
2259 * So we may process a disk offset more than one time(two extents in A)
2260 * and locate at the same extent(one extent in B), then insert two same
2261 * backrefs(both refer to the extent in B).
2262 */
2263 return 0;
2264}
2265
2266static void backref_insert(struct rb_root *root,
2267 struct sa_defrag_extent_backref *backref)
2268{
2269 struct rb_node **p = &root->rb_node;
2270 struct rb_node *parent = NULL;
2271 struct sa_defrag_extent_backref *entry;
2272 int ret;
2273
2274 while (*p) {
2275 parent = *p;
2276 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2277
2278 ret = backref_comp(backref, entry);
2279 if (ret < 0)
2280 p = &(*p)->rb_left;
2281 else
2282 p = &(*p)->rb_right;
2283 }
2284
2285 rb_link_node(&backref->node, parent, p);
2286 rb_insert_color(&backref->node, root);
2287}
2288
2289/*
2290 * Note the backref might has changed, and in this case we just return 0.
2291 */
2292static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2293 void *ctx)
2294{
2295 struct btrfs_file_extent_item *extent;
2296 struct btrfs_fs_info *fs_info;
2297 struct old_sa_defrag_extent *old = ctx;
2298 struct new_sa_defrag_extent *new = old->new;
2299 struct btrfs_path *path = new->path;
2300 struct btrfs_key key;
2301 struct btrfs_root *root;
2302 struct sa_defrag_extent_backref *backref;
2303 struct extent_buffer *leaf;
2304 struct inode *inode = new->inode;
2305 int slot;
2306 int ret;
2307 u64 extent_offset;
2308 u64 num_bytes;
2309
2310 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2311 inum == btrfs_ino(inode))
2312 return 0;
2313
2314 key.objectid = root_id;
2315 key.type = BTRFS_ROOT_ITEM_KEY;
2316 key.offset = (u64)-1;
2317
2318 fs_info = BTRFS_I(inode)->root->fs_info;
2319 root = btrfs_read_fs_root_no_name(fs_info, &key);
2320 if (IS_ERR(root)) {
2321 if (PTR_ERR(root) == -ENOENT)
2322 return 0;
2323 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002324 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002325 inum, offset, root_id);
2326 return PTR_ERR(root);
2327 }
2328
2329 key.objectid = inum;
2330 key.type = BTRFS_EXTENT_DATA_KEY;
2331 if (offset > (u64)-1 << 32)
2332 key.offset = 0;
2333 else
2334 key.offset = offset;
2335
2336 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302337 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002338 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002339 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002340
2341 while (1) {
2342 cond_resched();
2343
2344 leaf = path->nodes[0];
2345 slot = path->slots[0];
2346
2347 if (slot >= btrfs_header_nritems(leaf)) {
2348 ret = btrfs_next_leaf(root, path);
2349 if (ret < 0) {
2350 goto out;
2351 } else if (ret > 0) {
2352 ret = 0;
2353 goto out;
2354 }
2355 continue;
2356 }
2357
2358 path->slots[0]++;
2359
2360 btrfs_item_key_to_cpu(leaf, &key, slot);
2361
2362 if (key.objectid > inum)
2363 goto out;
2364
2365 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2366 continue;
2367
2368 extent = btrfs_item_ptr(leaf, slot,
2369 struct btrfs_file_extent_item);
2370
2371 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2372 continue;
2373
Liu Boe68afa42013-07-01 22:13:26 +08002374 /*
2375 * 'offset' refers to the exact key.offset,
2376 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2377 * (key.offset - extent_offset).
2378 */
2379 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002380 continue;
2381
Liu Boe68afa42013-07-01 22:13:26 +08002382 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002383 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002384
Liu Bo38c227d2013-01-29 03:18:40 +00002385 if (extent_offset >= old->extent_offset + old->offset +
2386 old->len || extent_offset + num_bytes <=
2387 old->extent_offset + old->offset)
2388 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002389 break;
2390 }
2391
2392 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2393 if (!backref) {
2394 ret = -ENOENT;
2395 goto out;
2396 }
2397
2398 backref->root_id = root_id;
2399 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002400 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002401 backref->num_bytes = num_bytes;
2402 backref->extent_offset = extent_offset;
2403 backref->generation = btrfs_file_extent_generation(leaf, extent);
2404 backref->old = old;
2405 backref_insert(&new->root, backref);
2406 old->count++;
2407out:
2408 btrfs_release_path(path);
2409 WARN_ON(ret);
2410 return ret;
2411}
2412
2413static noinline bool record_extent_backrefs(struct btrfs_path *path,
2414 struct new_sa_defrag_extent *new)
2415{
2416 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2417 struct old_sa_defrag_extent *old, *tmp;
2418 int ret;
2419
2420 new->path = path;
2421
2422 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002423 ret = iterate_inodes_from_logical(old->bytenr +
2424 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002425 path, record_one_backref,
2426 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002427 if (ret < 0 && ret != -ENOENT)
2428 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002429
2430 /* no backref to be processed for this extent */
2431 if (!old->count) {
2432 list_del(&old->list);
2433 kfree(old);
2434 }
2435 }
2436
2437 if (list_empty(&new->head))
2438 return false;
2439
2440 return true;
2441}
2442
2443static int relink_is_mergable(struct extent_buffer *leaf,
2444 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002445 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002446{
Liu Bo116e0022013-08-02 16:30:40 +08002447 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002448 return 0;
2449
2450 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2451 return 0;
2452
Liu Bo116e0022013-08-02 16:30:40 +08002453 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2454 return 0;
2455
2456 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002457 btrfs_file_extent_other_encoding(leaf, fi))
2458 return 0;
2459
2460 return 1;
2461}
2462
2463/*
2464 * Note the backref might has changed, and in this case we just return 0.
2465 */
2466static noinline int relink_extent_backref(struct btrfs_path *path,
2467 struct sa_defrag_extent_backref *prev,
2468 struct sa_defrag_extent_backref *backref)
2469{
2470 struct btrfs_file_extent_item *extent;
2471 struct btrfs_file_extent_item *item;
2472 struct btrfs_ordered_extent *ordered;
2473 struct btrfs_trans_handle *trans;
2474 struct btrfs_fs_info *fs_info;
2475 struct btrfs_root *root;
2476 struct btrfs_key key;
2477 struct extent_buffer *leaf;
2478 struct old_sa_defrag_extent *old = backref->old;
2479 struct new_sa_defrag_extent *new = old->new;
2480 struct inode *src_inode = new->inode;
2481 struct inode *inode;
2482 struct extent_state *cached = NULL;
2483 int ret = 0;
2484 u64 start;
2485 u64 len;
2486 u64 lock_start;
2487 u64 lock_end;
2488 bool merge = false;
2489 int index;
2490
2491 if (prev && prev->root_id == backref->root_id &&
2492 prev->inum == backref->inum &&
2493 prev->file_pos + prev->num_bytes == backref->file_pos)
2494 merge = true;
2495
2496 /* step 1: get root */
2497 key.objectid = backref->root_id;
2498 key.type = BTRFS_ROOT_ITEM_KEY;
2499 key.offset = (u64)-1;
2500
2501 fs_info = BTRFS_I(src_inode)->root->fs_info;
2502 index = srcu_read_lock(&fs_info->subvol_srcu);
2503
2504 root = btrfs_read_fs_root_no_name(fs_info, &key);
2505 if (IS_ERR(root)) {
2506 srcu_read_unlock(&fs_info->subvol_srcu, index);
2507 if (PTR_ERR(root) == -ENOENT)
2508 return 0;
2509 return PTR_ERR(root);
2510 }
Liu Bo38c227d2013-01-29 03:18:40 +00002511
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002512 if (btrfs_root_readonly(root)) {
2513 srcu_read_unlock(&fs_info->subvol_srcu, index);
2514 return 0;
2515 }
2516
Liu Bo38c227d2013-01-29 03:18:40 +00002517 /* step 2: get inode */
2518 key.objectid = backref->inum;
2519 key.type = BTRFS_INODE_ITEM_KEY;
2520 key.offset = 0;
2521
2522 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2523 if (IS_ERR(inode)) {
2524 srcu_read_unlock(&fs_info->subvol_srcu, index);
2525 return 0;
2526 }
2527
2528 srcu_read_unlock(&fs_info->subvol_srcu, index);
2529
2530 /* step 3: relink backref */
2531 lock_start = backref->file_pos;
2532 lock_end = backref->file_pos + backref->num_bytes - 1;
2533 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002534 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002535
2536 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2537 if (ordered) {
2538 btrfs_put_ordered_extent(ordered);
2539 goto out_unlock;
2540 }
2541
2542 trans = btrfs_join_transaction(root);
2543 if (IS_ERR(trans)) {
2544 ret = PTR_ERR(trans);
2545 goto out_unlock;
2546 }
2547
2548 key.objectid = backref->inum;
2549 key.type = BTRFS_EXTENT_DATA_KEY;
2550 key.offset = backref->file_pos;
2551
2552 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2553 if (ret < 0) {
2554 goto out_free_path;
2555 } else if (ret > 0) {
2556 ret = 0;
2557 goto out_free_path;
2558 }
2559
2560 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2561 struct btrfs_file_extent_item);
2562
2563 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2564 backref->generation)
2565 goto out_free_path;
2566
2567 btrfs_release_path(path);
2568
2569 start = backref->file_pos;
2570 if (backref->extent_offset < old->extent_offset + old->offset)
2571 start += old->extent_offset + old->offset -
2572 backref->extent_offset;
2573
2574 len = min(backref->extent_offset + backref->num_bytes,
2575 old->extent_offset + old->offset + old->len);
2576 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2577
2578 ret = btrfs_drop_extents(trans, root, inode, start,
2579 start + len, 1);
2580 if (ret)
2581 goto out_free_path;
2582again:
2583 key.objectid = btrfs_ino(inode);
2584 key.type = BTRFS_EXTENT_DATA_KEY;
2585 key.offset = start;
2586
Liu Boa09a0a72013-03-11 09:20:58 +00002587 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002588 if (merge) {
2589 struct btrfs_file_extent_item *fi;
2590 u64 extent_len;
2591 struct btrfs_key found_key;
2592
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002593 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002594 if (ret < 0)
2595 goto out_free_path;
2596
2597 path->slots[0]--;
2598 leaf = path->nodes[0];
2599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2600
2601 fi = btrfs_item_ptr(leaf, path->slots[0],
2602 struct btrfs_file_extent_item);
2603 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2604
Liu Bo116e0022013-08-02 16:30:40 +08002605 if (extent_len + found_key.offset == start &&
2606 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002607 btrfs_set_file_extent_num_bytes(leaf, fi,
2608 extent_len + len);
2609 btrfs_mark_buffer_dirty(leaf);
2610 inode_add_bytes(inode, len);
2611
2612 ret = 1;
2613 goto out_free_path;
2614 } else {
2615 merge = false;
2616 btrfs_release_path(path);
2617 goto again;
2618 }
2619 }
2620
2621 ret = btrfs_insert_empty_item(trans, root, path, &key,
2622 sizeof(*extent));
2623 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002624 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002625 goto out_free_path;
2626 }
2627
2628 leaf = path->nodes[0];
2629 item = btrfs_item_ptr(leaf, path->slots[0],
2630 struct btrfs_file_extent_item);
2631 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2632 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2633 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2634 btrfs_set_file_extent_num_bytes(leaf, item, len);
2635 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2636 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2637 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2638 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2639 btrfs_set_file_extent_encryption(leaf, item, 0);
2640 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2641
2642 btrfs_mark_buffer_dirty(leaf);
2643 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002644 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002645
2646 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2647 new->disk_len, 0,
2648 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002649 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002650 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002651 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002652 goto out_free_path;
2653 }
2654
2655 ret = 1;
2656out_free_path:
2657 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002658 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002659 btrfs_end_transaction(trans, root);
2660out_unlock:
2661 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2662 &cached, GFP_NOFS);
2663 iput(inode);
2664 return ret;
2665}
2666
Liu Bo6f519562013-10-29 10:45:05 +08002667static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2668{
2669 struct old_sa_defrag_extent *old, *tmp;
2670
2671 if (!new)
2672 return;
2673
2674 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002675 kfree(old);
2676 }
2677 kfree(new);
2678}
2679
Liu Bo38c227d2013-01-29 03:18:40 +00002680static void relink_file_extents(struct new_sa_defrag_extent *new)
2681{
2682 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002683 struct sa_defrag_extent_backref *backref;
2684 struct sa_defrag_extent_backref *prev = NULL;
2685 struct inode *inode;
2686 struct btrfs_root *root;
2687 struct rb_node *node;
2688 int ret;
2689
2690 inode = new->inode;
2691 root = BTRFS_I(inode)->root;
2692
2693 path = btrfs_alloc_path();
2694 if (!path)
2695 return;
2696
2697 if (!record_extent_backrefs(path, new)) {
2698 btrfs_free_path(path);
2699 goto out;
2700 }
2701 btrfs_release_path(path);
2702
2703 while (1) {
2704 node = rb_first(&new->root);
2705 if (!node)
2706 break;
2707 rb_erase(node, &new->root);
2708
2709 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2710
2711 ret = relink_extent_backref(path, prev, backref);
2712 WARN_ON(ret < 0);
2713
2714 kfree(prev);
2715
2716 if (ret == 1)
2717 prev = backref;
2718 else
2719 prev = NULL;
2720 cond_resched();
2721 }
2722 kfree(prev);
2723
2724 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002725out:
Liu Bo6f519562013-10-29 10:45:05 +08002726 free_sa_defrag_extent(new);
2727
Liu Bo38c227d2013-01-29 03:18:40 +00002728 atomic_dec(&root->fs_info->defrag_running);
2729 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002730}
2731
2732static struct new_sa_defrag_extent *
2733record_old_file_extents(struct inode *inode,
2734 struct btrfs_ordered_extent *ordered)
2735{
2736 struct btrfs_root *root = BTRFS_I(inode)->root;
2737 struct btrfs_path *path;
2738 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002739 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002740 struct new_sa_defrag_extent *new;
2741 int ret;
2742
2743 new = kmalloc(sizeof(*new), GFP_NOFS);
2744 if (!new)
2745 return NULL;
2746
2747 new->inode = inode;
2748 new->file_pos = ordered->file_offset;
2749 new->len = ordered->len;
2750 new->bytenr = ordered->start;
2751 new->disk_len = ordered->disk_len;
2752 new->compress_type = ordered->compress_type;
2753 new->root = RB_ROOT;
2754 INIT_LIST_HEAD(&new->head);
2755
2756 path = btrfs_alloc_path();
2757 if (!path)
2758 goto out_kfree;
2759
2760 key.objectid = btrfs_ino(inode);
2761 key.type = BTRFS_EXTENT_DATA_KEY;
2762 key.offset = new->file_pos;
2763
2764 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2765 if (ret < 0)
2766 goto out_free_path;
2767 if (ret > 0 && path->slots[0] > 0)
2768 path->slots[0]--;
2769
2770 /* find out all the old extents for the file range */
2771 while (1) {
2772 struct btrfs_file_extent_item *extent;
2773 struct extent_buffer *l;
2774 int slot;
2775 u64 num_bytes;
2776 u64 offset;
2777 u64 end;
2778 u64 disk_bytenr;
2779 u64 extent_offset;
2780
2781 l = path->nodes[0];
2782 slot = path->slots[0];
2783
2784 if (slot >= btrfs_header_nritems(l)) {
2785 ret = btrfs_next_leaf(root, path);
2786 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002787 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002788 else if (ret > 0)
2789 break;
2790 continue;
2791 }
2792
2793 btrfs_item_key_to_cpu(l, &key, slot);
2794
2795 if (key.objectid != btrfs_ino(inode))
2796 break;
2797 if (key.type != BTRFS_EXTENT_DATA_KEY)
2798 break;
2799 if (key.offset >= new->file_pos + new->len)
2800 break;
2801
2802 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2803
2804 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2805 if (key.offset + num_bytes < new->file_pos)
2806 goto next;
2807
2808 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2809 if (!disk_bytenr)
2810 goto next;
2811
2812 extent_offset = btrfs_file_extent_offset(l, extent);
2813
2814 old = kmalloc(sizeof(*old), GFP_NOFS);
2815 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002816 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002817
2818 offset = max(new->file_pos, key.offset);
2819 end = min(new->file_pos + new->len, key.offset + num_bytes);
2820
2821 old->bytenr = disk_bytenr;
2822 old->extent_offset = extent_offset;
2823 old->offset = offset - key.offset;
2824 old->len = end - offset;
2825 old->new = new;
2826 old->count = 0;
2827 list_add_tail(&old->list, &new->head);
2828next:
2829 path->slots[0]++;
2830 cond_resched();
2831 }
2832
2833 btrfs_free_path(path);
2834 atomic_inc(&root->fs_info->defrag_running);
2835
2836 return new;
2837
Liu Bo38c227d2013-01-29 03:18:40 +00002838out_free_path:
2839 btrfs_free_path(path);
2840out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002841 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002842 return NULL;
2843}
2844
Miao Xiee570fd22014-06-19 10:42:50 +08002845static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2846 u64 start, u64 len)
2847{
2848 struct btrfs_block_group_cache *cache;
2849
2850 cache = btrfs_lookup_block_group(root->fs_info, start);
2851 ASSERT(cache);
2852
2853 spin_lock(&cache->lock);
2854 cache->delalloc_bytes -= len;
2855 spin_unlock(&cache->lock);
2856
2857 btrfs_put_block_group(cache);
2858}
2859
Chris Masond352ac62008-09-29 15:18:18 -04002860/* as ordered data IO finishes, this gets called so we can finish
2861 * an ordered extent if the range of bytes in the file it covers are
2862 * fully written.
2863 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002864static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002865{
Josef Bacik5fd02042012-05-02 14:00:54 -04002866 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002867 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002868 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002869 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002870 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002871 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002872 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002873 int ret = 0;
2874 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002875 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002876 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002877
Liu Bo83eea1f2012-07-10 05:28:39 -06002878 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002879
Josef Bacik5fd02042012-05-02 14:00:54 -04002880 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2881 ret = -EIO;
2882 goto out;
2883 }
2884
Miao Xief6124962014-09-12 18:44:04 +08002885 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2886 ordered_extent->file_offset +
2887 ordered_extent->len - 1);
2888
Josef Bacik77cef2e2013-08-29 13:57:21 -04002889 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2890 truncated = true;
2891 logical_len = ordered_extent->truncated_len;
2892 /* Truncated the entire extent, don't bother adding */
2893 if (!logical_len)
2894 goto out;
2895 }
2896
Yan, Zhengc2167752009-11-12 09:34:21 +00002897 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002898 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002899
2900 /*
2901 * For mwrite(mmap + memset to write) case, we still reserve
2902 * space for NOCOW range.
2903 * As NOCOW won't cause a new delayed ref, just free the space
2904 */
2905 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2906 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002907 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2908 if (nolock)
2909 trans = btrfs_join_transaction_nolock(root);
2910 else
2911 trans = btrfs_join_transaction(root);
2912 if (IS_ERR(trans)) {
2913 ret = PTR_ERR(trans);
2914 trans = NULL;
2915 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002916 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002917 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2918 ret = btrfs_update_inode_fallback(trans, root, inode);
2919 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002920 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002921 goto out;
2922 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923
Josef Bacik2ac55d42010-02-03 19:33:23 +00002924 lock_extent_bits(io_tree, ordered_extent->file_offset,
2925 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002926 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002927
Liu Bo38c227d2013-01-29 03:18:40 +00002928 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2929 ordered_extent->file_offset + ordered_extent->len - 1,
2930 EXTENT_DEFRAG, 1, cached_state);
2931 if (ret) {
2932 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002933 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002934 /* the inode is shared */
2935 new = record_old_file_extents(inode, ordered_extent);
2936
2937 clear_extent_bit(io_tree, ordered_extent->file_offset,
2938 ordered_extent->file_offset + ordered_extent->len - 1,
2939 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2940 }
2941
Josef Bacik0cb59c92010-07-02 12:14:14 -04002942 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002943 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002944 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002945 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002946 if (IS_ERR(trans)) {
2947 ret = PTR_ERR(trans);
2948 trans = NULL;
2949 goto out_unlock;
2950 }
Chris Masona79b7d42014-05-22 16:18:52 -07002951
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002952 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002953
Chris Masonc8b97812008-10-29 14:49:59 -04002954 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002955 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002956 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002957 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002958 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002959 ordered_extent->file_offset,
2960 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002961 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002962 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002963 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002964 ret = insert_reserved_file_extent(trans, inode,
2965 ordered_extent->file_offset,
2966 ordered_extent->start,
2967 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002968 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002969 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002970 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002971 if (!ret)
2972 btrfs_release_delalloc_bytes(root,
2973 ordered_extent->start,
2974 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002975 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002976 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2977 ordered_extent->file_offset, ordered_extent->len,
2978 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002980 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002981 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002982 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002983
Chris Masone6dcd2d2008-07-17 12:53:50 -04002984 add_pending_csums(trans, inode, ordered_extent->file_offset,
2985 &ordered_extent->list);
2986
Josef Bacik6c760c02012-11-09 10:53:21 -05002987 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2988 ret = btrfs_update_inode_fallback(trans, root, inode);
2989 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002990 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002991 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002992 }
2993 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002994out_unlock:
2995 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2996 ordered_extent->file_offset +
2997 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002998out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002999 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003000 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003001 if (trans)
3002 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003003
Josef Bacik77cef2e2013-08-29 13:57:21 -04003004 if (ret || truncated) {
3005 u64 start, end;
3006
3007 if (truncated)
3008 start = ordered_extent->file_offset + logical_len;
3009 else
3010 start = ordered_extent->file_offset;
3011 end = ordered_extent->file_offset + ordered_extent->len - 1;
3012 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3013
3014 /* Drop the cache for the part of the extent we didn't write. */
3015 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003016
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003017 /*
3018 * If the ordered extent had an IOERR or something else went
3019 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003020 * back to the allocator. We only free the extent in the
3021 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003022 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003023 if ((ret || !logical_len) &&
3024 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003025 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3026 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003027 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003028 }
3029
3030
Josef Bacik5fd02042012-05-02 14:00:54 -04003031 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003032 * This needs to be done to make sure anybody waiting knows we are done
3033 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003034 */
3035 btrfs_remove_ordered_extent(inode, ordered_extent);
3036
Liu Bo38c227d2013-01-29 03:18:40 +00003037 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003038 if (new) {
3039 if (ret) {
3040 free_sa_defrag_extent(new);
3041 atomic_dec(&root->fs_info->defrag_running);
3042 } else {
3043 relink_file_extents(new);
3044 }
3045 }
Liu Bo38c227d2013-01-29 03:18:40 +00003046
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047 /* once for us */
3048 btrfs_put_ordered_extent(ordered_extent);
3049 /* once for the tree */
3050 btrfs_put_ordered_extent(ordered_extent);
3051
Josef Bacik5fd02042012-05-02 14:00:54 -04003052 return ret;
3053}
3054
3055static void finish_ordered_fn(struct btrfs_work *work)
3056{
3057 struct btrfs_ordered_extent *ordered_extent;
3058 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3059 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060}
3061
Christoph Hellwigb2950862008-12-02 09:54:17 -05003062static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003063 struct extent_state *state, int uptodate)
3064{
Josef Bacik5fd02042012-05-02 14:00:54 -04003065 struct inode *inode = page->mapping->host;
3066 struct btrfs_root *root = BTRFS_I(inode)->root;
3067 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003068 struct btrfs_workqueue *wq;
3069 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003070
liubo1abe9b82011-03-24 11:18:59 +00003071 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3072
Chris Mason8b62b722009-09-02 16:53:46 -04003073 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003074 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3075 end - start + 1, uptodate))
3076 return 0;
3077
Liu Bo9e0af232014-08-15 23:36:53 +08003078 if (btrfs_is_free_space_inode(inode)) {
3079 wq = root->fs_info->endio_freespace_worker;
3080 func = btrfs_freespace_write_helper;
3081 } else {
3082 wq = root->fs_info->endio_write_workers;
3083 func = btrfs_endio_write_helper;
3084 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003085
Liu Bo9e0af232014-08-15 23:36:53 +08003086 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3087 NULL);
3088 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003089
3090 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003091}
3092
Miao Xiedc380ae2014-09-12 18:43:55 +08003093static int __readpage_endio_check(struct inode *inode,
3094 struct btrfs_io_bio *io_bio,
3095 int icsum, struct page *page,
3096 int pgoff, u64 start, size_t len)
3097{
3098 char *kaddr;
3099 u32 csum_expected;
3100 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003101
3102 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3103
3104 kaddr = kmap_atomic(page);
3105 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3106 btrfs_csum_final(csum, (char *)&csum);
3107 if (csum != csum_expected)
3108 goto zeroit;
3109
3110 kunmap_atomic(kaddr);
3111 return 0;
3112zeroit:
David Sterba94647322015-10-08 11:01:36 +02003113 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3114 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003115 btrfs_ino(inode), start, csum, csum_expected);
3116 memset(kaddr + pgoff, 1, len);
3117 flush_dcache_page(page);
3118 kunmap_atomic(kaddr);
3119 if (csum_expected == 0)
3120 return 0;
3121 return -EIO;
3122}
3123
Chris Masond352ac62008-09-29 15:18:18 -04003124/*
Chris Masond352ac62008-09-29 15:18:18 -04003125 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003126 * if there's a match, we allow the bio to finish. If not, the code in
3127 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003128 */
Miao Xiefacc8a22013-07-25 19:22:34 +08003129static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3130 u64 phy_offset, struct page *page,
3131 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003132{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003133 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003134 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003135 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003136 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003137
Chris Masond20f7042008-12-08 16:58:54 -05003138 if (PageChecked(page)) {
3139 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003140 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003141 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003142
3143 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003144 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003145
Yan Zheng17d217f2008-12-12 10:03:38 -05003146 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003147 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003148 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003149 return 0;
3150 }
3151
Miao Xiefacc8a22013-07-25 19:22:34 +08003152 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003153 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3154 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003155}
Chris Masonb888db22007-08-27 16:49:44 -04003156
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003157void btrfs_add_delayed_iput(struct inode *inode)
3158{
3159 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003160 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003161
3162 if (atomic_add_unless(&inode->i_count, -1, 1))
3163 return;
3164
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003165 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003166 if (binode->delayed_iput_count == 0) {
3167 ASSERT(list_empty(&binode->delayed_iput));
3168 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3169 } else {
3170 binode->delayed_iput_count++;
3171 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003172 spin_unlock(&fs_info->delayed_iput_lock);
3173}
3174
3175void btrfs_run_delayed_iputs(struct btrfs_root *root)
3176{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003177 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003179 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003180 while (!list_empty(&fs_info->delayed_iputs)) {
3181 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003182
David Sterba8089fe62015-11-19 14:15:51 +01003183 inode = list_first_entry(&fs_info->delayed_iputs,
3184 struct btrfs_inode, delayed_iput);
3185 if (inode->delayed_iput_count) {
3186 inode->delayed_iput_count--;
3187 list_move_tail(&inode->delayed_iput,
3188 &fs_info->delayed_iputs);
3189 } else {
3190 list_del_init(&inode->delayed_iput);
3191 }
3192 spin_unlock(&fs_info->delayed_iput_lock);
3193 iput(&inode->vfs_inode);
3194 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003195 }
David Sterba8089fe62015-11-19 14:15:51 +01003196 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003197}
3198
Yan, Zhengd68fc572010-05-16 10:49:58 -04003199/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003200 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003201 * files in the subvolume, it removes orphan item and frees block_rsv
3202 * structure.
3203 */
3204void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3205 struct btrfs_root *root)
3206{
Josef Bacik90290e12011-12-02 15:44:12 -05003207 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 int ret;
3209
Josef Bacik8a35d952012-05-23 14:26:42 -04003210 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3212 return;
3213
Josef Bacik90290e12011-12-02 15:44:12 -05003214 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003215 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003216 spin_unlock(&root->orphan_lock);
3217 return;
3218 }
3219
3220 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3221 spin_unlock(&root->orphan_lock);
3222 return;
3223 }
3224
3225 block_rsv = root->orphan_block_rsv;
3226 root->orphan_block_rsv = NULL;
3227 spin_unlock(&root->orphan_lock);
3228
Miao Xie27cdeb72014-04-02 19:51:05 +08003229 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230 btrfs_root_refs(&root->root_item) > 0) {
3231 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3232 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003233 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003234 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003235 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003236 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3237 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 }
3239
Josef Bacik90290e12011-12-02 15:44:12 -05003240 if (block_rsv) {
3241 WARN_ON(block_rsv->size > 0);
3242 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 }
3244}
3245
3246/*
Josef Bacik7b128762008-07-24 12:17:14 -04003247 * This creates an orphan entry for the given inode in case something goes
3248 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 *
3250 * NOTE: caller of this function should reserve 5 units of metadata for
3251 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003252 */
3253int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3254{
3255 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 struct btrfs_block_rsv *block_rsv = NULL;
3257 int reserve = 0;
3258 int insert = 0;
3259 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003260
Yan, Zhengd68fc572010-05-16 10:49:58 -04003261 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003262 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003263 if (!block_rsv)
3264 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003265 }
3266
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 spin_lock(&root->orphan_lock);
3268 if (!root->orphan_block_rsv) {
3269 root->orphan_block_rsv = block_rsv;
3270 } else if (block_rsv) {
3271 btrfs_free_block_rsv(root, block_rsv);
3272 block_rsv = NULL;
3273 }
Josef Bacik7b128762008-07-24 12:17:14 -04003274
Josef Bacik8a35d952012-05-23 14:26:42 -04003275 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3276 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277#if 0
3278 /*
3279 * For proper ENOSPC handling, we should do orphan
3280 * cleanup when mounting. But this introduces backward
3281 * compatibility issue.
3282 */
3283 if (!xchg(&root->orphan_item_inserted, 1))
3284 insert = 2;
3285 else
3286 insert = 1;
3287#endif
3288 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003289 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 }
Josef Bacik7b128762008-07-24 12:17:14 -04003291
Josef Bacik72ac3c02012-05-23 14:13:11 -04003292 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3293 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003294 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003296
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 /* grab metadata reservation from transaction handle */
3298 if (reserve) {
3299 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003300 ASSERT(!ret);
3301 if (ret) {
3302 atomic_dec(&root->orphan_inodes);
3303 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3304 &BTRFS_I(inode)->runtime_flags);
3305 if (insert)
3306 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3307 &BTRFS_I(inode)->runtime_flags);
3308 return ret;
3309 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 }
3311
3312 /* insert an orphan item to track this unlinked/truncated file */
3313 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003314 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003315 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003316 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003317 if (reserve) {
3318 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3319 &BTRFS_I(inode)->runtime_flags);
3320 btrfs_orphan_release_metadata(inode);
3321 }
3322 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003323 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3324 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003325 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003326 return ret;
3327 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003328 }
3329 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003330 }
3331
3332 /* insert an orphan item to track subvolume contains orphan files */
3333 if (insert >= 2) {
3334 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3335 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003336 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003337 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003338 return ret;
3339 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 }
3341 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003342}
3343
3344/*
3345 * We have done the truncate/delete so we can go ahead and remove the orphan
3346 * item for this particular inode.
3347 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003348static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3349 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003350{
3351 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 int delete_item = 0;
3353 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003354 int ret = 0;
3355
Yan, Zhengd68fc572010-05-16 10:49:58 -04003356 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003357 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3358 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003359 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003360
Josef Bacik72ac3c02012-05-23 14:13:11 -04003361 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3362 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003364 spin_unlock(&root->orphan_lock);
3365
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003366 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003367 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003368 if (trans)
3369 ret = btrfs_del_orphan_item(trans, root,
3370 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003371 }
Josef Bacik7b128762008-07-24 12:17:14 -04003372
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003373 if (release_rsv)
3374 btrfs_orphan_release_metadata(inode);
3375
Josef Bacik4ef31a42013-08-13 14:10:08 -04003376 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003377}
3378
3379/*
3380 * this cleans up any orphans that may be left on the list from the last use
3381 * of this root.
3382 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003383int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003384{
3385 struct btrfs_path *path;
3386 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003387 struct btrfs_key key, found_key;
3388 struct btrfs_trans_handle *trans;
3389 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003390 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003391 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3392
Yan, Zhengd68fc572010-05-16 10:49:58 -04003393 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003394 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003395
3396 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003397 if (!path) {
3398 ret = -ENOMEM;
3399 goto out;
3400 }
David Sterbae4058b52015-11-27 16:31:35 +01003401 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003402
3403 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003404 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003405 key.offset = (u64)-1;
3406
Josef Bacik7b128762008-07-24 12:17:14 -04003407 while (1) {
3408 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003409 if (ret < 0)
3410 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003411
3412 /*
3413 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003414 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003415 * find the key and see if we have stuff that matches
3416 */
3417 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003418 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003419 if (path->slots[0] == 0)
3420 break;
3421 path->slots[0]--;
3422 }
3423
3424 /* pull out the item */
3425 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003426 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3427
3428 /* make sure the item matches what we want */
3429 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3430 break;
David Sterba962a2982014-06-04 18:41:45 +02003431 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003432 break;
3433
3434 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003435 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003436
3437 /*
3438 * this is where we are basically btrfs_lookup, without the
3439 * crossing root thing. we store the inode number in the
3440 * offset of the orphan item.
3441 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003442
3443 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003444 btrfs_err(root->fs_info,
3445 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003446 ret = -EINVAL;
3447 goto out;
3448 }
3449
3450 last_objectid = found_key.offset;
3451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003452 found_key.objectid = found_key.offset;
3453 found_key.type = BTRFS_INODE_ITEM_KEY;
3454 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003455 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303456 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003457 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003458 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003459
Filipe Manana67710892016-06-06 11:51:25 +01003460 if (ret == -ENOENT && root == root->fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003461 struct btrfs_root *dead_root;
3462 struct btrfs_fs_info *fs_info = root->fs_info;
3463 int is_dead_root = 0;
3464
3465 /*
3466 * this is an orphan in the tree root. Currently these
3467 * could come from 2 sources:
3468 * a) a snapshot deletion in progress
3469 * b) a free space cache inode
3470 * We need to distinguish those two, as the snapshot
3471 * orphan must not get deleted.
3472 * find_dead_roots already ran before us, so if this
3473 * is a snapshot deletion, we should find the root
3474 * in the dead_roots list
3475 */
3476 spin_lock(&fs_info->trans_lock);
3477 list_for_each_entry(dead_root, &fs_info->dead_roots,
3478 root_list) {
3479 if (dead_root->root_key.objectid ==
3480 found_key.objectid) {
3481 is_dead_root = 1;
3482 break;
3483 }
3484 }
3485 spin_unlock(&fs_info->trans_lock);
3486 if (is_dead_root) {
3487 /* prevent this orphan from being found again */
3488 key.offset = found_key.objectid - 1;
3489 continue;
3490 }
3491 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003492 /*
3493 * Inode is already gone but the orphan item is still there,
3494 * kill the orphan item.
3495 */
Filipe Manana67710892016-06-06 11:51:25 +01003496 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003497 trans = btrfs_start_transaction(root, 1);
3498 if (IS_ERR(trans)) {
3499 ret = PTR_ERR(trans);
3500 goto out;
3501 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003502 btrfs_debug(root->fs_info, "auto deleting %Lu",
3503 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003504 ret = btrfs_del_orphan_item(trans, root,
3505 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003506 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003507 if (ret)
3508 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003509 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003510 }
Josef Bacik7b128762008-07-24 12:17:14 -04003511
Josef Bacik7b128762008-07-24 12:17:14 -04003512 /*
3513 * add this inode to the orphan list so btrfs_orphan_del does
3514 * the proper thing when we hit it
3515 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003516 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3517 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003518 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003519
Josef Bacik7b128762008-07-24 12:17:14 -04003520 /* if we have links, this was a truncate, lets do that */
3521 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303522 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003523 iput(inode);
3524 continue;
3525 }
Josef Bacik7b128762008-07-24 12:17:14 -04003526 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003527
3528 /* 1 for the orphan item deletion. */
3529 trans = btrfs_start_transaction(root, 1);
3530 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003531 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003532 ret = PTR_ERR(trans);
3533 goto out;
3534 }
3535 ret = btrfs_orphan_add(trans, inode);
3536 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003537 if (ret) {
3538 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003539 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003540 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003541
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003542 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003543 if (ret)
3544 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003545 } else {
3546 nr_unlink++;
3547 }
3548
3549 /* this will do delete_inode and everything for us */
3550 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003551 if (ret)
3552 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003553 }
Miao Xie3254c872011-11-10 20:45:05 -05003554 /* release the path since we're done with it */
3555 btrfs_release_path(path);
3556
Yan, Zhengd68fc572010-05-16 10:49:58 -04003557 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3558
3559 if (root->orphan_block_rsv)
3560 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3561 (u64)-1);
3562
Miao Xie27cdeb72014-04-02 19:51:05 +08003563 if (root->orphan_block_rsv ||
3564 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003565 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003566 if (!IS_ERR(trans))
3567 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003568 }
Josef Bacik7b128762008-07-24 12:17:14 -04003569
3570 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003571 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003572 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003573 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003574
3575out:
3576 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003577 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003578 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003579 btrfs_free_path(path);
3580 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003581}
3582
Chris Masond352ac62008-09-29 15:18:18 -04003583/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003584 * very simple check to peek ahead in the leaf looking for xattrs. If we
3585 * don't find any xattrs, we know there can't be any acls.
3586 *
3587 * slot is the slot the inode is in, objectid is the objectid of the inode
3588 */
3589static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003590 int slot, u64 objectid,
3591 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003592{
3593 u32 nritems = btrfs_header_nritems(leaf);
3594 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003595 static u64 xattr_access = 0;
3596 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003597 int scanned = 0;
3598
Josef Bacikf23b5a52013-06-19 10:16:26 -04003599 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003600 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3601 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3602 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3603 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003604 }
3605
Chris Mason46a53cc2009-04-27 11:47:50 -04003606 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003607 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003608 while (slot < nritems) {
3609 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3610
3611 /* we found a different objectid, there must not be acls */
3612 if (found_key.objectid != objectid)
3613 return 0;
3614
3615 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003616 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003617 if (*first_xattr_slot == -1)
3618 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003619 if (found_key.offset == xattr_access ||
3620 found_key.offset == xattr_default)
3621 return 1;
3622 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003623
3624 /*
3625 * we found a key greater than an xattr key, there can't
3626 * be any acls later on
3627 */
3628 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3629 return 0;
3630
3631 slot++;
3632 scanned++;
3633
3634 /*
3635 * it goes inode, inode backrefs, xattrs, extents,
3636 * so if there are a ton of hard links to an inode there can
3637 * be a lot of backrefs. Don't waste time searching too hard,
3638 * this is just an optimization
3639 */
3640 if (scanned >= 8)
3641 break;
3642 }
3643 /* we hit the end of the leaf before we found an xattr or
3644 * something larger than an xattr. We have to assume the inode
3645 * has acls
3646 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003647 if (*first_xattr_slot == -1)
3648 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003649 return 1;
3650}
3651
3652/*
Chris Masond352ac62008-09-29 15:18:18 -04003653 * read an inode from the btree into the in-memory inode
3654 */
Filipe Manana67710892016-06-06 11:51:25 +01003655static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003656{
3657 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003658 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003659 struct btrfs_inode_item *inode_item;
3660 struct btrfs_root *root = BTRFS_I(inode)->root;
3661 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003662 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003663 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003664 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003665 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003666 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003667 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003668
3669 ret = btrfs_fill_inode(inode, &rdev);
3670 if (!ret)
3671 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003672
3673 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003674 if (!path) {
3675 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003676 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003677 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003678
Chris Mason39279cc2007-06-12 06:35:45 -04003679 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003680
Chris Mason39279cc2007-06-12 06:35:45 -04003681 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003682 if (ret) {
3683 if (ret > 0)
3684 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003685 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003686 }
Chris Mason39279cc2007-06-12 06:35:45 -04003687
Chris Mason5f39d392007-10-15 16:14:19 -04003688 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003689
3690 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003691 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003692
Chris Mason5f39d392007-10-15 16:14:19 -04003693 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3694 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003695 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003696 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003697 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3698 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003699 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003700
David Sterbaa937b972014-12-12 17:39:12 +01003701 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3702 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003703
David Sterbaa937b972014-12-12 17:39:12 +01003704 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3705 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003706
David Sterbaa937b972014-12-12 17:39:12 +01003707 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3708 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003709
chandan r9cc97d62012-07-04 12:48:07 +05303710 BTRFS_I(inode)->i_otime.tv_sec =
3711 btrfs_timespec_sec(leaf, &inode_item->otime);
3712 BTRFS_I(inode)->i_otime.tv_nsec =
3713 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003714
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003715 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003716 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003717 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3718
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003719 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003720 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003721 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003722 rdev = btrfs_inode_rdev(leaf, inode_item);
3723
Josef Bacikaec74772008-07-24 12:12:38 -04003724 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003725 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003726
3727cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003728 /*
3729 * If we were modified in the current generation and evicted from memory
3730 * and then re-read we need to do a full sync since we don't have any
3731 * idea about which extents were modified before we were evicted from
3732 * cache.
3733 *
3734 * This is required for both inode re-read from disk and delayed inode
3735 * in delayed_nodes_tree.
3736 */
3737 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3738 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3739 &BTRFS_I(inode)->runtime_flags);
3740
Filipe Mananabde6c242015-07-24 00:00:19 +01003741 /*
3742 * We don't persist the id of the transaction where an unlink operation
3743 * against the inode was last made. So here we assume the inode might
3744 * have been evicted, and therefore the exact value of last_unlink_trans
3745 * lost, and set it to last_trans to avoid metadata inconsistencies
3746 * between the inode and its parent if the inode is fsync'ed and the log
3747 * replayed. For example, in the scenario:
3748 *
3749 * touch mydir/foo
3750 * ln mydir/foo mydir/bar
3751 * sync
3752 * unlink mydir/bar
3753 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3754 * xfs_io -c fsync mydir/foo
3755 * <power failure>
3756 * mount fs, triggers fsync log replay
3757 *
3758 * We must make sure that when we fsync our inode foo we also log its
3759 * parent inode, otherwise after log replay the parent still has the
3760 * dentry with the "bar" name but our inode foo has a link count of 1
3761 * and doesn't have an inode ref with the name "bar" anymore.
3762 *
3763 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003764 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003765 * transaction commits on fsync if our inode is a directory, or if our
3766 * inode is not a directory, logging its parent unnecessarily.
3767 */
3768 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3769
Miao Xie67de1172013-12-26 13:07:06 +08003770 path->slots[0]++;
3771 if (inode->i_nlink != 1 ||
3772 path->slots[0] >= btrfs_header_nritems(leaf))
3773 goto cache_acl;
3774
3775 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3776 if (location.objectid != btrfs_ino(inode))
3777 goto cache_acl;
3778
3779 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3780 if (location.type == BTRFS_INODE_REF_KEY) {
3781 struct btrfs_inode_ref *ref;
3782
3783 ref = (struct btrfs_inode_ref *)ptr;
3784 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3785 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3786 struct btrfs_inode_extref *extref;
3787
3788 extref = (struct btrfs_inode_extref *)ptr;
3789 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3790 extref);
3791 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003792cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003793 /*
3794 * try to precache a NULL acl entry for files that don't have
3795 * any xattrs or acls
3796 */
Li Zefan33345d012011-04-20 10:31:50 +08003797 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003798 btrfs_ino(inode), &first_xattr_slot);
3799 if (first_xattr_slot != -1) {
3800 path->slots[0] = first_xattr_slot;
3801 ret = btrfs_load_inode_props(inode, path);
3802 if (ret)
3803 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003804 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003805 btrfs_ino(inode),
3806 root->root_key.objectid, ret);
3807 }
3808 btrfs_free_path(path);
3809
Al Viro72c04902009-06-24 16:58:48 -04003810 if (!maybe_acls)
3811 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003812
Chris Mason39279cc2007-06-12 06:35:45 -04003813 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003814 case S_IFREG:
3815 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003816 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003817 inode->i_fop = &btrfs_file_operations;
3818 inode->i_op = &btrfs_file_inode_operations;
3819 break;
3820 case S_IFDIR:
3821 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval79babd42017-01-25 17:06:38 -08003822 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003823 break;
3824 case S_IFLNK:
3825 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003826 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003827 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3828 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003829 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003830 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003831 init_special_inode(inode, inode->i_mode, rdev);
3832 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003833 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003834
3835 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003836 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003837
3838make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003839 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003840 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003841 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003842}
3843
Chris Masond352ac62008-09-29 15:18:18 -04003844/*
3845 * given a leaf and an inode, copy the inode fields into the leaf
3846 */
Chris Masone02119d2008-09-05 16:13:11 -04003847static void fill_inode_item(struct btrfs_trans_handle *trans,
3848 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003849 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003850 struct inode *inode)
3851{
Liu Bo51fab692012-12-27 09:01:21 +00003852 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003853
Liu Bo51fab692012-12-27 09:01:21 +00003854 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003855
Liu Bo51fab692012-12-27 09:01:21 +00003856 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3857 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3858 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3859 &token);
3860 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3861 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003862
David Sterbaa937b972014-12-12 17:39:12 +01003863 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003864 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003865 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003866 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003867
David Sterbaa937b972014-12-12 17:39:12 +01003868 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003869 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003870 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003871 inode->i_mtime.tv_nsec, &token);
3872
David Sterbaa937b972014-12-12 17:39:12 +01003873 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003874 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003875 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003876 inode->i_ctime.tv_nsec, &token);
3877
chandan r9cc97d62012-07-04 12:48:07 +05303878 btrfs_set_token_timespec_sec(leaf, &item->otime,
3879 BTRFS_I(inode)->i_otime.tv_sec, &token);
3880 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3881 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3882
Liu Bo51fab692012-12-27 09:01:21 +00003883 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3884 &token);
3885 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3886 &token);
3887 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3888 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3889 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3890 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3891 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003892}
3893
Chris Masond352ac62008-09-29 15:18:18 -04003894/*
3895 * copy everything in the in-memory inode into the btree.
3896 */
Chris Mason21151332011-11-10 20:39:08 -05003897static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003898 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003899{
3900 struct btrfs_inode_item *inode_item;
3901 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003902 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003903 int ret;
3904
3905 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003906 if (!path)
3907 return -ENOMEM;
3908
Chris Masonb9473432009-03-13 11:00:37 -04003909 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003910 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3911 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003912 if (ret) {
3913 if (ret > 0)
3914 ret = -ENOENT;
3915 goto failed;
3916 }
3917
Chris Mason5f39d392007-10-15 16:14:19 -04003918 leaf = path->nodes[0];
3919 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003920 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003921
Chris Masone02119d2008-09-05 16:13:11 -04003922 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003923 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003924 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003925 ret = 0;
3926failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003927 btrfs_free_path(path);
3928 return ret;
3929}
3930
Chris Masond352ac62008-09-29 15:18:18 -04003931/*
Chris Mason21151332011-11-10 20:39:08 -05003932 * copy everything in the in-memory inode into the btree.
3933 */
3934noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3935 struct btrfs_root *root, struct inode *inode)
3936{
3937 int ret;
3938
3939 /*
3940 * If the inode is a free space inode, we can deadlock during commit
3941 * if we put it into the delayed code.
3942 *
3943 * The data relocation inode should also be directly updated
3944 * without delay
3945 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003946 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003947 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacikafcdd122016-09-02 15:40:02 -04003948 && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003949 btrfs_update_root_times(trans, root);
3950
Chris Mason21151332011-11-10 20:39:08 -05003951 ret = btrfs_delayed_update_inode(trans, root, inode);
3952 if (!ret)
3953 btrfs_set_inode_last_trans(trans, inode);
3954 return ret;
3955 }
3956
3957 return btrfs_update_inode_item(trans, root, inode);
3958}
3959
Josef Bacikbe6aef62012-10-22 15:43:12 -04003960noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3961 struct btrfs_root *root,
3962 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003963{
3964 int ret;
3965
3966 ret = btrfs_update_inode(trans, root, inode);
3967 if (ret == -ENOSPC)
3968 return btrfs_update_inode_item(trans, root, inode);
3969 return ret;
3970}
3971
3972/*
Chris Masond352ac62008-09-29 15:18:18 -04003973 * unlink helper that gets used here in inode.c and in the tree logging
3974 * recovery code. It remove a link in a directory with a given name, and
3975 * also drops the back refs in the inode to the directory
3976 */
Al Viro92986792011-03-04 17:14:37 +00003977static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3978 struct btrfs_root *root,
3979 struct inode *dir, struct inode *inode,
3980 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003981{
3982 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003983 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003984 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003986 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003987 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003988 u64 ino = btrfs_ino(inode);
3989 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003990
3991 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003992 if (!path) {
3993 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003994 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003995 }
3996
Chris Masonb9473432009-03-13 11:00:37 -04003997 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003998 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003999 name, name_len, -1);
4000 if (IS_ERR(di)) {
4001 ret = PTR_ERR(di);
4002 goto err;
4003 }
4004 if (!di) {
4005 ret = -ENOENT;
4006 goto err;
4007 }
Chris Mason5f39d392007-10-15 16:14:19 -04004008 leaf = path->nodes[0];
4009 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004010 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004011 if (ret)
4012 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004013 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004014
Miao Xie67de1172013-12-26 13:07:06 +08004015 /*
4016 * If we don't have dir index, we have to get it by looking up
4017 * the inode ref, since we get the inode ref, remove it directly,
4018 * it is unnecessary to do delayed deletion.
4019 *
4020 * But if we have dir index, needn't search inode ref to get it.
4021 * Since the inode ref is close to the inode item, it is better
4022 * that we delay to delete it, and just do this deletion when
4023 * we update the inode item.
4024 */
4025 if (BTRFS_I(inode)->dir_index) {
4026 ret = btrfs_delayed_delete_inode_ref(inode);
4027 if (!ret) {
4028 index = BTRFS_I(inode)->dir_index;
4029 goto skip_backref;
4030 }
4031 }
4032
Li Zefan33345d012011-04-20 10:31:50 +08004033 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4034 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004035 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004036 btrfs_info(root->fs_info,
4037 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004038 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004039 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004040 goto err;
4041 }
Miao Xie67de1172013-12-26 13:07:06 +08004042skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004043 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004044 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004045 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004046 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004047 }
Chris Mason39279cc2007-06-12 06:35:45 -04004048
Chris Masone02119d2008-09-05 16:13:11 -04004049 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004050 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004051 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004052 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004053 goto err;
4054 }
Chris Masone02119d2008-09-05 16:13:11 -04004055
4056 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4057 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004058 if (ret == -ENOENT)
4059 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004060 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004061 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004062err:
4063 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004064 if (ret)
4065 goto out;
4066
4067 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004068 inode_inc_iversion(inode);
4069 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004070 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004071 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004072 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004073out:
Chris Mason39279cc2007-06-12 06:35:45 -04004074 return ret;
4075}
4076
Al Viro92986792011-03-04 17:14:37 +00004077int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4078 struct btrfs_root *root,
4079 struct inode *dir, struct inode *inode,
4080 const char *name, int name_len)
4081{
4082 int ret;
4083 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4084 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004085 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004086 ret = btrfs_update_inode(trans, root, inode);
4087 }
4088 return ret;
4089}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004090
4091/*
4092 * helper to start transaction for unlink and rmdir.
4093 *
Josef Bacikd52be812013-05-29 14:54:47 -04004094 * unlink and rmdir are special in btrfs, they do not always free space, so
4095 * if we cannot make our reservations the normal way try and see if there is
4096 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4097 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004098 */
Josef Bacikd52be812013-05-29 14:54:47 -04004099static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004100{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004101 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004102
Josef Bacike70bea52011-10-11 14:18:24 -04004103 /*
4104 * 1 for the possible orphan item
4105 * 1 for the dir item
4106 * 1 for the dir index
4107 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004108 * 1 for the inode
4109 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004110 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004111}
4112
Chris Mason39279cc2007-06-12 06:35:45 -04004113static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4114{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004115 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004116 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004117 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004118 int ret;
4119
Josef Bacikd52be812013-05-29 14:54:47 -04004120 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004121 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004122 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004123
David Howells2b0143b2015-03-17 22:25:59 +00004124 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004125
David Howells2b0143b2015-03-17 22:25:59 +00004126 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004127 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004128 if (ret)
4129 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004130
Yan, Zhenga22285a2010-05-16 10:48:46 -04004131 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004132 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004133 if (ret)
4134 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004135 }
Josef Bacik7b128762008-07-24 12:17:14 -04004136
Tsutomu Itohb5324022011-07-19 07:27:20 +00004137out:
Josef Bacikd52be812013-05-29 14:54:47 -04004138 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004139 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004140 return ret;
4141}
4142
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4144 struct btrfs_root *root,
4145 struct inode *dir, u64 objectid,
4146 const char *name, int name_len)
4147{
4148 struct btrfs_path *path;
4149 struct extent_buffer *leaf;
4150 struct btrfs_dir_item *di;
4151 struct btrfs_key key;
4152 u64 index;
4153 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004154 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004155
4156 path = btrfs_alloc_path();
4157 if (!path)
4158 return -ENOMEM;
4159
Li Zefan33345d012011-04-20 10:31:50 +08004160 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004161 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 if (IS_ERR_OR_NULL(di)) {
4163 if (!di)
4164 ret = -ENOENT;
4165 else
4166 ret = PTR_ERR(di);
4167 goto out;
4168 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004169
4170 leaf = path->nodes[0];
4171 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4172 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4173 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004174 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004175 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 goto out;
4177 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004178 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004179
4180 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4181 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004182 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004183 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004184 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004185 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186 goto out;
4187 }
Li Zefan33345d012011-04-20 10:31:50 +08004188 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004189 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004190 if (IS_ERR_OR_NULL(di)) {
4191 if (!di)
4192 ret = -ENOENT;
4193 else
4194 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004195 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004196 goto out;
4197 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198
4199 leaf = path->nodes[0];
4200 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004201 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 index = key.offset;
4203 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004204 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205
Miao Xie16cdcec2011-04-22 18:12:22 +08004206 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004207 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004208 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004209 goto out;
4210 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004211
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004213 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004214 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004215 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004216 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004217 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004219 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004220 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221}
4222
Chris Mason39279cc2007-06-12 06:35:45 -04004223static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4224{
David Howells2b0143b2015-03-17 22:25:59 +00004225 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004226 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004227 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004228 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004229 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004230
David Sterbab3ae2442012-09-13 16:04:34 -06004231 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004232 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004233 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4234 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004235
Josef Bacikd52be812013-05-29 14:54:47 -04004236 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004237 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004238 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004239
Li Zefan33345d012011-04-20 10:31:50 +08004240 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 err = btrfs_unlink_subvol(trans, root, dir,
4242 BTRFS_I(inode)->location.objectid,
4243 dentry->d_name.name,
4244 dentry->d_name.len);
4245 goto out;
4246 }
4247
Josef Bacik7b128762008-07-24 12:17:14 -04004248 err = btrfs_orphan_add(trans, inode);
4249 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004251
Filipe Manana44f714d2016-06-06 16:11:13 +01004252 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4253
Chris Mason39279cc2007-06-12 06:35:45 -04004254 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004255 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004256 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004257 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004258 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004259 /*
4260 * Propagate the last_unlink_trans value of the deleted dir to
4261 * its parent directory. This is to prevent an unrecoverable
4262 * log tree in the case we do something like this:
4263 * 1) create dir foo
4264 * 2) create snapshot under dir foo
4265 * 3) delete the snapshot
4266 * 4) rmdir foo
4267 * 5) mkdir foo
4268 * 6) fsync foo or some file inside foo
4269 */
4270 if (last_unlink_trans >= trans->transid)
4271 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4272 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004273out:
Josef Bacikd52be812013-05-29 14:54:47 -04004274 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004275 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004276
Chris Mason39279cc2007-06-12 06:35:45 -04004277 return err;
4278}
4279
Chris Mason28f75a02015-02-04 06:59:29 -08004280static int truncate_space_check(struct btrfs_trans_handle *trans,
4281 struct btrfs_root *root,
4282 u64 bytes_deleted)
4283{
4284 int ret;
4285
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004286 /*
4287 * This is only used to apply pressure to the enospc system, we don't
4288 * intend to use this reservation at all.
4289 */
Chris Mason28f75a02015-02-04 06:59:29 -08004290 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004291 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004292 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4293 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004294 if (!ret) {
4295 trace_btrfs_space_reservation(root->fs_info, "transaction",
4296 trans->transid,
4297 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004298 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004299 }
Chris Mason28f75a02015-02-04 06:59:29 -08004300 return ret;
4301
4302}
4303
Filipe Manana0305cd52015-10-16 12:34:25 +01004304static int truncate_inline_extent(struct inode *inode,
4305 struct btrfs_path *path,
4306 struct btrfs_key *found_key,
4307 const u64 item_end,
4308 const u64 new_size)
4309{
4310 struct extent_buffer *leaf = path->nodes[0];
4311 int slot = path->slots[0];
4312 struct btrfs_file_extent_item *fi;
4313 u32 size = (u32)(new_size - found_key->offset);
4314 struct btrfs_root *root = BTRFS_I(inode)->root;
4315
4316 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4317
4318 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4319 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004320 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004321
4322 /*
4323 * Zero out the remaining of the last page of our inline extent,
4324 * instead of directly truncating our inline extent here - that
4325 * would be much more complex (decompressing all the data, then
4326 * compressing the truncated data, which might be bigger than
4327 * the size of the inline extent, resize the extent, etc).
4328 * We release the path because to get the page we might need to
4329 * read the extent item from disk (data not in the page cache).
4330 */
4331 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304332 return btrfs_truncate_block(inode, offset, page_end - offset,
4333 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004334 }
4335
4336 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4337 size = btrfs_file_extent_calc_inline_size(size);
4338 btrfs_truncate_item(root, path, size, 1);
4339
4340 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4341 inode_sub_bytes(inode, item_end + 1 - new_size);
4342
4343 return 0;
4344}
4345
Chris Mason323ac952008-10-01 19:05:46 -04004346/*
Chris Mason39279cc2007-06-12 06:35:45 -04004347 * this can truncate away extent items, csum items and directory items.
4348 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004349 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004350 *
4351 * csum items that cross the new i_size are truncated to the new size
4352 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004353 *
4354 * min_type is the minimum key type to truncate down to. If set to 0, this
4355 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004356 */
Yan, Zheng80825102009-11-12 09:35:36 +00004357int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4358 struct btrfs_root *root,
4359 struct inode *inode,
4360 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004361{
Chris Mason39279cc2007-06-12 06:35:45 -04004362 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004363 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004364 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004365 struct btrfs_key key;
4366 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004367 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004368 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004369 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004370 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004371 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004372 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004373 int found_extent;
4374 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004375 int pending_del_nr = 0;
4376 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004377 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004378 int ret;
4379 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004380 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004381 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004382 bool be_nice = 0;
4383 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004384 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004385
4386 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004387
Chris Mason28ed1342014-12-17 09:41:04 -08004388 /*
4389 * for non-free space inodes and ref cows, we want to back off from
4390 * time to time
4391 */
4392 if (!btrfs_is_free_space_inode(inode) &&
4393 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4394 be_nice = 1;
4395
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004396 path = btrfs_alloc_path();
4397 if (!path)
4398 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004399 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004400
Josef Bacik5dc562c2012-08-17 13:14:17 -04004401 /*
4402 * We want to drop from the next block forward in case this new size is
4403 * not block aligned since we will be keeping the last block of the
4404 * extent just the way it is.
4405 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004406 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4407 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004408 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4409 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004410
Miao Xie16cdcec2011-04-22 18:12:22 +08004411 /*
4412 * This function is also used to drop the items in the log tree before
4413 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4414 * it is used to drop the loged items. So we shouldn't kill the delayed
4415 * items.
4416 */
4417 if (min_type == 0 && root == BTRFS_I(inode)->root)
4418 btrfs_kill_delayed_inode_items(inode);
4419
Li Zefan33345d012011-04-20 10:31:50 +08004420 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004422 key.type = (u8)-1;
4423
Chris Mason85e21ba2008-01-29 15:11:36 -05004424search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004425 /*
4426 * with a 16K leaf size and 128MB extents, you can actually queue
4427 * up a huge file in a single leaf. Most of the time that
4428 * bytes_deleted is > 0, it will be huge by the time we get here
4429 */
Byongho Leeee221842015-12-15 01:42:10 +09004430 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004431 if (btrfs_should_end_transaction(trans, root)) {
4432 err = -EAGAIN;
4433 goto error;
4434 }
4435 }
4436
4437
Chris Masonb9473432009-03-13 11:00:37 -04004438 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004439 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004440 if (ret < 0) {
4441 err = ret;
4442 goto out;
4443 }
Chris Masond3977122009-01-05 21:25:51 -05004444
Chris Mason85e21ba2008-01-29 15:11:36 -05004445 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004446 /* there are no items in the tree for us to truncate, we're
4447 * done
4448 */
Yan, Zheng80825102009-11-12 09:35:36 +00004449 if (path->slots[0] == 0)
4450 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004451 path->slots[0]--;
4452 }
4453
Chris Masond3977122009-01-05 21:25:51 -05004454 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004455 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004456 leaf = path->nodes[0];
4457 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004458 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004459
Li Zefan33345d012011-04-20 10:31:50 +08004460 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004461 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004462
Chris Mason85e21ba2008-01-29 15:11:36 -05004463 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004464 break;
4465
Chris Mason5f39d392007-10-15 16:14:19 -04004466 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004467 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004468 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004469 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004470 extent_type = btrfs_file_extent_type(leaf, fi);
4471 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004472 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004473 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004474 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004475 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004476 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004477 }
Yan008630c2007-11-07 13:31:09 -05004478 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 }
Yan, Zheng80825102009-11-12 09:35:36 +00004480 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004481 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004482 } else {
Liu Boc3eab852016-12-01 13:43:31 -08004483 if (item_end < new_size) {
4484 /*
4485 * With NO_HOLES mode, for the following mapping
4486 *
4487 * [0-4k][hole][8k-12k]
4488 *
4489 * if truncating isize down to 6k, it ends up
4490 * isize being 8k.
4491 */
4492 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4493 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004494 break;
Liu Boc3eab852016-12-01 13:43:31 -08004495 }
Yan, Zheng80825102009-11-12 09:35:36 +00004496 if (found_key.offset >= new_size)
4497 del_item = 1;
4498 else
4499 del_item = 0;
4500 }
Chris Mason39279cc2007-06-12 06:35:45 -04004501 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004502 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004503 if (found_type != BTRFS_EXTENT_DATA_KEY)
4504 goto delete;
4505
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004506 if (del_item)
4507 last_size = found_key.offset;
4508 else
4509 last_size = new_size;
4510
Chris Mason179e29e2007-11-01 11:28:41 -04004511 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004512 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004513 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004514 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004515 u64 orig_num_bytes =
4516 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004517 extent_num_bytes = ALIGN(new_size -
4518 found_key.offset,
4519 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004520 btrfs_set_file_extent_num_bytes(leaf, fi,
4521 extent_num_bytes);
4522 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004523 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004524 if (test_bit(BTRFS_ROOT_REF_COWS,
4525 &root->state) &&
4526 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004527 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004528 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004529 } else {
Chris Masondb945352007-10-15 16:15:53 -04004530 extent_num_bytes =
4531 btrfs_file_extent_disk_num_bytes(leaf,
4532 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004533 extent_offset = found_key.offset -
4534 btrfs_file_extent_offset(leaf, fi);
4535
Chris Mason39279cc2007-06-12 06:35:45 -04004536 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004537 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004538 if (extent_start != 0) {
4539 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004540 if (test_bit(BTRFS_ROOT_REF_COWS,
4541 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004542 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004543 }
4544 }
Chris Mason90692182008-02-08 13:49:28 -05004545 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004546 /*
4547 * we can't truncate inline items that have had
4548 * special encodings
4549 */
4550 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004551 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4552 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004553
4554 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004555 * Need to release path in order to truncate a
4556 * compressed extent. So delete any accumulated
4557 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004558 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004559 if (btrfs_file_extent_compression(leaf, fi) !=
4560 BTRFS_COMPRESS_NONE && pending_del_nr) {
4561 err = btrfs_del_items(trans, root, path,
4562 pending_del_slot,
4563 pending_del_nr);
4564 if (err) {
4565 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004566 err);
4567 goto error;
4568 }
4569 pending_del_nr = 0;
4570 }
4571
4572 err = truncate_inline_extent(inode, path,
4573 &found_key,
4574 item_end,
4575 new_size);
4576 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004577 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004578 goto error;
4579 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004580 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4581 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004582 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004583 }
Chris Mason39279cc2007-06-12 06:35:45 -04004584 }
Chris Mason179e29e2007-11-01 11:28:41 -04004585delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004586 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004587 if (!pending_del_nr) {
4588 /* no pending yet, add ourselves */
4589 pending_del_slot = path->slots[0];
4590 pending_del_nr = 1;
4591 } else if (pending_del_nr &&
4592 path->slots[0] + 1 == pending_del_slot) {
4593 /* hop on the pending chunk */
4594 pending_del_nr++;
4595 pending_del_slot = path->slots[0];
4596 } else {
Chris Masond3977122009-01-05 21:25:51 -05004597 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004598 }
Chris Mason39279cc2007-06-12 06:35:45 -04004599 } else {
4600 break;
4601 }
Chris Mason28f75a02015-02-04 06:59:29 -08004602 should_throttle = 0;
4603
Miao Xie27cdeb72014-04-02 19:51:05 +08004604 if (found_extent &&
4605 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4606 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004607 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004608 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004609 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004610 extent_num_bytes, 0,
4611 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004612 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004613 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004614 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004615 btrfs_async_run_delayed_refs(root,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004616 trans->delayed_ref_updates * 2,
4617 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004618 if (be_nice) {
4619 if (truncate_space_check(trans, root,
4620 extent_num_bytes)) {
4621 should_end = 1;
4622 }
4623 if (btrfs_should_throttle_delayed_refs(trans,
4624 root)) {
4625 should_throttle = 1;
4626 }
4627 }
Chris Mason39279cc2007-06-12 06:35:45 -04004628 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004629
Yan, Zheng80825102009-11-12 09:35:36 +00004630 if (found_type == BTRFS_INODE_ITEM_KEY)
4631 break;
4632
4633 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004634 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004635 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004636 if (pending_del_nr) {
4637 ret = btrfs_del_items(trans, root, path,
4638 pending_del_slot,
4639 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004640 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004641 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004642 goto error;
4643 }
Yan, Zheng80825102009-11-12 09:35:36 +00004644 pending_del_nr = 0;
4645 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004646 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004647 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004648 unsigned long updates = trans->delayed_ref_updates;
4649 if (updates) {
4650 trans->delayed_ref_updates = 0;
4651 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4652 if (ret && !err)
4653 err = ret;
4654 }
4655 }
Chris Mason28f75a02015-02-04 06:59:29 -08004656 /*
4657 * if we failed to refill our space rsv, bail out
4658 * and let the transaction restart
4659 */
4660 if (should_end) {
4661 err = -EAGAIN;
4662 goto error;
4663 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004664 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004665 } else {
4666 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004667 }
Chris Mason39279cc2007-06-12 06:35:45 -04004668 }
Yan, Zheng80825102009-11-12 09:35:36 +00004669out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004670 if (pending_del_nr) {
4671 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4672 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004673 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004674 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004675 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004676error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004677 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004678 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004679
Chris Mason39279cc2007-06-12 06:35:45 -04004680 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004681
Byongho Leeee221842015-12-15 01:42:10 +09004682 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004683 unsigned long updates = trans->delayed_ref_updates;
4684 if (updates) {
4685 trans->delayed_ref_updates = 0;
4686 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4687 if (ret && !err)
4688 err = ret;
4689 }
4690 }
Yan, Zheng80825102009-11-12 09:35:36 +00004691 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004692}
4693
Chris Masona52d9a82007-08-27 16:49:44 -04004694/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304695 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004696 * @inode - inode that we're zeroing
4697 * @from - the offset to start zeroing
4698 * @len - the length to zero, 0 to zero the entire range respective to the
4699 * offset
4700 * @front - zero up to the offset instead of from the offset on
4701 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304702 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004703 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004704 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304705int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004706 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004707{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004708 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004709 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4711 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004712 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004713 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004714 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004715 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304716 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004717 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004718 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004719 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 u64 block_start;
4721 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004722
Josef Bacik2aaa6652012-08-29 14:27:18 -04004723 if ((offset & (blocksize - 1)) == 0 &&
4724 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004725 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304726
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004727 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004729 if (ret)
4730 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004731
Chris Mason211c17f2008-05-15 09:13:45 -04004732again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004733 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004734 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004735 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 round_down(from, blocksize),
4737 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004738 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004739 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004740 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004741
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304742 block_start = round_down(from, blocksize);
4743 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004744
Chris Masona52d9a82007-08-27 16:49:44 -04004745 if (!PageUptodate(page)) {
4746 ret = btrfs_readpage(NULL, page);
4747 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004748 if (page->mapping != mapping) {
4749 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004750 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004751 goto again;
4752 }
Chris Masona52d9a82007-08-27 16:49:44 -04004753 if (!PageUptodate(page)) {
4754 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004755 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004756 }
4757 }
Chris Mason211c17f2008-05-15 09:13:45 -04004758 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004759
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004761 set_page_extent_mapped(page);
4762
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004764 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004766 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004767 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004768 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004769 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004770 btrfs_put_ordered_extent(ordered);
4771 goto again;
4772 }
4773
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004775 EXTENT_DIRTY | EXTENT_DELALLOC |
4776 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004777 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004778
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004780 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004781 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004783 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004784 goto out_unlock;
4785 }
4786
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004788 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304789 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004790 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004791 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792 memset(kaddr + (block_start - page_offset(page)),
4793 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004794 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795 memset(kaddr + (block_start - page_offset(page)) + offset,
4796 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797 flush_dcache_page(page);
4798 kunmap(page);
4799 }
Chris Mason247e7432008-07-17 12:53:51 -04004800 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004803 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004804
Chris Mason89642222008-07-24 09:41:53 -04004805out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004806 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 btrfs_delalloc_release_space(inode, block_start,
4808 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004809 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004810 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004811out:
4812 return ret;
4813}
4814
Josef Bacik16e75492013-10-22 12:18:51 -04004815static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4816 u64 offset, u64 len)
4817{
4818 struct btrfs_trans_handle *trans;
4819 int ret;
4820
4821 /*
4822 * Still need to make sure the inode looks like it's been updated so
4823 * that any holes get logged if we fsync.
4824 */
4825 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4826 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4827 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4828 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4829 return 0;
4830 }
4831
4832 /*
4833 * 1 - for the one we're dropping
4834 * 1 - for the one we're adding
4835 * 1 - for updating the inode.
4836 */
4837 trans = btrfs_start_transaction(root, 3);
4838 if (IS_ERR(trans))
4839 return PTR_ERR(trans);
4840
4841 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4842 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004843 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004844 btrfs_end_transaction(trans, root);
4845 return ret;
4846 }
4847
4848 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4849 0, 0, len, 0, len, 0, 0, 0);
4850 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004851 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004852 else
4853 btrfs_update_inode(trans, root, inode);
4854 btrfs_end_transaction(trans, root);
4855 return ret;
4856}
4857
Josef Bacik695a0d02011-03-04 15:46:53 -05004858/*
4859 * This function puts in dummy file extents for the area we're creating a hole
4860 * for. So if we are truncating this file to a larger size we need to insert
4861 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4862 * the range between oldsize and size
4863 */
Josef Bacika41ad392011-01-31 15:30:16 -05004864int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004865{
Yan Zheng9036c102008-10-30 14:19:41 -04004866 struct btrfs_root *root = BTRFS_I(inode)->root;
4867 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004868 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004869 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004871 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4872 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004873 u64 last_byte;
4874 u64 cur_offset;
4875 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004876 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004877
Josef Bacika71754f2013-06-17 17:14:39 -04004878 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304879 * If our size started in the middle of a block we need to zero out the
4880 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004881 * expose stale data.
4882 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304883 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004884 if (err)
4885 return err;
4886
Yan Zheng9036c102008-10-30 14:19:41 -04004887 if (size <= hole_start)
4888 return 0;
4889
Yan Zheng9036c102008-10-30 14:19:41 -04004890 while (1) {
4891 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004892
David Sterbaff13db42015-12-03 14:30:40 +01004893 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004894 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004895 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4896 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004897 if (!ordered)
4898 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004899 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4900 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004901 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004902 btrfs_put_ordered_extent(ordered);
4903 }
4904
Yan Zheng9036c102008-10-30 14:19:41 -04004905 cur_offset = hole_start;
4906 while (1) {
4907 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4908 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004909 if (IS_ERR(em)) {
4910 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004911 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004912 break;
4913 }
Yan Zheng9036c102008-10-30 14:19:41 -04004914 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004915 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004916 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004917 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004918 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004919
Josef Bacik16e75492013-10-22 12:18:51 -04004920 err = maybe_insert_hole(root, inode, cur_offset,
4921 hole_size);
4922 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004923 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004924 btrfs_drop_extent_cache(inode, cur_offset,
4925 cur_offset + hole_size - 1, 0);
4926 hole_em = alloc_extent_map();
4927 if (!hole_em) {
4928 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4929 &BTRFS_I(inode)->runtime_flags);
4930 goto next;
4931 }
4932 hole_em->start = cur_offset;
4933 hole_em->len = hole_size;
4934 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004935
Josef Bacik5dc562c2012-08-17 13:14:17 -04004936 hole_em->block_start = EXTENT_MAP_HOLE;
4937 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004938 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004939 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004940 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4941 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004942 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943
4944 while (1) {
4945 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004946 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004947 write_unlock(&em_tree->lock);
4948 if (err != -EEXIST)
4949 break;
4950 btrfs_drop_extent_cache(inode, cur_offset,
4951 cur_offset +
4952 hole_size - 1, 0);
4953 }
4954 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004955 }
Josef Bacik16e75492013-10-22 12:18:51 -04004956next:
Yan Zheng9036c102008-10-30 14:19:41 -04004957 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004958 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004959 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004960 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004961 break;
4962 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004963 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004964 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4965 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004966 return err;
4967}
4968
Eric Sandeen3972f262013-01-12 02:57:22 +00004969static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004970{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004971 struct btrfs_root *root = BTRFS_I(inode)->root;
4972 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004973 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004974 loff_t newsize = attr->ia_size;
4975 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004976 int ret;
4977
Eric Sandeen3972f262013-01-12 02:57:22 +00004978 /*
4979 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4980 * special case where we need to update the times despite not having
4981 * these flags set. For all other operations the VFS set these flags
4982 * explicitly if it wants a timestamp update.
4983 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004984 if (newsize != oldsize) {
4985 inode_inc_iversion(inode);
4986 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4987 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004988 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004989 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004990
Josef Bacika41ad392011-01-31 15:30:16 -05004991 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004992 /*
4993 * Don't do an expanding truncate while snapshoting is ongoing.
4994 * This is to ensure the snapshot captures a fully consistent
4995 * state of this file - if the snapshot captures this expanding
4996 * truncation, it must capture all writes that happened before
4997 * this truncation.
4998 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004999 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005000 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005001 if (ret) {
5002 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005003 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005004 }
Yan, Zheng80825102009-11-12 09:35:36 +00005005
Miao Xief4a2f4c2011-12-14 20:12:01 -05005006 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005007 if (IS_ERR(trans)) {
5008 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005009 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005010 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005011
5012 i_size_write(inode, newsize);
5013 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305014 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005015 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005016 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005017 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05005018 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005019
Josef Bacika41ad392011-01-31 15:30:16 -05005020 /*
5021 * We're truncating a file that used to have good data down to
5022 * zero. Make sure it gets into the ordered flush list so that
5023 * any new writes get down to disk quickly.
5024 */
5025 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005026 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5027 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005028
Josef Bacikf3fe8202013-01-07 17:03:21 -05005029 /*
5030 * 1 for the orphan item we're going to add
5031 * 1 for the orphan item deletion.
5032 */
5033 trans = btrfs_start_transaction(root, 2);
5034 if (IS_ERR(trans))
5035 return PTR_ERR(trans);
5036
5037 /*
5038 * We need to do this in case we fail at _any_ point during the
5039 * actual truncate. Once we do the truncate_setsize we could
5040 * invalidate pages which forces any outstanding ordered io to
5041 * be instantly completed which will give us extents that need
5042 * to be truncated. If we fail to get an orphan inode down we
5043 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005044 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005045 * will be consistent.
5046 */
5047 ret = btrfs_orphan_add(trans, inode);
5048 btrfs_end_transaction(trans, root);
5049 if (ret)
5050 return ret;
5051
Josef Bacika41ad392011-01-31 15:30:16 -05005052 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5053 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005054
5055 /* Disable nonlocked read DIO to avoid the end less truncate */
5056 btrfs_inode_block_unlocked_dio(inode);
5057 inode_dio_wait(inode);
5058 btrfs_inode_resume_unlocked_dio(inode);
5059
Josef Bacika41ad392011-01-31 15:30:16 -05005060 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005061 if (ret && inode->i_nlink) {
5062 int err;
5063
5064 /*
5065 * failed to truncate, disk_i_size is only adjusted down
5066 * as we remove extents, so it should represent the true
5067 * size of the inode, so reset the in memory size and
5068 * delete our orphan entry.
5069 */
5070 trans = btrfs_join_transaction(root);
5071 if (IS_ERR(trans)) {
5072 btrfs_orphan_del(NULL, inode);
5073 return ret;
5074 }
5075 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5076 err = btrfs_orphan_del(trans, inode);
5077 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005078 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005079 btrfs_end_transaction(trans, root);
5080 }
Yan, Zheng80825102009-11-12 09:35:36 +00005081 }
5082
Josef Bacika41ad392011-01-31 15:30:16 -05005083 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005084}
5085
Chris Mason39279cc2007-06-12 06:35:45 -04005086static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5087{
David Howells2b0143b2015-03-17 22:25:59 +00005088 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005089 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005090 int err;
5091
Li Zefanb83cc962010-12-20 16:04:08 +08005092 if (btrfs_root_readonly(root))
5093 return -EROFS;
5094
Jan Kara31051c82016-05-26 16:55:18 +02005095 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005096 if (err)
5097 return err;
5098
Chris Mason5a3f23d2009-03-31 13:27:11 -04005099 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005100 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005101 if (err)
5102 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005103 }
Yan Zheng9036c102008-10-30 14:19:41 -04005104
Christoph Hellwig10257742010-06-04 11:30:02 +02005105 if (attr->ia_valid) {
5106 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005107 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005108 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005109
Josef Bacik22c44fe2011-11-30 10:45:38 -05005110 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005111 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005112 }
5113
Chris Mason39279cc2007-06-12 06:35:45 -04005114 return err;
5115}
Chris Mason61295eb2008-01-14 16:24:38 -05005116
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005117/*
5118 * While truncating the inode pages during eviction, we get the VFS calling
5119 * btrfs_invalidatepage() against each page of the inode. This is slow because
5120 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5121 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5122 * extent_state structures over and over, wasting lots of time.
5123 *
5124 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5125 * those expensive operations on a per page basis and do only the ordered io
5126 * finishing, while we release here the extent_map and extent_state structures,
5127 * without the excessive merging and splitting.
5128 */
5129static void evict_inode_truncate_pages(struct inode *inode)
5130{
5131 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5132 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5133 struct rb_node *node;
5134
5135 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005136 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005137
5138 write_lock(&map_tree->lock);
5139 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5140 struct extent_map *em;
5141
5142 node = rb_first(&map_tree->map);
5143 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005144 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5145 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005146 remove_extent_mapping(map_tree, em);
5147 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005148 if (need_resched()) {
5149 write_unlock(&map_tree->lock);
5150 cond_resched();
5151 write_lock(&map_tree->lock);
5152 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005153 }
5154 write_unlock(&map_tree->lock);
5155
Filipe Manana6ca07092015-05-26 00:55:42 +01005156 /*
5157 * Keep looping until we have no more ranges in the io tree.
5158 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005159 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5160 * still in progress (unlocked the pages in the bio but did not yet
5161 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005162 * ranges can still be locked and eviction started because before
5163 * submitting those bios, which are executed by a separate task (work
5164 * queue kthread), inode references (inode->i_count) were not taken
5165 * (which would be dropped in the end io callback of each bio).
5166 * Therefore here we effectively end up waiting for those bios and
5167 * anyone else holding locked ranges without having bumped the inode's
5168 * reference count - if we don't do it, when they access the inode's
5169 * io_tree to unlock a range it may be too late, leading to an
5170 * use-after-free issue.
5171 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005172 spin_lock(&io_tree->lock);
5173 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5174 struct extent_state *state;
5175 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005176 u64 start;
5177 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005178
5179 node = rb_first(&io_tree->state);
5180 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005181 start = state->start;
5182 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005183 spin_unlock(&io_tree->lock);
5184
David Sterbaff13db42015-12-03 14:30:40 +01005185 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005186
5187 /*
5188 * If still has DELALLOC flag, the extent didn't reach disk,
5189 * and its reserved space won't be freed by delayed_ref.
5190 * So we need to free its reserved space here.
5191 * (Refer to comment in btrfs_invalidatepage, case 2)
5192 *
5193 * Note, end is the bytenr of last byte, so we need + 1 here.
5194 */
5195 if (state->state & EXTENT_DELALLOC)
5196 btrfs_qgroup_free_data(inode, start, end - start + 1);
5197
Filipe Manana6ca07092015-05-26 00:55:42 +01005198 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005199 EXTENT_LOCKED | EXTENT_DIRTY |
5200 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5201 EXTENT_DEFRAG, 1, 1,
5202 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005203
Filipe Manana7064dd52014-08-08 02:47:05 +01005204 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205 spin_lock(&io_tree->lock);
5206 }
5207 spin_unlock(&io_tree->lock);
5208}
5209
Al Virobd555972010-06-07 11:35:40 -04005210void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005211{
5212 struct btrfs_trans_handle *trans;
5213 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005214 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005215 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005216 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005217 int ret;
5218
liubo1abe9b82011-03-24 11:18:59 +00005219 trace_btrfs_inode_evict(inode);
5220
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005221 if (!root) {
5222 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5223 return;
5224 }
5225
5226 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5227
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005228 evict_inode_truncate_pages(inode);
5229
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005230 if (inode->i_nlink &&
5231 ((btrfs_root_refs(&root->root_item) != 0 &&
5232 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5233 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005234 goto no_delete;
5235
Chris Mason39279cc2007-06-12 06:35:45 -04005236 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005237 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005238 goto no_delete;
5239 }
Al Virobd555972010-06-07 11:35:40 -04005240 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005241 if (!special_file(inode->i_mode))
5242 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005243
Miao Xief6124962014-09-12 18:44:04 +08005244 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5245
Josef Bacikafcdd122016-09-02 15:40:02 -04005246 if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005247 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005248 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005249 goto no_delete;
5250 }
5251
Yan, Zheng76dda932009-09-21 16:00:26 -04005252 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005253 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5254 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005255 goto no_delete;
5256 }
5257
Miao Xie0e8c36a2012-12-19 06:59:51 +00005258 ret = btrfs_commit_inode_delayed_inode(inode);
5259 if (ret) {
5260 btrfs_orphan_del(NULL, inode);
5261 goto no_delete;
5262 }
5263
Miao Xie66d8f3d2012-09-06 04:02:28 -06005264 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005265 if (!rsv) {
5266 btrfs_orphan_del(NULL, inode);
5267 goto no_delete;
5268 }
Josef Bacik4a338542011-08-29 11:01:31 -04005269 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005270 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005271 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005272
Chris Masondbe674a2008-07-17 12:54:05 -04005273 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005274
Josef Bacik4289a662011-08-05 13:22:24 -04005275 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005276 * This is a bit simpler than btrfs_truncate since we've already
5277 * reserved our space for our orphan item in the unlink, so we just
5278 * need to reserve some slack space in case we add bytes and update
5279 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005280 */
Yan, Zheng80825102009-11-12 09:35:36 +00005281 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005282 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5283 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005284
Josef Bacik726c35f2011-09-26 15:46:06 -04005285 /*
5286 * Try and steal from the global reserve since we will
5287 * likely not use this space anyway, we want to try as
5288 * hard as possible to get this to work.
5289 */
5290 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005291 steal_from_global++;
5292 else
5293 steal_from_global = 0;
5294 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005295
Josef Bacik3bce8762015-02-24 12:35:51 -08005296 /*
5297 * steal_from_global == 0: we reserved stuff, hooray!
5298 * steal_from_global == 1: we didn't reserve stuff, boo!
5299 * steal_from_global == 2: we've committed, still not a lot of
5300 * room but maybe we'll have room in the global reserve this
5301 * time.
5302 * steal_from_global == 3: abandon all hope!
5303 */
5304 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005305 btrfs_warn(root->fs_info,
5306 "Could not get space for a delete, will truncate on mount %d",
5307 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005308 btrfs_orphan_del(NULL, inode);
5309 btrfs_free_block_rsv(root, rsv);
5310 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005311 }
5312
Miao Xie0e8c36a2012-12-19 06:59:51 +00005313 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005314 if (IS_ERR(trans)) {
5315 btrfs_orphan_del(NULL, inode);
5316 btrfs_free_block_rsv(root, rsv);
5317 goto no_delete;
5318 }
5319
Josef Bacik3bce8762015-02-24 12:35:51 -08005320 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005321 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005322 * sure there is room to do it, if not we need to commit and try
5323 * again.
5324 */
5325 if (steal_from_global) {
5326 if (!btrfs_check_space_for_delayed_refs(trans, root))
5327 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005328 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005329 else
5330 ret = -ENOSPC;
5331 }
5332
5333 /*
5334 * Couldn't steal from the global reserve, we have too much
5335 * pending stuff built up, commit the transaction and try it
5336 * again.
5337 */
5338 if (ret) {
5339 ret = btrfs_commit_transaction(trans, root);
5340 if (ret) {
5341 btrfs_orphan_del(NULL, inode);
5342 btrfs_free_block_rsv(root, rsv);
5343 goto no_delete;
5344 }
5345 continue;
5346 } else {
5347 steal_from_global = 0;
5348 }
5349
Josef Bacik4289a662011-08-05 13:22:24 -04005350 trans->block_rsv = rsv;
5351
Yan, Zhengd68fc572010-05-16 10:49:58 -04005352 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005353 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005354 break;
5355
Miao Xie8407aa42012-09-07 01:43:32 -06005356 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005357 btrfs_end_transaction(trans, root);
5358 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005359 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005360 }
5361
Josef Bacik4289a662011-08-05 13:22:24 -04005362 btrfs_free_block_rsv(root, rsv);
5363
Josef Bacik4ef31a42013-08-13 14:10:08 -04005364 /*
5365 * Errors here aren't a big deal, it just means we leave orphan items
5366 * in the tree. They will be cleaned up on the next mount.
5367 */
Yan, Zheng80825102009-11-12 09:35:36 +00005368 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005369 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005370 btrfs_orphan_del(trans, inode);
5371 } else {
5372 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005373 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005374
Josef Bacik4289a662011-08-05 13:22:24 -04005375 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005376 if (!(root == root->fs_info->tree_root ||
5377 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005378 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005379
Chris Mason54aa1f42007-06-22 14:16:25 -04005380 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005381 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005382no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005383 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005384 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005385}
5386
5387/*
5388 * this returns the key found in the dir entry in the location pointer.
5389 * If no dir entries were found, location->objectid is 0.
5390 */
5391static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5392 struct btrfs_key *location)
5393{
5394 const char *name = dentry->d_name.name;
5395 int namelen = dentry->d_name.len;
5396 struct btrfs_dir_item *di;
5397 struct btrfs_path *path;
5398 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005399 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005400
5401 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005402 if (!path)
5403 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005404
Li Zefan33345d012011-04-20 10:31:50 +08005405 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005406 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005407 if (IS_ERR(di))
5408 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005409
David Sterbac7040052011-04-19 18:00:01 +02005410 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005411 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005412
Chris Mason5f39d392007-10-15 16:14:19 -04005413 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005414out:
Chris Mason39279cc2007-06-12 06:35:45 -04005415 btrfs_free_path(path);
5416 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005417out_err:
5418 location->objectid = 0;
5419 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005420}
5421
5422/*
5423 * when we hit a tree root in a directory, the btrfs part of the inode
5424 * needs to be changed to reflect the root directory of the tree root. This
5425 * is kind of like crossing a mount point.
5426 */
5427static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428 struct inode *dir,
5429 struct dentry *dentry,
5430 struct btrfs_key *location,
5431 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005432{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005433 struct btrfs_path *path;
5434 struct btrfs_root *new_root;
5435 struct btrfs_root_ref *ref;
5436 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005437 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005438 int ret;
5439 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005440
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005441 path = btrfs_alloc_path();
5442 if (!path) {
5443 err = -ENOMEM;
5444 goto out;
5445 }
Chris Mason39279cc2007-06-12 06:35:45 -04005446
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005447 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005448 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5449 key.type = BTRFS_ROOT_REF_KEY;
5450 key.offset = location->objectid;
5451
5452 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5453 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005454 if (ret) {
5455 if (ret < 0)
5456 err = ret;
5457 goto out;
5458 }
Chris Mason39279cc2007-06-12 06:35:45 -04005459
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 leaf = path->nodes[0];
5461 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005462 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5464 goto out;
5465
5466 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5467 (unsigned long)(ref + 1),
5468 dentry->d_name.len);
5469 if (ret)
5470 goto out;
5471
David Sterbab3b4aa72011-04-21 01:20:15 +02005472 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473
5474 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5475 if (IS_ERR(new_root)) {
5476 err = PTR_ERR(new_root);
5477 goto out;
5478 }
5479
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 *sub_root = new_root;
5481 location->objectid = btrfs_root_dirid(&new_root->root_item);
5482 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005483 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 err = 0;
5485out:
5486 btrfs_free_path(path);
5487 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005488}
5489
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005490static void inode_tree_add(struct inode *inode)
5491{
5492 struct btrfs_root *root = BTRFS_I(inode)->root;
5493 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005494 struct rb_node **p;
5495 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005496 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005497 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498
Al Viro1d3382cb2010-10-23 15:19:20 -04005499 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005500 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005501 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005502 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005503 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005504 while (*p) {
5505 parent = *p;
5506 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5507
Li Zefan33345d012011-04-20 10:31:50 +08005508 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005509 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005510 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005511 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005512 else {
5513 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005514 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005515 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005516 RB_CLEAR_NODE(parent);
5517 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005518 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005519 }
5520 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005521 rb_link_node(new, parent, p);
5522 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005523 spin_unlock(&root->inode_lock);
5524}
5525
5526static void inode_tree_del(struct inode *inode)
5527{
5528 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005529 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005531 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005533 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005535 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005536 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005537 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005538
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005539 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005540 synchronize_srcu(&root->fs_info->subvol_srcu);
5541 spin_lock(&root->inode_lock);
5542 empty = RB_EMPTY_ROOT(&root->inode_tree);
5543 spin_unlock(&root->inode_lock);
5544 if (empty)
5545 btrfs_add_dead_root(root);
5546 }
5547}
5548
Jeff Mahoney143bede2012-03-01 14:56:26 +01005549void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005550{
5551 struct rb_node *node;
5552 struct rb_node *prev;
5553 struct btrfs_inode *entry;
5554 struct inode *inode;
5555 u64 objectid = 0;
5556
Liu Bo7813b3d2014-02-10 17:37:25 +08005557 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5558 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005559
5560 spin_lock(&root->inode_lock);
5561again:
5562 node = root->inode_tree.rb_node;
5563 prev = NULL;
5564 while (node) {
5565 prev = node;
5566 entry = rb_entry(node, struct btrfs_inode, rb_node);
5567
Li Zefan33345d012011-04-20 10:31:50 +08005568 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005569 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005570 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005571 node = node->rb_right;
5572 else
5573 break;
5574 }
5575 if (!node) {
5576 while (prev) {
5577 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005578 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 node = prev;
5580 break;
5581 }
5582 prev = rb_next(prev);
5583 }
5584 }
5585 while (node) {
5586 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005587 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005588 inode = igrab(&entry->vfs_inode);
5589 if (inode) {
5590 spin_unlock(&root->inode_lock);
5591 if (atomic_read(&inode->i_count) > 1)
5592 d_prune_aliases(inode);
5593 /*
Al Viro45321ac2010-06-07 13:43:19 -04005594 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005595 * the inode cache when its usage count
5596 * hits zero.
5597 */
5598 iput(inode);
5599 cond_resched();
5600 spin_lock(&root->inode_lock);
5601 goto again;
5602 }
5603
5604 if (cond_resched_lock(&root->inode_lock))
5605 goto again;
5606
5607 node = rb_next(node);
5608 }
5609 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610}
5611
Chris Masone02119d2008-09-05 16:13:11 -04005612static int btrfs_init_locked_inode(struct inode *inode, void *p)
5613{
5614 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005615 inode->i_ino = args->location->objectid;
5616 memcpy(&BTRFS_I(inode)->location, args->location,
5617 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005618 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005619 return 0;
5620}
5621
5622static int btrfs_find_actor(struct inode *inode, void *opaque)
5623{
5624 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005625 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005626 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005627}
5628
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005629static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005630 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005631 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005632{
5633 struct inode *inode;
5634 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005635 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005636
Chris Mason90d3e592014-01-09 17:28:00 -08005637 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005638 args.root = root;
5639
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005640 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005641 btrfs_init_locked_inode,
5642 (void *)&args);
5643 return inode;
5644}
5645
Balaji Rao1a54ef82008-07-21 02:01:04 +05305646/* Get an inode object given its location and corresponding root.
5647 * Returns in *is_new if the inode was read from disk
5648 */
5649struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005650 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305651{
5652 struct inode *inode;
5653
Chris Mason90d3e592014-01-09 17:28:00 -08005654 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305655 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005656 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305657
5658 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005659 int ret;
5660
5661 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005662 if (!is_bad_inode(inode)) {
5663 inode_tree_add(inode);
5664 unlock_new_inode(inode);
5665 if (new)
5666 *new = 1;
5667 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005668 unlock_new_inode(inode);
5669 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005670 ASSERT(ret < 0);
5671 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005672 }
5673 }
5674
Balaji Rao1a54ef82008-07-21 02:01:04 +05305675 return inode;
5676}
5677
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678static struct inode *new_simple_dir(struct super_block *s,
5679 struct btrfs_key *key,
5680 struct btrfs_root *root)
5681{
5682 struct inode *inode = new_inode(s);
5683
5684 if (!inode)
5685 return ERR_PTR(-ENOMEM);
5686
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005687 BTRFS_I(inode)->root = root;
5688 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005689 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005690
5691 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005692 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandovalad80fad2017-01-25 17:06:39 -08005693 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005694 inode->i_fop = &simple_dir_operations;
5695 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005696 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305697 inode->i_atime = inode->i_mtime;
5698 inode->i_ctime = inode->i_mtime;
5699 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005700
5701 return inode;
5702}
5703
Chris Mason3de45862008-11-17 21:02:50 -05005704struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005705{
Chris Masond3977122009-01-05 21:25:51 -05005706 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005707 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005708 struct btrfs_root *sub_root = root;
5709 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005710 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005711 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005712
5713 if (dentry->d_name.len > BTRFS_NAME_LEN)
5714 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005715
Jeff Layton39e3c952012-11-28 11:30:53 -05005716 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005717 if (ret < 0)
5718 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005719
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005720 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005721 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722
5723 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005724 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005726 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005727
5728 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5729
Yan, Zheng76dda932009-09-21 16:00:26 -04005730 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731 ret = fixup_tree_root_location(root, dir, dentry,
5732 &location, &sub_root);
5733 if (ret < 0) {
5734 if (ret != -ENOENT)
5735 inode = ERR_PTR(ret);
5736 else
5737 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5738 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005739 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005740 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005741 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5742
Julia Lawall34d19ba2011-01-24 19:55:19 +00005743 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005744 down_read(&root->fs_info->cleanup_work_sem);
5745 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005746 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005747 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005748 if (ret) {
5749 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005750 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005751 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005752 }
5753
Chris Mason3de45862008-11-17 21:02:50 -05005754 return inode;
5755}
5756
Nick Pigginfe15ce42011-01-07 17:49:23 +11005757static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005758{
5759 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005760 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005761
Li Zefan848cce02012-02-21 17:04:28 +08005762 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005763 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005764
Li Zefan848cce02012-02-21 17:04:28 +08005765 if (inode) {
5766 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005767 if (btrfs_root_refs(&root->root_item) == 0)
5768 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005769
5770 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5771 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005772 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005773 return 0;
5774}
5775
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005776static void btrfs_dentry_release(struct dentry *dentry)
5777{
Daeseok Youn944a4512014-04-14 15:37:02 +09005778 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005779}
5780
Chris Mason3de45862008-11-17 21:02:50 -05005781static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005782 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005783{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005784 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005785
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005786 inode = btrfs_lookup_dentry(dir, dentry);
5787 if (IS_ERR(inode)) {
5788 if (PTR_ERR(inode) == -ENOENT)
5789 inode = NULL;
5790 else
5791 return ERR_CAST(inode);
5792 }
5793
Al Viro41d28bc2014-10-12 22:24:21 -04005794 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005795}
5796
Miao Xie16cdcec2011-04-22 18:12:22 +08005797unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005798 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5799};
5800
Al Viro9cdda8d2013-05-22 16:48:09 -04005801static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005802{
Al Viro9cdda8d2013-05-22 16:48:09 -04005803 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005804 struct btrfs_root *root = BTRFS_I(inode)->root;
5805 struct btrfs_item *item;
5806 struct btrfs_dir_item *di;
5807 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005808 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005809 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005810 struct list_head ins_list;
5811 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005812 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005813 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005815 unsigned char d_type;
5816 int over = 0;
5817 u32 di_cur;
5818 u32 di_total;
5819 u32 di_len;
5820 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005821 char tmp_name[32];
5822 char *name_ptr;
5823 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005824 int is_curr = 0; /* ctx->pos points to the current index? */
David Sterbabc4ef752015-11-13 13:44:28 +01005825 bool emitted;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005826 bool put = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005827
5828 /* FIXME, use a real flag for deciding about the key type */
5829 if (root->fs_info->tree_root == root)
5830 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005831
Al Viro9cdda8d2013-05-22 16:48:09 -04005832 if (!dir_emit_dots(file, ctx))
5833 return 0;
5834
David Woodhouse49593bf2008-08-17 17:08:36 +01005835 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005836 if (!path)
5837 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005838
David Sterbae4058b52015-11-27 16:31:35 +01005839 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005840
Miao Xie16cdcec2011-04-22 18:12:22 +08005841 if (key_type == BTRFS_DIR_INDEX_KEY) {
5842 INIT_LIST_HEAD(&ins_list);
5843 INIT_LIST_HEAD(&del_list);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005844 put = btrfs_readdir_get_delayed_items(inode, &ins_list,
5845 &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005846 }
5847
David Sterba962a2982014-06-04 18:41:45 +02005848 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005849 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005850 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005851
Chris Mason39279cc2007-06-12 06:35:45 -04005852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5853 if (ret < 0)
5854 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005855
David Sterbabc4ef752015-11-13 13:44:28 +01005856 emitted = false;
David Woodhouse49593bf2008-08-17 17:08:36 +01005857 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005858 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005859 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005860 if (slot >= btrfs_header_nritems(leaf)) {
5861 ret = btrfs_next_leaf(root, path);
5862 if (ret < 0)
5863 goto err;
5864 else if (ret > 0)
5865 break;
5866 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005867 }
Chris Mason3de45862008-11-17 21:02:50 -05005868
Ross Kirkdd3cc162013-09-16 15:58:09 +01005869 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005870 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5871
5872 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005873 break;
David Sterba962a2982014-06-04 18:41:45 +02005874 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005875 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005876 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005877 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005878 if (key_type == BTRFS_DIR_INDEX_KEY &&
5879 btrfs_should_delete_dir_index(&del_list,
5880 found_key.offset))
5881 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005882
Al Viro9cdda8d2013-05-22 16:48:09 -04005883 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005884 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005885
Chris Mason39279cc2007-06-12 06:35:45 -04005886 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5887 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005888 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005889
5890 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005891 struct btrfs_key location;
5892
Josef Bacik22a94d42011-03-16 16:47:17 -04005893 if (verify_dir_item(root, leaf, di))
5894 break;
5895
Chris Mason5f39d392007-10-15 16:14:19 -04005896 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005897 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005898 name_ptr = tmp_name;
5899 } else {
David Sterba49e350a2016-02-11 14:25:38 +01005900 name_ptr = kmalloc(name_len, GFP_KERNEL);
David Woodhouse49593bf2008-08-17 17:08:36 +01005901 if (!name_ptr) {
5902 ret = -ENOMEM;
5903 goto err;
5904 }
Chris Mason5f39d392007-10-15 16:14:19 -04005905 }
5906 read_extent_buffer(leaf, name_ptr,
5907 (unsigned long)(di + 1), name_len);
5908
5909 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5910 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005911
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005912
Chris Mason3de45862008-11-17 21:02:50 -05005913 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005914 * skip it.
5915 *
5916 * In contrast to old kernels, we insert the snapshot's
5917 * dir item and dir index after it has been created, so
5918 * we won't find a reference to our own snapshot. We
5919 * still keep the following code for backward
5920 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005921 */
5922 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5923 location.objectid == root->root_key.objectid) {
5924 over = 0;
5925 goto skip;
5926 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005927 over = !dir_emit(ctx, name_ptr, name_len,
5928 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005929
Chris Mason3de45862008-11-17 21:02:50 -05005930skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005931 if (name_ptr != tmp_name)
5932 kfree(name_ptr);
5933
Chris Mason39279cc2007-06-12 06:35:45 -04005934 if (over)
5935 goto nopos;
David Sterbabc4ef752015-11-13 13:44:28 +01005936 emitted = true;
Josef Bacik5103e942007-11-16 11:45:54 -05005937 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005938 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005939 di_cur += di_len;
5940 di = (struct btrfs_dir_item *)((char *)di + di_len);
5941 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005942next:
5943 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005944 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005945
Miao Xie16cdcec2011-04-22 18:12:22 +08005946 if (key_type == BTRFS_DIR_INDEX_KEY) {
5947 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005948 ctx->pos++;
David Sterbabc4ef752015-11-13 13:44:28 +01005949 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
Miao Xie16cdcec2011-04-22 18:12:22 +08005950 if (ret)
5951 goto nopos;
5952 }
5953
David Sterbabc4ef752015-11-13 13:44:28 +01005954 /*
5955 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5956 * it was was set to the termination value in previous call. We assume
5957 * that "." and ".." were emitted if we reach this point and set the
5958 * termination value as well for an empty directory.
5959 */
5960 if (ctx->pos > 2 && !emitted)
5961 goto nopos;
5962
David Woodhouse49593bf2008-08-17 17:08:36 +01005963 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005964 ctx->pos++;
5965
5966 /*
5967 * Stop new entries from being returned after we return the last
5968 * entry.
5969 *
5970 * New directory entries are assigned a strictly increasing
5971 * offset. This means that new entries created during readdir
5972 * are *guaranteed* to be seen in the future by that readdir.
5973 * This has broken buggy programs which operate on names as
5974 * they're returned by readdir. Until we re-use freed offsets
5975 * we have this hack to stop new entries from being returned
5976 * under the assumption that they'll never reach this huge
5977 * offset.
5978 *
5979 * This is being careful not to overflow 32bit loff_t unless the
5980 * last entry requires it because doing so has broken 32bit apps
5981 * in the past.
5982 */
5983 if (key_type == BTRFS_DIR_INDEX_KEY) {
5984 if (ctx->pos >= INT_MAX)
5985 ctx->pos = LLONG_MAX;
5986 else
5987 ctx->pos = INT_MAX;
5988 }
Chris Mason39279cc2007-06-12 06:35:45 -04005989nopos:
5990 ret = 0;
5991err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005992 if (put)
5993 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005994 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005995 return ret;
5996}
5997
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005998int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005999{
6000 struct btrfs_root *root = BTRFS_I(inode)->root;
6001 struct btrfs_trans_handle *trans;
6002 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006003 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006004
Josef Bacik72ac3c02012-05-23 14:13:11 -04006005 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006006 return 0;
6007
Liu Bo83eea1f2012-07-10 05:28:39 -06006008 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08006009 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006010
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006011 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006012 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006013 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006014 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006015 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006016 if (IS_ERR(trans))
6017 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06006018 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006019 }
6020 return ret;
6021}
6022
6023/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006024 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006025 * inode changes. But, it is most likely to find the inode in cache.
6026 * FIXME, needs more benchmarking...there are no reasons other than performance
6027 * to keep or drop this code.
6028 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006029static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006030{
6031 struct btrfs_root *root = BTRFS_I(inode)->root;
6032 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006033 int ret;
6034
Josef Bacik72ac3c02012-05-23 14:13:11 -04006035 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006036 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006037
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006038 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006039 if (IS_ERR(trans))
6040 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006041
6042 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006043 if (ret && ret == -ENOSPC) {
6044 /* whoops, lets try again with the full transaction */
6045 btrfs_end_transaction(trans, root);
6046 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006047 if (IS_ERR(trans))
6048 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006049
Chris Mason94b60442010-05-26 11:02:00 -04006050 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006051 }
Chris Mason39279cc2007-06-12 06:35:45 -04006052 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08006053 if (BTRFS_I(inode)->delayed_node)
6054 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006055
6056 return ret;
6057}
6058
6059/*
6060 * This is a copy of file_update_time. We need this so we can return error on
6061 * ENOSPC for updating the inode in the case of file write and mmap writes.
6062 */
Josef Bacike41f9412012-03-26 09:46:47 -04006063static int btrfs_update_time(struct inode *inode, struct timespec *now,
6064 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006065{
Alexander Block2bc5565282012-06-15 09:49:33 +02006066 struct btrfs_root *root = BTRFS_I(inode)->root;
6067
6068 if (btrfs_root_readonly(root))
6069 return -EROFS;
6070
Josef Bacike41f9412012-03-26 09:46:47 -04006071 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006072 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006073 if (flags & S_CTIME)
6074 inode->i_ctime = *now;
6075 if (flags & S_MTIME)
6076 inode->i_mtime = *now;
6077 if (flags & S_ATIME)
6078 inode->i_atime = *now;
6079 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006080}
6081
Chris Masond352ac62008-09-29 15:18:18 -04006082/*
6083 * find the highest existing sequence number in a directory
6084 * and then set the in-memory index_cnt variable to reflect
6085 * free sequence numbers
6086 */
Josef Bacikaec74772008-07-24 12:12:38 -04006087static int btrfs_set_inode_index_count(struct inode *inode)
6088{
6089 struct btrfs_root *root = BTRFS_I(inode)->root;
6090 struct btrfs_key key, found_key;
6091 struct btrfs_path *path;
6092 struct extent_buffer *leaf;
6093 int ret;
6094
Li Zefan33345d012011-04-20 10:31:50 +08006095 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006096 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006097 key.offset = (u64)-1;
6098
6099 path = btrfs_alloc_path();
6100 if (!path)
6101 return -ENOMEM;
6102
6103 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6104 if (ret < 0)
6105 goto out;
6106 /* FIXME: we should be able to handle this */
6107 if (ret == 0)
6108 goto out;
6109 ret = 0;
6110
6111 /*
6112 * MAGIC NUMBER EXPLANATION:
6113 * since we search a directory based on f_pos we have to start at 2
6114 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6115 * else has to start at 2
6116 */
6117 if (path->slots[0] == 0) {
6118 BTRFS_I(inode)->index_cnt = 2;
6119 goto out;
6120 }
6121
6122 path->slots[0]--;
6123
6124 leaf = path->nodes[0];
6125 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6126
Li Zefan33345d012011-04-20 10:31:50 +08006127 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006128 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006129 BTRFS_I(inode)->index_cnt = 2;
6130 goto out;
6131 }
6132
6133 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6134out:
6135 btrfs_free_path(path);
6136 return ret;
6137}
6138
Chris Masond352ac62008-09-29 15:18:18 -04006139/*
6140 * helper to find a free sequence number in a given directory. This current
6141 * code is very simple, later versions will do smarter things in the btree
6142 */
Chris Mason3de45862008-11-17 21:02:50 -05006143int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006144{
6145 int ret = 0;
6146
6147 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006148 ret = btrfs_inode_delayed_dir_index_count(dir);
6149 if (ret) {
6150 ret = btrfs_set_inode_index_count(dir);
6151 if (ret)
6152 return ret;
6153 }
Josef Bacikaec74772008-07-24 12:12:38 -04006154 }
6155
Chris Mason00e4e6b2008-08-05 11:18:09 -04006156 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006157 BTRFS_I(dir)->index_cnt++;
6158
6159 return ret;
6160}
6161
Chris Masonb0d5d102014-09-08 13:08:51 -07006162static int btrfs_insert_inode_locked(struct inode *inode)
6163{
6164 struct btrfs_iget_args args;
6165 args.location = &BTRFS_I(inode)->location;
6166 args.root = BTRFS_I(inode)->root;
6167
6168 return insert_inode_locked4(inode,
6169 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6170 btrfs_find_actor, &args);
6171}
6172
Chris Mason39279cc2007-06-12 06:35:45 -04006173static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6174 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006175 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006176 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006177 u64 ref_objectid, u64 objectid,
6178 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006179{
6180 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006181 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006182 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006183 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006184 struct btrfs_inode_ref *ref;
6185 struct btrfs_key key[2];
6186 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006187 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006188 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006189 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006190
Chris Mason5f39d392007-10-15 16:14:19 -04006191 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006192 if (!path)
6193 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006194
Chris Mason39279cc2007-06-12 06:35:45 -04006195 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006196 if (!inode) {
6197 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006198 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006199 }
Chris Mason39279cc2007-06-12 06:35:45 -04006200
Li Zefan581bb052011-04-20 10:06:11 +08006201 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006202 * O_TMPFILE, set link count to 0, so that after this point,
6203 * we fill in an inode item with the correct link count.
6204 */
6205 if (!name)
6206 set_nlink(inode, 0);
6207
6208 /*
Li Zefan581bb052011-04-20 10:06:11 +08006209 * we have to initialize this early, so we can reclaim the inode
6210 * number if we fail afterwards in this function.
6211 */
6212 inode->i_ino = objectid;
6213
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006214 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006215 trace_btrfs_inode_request(dir);
6216
Chris Mason3de45862008-11-17 21:02:50 -05006217 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006218 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006219 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006220 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006221 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006222 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006223 } else if (dir) {
6224 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006225 }
6226 /*
6227 * index_cnt is ignored for everything but a dir,
6228 * btrfs_get_inode_index_count has an explanation for the magic
6229 * number
6230 */
6231 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006232 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006233 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006234 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006235 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006236
Josef Bacik5dc562c2012-08-17 13:14:17 -04006237 /*
6238 * We could have gotten an inode number from somebody who was fsynced
6239 * and then removed in this same transaction, so let's just set full
6240 * sync since it will be a full sync anyway and this will blow away the
6241 * old info in the log.
6242 */
6243 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6244
Chris Mason9c583092008-01-29 15:15:18 -05006245 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006246 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006247 key[0].offset = 0;
6248
Chris Mason9c583092008-01-29 15:15:18 -05006249 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006250
6251 if (name) {
6252 /*
6253 * Start new inodes with an inode_ref. This is slightly more
6254 * efficient for small numbers of hard links since they will
6255 * be packed into one item. Extended refs will kick in if we
6256 * add more hard links than can fit in the ref item.
6257 */
6258 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006259 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006260 key[1].offset = ref_objectid;
6261
6262 sizes[1] = name_len + sizeof(*ref);
6263 }
Chris Mason9c583092008-01-29 15:15:18 -05006264
Chris Masonb0d5d102014-09-08 13:08:51 -07006265 location = &BTRFS_I(inode)->location;
6266 location->objectid = objectid;
6267 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006268 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006269
6270 ret = btrfs_insert_inode_locked(inode);
6271 if (ret < 0)
6272 goto fail;
6273
Chris Masonb9473432009-03-13 11:00:37 -04006274 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006275 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006276 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006277 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006278
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006279 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006280 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306281
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006282 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306283 inode->i_atime = inode->i_mtime;
6284 inode->i_ctime = inode->i_mtime;
6285 BTRFS_I(inode)->i_otime = inode->i_mtime;
6286
Chris Mason5f39d392007-10-15 16:14:19 -04006287 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6288 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006289 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6290 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006291 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006292
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006293 if (name) {
6294 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6295 struct btrfs_inode_ref);
6296 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6297 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6298 ptr = (unsigned long)(ref + 1);
6299 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6300 }
Chris Mason9c583092008-01-29 15:15:18 -05006301
Chris Mason5f39d392007-10-15 16:14:19 -04006302 btrfs_mark_buffer_dirty(path->nodes[0]);
6303 btrfs_free_path(path);
6304
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006305 btrfs_inherit_iflags(inode, dir);
6306
Al Viro569254b2011-07-24 17:08:40 -04006307 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006308 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006309 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006310 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006311 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6312 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006313 }
6314
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006315 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006316
6317 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006318 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006319
Alexander Block8ea05e32012-07-25 17:35:53 +02006320 btrfs_update_root_times(trans, root);
6321
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006322 ret = btrfs_inode_inherit_props(trans, inode, dir);
6323 if (ret)
6324 btrfs_err(root->fs_info,
6325 "error inheriting props for ino %llu (root %llu): %d",
6326 btrfs_ino(inode), root->root_key.objectid, ret);
6327
Chris Mason39279cc2007-06-12 06:35:45 -04006328 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006329
6330fail_unlock:
6331 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006332fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006333 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006334 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006335 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006336 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006337 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006338}
6339
6340static inline u8 btrfs_inode_type(struct inode *inode)
6341{
6342 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6343}
6344
Chris Masond352ac62008-09-29 15:18:18 -04006345/*
6346 * utility function to add 'inode' into 'parent_inode' with
6347 * a give name and a given sequence number.
6348 * if 'add_backref' is true, also insert a backref from the
6349 * inode to the parent directory.
6350 */
Chris Masone02119d2008-09-05 16:13:11 -04006351int btrfs_add_link(struct btrfs_trans_handle *trans,
6352 struct inode *parent_inode, struct inode *inode,
6353 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006354{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006355 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006356 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006357 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006358 u64 ino = btrfs_ino(inode);
6359 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006360
Li Zefan33345d012011-04-20 10:31:50 +08006361 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006362 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6363 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006364 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006365 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006366 key.offset = 0;
6367 }
Chris Mason39279cc2007-06-12 06:35:45 -04006368
Li Zefan33345d012011-04-20 10:31:50 +08006369 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006370 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6371 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006372 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006373 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006374 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6375 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006376 }
6377
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006378 /* Nothing to clean up yet */
6379 if (ret)
6380 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006381
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006382 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6383 parent_inode, &key,
6384 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006385 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006386 goto fail_dir_item;
6387 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006388 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006389 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006390 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006391
6392 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6393 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006394 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006395 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006396 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006397 ret = btrfs_update_inode(trans, root, parent_inode);
6398 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006399 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006400 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006401
6402fail_dir_item:
6403 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6404 u64 local_index;
6405 int err;
6406 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6407 key.objectid, root->root_key.objectid,
6408 parent_ino, &local_index, name, name_len);
6409
6410 } else if (add_backref) {
6411 u64 local_index;
6412 int err;
6413
6414 err = btrfs_del_inode_ref(trans, root, name, name_len,
6415 ino, parent_ino, &local_index);
6416 }
6417 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006418}
6419
6420static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006421 struct inode *dir, struct dentry *dentry,
6422 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006423{
Josef Bacika1b075d2010-11-19 20:36:11 +00006424 int err = btrfs_add_link(trans, dir, inode,
6425 dentry->d_name.name, dentry->d_name.len,
6426 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006427 if (err > 0)
6428 err = -EEXIST;
6429 return err;
6430}
6431
Josef Bacik618e21d2007-07-11 10:18:17 -04006432static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006433 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006434{
6435 struct btrfs_trans_handle *trans;
6436 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006437 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006438 int err;
6439 int drop_inode = 0;
6440 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006441 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006442
Josef Bacik9ed74f22009-09-11 16:12:44 -04006443 /*
6444 * 2 for inode item and ref
6445 * 2 for dir items
6446 * 1 for xattr if selinux is on
6447 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006448 trans = btrfs_start_transaction(root, 5);
6449 if (IS_ERR(trans))
6450 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006451
Li Zefan581bb052011-04-20 10:06:11 +08006452 err = btrfs_find_free_ino(root, &objectid);
6453 if (err)
6454 goto out_unlock;
6455
Josef Bacikaec74772008-07-24 12:12:38 -04006456 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006457 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006458 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006459 if (IS_ERR(inode)) {
6460 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006461 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006462 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006463
Casey Schauflerad19db72011-12-15 10:09:07 -05006464 /*
6465 * If the active LSM wants to access the inode during
6466 * d_instantiate it needs these. Smack checks to see
6467 * if the filesystem supports xattrs by looking at the
6468 * ops vector.
6469 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006470 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006471 init_special_inode(inode, inode->i_mode, rdev);
6472
6473 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006474 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006475 goto out_unlock_inode;
6476
6477 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6478 if (err) {
6479 goto out_unlock_inode;
6480 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006481 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006482 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006483 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006484 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006485
Josef Bacik618e21d2007-07-11 10:18:17 -04006486out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006487 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006488 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006489 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006490 if (drop_inode) {
6491 inode_dec_link_count(inode);
6492 iput(inode);
6493 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006494 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006495
6496out_unlock_inode:
6497 drop_inode = 1;
6498 unlock_new_inode(inode);
6499 goto out_unlock;
6500
Josef Bacik618e21d2007-07-11 10:18:17 -04006501}
6502
Chris Mason39279cc2007-06-12 06:35:45 -04006503static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006504 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006505{
6506 struct btrfs_trans_handle *trans;
6507 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006508 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006509 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006510 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006511 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006512 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006513
Josef Bacik9ed74f22009-09-11 16:12:44 -04006514 /*
6515 * 2 for inode item and ref
6516 * 2 for dir items
6517 * 1 for xattr if selinux is on
6518 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006519 trans = btrfs_start_transaction(root, 5);
6520 if (IS_ERR(trans))
6521 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006522
Li Zefan581bb052011-04-20 10:06:11 +08006523 err = btrfs_find_free_ino(root, &objectid);
6524 if (err)
6525 goto out_unlock;
6526
Josef Bacikaec74772008-07-24 12:12:38 -04006527 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006528 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006529 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006530 if (IS_ERR(inode)) {
6531 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006532 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006533 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006534 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006535 /*
6536 * If the active LSM wants to access the inode during
6537 * d_instantiate it needs these. Smack checks to see
6538 * if the filesystem supports xattrs by looking at the
6539 * ops vector.
6540 */
6541 inode->i_fop = &btrfs_file_operations;
6542 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006543 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006544
6545 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6546 if (err)
6547 goto out_unlock_inode;
6548
6549 err = btrfs_update_inode(trans, root, inode);
6550 if (err)
6551 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006552
Josef Bacika1b075d2010-11-19 20:36:11 +00006553 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006554 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006555 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006556
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006557 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006558 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006559 d_instantiate(dentry, inode);
6560
Chris Mason39279cc2007-06-12 06:35:45 -04006561out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006562 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006563 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006564 inode_dec_link_count(inode);
6565 iput(inode);
6566 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006567 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006568 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006569 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006570
6571out_unlock_inode:
6572 unlock_new_inode(inode);
6573 goto out_unlock;
6574
Chris Mason39279cc2007-06-12 06:35:45 -04006575}
6576
6577static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6578 struct dentry *dentry)
6579{
Filipe Manana271dba42016-01-05 16:24:05 +00006580 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006581 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006582 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006583 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006584 int err;
6585 int drop_inode = 0;
6586
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006587 /* do not allow sys_link's with other subvols of the same device */
6588 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006589 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006590
Mark Fashehf1863732012-08-08 11:32:27 -07006591 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006592 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006593
Chris Mason3de45862008-11-17 21:02:50 -05006594 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006595 if (err)
6596 goto fail;
6597
Yan, Zhenga22285a2010-05-16 10:48:46 -04006598 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006599 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006600 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006601 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006602 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006603 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006604 if (IS_ERR(trans)) {
6605 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006606 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006607 goto fail;
6608 }
Chris Mason5f39d392007-10-15 16:14:19 -04006609
Miao Xie67de1172013-12-26 13:07:06 +08006610 /* There are several dir indexes for this inode, clear the cache. */
6611 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006612 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006613 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006614 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006615 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006616 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006617
Josef Bacika1b075d2010-11-19 20:36:11 +00006618 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006619
Yan, Zhenga5719522009-09-24 09:17:31 -04006620 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006621 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006622 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006623 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006624 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006625 if (err)
6626 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006627 if (inode->i_nlink == 1) {
6628 /*
6629 * If new hard link count is 1, it's a file created
6630 * with open(2) O_TMPFILE flag.
6631 */
6632 err = btrfs_orphan_del(trans, inode);
6633 if (err)
6634 goto fail;
6635 }
Al Viro08c422c2011-12-23 07:58:13 -05006636 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006637 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006638 }
Chris Mason39279cc2007-06-12 06:35:45 -04006639
Josef Bacikc581afc2014-02-06 16:06:06 -05006640 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006641fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006642 if (trans)
6643 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006644 if (drop_inode) {
6645 inode_dec_link_count(inode);
6646 iput(inode);
6647 }
Liu Bob53d3f52012-11-14 14:34:34 +00006648 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006649 return err;
6650}
6651
Al Viro18bb1db2011-07-26 01:41:39 -04006652static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006653{
Chris Masonb9d86662008-05-02 16:13:49 -04006654 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006655 struct btrfs_trans_handle *trans;
6656 struct btrfs_root *root = BTRFS_I(dir)->root;
6657 int err = 0;
6658 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006659 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006660 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006661
Josef Bacik9ed74f22009-09-11 16:12:44 -04006662 /*
6663 * 2 items for inode and ref
6664 * 2 items for dir items
6665 * 1 for xattr if selinux is on
6666 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006667 trans = btrfs_start_transaction(root, 5);
6668 if (IS_ERR(trans))
6669 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006670
Li Zefan581bb052011-04-20 10:06:11 +08006671 err = btrfs_find_free_ino(root, &objectid);
6672 if (err)
6673 goto out_fail;
6674
Josef Bacikaec74772008-07-24 12:12:38 -04006675 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006676 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006677 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006678 if (IS_ERR(inode)) {
6679 err = PTR_ERR(inode);
6680 goto out_fail;
6681 }
Chris Mason5f39d392007-10-15 16:14:19 -04006682
Chris Mason39279cc2007-06-12 06:35:45 -04006683 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006684 /* these must be set before we unlock the inode */
6685 inode->i_op = &btrfs_dir_inode_operations;
6686 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006687
Eric Paris2a7dba32011-02-01 11:05:39 -05006688 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006689 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006690 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006691
Chris Masondbe674a2008-07-17 12:54:05 -04006692 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006693 err = btrfs_update_inode(trans, root, inode);
6694 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006695 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006696
Josef Bacika1b075d2010-11-19 20:36:11 +00006697 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6698 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006699 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006700 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006701
Chris Mason39279cc2007-06-12 06:35:45 -04006702 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006703 /*
6704 * mkdir is special. We're unlocking after we call d_instantiate
6705 * to avoid a race with nfsd calling d_instantiate.
6706 */
6707 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006708 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006709
6710out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006711 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006712 if (drop_on_err) {
6713 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006714 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006715 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006716 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006717 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006718 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006719
6720out_fail_inode:
6721 unlock_new_inode(inode);
6722 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006723}
6724
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006725/* Find next extent map of a given extent map, caller needs to ensure locks */
6726static struct extent_map *next_extent_map(struct extent_map *em)
6727{
6728 struct rb_node *next;
6729
6730 next = rb_next(&em->rb_node);
6731 if (!next)
6732 return NULL;
6733 return container_of(next, struct extent_map, rb_node);
6734}
6735
6736static struct extent_map *prev_extent_map(struct extent_map *em)
6737{
6738 struct rb_node *prev;
6739
6740 prev = rb_prev(&em->rb_node);
6741 if (!prev)
6742 return NULL;
6743 return container_of(prev, struct extent_map, rb_node);
6744}
6745
Chris Masond352ac62008-09-29 15:18:18 -04006746/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006747 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006748 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006749 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006750 */
Chris Mason3b951512008-04-17 11:29:12 -04006751static int merge_extent_mapping(struct extent_map_tree *em_tree,
6752 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006753 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006754 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006755{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006756 struct extent_map *prev;
6757 struct extent_map *next;
6758 u64 start;
6759 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006760 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006761
Chris Masone6dcd2d2008-07-17 12:53:50 -04006762 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006763
6764 if (existing->start > map_start) {
6765 next = existing;
6766 prev = prev_extent_map(next);
6767 } else {
6768 prev = existing;
6769 next = next_extent_map(prev);
6770 }
6771
6772 start = prev ? extent_map_end(prev) : em->start;
6773 start = max_t(u64, start, em->start);
6774 end = next ? next->start : extent_map_end(em);
6775 end = min_t(u64, end, extent_map_end(em));
6776 start_diff = start - em->start;
6777 em->start = start;
6778 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006779 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6780 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006781 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006782 em->block_len -= start_diff;
6783 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006784 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006785}
6786
Chris Masonc8b97812008-10-29 14:49:59 -04006787static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006788 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006789 size_t pg_offset, u64 extent_offset,
6790 struct btrfs_file_extent_item *item)
6791{
6792 int ret;
6793 struct extent_buffer *leaf = path->nodes[0];
6794 char *tmp;
6795 size_t max_size;
6796 unsigned long inline_size;
6797 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006798 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006799
6800 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006801 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006802 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6803 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006804 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006805 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006806 if (!tmp)
6807 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006808 ptr = btrfs_file_extent_inline_start(item);
6809
6810 read_extent_buffer(leaf, tmp, ptr, inline_size);
6811
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006812 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006813 ret = btrfs_decompress(compress_type, tmp, page,
6814 extent_offset, inline_size, max_size);
Zygo Blaxell8f60ef92017-03-10 16:45:44 -05006815
6816 /*
6817 * decompression code contains a memset to fill in any space between the end
6818 * of the uncompressed data and the end of max_size in case the decompressed
6819 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6820 * the end of an inline extent and the beginning of the next block, so we
6821 * cover that region here.
6822 */
6823
6824 if (max_size + pg_offset < PAGE_SIZE) {
6825 char *map = kmap(page);
6826 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6827 kunmap(page);
6828 }
Chris Masonc8b97812008-10-29 14:49:59 -04006829 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006830 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006831}
6832
Chris Masond352ac62008-09-29 15:18:18 -04006833/*
6834 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006835 * the ugly parts come from merging extents from the disk with the in-ram
6836 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006837 * where the in-ram extents might be locked pending data=ordered completion.
6838 *
6839 * This also copies inline extents directly into the page.
6840 */
Chris Masond3977122009-01-05 21:25:51 -05006841
Chris Masona52d9a82007-08-27 16:49:44 -04006842struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006843 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006844 int create)
6845{
6846 int ret;
6847 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006848 u64 extent_start = 0;
6849 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006850 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006851 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006852 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006853 struct btrfs_root *root = BTRFS_I(inode)->root;
6854 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006855 struct extent_buffer *leaf;
6856 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006857 struct extent_map *em = NULL;
6858 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006859 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006860 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006861 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006862
Chris Masona52d9a82007-08-27 16:49:44 -04006863again:
Chris Mason890871b2009-09-02 16:24:52 -04006864 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006865 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006866 if (em)
6867 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006868 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006869
Chris Masona52d9a82007-08-27 16:49:44 -04006870 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006871 if (em->start > start || em->start + em->len <= start)
6872 free_extent_map(em);
6873 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006874 free_extent_map(em);
6875 else
6876 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006877 }
David Sterba172ddd62011-04-21 00:48:27 +02006878 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006879 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006880 err = -ENOMEM;
6881 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006882 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006883 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006884 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006885 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006886 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006887 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006888
6889 if (!path) {
6890 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006891 if (!path) {
6892 err = -ENOMEM;
6893 goto out;
6894 }
6895 /*
6896 * Chances are we'll be called again, so go ahead and do
6897 * readahead
6898 */
David Sterbae4058b52015-11-27 16:31:35 +01006899 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006900 }
6901
Chris Mason179e29e2007-11-01 11:28:41 -04006902 ret = btrfs_lookup_file_extent(trans, root, path,
6903 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006904 if (ret < 0) {
6905 err = ret;
6906 goto out;
6907 }
6908
6909 if (ret != 0) {
6910 if (path->slots[0] == 0)
6911 goto not_found;
6912 path->slots[0]--;
6913 }
6914
Chris Mason5f39d392007-10-15 16:14:19 -04006915 leaf = path->nodes[0];
6916 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006917 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006918 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006919 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006920 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006921 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006922 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006923 /*
6924 * If we backup past the first extent we want to move forward
6925 * and see if there is an extent in front of us, otherwise we'll
6926 * say there is a hole for our whole search range which can
6927 * cause problems.
6928 */
6929 extent_end = start;
6930 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006931 }
6932
Chris Mason5f39d392007-10-15 16:14:19 -04006933 found_type = btrfs_file_extent_type(leaf, item);
6934 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006935 if (found_type == BTRFS_FILE_EXTENT_REG ||
6936 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006937 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006938 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006939 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6940 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006941 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006942 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006943 }
Josef Bacik25a50342013-10-14 12:08:38 -04006944next:
Yan Zheng9036c102008-10-30 14:19:41 -04006945 if (start >= extent_end) {
6946 path->slots[0]++;
6947 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6948 ret = btrfs_next_leaf(root, path);
6949 if (ret < 0) {
6950 err = ret;
6951 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006952 }
Yan Zheng9036c102008-10-30 14:19:41 -04006953 if (ret > 0)
6954 goto not_found;
6955 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006956 }
Yan Zheng9036c102008-10-30 14:19:41 -04006957 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6958 if (found_key.objectid != objectid ||
6959 found_key.type != BTRFS_EXTENT_DATA_KEY)
6960 goto not_found;
6961 if (start + len <= found_key.offset)
6962 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006963 if (start > found_key.offset)
6964 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006965 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006966 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006967 em->len = found_key.offset - start;
6968 goto not_found_em;
6969 }
6970
Filipe Manana7ffbb592014-06-09 03:48:05 +01006971 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6972
Yan Zhengd899e052008-10-30 14:25:28 -04006973 if (found_type == BTRFS_FILE_EXTENT_REG ||
6974 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006975 goto insert;
6976 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006977 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006978 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006979 size_t size;
6980 size_t extent_offset;
6981 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006982
Filipe Manana7ffbb592014-06-09 03:48:05 +01006983 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006984 goto out;
Yan689f9342007-10-29 11:41:07 -04006985
Chris Mason514ac8a2014-01-03 21:07:00 -08006986 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006987 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006988 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6989 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006990 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006991 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006992 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006993 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006994 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006995 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006996 if (btrfs_file_extent_compression(leaf, item) !=
6997 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006998 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006999 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007000 if (ret) {
7001 err = ret;
7002 goto out;
7003 }
Chris Masonc8b97812008-10-29 14:49:59 -04007004 } else {
7005 map = kmap(page);
7006 read_extent_buffer(leaf, map + pg_offset, ptr,
7007 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007008 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007009 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007010 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007011 copy_size);
7012 }
Chris Masonc8b97812008-10-29 14:49:59 -04007013 kunmap(page);
7014 }
Chris Mason179e29e2007-11-01 11:28:41 -04007015 flush_dcache_page(page);
7016 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007017 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007018 if (!trans) {
7019 kunmap(page);
7020 free_extent_map(em);
7021 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007022
David Sterbab3b4aa72011-04-21 01:20:15 +02007023 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007024 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007025
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007026 if (IS_ERR(trans))
7027 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007028 goto again;
7029 }
Chris Masonc8b97812008-10-29 14:49:59 -04007030 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007031 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007032 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007033 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007034 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007035 }
Chris Masond1310b22008-01-24 16:13:08 -05007036 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007037 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007038 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007039 }
7040not_found:
7041 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007042 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007043 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007044not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007045 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007046 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007047insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007048 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007049 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007050 btrfs_err(root->fs_info,
7051 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7052 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007053 err = -EIO;
7054 goto out;
7055 }
Chris Masond1310b22008-01-24 16:13:08 -05007056
7057 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007058 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007059 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007060 /* it is possible that someone inserted the extent into the tree
7061 * while we had the lock dropped. It is also possible that
7062 * an overlapping map exists in the tree
7063 */
Chris Masona52d9a82007-08-27 16:49:44 -04007064 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007065 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007066
7067 ret = 0;
7068
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007069 existing = search_extent_mapping(em_tree, start, len);
7070 /*
7071 * existing will always be non-NULL, since there must be
7072 * extent causing the -EEXIST.
7073 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007074 if (existing->start == em->start &&
7075 extent_map_end(existing) == extent_map_end(em) &&
7076 em->block_start == existing->block_start) {
7077 /*
7078 * these two extents are the same, it happens
7079 * with inlines especially
7080 */
7081 free_extent_map(em);
7082 em = existing;
7083 err = 0;
7084
7085 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007086 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007087 /*
7088 * The existing extent map is the one nearest to
7089 * the [start, start + len) range which overlaps
7090 */
7091 err = merge_extent_mapping(em_tree, existing,
7092 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007093 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007094 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007095 free_extent_map(em);
7096 em = NULL;
7097 }
7098 } else {
7099 free_extent_map(em);
7100 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007101 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007102 }
Chris Masona52d9a82007-08-27 16:49:44 -04007103 }
Chris Mason890871b2009-09-02 16:24:52 -04007104 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007105out:
liubo1abe9b82011-03-24 11:18:59 +00007106
Steven Rostedt4cd85872013-11-14 22:57:29 -05007107 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007108
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007109 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007110 if (trans) {
7111 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007112 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007113 err = ret;
7114 }
Chris Masona52d9a82007-08-27 16:49:44 -04007115 if (err) {
7116 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007117 return ERR_PTR(err);
7118 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007119 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007120 return em;
7121}
7122
Chris Masonec29ed52011-02-23 16:23:20 -05007123struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7124 size_t pg_offset, u64 start, u64 len,
7125 int create)
7126{
7127 struct extent_map *em;
7128 struct extent_map *hole_em = NULL;
7129 u64 range_start = start;
7130 u64 end;
7131 u64 found;
7132 u64 found_end;
7133 int err = 0;
7134
7135 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7136 if (IS_ERR(em))
7137 return em;
7138 if (em) {
7139 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007140 * if our em maps to
7141 * - a hole or
7142 * - a pre-alloc extent,
7143 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007144 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007145 if (em->block_start != EXTENT_MAP_HOLE &&
7146 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007147 return em;
7148 else
7149 hole_em = em;
7150 }
7151
7152 /* check to see if we've wrapped (len == -1 or similar) */
7153 end = start + len;
7154 if (end < start)
7155 end = (u64)-1;
7156 else
7157 end -= 1;
7158
7159 em = NULL;
7160
7161 /* ok, we didn't find anything, lets look for delalloc */
7162 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7163 end, len, EXTENT_DELALLOC, 1);
7164 found_end = range_start + found;
7165 if (found_end < range_start)
7166 found_end = (u64)-1;
7167
7168 /*
7169 * we didn't find anything useful, return
7170 * the original results from get_extent()
7171 */
7172 if (range_start > end || found_end <= start) {
7173 em = hole_em;
7174 hole_em = NULL;
7175 goto out;
7176 }
7177
7178 /* adjust the range_start to make sure it doesn't
7179 * go backwards from the start they passed in
7180 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307181 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007182 found = found_end - range_start;
7183
7184 if (found > 0) {
7185 u64 hole_start = start;
7186 u64 hole_len = len;
7187
David Sterba172ddd62011-04-21 00:48:27 +02007188 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007189 if (!em) {
7190 err = -ENOMEM;
7191 goto out;
7192 }
7193 /*
7194 * when btrfs_get_extent can't find anything it
7195 * returns one huge hole
7196 *
7197 * make sure what it found really fits our range, and
7198 * adjust to make sure it is based on the start from
7199 * the caller
7200 */
7201 if (hole_em) {
7202 u64 calc_end = extent_map_end(hole_em);
7203
7204 if (calc_end <= start || (hole_em->start > end)) {
7205 free_extent_map(hole_em);
7206 hole_em = NULL;
7207 } else {
7208 hole_start = max(hole_em->start, start);
7209 hole_len = calc_end - hole_start;
7210 }
7211 }
7212 em->bdev = NULL;
7213 if (hole_em && range_start > hole_start) {
7214 /* our hole starts before our delalloc, so we
7215 * have to return just the parts of the hole
7216 * that go until the delalloc starts
7217 */
7218 em->len = min(hole_len,
7219 range_start - hole_start);
7220 em->start = hole_start;
7221 em->orig_start = hole_start;
7222 /*
7223 * don't adjust block start at all,
7224 * it is fixed at EXTENT_MAP_HOLE
7225 */
7226 em->block_start = hole_em->block_start;
7227 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007228 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7229 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007230 } else {
7231 em->start = range_start;
7232 em->len = found;
7233 em->orig_start = range_start;
7234 em->block_start = EXTENT_MAP_DELALLOC;
7235 em->block_len = found;
7236 }
7237 } else if (hole_em) {
7238 return hole_em;
7239 }
7240out:
7241
7242 free_extent_map(hole_em);
7243 if (err) {
7244 free_extent_map(em);
7245 return ERR_PTR(err);
7246 }
7247 return em;
7248}
7249
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007250static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7251 const u64 start,
7252 const u64 len,
7253 const u64 orig_start,
7254 const u64 block_start,
7255 const u64 block_len,
7256 const u64 orig_block_len,
7257 const u64 ram_bytes,
7258 const int type)
7259{
7260 struct extent_map *em = NULL;
7261 int ret;
7262
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007263 if (type != BTRFS_ORDERED_NOCOW) {
7264 em = create_pinned_em(inode, start, len, orig_start,
7265 block_start, block_len, orig_block_len,
7266 ram_bytes, type);
7267 if (IS_ERR(em))
7268 goto out;
7269 }
7270 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7271 len, block_len, type);
7272 if (ret) {
7273 if (em) {
7274 free_extent_map(em);
7275 btrfs_drop_extent_cache(inode, start,
7276 start + len - 1, 0);
7277 }
7278 em = ERR_PTR(ret);
7279 }
7280 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007281
7282 return em;
7283}
7284
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7286 u64 start, u64 len)
7287{
7288 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007289 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290 struct btrfs_key ins;
7291 u64 alloc_hint;
7292 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007293
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294 alloc_hint = get_extent_allocation_hint(inode, start, len);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007295 ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007296 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007297 if (ret)
7298 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007300 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7301 ins.objectid, ins.offset, ins.offset,
7302 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007303 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007304 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007305 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007306
Josef Bacik4b46fce2010-05-23 11:00:55 -04007307 return em;
7308}
7309
Chris Mason46bfbb52010-05-26 11:04:10 -04007310/*
7311 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7312 * block must be cow'd
7313 */
Josef Bacik00361582013-08-14 14:02:47 -04007314noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007315 u64 *orig_start, u64 *orig_block_len,
7316 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007317{
Josef Bacik00361582013-08-14 14:02:47 -04007318 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007319 struct btrfs_path *path;
7320 int ret;
7321 struct extent_buffer *leaf;
7322 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007323 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007324 struct btrfs_file_extent_item *fi;
7325 struct btrfs_key key;
7326 u64 disk_bytenr;
7327 u64 backref_offset;
7328 u64 extent_end;
7329 u64 num_bytes;
7330 int slot;
7331 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007332 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007333
Chris Mason46bfbb52010-05-26 11:04:10 -04007334 path = btrfs_alloc_path();
7335 if (!path)
7336 return -ENOMEM;
7337
Josef Bacik00361582013-08-14 14:02:47 -04007338 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007339 offset, 0);
7340 if (ret < 0)
7341 goto out;
7342
7343 slot = path->slots[0];
7344 if (ret == 1) {
7345 if (slot == 0) {
7346 /* can't find the item, must cow */
7347 ret = 0;
7348 goto out;
7349 }
7350 slot--;
7351 }
7352 ret = 0;
7353 leaf = path->nodes[0];
7354 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007355 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 key.type != BTRFS_EXTENT_DATA_KEY) {
7357 /* not our file or wrong item type, must cow */
7358 goto out;
7359 }
7360
7361 if (key.offset > offset) {
7362 /* Wrong offset, must cow */
7363 goto out;
7364 }
7365
7366 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7367 found_type = btrfs_file_extent_type(leaf, fi);
7368 if (found_type != BTRFS_FILE_EXTENT_REG &&
7369 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7370 /* not a regular extent, must cow */
7371 goto out;
7372 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007373
7374 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7375 goto out;
7376
Miao Xiee77751a2013-12-27 21:11:50 +08007377 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7378 if (extent_end <= offset)
7379 goto out;
7380
Chris Mason46bfbb52010-05-26 11:04:10 -04007381 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007382 if (disk_bytenr == 0)
7383 goto out;
7384
7385 if (btrfs_file_extent_compression(leaf, fi) ||
7386 btrfs_file_extent_encryption(leaf, fi) ||
7387 btrfs_file_extent_other_encoding(leaf, fi))
7388 goto out;
7389
Chris Mason46bfbb52010-05-26 11:04:10 -04007390 backref_offset = btrfs_file_extent_offset(leaf, fi);
7391
Josef Bacik7ee9e442013-06-21 16:37:03 -04007392 if (orig_start) {
7393 *orig_start = key.offset - backref_offset;
7394 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7395 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7396 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007397
Chris Mason46bfbb52010-05-26 11:04:10 -04007398 if (btrfs_extent_readonly(root, disk_bytenr))
7399 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007400
7401 num_bytes = min(offset + *len, extent_end) - offset;
7402 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7403 u64 range_end;
7404
7405 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7406 ret = test_range_bit(io_tree, offset, range_end,
7407 EXTENT_DELALLOC, 0, NULL);
7408 if (ret) {
7409 ret = -EAGAIN;
7410 goto out;
7411 }
7412 }
7413
Josef Bacik1bda19e2013-10-18 12:10:36 -04007414 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007415
7416 /*
7417 * look for other files referencing this extent, if we
7418 * find any we must cow
7419 */
Josef Bacik00361582013-08-14 14:02:47 -04007420 trans = btrfs_join_transaction(root);
7421 if (IS_ERR(trans)) {
7422 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007423 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007424 }
7425
7426 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7427 key.offset - backref_offset, disk_bytenr);
7428 btrfs_end_transaction(trans, root);
7429 if (ret) {
7430 ret = 0;
7431 goto out;
7432 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007433
7434 /*
7435 * adjust disk_bytenr and num_bytes to cover just the bytes
7436 * in this extent we are about to write. If there
7437 * are any csums in that range we have to cow in order
7438 * to keep the csums correct
7439 */
7440 disk_bytenr += backref_offset;
7441 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007442 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7443 goto out;
7444 /*
7445 * all of the above have passed, it is safe to overwrite this extent
7446 * without cow
7447 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007448 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007449 ret = 1;
7450out:
7451 btrfs_free_path(path);
7452 return ret;
7453}
7454
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007455bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7456{
7457 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7458 int found = false;
7459 void **pagep = NULL;
7460 struct page *page = NULL;
David Sterba4d15ab92017-05-12 01:03:52 +02007461 unsigned long start_idx;
7462 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007463
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007464 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007465
7466 /*
7467 * end is the last byte in the last page. end == start is legal
7468 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007469 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007470
7471 rcu_read_lock();
7472
7473 /* Most of the code in this while loop is lifted from
7474 * find_get_page. It's been modified to begin searching from a
7475 * page and return just the first page found in that range. If the
7476 * found idx is less than or equal to the end idx then we know that
7477 * a page exists. If no pages are found or if those pages are
7478 * outside of the range then we're fine (yay!) */
7479 while (page == NULL &&
7480 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7481 page = radix_tree_deref_slot(pagep);
7482 if (unlikely(!page))
7483 break;
7484
7485 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007486 if (radix_tree_deref_retry(page)) {
7487 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007488 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007489 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007490 /*
7491 * Otherwise, shmem/tmpfs must be storing a swap entry
7492 * here as an exceptional entry: so return it without
7493 * attempting to raise page count.
7494 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007495 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007496 break; /* TODO: Is this relevant for this use case? */
7497 }
7498
Filipe Manana91405152014-06-05 13:22:24 +01007499 if (!page_cache_get_speculative(page)) {
7500 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007501 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007502 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007503
7504 /*
7505 * Has the page moved?
7506 * This is part of the lockless pagecache protocol. See
7507 * include/linux/pagemap.h for details.
7508 */
7509 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007510 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007511 page = NULL;
7512 }
7513 }
7514
7515 if (page) {
7516 if (page->index <= end_idx)
7517 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007518 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007519 }
7520
7521 rcu_read_unlock();
7522 return found;
7523}
7524
Josef Bacikeb838e72012-07-31 16:28:48 -04007525static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7526 struct extent_state **cached_state, int writing)
7527{
7528 struct btrfs_ordered_extent *ordered;
7529 int ret = 0;
7530
7531 while (1) {
7532 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007533 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007534 /*
7535 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007536 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007537 * extents in this range.
7538 */
7539 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7540 lockend - lockstart + 1);
7541
7542 /*
7543 * We need to make sure there are no buffered pages in this
7544 * range either, we could have raced between the invalidate in
7545 * generic_file_direct_write and locking the extent. The
7546 * invalidate needs to happen so that reads after a write do not
7547 * get stale data.
7548 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007549 if (!ordered &&
7550 (!writing ||
7551 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007552 break;
7553
7554 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7555 cached_state, GFP_NOFS);
7556
7557 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007558 /*
7559 * If we are doing a DIO read and the ordered extent we
7560 * found is for a buffered write, we can not wait for it
7561 * to complete and retry, because if we do so we can
7562 * deadlock with concurrent buffered writes on page
7563 * locks. This happens only if our DIO read covers more
7564 * than one extent map, if at this point has already
7565 * created an ordered extent for a previous extent map
7566 * and locked its range in the inode's io tree, and a
7567 * concurrent write against that previous extent map's
7568 * range and this range started (we unlock the ranges
7569 * in the io tree only when the bios complete and
7570 * buffered writes always lock pages before attempting
7571 * to lock range in the io tree).
7572 */
7573 if (writing ||
7574 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7575 btrfs_start_ordered_extent(inode, ordered, 1);
7576 else
7577 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007578 btrfs_put_ordered_extent(ordered);
7579 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007580 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007581 * We could trigger writeback for this range (and wait
7582 * for it to complete) and then invalidate the pages for
7583 * this range (through invalidate_inode_pages2_range()),
7584 * but that can lead us to a deadlock with a concurrent
7585 * call to readpages() (a buffered read or a defrag call
7586 * triggered a readahead) on a page lock due to an
7587 * ordered dio extent we created before but did not have
7588 * yet a corresponding bio submitted (whence it can not
7589 * complete), which makes readpages() wait for that
7590 * ordered extent to complete while holding a lock on
7591 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007592 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007593 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007594 }
7595
Filipe Mananaade77022016-02-18 14:28:55 +00007596 if (ret)
7597 break;
7598
Josef Bacikeb838e72012-07-31 16:28:48 -04007599 cond_resched();
7600 }
7601
7602 return ret;
7603}
7604
Josef Bacik69ffb542012-09-11 15:40:07 -04007605static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7606 u64 len, u64 orig_start,
7607 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007608 u64 orig_block_len, u64 ram_bytes,
7609 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007610{
7611 struct extent_map_tree *em_tree;
7612 struct extent_map *em;
7613 struct btrfs_root *root = BTRFS_I(inode)->root;
7614 int ret;
7615
7616 em_tree = &BTRFS_I(inode)->extent_tree;
7617 em = alloc_extent_map();
7618 if (!em)
7619 return ERR_PTR(-ENOMEM);
7620
7621 em->start = start;
7622 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007623 em->mod_start = start;
7624 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007625 em->len = len;
7626 em->block_len = block_len;
7627 em->block_start = block_start;
7628 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007629 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007630 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007631 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007632 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7633 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007634 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007635
7636 do {
7637 btrfs_drop_extent_cache(inode, em->start,
7638 em->start + em->len - 1, 0);
7639 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007640 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007641 write_unlock(&em_tree->lock);
7642 } while (ret == -EEXIST);
7643
7644 if (ret) {
7645 free_extent_map(em);
7646 return ERR_PTR(ret);
7647 }
7648
7649 return em;
7650}
7651
Filipe Manana9c9464c2015-11-04 09:52:04 +00007652static void adjust_dio_outstanding_extents(struct inode *inode,
7653 struct btrfs_dio_data *dio_data,
7654 const u64 len)
7655{
7656 unsigned num_extents;
7657
7658 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7659 BTRFS_MAX_EXTENT_SIZE);
7660 /*
7661 * If we have an outstanding_extents count still set then we're
7662 * within our reservation, otherwise we need to adjust our inode
7663 * counter appropriately.
7664 */
Liu Bof76ddff2016-12-22 17:13:54 -08007665 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007666 dio_data->outstanding_extents -= num_extents;
7667 } else {
Liu Bof76ddff2016-12-22 17:13:54 -08007668 /*
7669 * If dio write length has been split due to no large enough
7670 * contiguous space, we need to compensate our inode counter
7671 * appropriately.
7672 */
7673 u64 num_needed = num_extents - dio_data->outstanding_extents;
7674
Filipe Manana9c9464c2015-11-04 09:52:04 +00007675 spin_lock(&BTRFS_I(inode)->lock);
Liu Bof76ddff2016-12-22 17:13:54 -08007676 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007677 spin_unlock(&BTRFS_I(inode)->lock);
7678 }
7679}
7680
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7682 struct buffer_head *bh_result, int create)
7683{
7684 struct extent_map *em;
7685 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007686 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307687 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007688 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007689 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007690 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007691 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007692 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007693
Miao Xie172a5042013-02-21 02:48:22 -07007694 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007695 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007696 else
Josef Bacikc3298612012-08-03 16:49:19 -04007697 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007698
Josef Bacikc3298612012-08-03 16:49:19 -04007699 lockstart = start;
7700 lockend = start + len - 1;
7701
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007702 if (current->journal_info) {
7703 /*
7704 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007705 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007706 * confused.
7707 */
chandan50745b02015-08-28 21:10:13 +05307708 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007709 current->journal_info = NULL;
7710 }
7711
Josef Bacikeb838e72012-07-31 16:28:48 -04007712 /*
7713 * If this errors out it's because we couldn't invalidate pagecache for
7714 * this range and we need to fallback to buffered.
7715 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007716 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7717 create)) {
7718 ret = -ENOTBLK;
7719 goto err;
7720 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007721
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007723 if (IS_ERR(em)) {
7724 ret = PTR_ERR(em);
7725 goto unlock_err;
7726 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727
7728 /*
7729 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7730 * io. INLINE is special, and we could probably kludge it in here, but
7731 * it's still buffered so for safety lets just fall back to the generic
7732 * buffered path.
7733 *
7734 * For COMPRESSED we _have_ to read the entire extent in so we can
7735 * decompress it, so there will be buffering required no matter what we
7736 * do, so go ahead and fallback to buffered.
7737 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007738 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007739 * to buffered IO. Don't blame me, this is the price we pay for using
7740 * the generic code.
7741 */
7742 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7743 em->block_start == EXTENT_MAP_INLINE) {
7744 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007745 ret = -ENOTBLK;
7746 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747 }
7748
7749 /* Just a good old fashioned hole, return */
7750 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7751 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7752 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007753 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007754 }
7755
7756 /*
7757 * We don't allocate a new extent in the following cases
7758 *
7759 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7760 * existing extent.
7761 * 2) The extent is marked as PREALLOC. We're good to go here and can
7762 * just use the extent.
7763 *
7764 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007765 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 len = min(len, em->len - (start - em->start));
7767 lockstart = start + len;
7768 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007769 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770
7771 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7772 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7773 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007775 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007776
7777 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7778 type = BTRFS_ORDERED_PREALLOC;
7779 else
7780 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007781 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007782 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007783
Josef Bacik00361582013-08-14 14:02:47 -04007784 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007785 &orig_block_len, &ram_bytes) == 1 &&
7786 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007787 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007788
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007789 em2 = btrfs_create_dio_extent(inode, start, len,
7790 orig_start, block_start,
7791 len, orig_block_len,
7792 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007793 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007794 if (type == BTRFS_ORDERED_PREALLOC) {
7795 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007796 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007797 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007798 if (em2 && IS_ERR(em2)) {
7799 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007800 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007801 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007802 /*
7803 * For inode marked NODATACOW or extent marked PREALLOC,
7804 * use the existing or preallocated extent, so does not
7805 * need to adjust btrfs_space_info's bytes_may_use.
7806 */
7807 btrfs_free_reserved_data_space_noquota(inode,
7808 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007809 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007810 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007811 }
Josef Bacik00361582013-08-14 14:02:47 -04007812
Chris Mason46bfbb52010-05-26 11:04:10 -04007813 /*
7814 * this will cow the extent, reset the len in case we changed
7815 * it above
7816 */
7817 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007818 free_extent_map(em);
7819 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007820 if (IS_ERR(em)) {
7821 ret = PTR_ERR(em);
7822 goto unlock_err;
7823 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007824 len = min(len, em->len - (start - em->start));
7825unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007826 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7827 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007828 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829 bh_result->b_bdev = em->bdev;
7830 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007831 if (create) {
7832 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7833 set_buffer_new(bh_result);
7834
7835 /*
7836 * Need to update the i_size under the extent lock so buffered
7837 * readers will get the updated i_size when we unlock.
7838 */
7839 if (start + len > i_size_read(inode))
7840 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007841
Filipe Manana9c9464c2015-11-04 09:52:04 +00007842 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307843 WARN_ON(dio_data->reserve < len);
7844 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007845 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307846 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007847 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007848
Josef Bacikeb838e72012-07-31 16:28:48 -04007849 /*
7850 * In the case of write we need to clear and unlock the entire range,
7851 * in the case of read we need to unlock only the end area that we
7852 * aren't using if there is any left over space.
7853 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007854 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007855 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7856 lockend, unlock_bits, 1, 0,
7857 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007858 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007859 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007860 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007861
Josef Bacik4b46fce2010-05-23 11:00:55 -04007862 free_extent_map(em);
7863
7864 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007865
7866unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007867 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7868 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007869err:
chandan50745b02015-08-28 21:10:13 +05307870 if (dio_data)
7871 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007872 /*
7873 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7874 * write less data then expected, so that we don't underflow our inode's
7875 * outstanding extents counter.
7876 */
7877 if (create && dio_data)
7878 adjust_dio_outstanding_extents(inode, dio_data, len);
7879
Josef Bacikeb838e72012-07-31 16:28:48 -04007880 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007881}
7882
Miao Xie8b110e32014-09-12 18:44:03 +08007883static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f62016-06-05 14:31:54 -05007884 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007885{
7886 struct btrfs_root *root = BTRFS_I(inode)->root;
7887 int ret;
7888
Mike Christie37226b22016-06-05 14:31:52 -05007889 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007890
7891 bio_get(bio);
7892
7893 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7894 BTRFS_WQ_ENDIO_DIO_REPAIR);
7895 if (ret)
7896 goto err;
7897
Mike Christie81a75f62016-06-05 14:31:54 -05007898 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007899err:
7900 bio_put(bio);
7901 return ret;
7902}
7903
7904static int btrfs_check_dio_repairable(struct inode *inode,
7905 struct bio *failed_bio,
7906 struct io_failure_record *failrec,
7907 int failed_mirror)
7908{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007910 int num_copies;
7911
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007912 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007913 if (num_copies == 1) {
7914 /*
7915 * we only have a single copy of the data, so don't bother with
7916 * all the retry and error correction code that follows. no
7917 * matter what the error is, it is very likely to persist.
7918 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007919 btrfs_debug(fs_info,
7920 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7921 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007922 return 0;
7923 }
7924
7925 failrec->failed_mirror = failed_mirror;
7926 failrec->this_mirror++;
7927 if (failrec->this_mirror == failed_mirror)
7928 failrec->this_mirror++;
7929
7930 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007931 btrfs_debug(fs_info,
7932 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7933 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007934 return 0;
7935 }
7936
7937 return 1;
7938}
7939
7940static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307941 struct page *page, unsigned int pgoff,
7942 u64 start, u64 end, int failed_mirror,
7943 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007944{
7945 struct io_failure_record *failrec;
7946 struct bio *bio;
7947 int isector;
7948 int read_mode;
7949 int ret;
7950
Mike Christie37226b22016-06-05 14:31:52 -05007951 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007952
7953 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7954 if (ret)
7955 return ret;
7956
7957 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7958 failed_mirror);
7959 if (!ret) {
7960 free_io_failure(inode, failrec);
7961 return -EIO;
7962 }
7963
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 if ((failed_bio->bi_vcnt > 1)
7965 || (failed_bio->bi_io_vec->bv_len
7966 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007967 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7968 else
7969 read_mode = READ_SYNC;
7970
7971 isector = start - btrfs_io_bio(failed_bio)->logical;
7972 isector >>= inode->i_sb->s_blocksize_bits;
7973 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007975 if (!bio) {
7976 free_io_failure(inode, failrec);
7977 return -EIO;
7978 }
Mike Christie37226b22016-06-05 14:31:52 -05007979 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007980
7981 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7982 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7983 read_mode, failrec->this_mirror, failrec->in_validation);
7984
Mike Christie81a75f62016-06-05 14:31:54 -05007985 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007986 if (ret) {
7987 free_io_failure(inode, failrec);
7988 bio_put(bio);
7989 }
7990
7991 return ret;
7992}
7993
7994struct btrfs_retry_complete {
7995 struct completion done;
7996 struct inode *inode;
7997 u64 start;
7998 int uptodate;
7999};
8000
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008001static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008002{
8003 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008005 struct bio_vec *bvec;
8006 int i;
8007
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008008 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008009 goto end;
8010
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308011 ASSERT(bio->bi_vcnt == 1);
8012 inode = bio->bi_io_vec->bv_page->mapping->host;
8013 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8014
Miao Xie8b110e32014-09-12 18:44:03 +08008015 done->uptodate = 1;
8016 bio_for_each_segment_all(bvec, bio, i)
8017 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
8018end:
8019 complete(&done->done);
8020 bio_put(bio);
8021}
8022
8023static int __btrfs_correct_data_nocsum(struct inode *inode,
8024 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008025{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308026 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008027 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08008028 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008029 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030 unsigned int pgoff;
8031 u32 sectorsize;
8032 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008033 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08008034 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008035
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 fs_info = BTRFS_I(inode)->root->fs_info;
8037 sectorsize = BTRFS_I(inode)->root->sectorsize;
8038
Miao Xiec1dc0892014-09-12 18:43:56 +08008039 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008040 done.inode = inode;
8041
8042 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8044 pgoff = bvec->bv_offset;
8045
8046next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008047 done.uptodate = 0;
8048 done.start = start;
8049 init_completion(&done.done);
8050
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308051 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8052 pgoff, start, start + sectorsize - 1,
8053 io_bio->mirror_num,
8054 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008055 if (ret)
8056 return ret;
8057
8058 wait_for_completion(&done.done);
8059
8060 if (!done.uptodate) {
8061 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008063 }
8064
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 start += sectorsize;
8066
Liu Boc17acd22017-04-07 13:11:10 -07008067 nr_sectors--;
8068 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308069 pgoff += sectorsize;
Liu Boc17acd22017-04-07 13:11:10 -07008070 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 goto next_block_or_try_again;
8072 }
Miao Xie8b110e32014-09-12 18:44:03 +08008073 }
8074
8075 return 0;
8076}
8077
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008078static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008079{
8080 struct btrfs_retry_complete *done = bio->bi_private;
8081 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008083 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308084 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008085 int uptodate;
8086 int ret;
8087 int i;
8088
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008089 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008090 goto end;
8091
8092 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093
8094 start = done->start;
8095
8096 ASSERT(bio->bi_vcnt == 1);
8097 inode = bio->bi_io_vec->bv_page->mapping->host;
8098 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8099
Miao Xie8b110e32014-09-12 18:44:03 +08008100 bio_for_each_segment_all(bvec, bio, i) {
8101 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 bvec->bv_page, bvec->bv_offset,
8103 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008104 if (!ret)
8105 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308106 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008107 else
8108 uptodate = 0;
8109 }
8110
8111 done->uptodate = uptodate;
8112end:
8113 complete(&done->done);
8114 bio_put(bio);
8115}
8116
8117static int __btrfs_subio_endio_read(struct inode *inode,
8118 struct btrfs_io_bio *io_bio, int err)
8119{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308120 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008121 struct bio_vec *bvec;
8122 struct btrfs_retry_complete done;
8123 u64 start;
8124 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 u32 sectorsize;
8126 int nr_sectors;
8127 unsigned int pgoff;
8128 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008129 int i;
8130 int ret;
8131
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308132 fs_info = BTRFS_I(inode)->root->fs_info;
8133 sectorsize = BTRFS_I(inode)->root->sectorsize;
8134
Miao Xie8b110e32014-09-12 18:44:03 +08008135 err = 0;
8136 start = io_bio->logical;
8137 done.inode = inode;
8138
Miao Xiec1dc0892014-09-12 18:43:56 +08008139 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308140 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8141
8142 pgoff = bvec->bv_offset;
8143next_block:
8144 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8145 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8146 bvec->bv_page, pgoff, start,
8147 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008148 if (likely(!ret))
8149 goto next;
8150try_again:
8151 done.uptodate = 0;
8152 done.start = start;
8153 init_completion(&done.done);
8154
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308155 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8156 pgoff, start, start + sectorsize - 1,
8157 io_bio->mirror_num,
8158 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008159 if (ret) {
8160 err = ret;
8161 goto next;
8162 }
8163
8164 wait_for_completion(&done.done);
8165
8166 if (!done.uptodate) {
8167 /* We might have another mirror, so try again */
8168 goto try_again;
8169 }
8170next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308171 offset += sectorsize;
8172 start += sectorsize;
8173
8174 ASSERT(nr_sectors);
8175
Liu Boc17acd22017-04-07 13:11:10 -07008176 nr_sectors--;
8177 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308178 pgoff += sectorsize;
Liu Boc17acd22017-04-07 13:11:10 -07008179 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308180 goto next_block;
8181 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008182 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008183
8184 return err;
8185}
8186
Miao Xie8b110e32014-09-12 18:44:03 +08008187static int btrfs_subio_endio_read(struct inode *inode,
8188 struct btrfs_io_bio *io_bio, int err)
8189{
8190 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8191
8192 if (skip_csum) {
8193 if (unlikely(err))
8194 return __btrfs_correct_data_nocsum(inode, io_bio);
8195 else
8196 return 0;
8197 } else {
8198 return __btrfs_subio_endio_read(inode, io_bio, err);
8199 }
8200}
8201
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008202static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008203{
8204 struct btrfs_dio_private *dip = bio->bi_private;
8205 struct inode *inode = dip->inode;
8206 struct bio *dio_bio;
8207 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008208 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008209
Miao Xie8b110e32014-09-12 18:44:03 +08008210 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8211 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008212
Josef Bacik4b46fce2010-05-23 11:00:55 -04008213 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008214 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008215 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008216
Josef Bacik4b46fce2010-05-23 11:00:55 -04008217 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008218
Filipe Manana1636d1d2016-02-15 16:20:26 +00008219 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008220 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008221
8222 if (io_bio->end_io)
8223 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008224 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008225}
8226
Filipe Manana14543772015-11-24 16:23:54 +00008227static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8228 const u64 offset,
8229 const u64 bytes,
8230 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008231{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008232 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008234 u64 ordered_offset = offset;
8235 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008236 int ret;
8237
Chris Mason163cf092010-11-28 19:56:33 -05008238again:
8239 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8240 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008241 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008242 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008243 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008244 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008245
Liu Bo9e0af232014-08-15 23:36:53 +08008246 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8247 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008248 btrfs_queue_work(root->fs_info->endio_write_workers,
8249 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008250out_test:
8251 /*
8252 * our bio might span multiple ordered extents. If we haven't
8253 * completed the accounting for the whole dio, go back and try again
8254 */
Filipe Manana14543772015-11-24 16:23:54 +00008255 if (ordered_offset < offset + bytes) {
8256 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008257 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008258 goto again;
8259 }
Filipe Manana14543772015-11-24 16:23:54 +00008260}
8261
8262static void btrfs_endio_direct_write(struct bio *bio)
8263{
8264 struct btrfs_dio_private *dip = bio->bi_private;
8265 struct bio *dio_bio = dip->dio_bio;
8266
8267 btrfs_endio_direct_write_update_ordered(dip->inode,
8268 dip->logical_offset,
8269 dip->bytes,
8270 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008271
Josef Bacik4b46fce2010-05-23 11:00:55 -04008272 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008273
Filipe Manana1636d1d2016-02-15 16:20:26 +00008274 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008275 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008276 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008277}
8278
Mike Christie81a75f62016-06-05 14:31:54 -05008279static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008280 struct bio *bio, int mirror_num,
8281 unsigned long bio_flags, u64 offset)
8282{
8283 int ret;
8284 struct btrfs_root *root = BTRFS_I(inode)->root;
8285 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008286 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008287 return 0;
8288}
8289
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008290static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008291{
8292 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008293 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008294
Miao Xie8b110e32014-09-12 18:44:03 +08008295 if (err)
8296 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008297 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008298 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008299 (unsigned long long)bio->bi_iter.bi_sector,
8300 bio->bi_iter.bi_size, err);
8301
8302 if (dip->subio_endio)
8303 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008304
8305 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008306 dip->errors = 1;
8307
8308 /*
8309 * before atomic variable goto zero, we must make sure
8310 * dip->errors is perceived to be set.
8311 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008312 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008313 }
8314
8315 /* if there are more bios still pending for this dio, just exit */
8316 if (!atomic_dec_and_test(&dip->pending_bios))
8317 goto out;
8318
Chris Mason9be33952013-05-17 18:30:14 -04008319 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008320 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008321 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008322 dip->dio_bio->bi_error = 0;
8323 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008324 }
8325out:
8326 bio_put(bio);
8327}
8328
8329static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8330 u64 first_sector, gfp_t gfp_flags)
8331{
Chris Masonda2f0f72015-07-02 13:57:22 -07008332 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008333 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008334 if (bio)
8335 bio_associate_current(bio);
8336 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008337}
8338
Miao Xiec1dc0892014-09-12 18:43:56 +08008339static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8340 struct inode *inode,
8341 struct btrfs_dio_private *dip,
8342 struct bio *bio,
8343 u64 file_offset)
8344{
8345 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8346 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8347 int ret;
8348
8349 /*
8350 * We load all the csum data we need when we submit
8351 * the first bio to reduce the csum tree search and
8352 * contention.
8353 */
8354 if (dip->logical_offset == file_offset) {
8355 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8356 file_offset);
8357 if (ret)
8358 return ret;
8359 }
8360
8361 if (bio == dip->orig_bio)
8362 return 0;
8363
8364 file_offset -= dip->logical_offset;
8365 file_offset >>= inode->i_sb->s_blocksize_bits;
8366 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8367
8368 return 0;
8369}
8370
Miao Xiee65e1532010-11-22 03:04:43 +00008371static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f62016-06-05 14:31:54 -05008372 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008373 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008374{
Miao Xiefacc8a22013-07-25 19:22:34 +08008375 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008376 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008377 struct btrfs_root *root = BTRFS_I(inode)->root;
8378 int ret;
8379
Josef Bacikb812ce22012-11-16 13:56:32 -05008380 if (async_submit)
8381 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8382
Miao Xiee65e1532010-11-22 03:04:43 +00008383 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008384
8385 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008386 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8387 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008388 if (ret)
8389 goto err;
8390 }
Miao Xiee65e1532010-11-22 03:04:43 +00008391
Josef Bacik1ae39932011-04-06 14:41:34 -04008392 if (skip_sum)
8393 goto map;
8394
8395 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008396 ret = btrfs_wq_submit_bio(root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -05008397 inode, bio, 0, 0, file_offset,
Miao Xiee65e1532010-11-22 03:04:43 +00008398 __btrfs_submit_bio_start_direct_io,
8399 __btrfs_submit_bio_done);
8400 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008401 } else if (write) {
8402 /*
8403 * If we aren't doing async submit, calculate the csum of the
8404 * bio now.
8405 */
8406 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8407 if (ret)
8408 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008409 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008410 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8411 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008412 if (ret)
8413 goto err;
8414 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008415map:
Mike Christie81a75f62016-06-05 14:31:54 -05008416 ret = btrfs_map_bio(root, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008417err:
8418 bio_put(bio);
8419 return ret;
8420}
8421
Mike Christie81a75f62016-06-05 14:31:54 -05008422static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008423 int skip_sum)
8424{
8425 struct inode *inode = dip->inode;
8426 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008427 struct bio *bio;
8428 struct bio *orig_bio = dip->orig_bio;
8429 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008430 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008431 u64 file_offset = dip->logical_offset;
8432 u64 submit_len = 0;
8433 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308434 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008435 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308436 int nr_sectors;
8437 int ret;
8438 int i;
Miao Xiee65e1532010-11-22 03:04:43 +00008439
Kent Overstreet4f024f32013-10-11 15:44:27 -07008440 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008441 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
8442 start_sector << 9, &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008443 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008444 return -EIO;
Miao Xiefacc8a22013-07-25 19:22:34 +08008445
Kent Overstreet4f024f32013-10-11 15:44:27 -07008446 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008447 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008448 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008449 goto submit;
8450 }
8451
David Woodhouse53b381b2013-01-29 18:40:14 -05008452 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008453 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008454 async_submit = 0;
8455 else
8456 async_submit = 1;
8457
Josef Bacik02f57c72011-04-06 14:25:44 -04008458 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8459 if (!bio)
8460 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008461
Bart Van Assche4382e332016-09-14 10:45:36 +02008462 bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
Josef Bacik02f57c72011-04-06 14:25:44 -04008463 bio->bi_private = dip;
8464 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008465 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008466 atomic_inc(&dip->pending_bios);
8467
Miao Xiee65e1532010-11-22 03:04:43 +00008468 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308469 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8470 i = 0;
8471next_block:
8472 if (unlikely(map_length < submit_len + blocksize ||
8473 bio_add_page(bio, bvec->bv_page, blocksize,
8474 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008475 /*
8476 * inc the count before we submit the bio so
8477 * we know the end IO handler won't happen before
8478 * we inc the count. Otherwise, the dip might get freed
8479 * before we're done setting it up
8480 */
8481 atomic_inc(&dip->pending_bios);
Mike Christie81a75f62016-06-05 14:31:54 -05008482 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008483 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008484 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008485 if (ret) {
8486 bio_put(bio);
8487 atomic_dec(&dip->pending_bios);
8488 goto out_err;
8489 }
8490
Miao Xiee65e1532010-11-22 03:04:43 +00008491 start_sector += submit_len >> 9;
8492 file_offset += submit_len;
8493
8494 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008495
8496 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8497 start_sector, GFP_NOFS);
8498 if (!bio)
8499 goto out_err;
Bart Van Assche4382e332016-09-14 10:45:36 +02008500 bio_set_op_attrs(bio, bio_op(orig_bio),
8501 bio_flags(orig_bio));
Miao Xiee65e1532010-11-22 03:04:43 +00008502 bio->bi_private = dip;
8503 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008504 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008505
Kent Overstreet4f024f32013-10-11 15:44:27 -07008506 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008507 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008508 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008509 &map_length, NULL, 0);
8510 if (ret) {
8511 bio_put(bio);
8512 goto out_err;
8513 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308514
8515 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008516 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308517 submit_len += blocksize;
8518 if (--nr_sectors) {
8519 i++;
8520 goto next_block;
8521 }
Miao Xiee65e1532010-11-22 03:04:43 +00008522 bvec++;
8523 }
8524 }
8525
Josef Bacik02f57c72011-04-06 14:25:44 -04008526submit:
Mike Christie81a75f62016-06-05 14:31:54 -05008527 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008528 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008529 if (!ret)
8530 return 0;
8531
8532 bio_put(bio);
8533out_err:
8534 dip->errors = 1;
8535 /*
8536 * before atomic variable goto zero, we must
8537 * make sure dip->errors is perceived to be set.
8538 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008539 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008540 if (atomic_dec_and_test(&dip->pending_bios))
8541 bio_io_error(dip->orig_bio);
8542
8543 /* bio_end_io() will handle error, so we needn't return it */
8544 return 0;
8545}
8546
Mike Christie8a4c1e42016-06-05 14:31:50 -05008547static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8548 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008549{
Filipe Manana61de7182015-07-01 12:13:10 +01008550 struct btrfs_dio_private *dip = NULL;
8551 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008552 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008553 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008554 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008555 int ret = 0;
8556
8557 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8558
Chris Mason9be33952013-05-17 18:30:14 -04008559 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008560 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008561 ret = -ENOMEM;
8562 goto free_ordered;
8563 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008564
Miao Xiec1dc0892014-09-12 18:43:56 +08008565 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008566 if (!dip) {
8567 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008568 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008569 }
8570
8571 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008572 dip->inode = inode;
8573 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008574 dip->bytes = dio_bio->bi_iter.bi_size;
8575 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008576 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008577 dip->orig_bio = io_bio;
8578 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008579 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008580 btrfs_bio = btrfs_io_bio(io_bio);
8581 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008582
Miao Xiec1dc0892014-09-12 18:43:56 +08008583 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008584 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008585 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008586 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008587 dip->subio_endio = btrfs_subio_endio_read;
8588 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008589
Filipe Mananaf28a4922015-12-08 19:23:20 +00008590 /*
8591 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8592 * even if we fail to submit a bio, because in such case we do the
8593 * corresponding error handling below and it must not be done a second
8594 * time by btrfs_direct_IO().
8595 */
8596 if (write) {
8597 struct btrfs_dio_data *dio_data = current->journal_info;
8598
8599 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8600 dip->bytes;
8601 dio_data->unsubmitted_oe_range_start =
8602 dio_data->unsubmitted_oe_range_end;
8603 }
8604
Mike Christie81a75f62016-06-05 14:31:54 -05008605 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008606 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008607 return;
Chris Mason9be33952013-05-17 18:30:14 -04008608
Miao Xie23ea8e52014-09-12 18:43:54 +08008609 if (btrfs_bio->end_io)
8610 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008611
Josef Bacik4b46fce2010-05-23 11:00:55 -04008612free_ordered:
8613 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008614 * If we arrived here it means either we failed to submit the dip
8615 * or we either failed to clone the dio_bio or failed to allocate the
8616 * dip. If we cloned the dio_bio and allocated the dip, we can just
8617 * call bio_endio against our io_bio so that we get proper resource
8618 * cleanup if we fail to submit the dip, otherwise, we must do the
8619 * same as btrfs_endio_direct_[write|read] because we can't call these
8620 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621 */
Filipe Manana61de7182015-07-01 12:13:10 +01008622 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008623 io_bio->bi_error = -EIO;
8624 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008625 /*
8626 * The end io callbacks free our dip, do the final put on io_bio
8627 * and all the cleanup and final put for dio_bio (through
8628 * dio_end_io()).
8629 */
8630 dip = NULL;
8631 io_bio = NULL;
8632 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008633 if (write)
8634 btrfs_endio_direct_write_update_ordered(inode,
8635 file_offset,
8636 dio_bio->bi_iter.bi_size,
8637 0);
8638 else
Filipe Manana61de7182015-07-01 12:13:10 +01008639 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8640 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008641
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008642 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008643 /*
8644 * Releases and cleans up our dio_bio, no need to bio_put()
8645 * nor bio_endio()/bio_io_error() against dio_bio.
8646 */
8647 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008648 }
Filipe Manana61de7182015-07-01 12:13:10 +01008649 if (io_bio)
8650 bio_put(io_bio);
8651 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008652}
8653
Omar Sandoval6f673762015-03-16 04:33:52 -07008654static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008655 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008656{
8657 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008658 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008659 unsigned blocksize_mask = root->sectorsize - 1;
8660 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008661
8662 if (offset & blocksize_mask)
8663 goto out;
8664
Al Viro28060d52014-03-22 05:15:17 -04008665 if (iov_iter_alignment(iter) & blocksize_mask)
8666 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008667
Al Viro28060d52014-03-22 05:15:17 -04008668 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008669 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008670 return 0;
8671 /*
8672 * Check to make sure we don't have duplicate iov_base's in this
8673 * iovec, if so return EINVAL, otherwise we'll get csum errors
8674 * when reading back.
8675 */
8676 for (seg = 0; seg < iter->nr_segs; seg++) {
8677 for (i = seg + 1; i < iter->nr_segs; i++) {
8678 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008679 goto out;
8680 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008681 }
8682 retval = 0;
8683out:
8684 return retval;
8685}
Josef Bacikeb838e72012-07-31 16:28:48 -04008686
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008687static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008688{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008689 struct file *file = iocb->ki_filp;
8690 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308691 struct btrfs_root *root = BTRFS_I(inode)->root;
8692 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008693 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008694 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008695 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008696 bool wakeup = true;
8697 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008698 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008699
Omar Sandoval6f673762015-03-16 04:33:52 -07008700 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008701 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008702
Jens Axboefe0f07d2015-04-15 17:05:48 -06008703 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008704 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008705
Josef Bacik0e267c42013-07-02 10:38:02 -04008706 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008707 * The generic stuff only does filemap_write_and_wait_range, which
8708 * isn't enough if we've written compressed pages to this area, so
8709 * we need to flush the dirty pages again to make absolutely sure
8710 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008711 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008712 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008713 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8714 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008715 filemap_fdatawrite_range(inode->i_mapping, offset,
8716 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008717
Omar Sandoval6f673762015-03-16 04:33:52 -07008718 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008719 /*
8720 * If the write DIO is beyond the EOF, we need update
8721 * the isize, but it is protected by i_mutex. So we can
8722 * not unlock the i_mutex at this case.
8723 */
8724 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008725 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008726 relock = true;
8727 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008728 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008729 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008730 goto out;
chandan50745b02015-08-28 21:10:13 +05308731 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008732 BTRFS_MAX_EXTENT_SIZE - 1,
8733 BTRFS_MAX_EXTENT_SIZE);
8734
8735 /*
8736 * We need to know how many extents we reserved so that we can
8737 * do the accounting properly if we go over the number we
8738 * originally calculated. Abuse current->journal_info for this.
8739 */
chandan50745b02015-08-28 21:10:13 +05308740 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008741 dio_data.unsubmitted_oe_range_start = (u64)offset;
8742 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308743 current->journal_info = &dio_data;
Chandan Rajendrae8b50682016-12-23 15:00:18 +05308744 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008745 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8746 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008747 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008748 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8749 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008750 }
8751
Omar Sandoval17f8c842015-03-16 04:33:50 -07008752 ret = __blockdev_direct_IO(iocb, inode,
8753 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008754 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008755 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008756 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendrae8b50682016-12-23 15:00:18 +05308757 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008758 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008759 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308760 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008761 btrfs_delalloc_release_space(inode, offset,
8762 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008763 /*
8764 * On error we might have left some ordered extents
8765 * without submitting corresponding bios for them, so
8766 * cleanup them up to avoid other tasks getting them
8767 * and waiting for them to complete forever.
8768 */
8769 if (dio_data.unsubmitted_oe_range_start <
8770 dio_data.unsubmitted_oe_range_end)
8771 btrfs_endio_direct_write_update_ordered(inode,
8772 dio_data.unsubmitted_oe_range_start,
8773 dio_data.unsubmitted_oe_range_end -
8774 dio_data.unsubmitted_oe_range_start,
8775 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008776 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008777 btrfs_delalloc_release_space(inode, offset,
8778 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008779 }
Miao Xie38851cc2013-02-08 07:04:11 +00008780out:
Miao Xie2e60a512013-02-08 07:01:08 +00008781 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008782 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008783 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008784 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008785
8786 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008787}
8788
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008789#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8790
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008791static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8792 __u64 start, __u64 len)
8793{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008794 int ret;
8795
8796 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8797 if (ret)
8798 return ret;
8799
Chris Masonec29ed52011-02-23 16:23:20 -05008800 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008801}
8802
Chris Mason9ebefb182007-06-15 13:50:00 -04008803int btrfs_readpage(struct file *file, struct page *page)
8804{
Chris Masond1310b22008-01-24 16:13:08 -05008805 struct extent_io_tree *tree;
8806 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008807 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008808}
Chris Mason1832a6d2007-12-21 16:27:21 -05008809
Chris Mason39279cc2007-06-12 06:35:45 -04008810static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8811{
Chris Masond1310b22008-01-24 16:13:08 -05008812 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008813 struct inode *inode = page->mapping->host;
8814 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008815
8816 if (current->flags & PF_MEMALLOC) {
8817 redirty_page_for_writepage(wbc, page);
8818 unlock_page(page);
8819 return 0;
8820 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008821
8822 /*
8823 * If we are under memory pressure we will call this directly from the
8824 * VM, we need to make sure we have the inode referenced for the ordered
8825 * extent. If not just return like we didn't do anything.
8826 */
8827 if (!igrab(inode)) {
8828 redirty_page_for_writepage(wbc, page);
8829 return AOP_WRITEPAGE_ACTIVATE;
8830 }
Chris Masond1310b22008-01-24 16:13:08 -05008831 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008832 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8833 btrfs_add_delayed_iput(inode);
8834 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008835}
Chris Mason39279cc2007-06-12 06:35:45 -04008836
Eric Sandeen48a3b632013-04-25 20:41:01 +00008837static int btrfs_writepages(struct address_space *mapping,
8838 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008839{
Chris Masond1310b22008-01-24 16:13:08 -05008840 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008841
Chris Masond1310b22008-01-24 16:13:08 -05008842 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008843 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8844}
8845
Chris Mason3ab2fb52007-11-08 10:59:22 -05008846static int
8847btrfs_readpages(struct file *file, struct address_space *mapping,
8848 struct list_head *pages, unsigned nr_pages)
8849{
Chris Masond1310b22008-01-24 16:13:08 -05008850 struct extent_io_tree *tree;
8851 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008852 return extent_readpages(tree, mapping, pages, nr_pages,
8853 btrfs_get_extent);
8854}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008855static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008856{
Chris Masond1310b22008-01-24 16:13:08 -05008857 struct extent_io_tree *tree;
8858 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008859 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008860
Chris Masond1310b22008-01-24 16:13:08 -05008861 tree = &BTRFS_I(page->mapping->host)->io_tree;
8862 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008863 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008864 if (ret == 1) {
8865 ClearPagePrivate(page);
8866 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008867 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008868 }
8869 return ret;
8870}
Chris Mason39279cc2007-06-12 06:35:45 -04008871
Chris Masone6dcd2d2008-07-17 12:53:50 -04008872static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8873{
Chris Mason98509cf2008-09-11 15:51:43 -04008874 if (PageWriteback(page) || PageDirty(page))
8875 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008876 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008877}
8878
Lukas Czernerd47992f2013-05-21 23:17:23 -04008879static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8880 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008881{
Josef Bacik5fd02042012-05-02 14:00:54 -04008882 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008883 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008884 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008885 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008886 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008887 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308888 u64 start;
8889 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008890 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008891
Chris Mason8b62b722009-09-02 16:53:46 -04008892 /*
8893 * we have the page locked, so new writeback can't start,
8894 * and the dirty bit won't be cleared while we are here.
8895 *
8896 * Wait for IO on this page so that we can safely clear
8897 * the PagePrivate2 bit and do ordered accounting
8898 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008899 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008900
Josef Bacik5fd02042012-05-02 14:00:54 -04008901 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008902 if (offset) {
8903 btrfs_releasepage(page, GFP_NOFS);
8904 return;
8905 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008906
8907 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008908 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308909again:
8910 start = page_start;
8911 ordered = btrfs_lookup_ordered_range(inode, start,
8912 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308914 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008915 /*
8916 * IO on this page will never be started, so we need
8917 * to account for any ordered extents now
8918 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008919 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308920 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008921 EXTENT_DIRTY | EXTENT_DELALLOC |
8922 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8923 EXTENT_DEFRAG, 1, 0, &cached_state,
8924 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008925 /*
8926 * whoever cleared the private bit is responsible
8927 * for the finish_ordered_io
8928 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008929 if (TestClearPagePrivate2(page)) {
8930 struct btrfs_ordered_inode_tree *tree;
8931 u64 new_len;
8932
8933 tree = &BTRFS_I(inode)->ordered_tree;
8934
8935 spin_lock_irq(&tree->lock);
8936 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308937 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008938 if (new_len < ordered->truncated_len)
8939 ordered->truncated_len = new_len;
8940 spin_unlock_irq(&tree->lock);
8941
8942 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308943 start,
8944 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008945 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008946 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008948 if (!inode_evicting) {
8949 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308950 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008951 &cached_state);
8952 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308953
8954 start = end + 1;
8955 if (start < page_end)
8956 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008957 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008958
Qu Wenruob9d0b382015-09-29 10:35:16 +08008959 /*
8960 * Qgroup reserved space handler
8961 * Page here will be either
8962 * 1) Already written to disk
8963 * In this case, its reserved space is released from data rsv map
8964 * and will be freed by delayed_ref handler finally.
8965 * So even we call qgroup_free_data(), it won't decrease reserved
8966 * space.
8967 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008968 * This means the reserved space should be freed here. However,
8969 * if a truncate invalidates the page (by clearing PageDirty)
8970 * and the page is accounted for while allocating extent
8971 * in btrfs_check_data_free_space() we let delayed_ref to
8972 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008973 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008974 if (PageDirty(page))
8975 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008976 if (!inode_evicting) {
8977 clear_extent_bit(tree, page_start, page_end,
8978 EXTENT_LOCKED | EXTENT_DIRTY |
8979 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8980 EXTENT_DEFRAG, 1, 1,
8981 &cached_state, GFP_NOFS);
8982
8983 __btrfs_releasepage(page, GFP_NOFS);
8984 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008985
Chris Mason4a096752008-07-21 10:29:44 -04008986 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008987 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008988 ClearPagePrivate(page);
8989 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008990 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008991 }
Chris Mason39279cc2007-06-12 06:35:45 -04008992}
8993
Chris Mason9ebefb182007-06-15 13:50:00 -04008994/*
8995 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8996 * called from a page fault handler when a page is first dirtied. Hence we must
8997 * be careful to check for EOF conditions here. We set the page up correctly
8998 * for a written page which means we get ENOSPC checking when writing into
8999 * holes and correct delalloc and unwritten extent mapping on filesystems that
9000 * support these features.
9001 *
9002 * We are not allowed to take the i_mutex here so we have to play games to
9003 * protect against truncate races as the page could now be beyond EOF. Because
9004 * vmtruncate() writes the inode size before removing pages, once we have the
9005 * page lock we can determine safely if the page is beyond EOF. If it is not
9006 * beyond EOF, then the page is guaranteed safe against truncation until we
9007 * unlock the page.
9008 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07009009int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009010{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009011 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05009012 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05009013 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9015 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009016 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017 char *kaddr;
9018 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009019 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009020 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009021 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309022 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009023 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309025 u64 end;
9026
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009027 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009028
Jan Karab2b5ef52012-06-12 16:20:45 +02009029 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009030 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009031 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309032 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009033
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309034 /*
9035 * Reserving delalloc space after obtaining the page lock can lead to
9036 * deadlock. For example, if a dirty page is locked by this function
9037 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9038 * dirty page write out, then the btrfs_writepage() function could
9039 * end up waiting indefinitely to get a lock on the page currently
9040 * being processed by btrfs_page_mkwrite() function.
9041 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009042 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309043 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009044 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009045 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009046 reserved = 1;
9047 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009048 if (ret) {
9049 if (ret == -ENOMEM)
9050 ret = VM_FAULT_OOM;
9051 else /* -ENOSPC, -EIO, etc */
9052 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009053 if (reserved)
9054 goto out;
9055 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009056 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009057
Nick Piggin56a76f82009-03-31 15:23:23 -07009058 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009059again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009060 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009061 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009062
Chris Mason9ebefb182007-06-15 13:50:00 -04009063 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009064 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009065 /* page got truncated out from underneath us */
9066 goto out_unlock;
9067 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068 wait_on_page_writeback(page);
9069
David Sterbaff13db42015-12-03 14:30:40 +01009070 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009071 set_page_extent_mapped(page);
9072
Chris Masoneb84ae02008-07-17 13:53:27 -04009073 /*
9074 * we can't set the delalloc bits if there are pending ordered
9075 * extents. Drop our locks and wait for them to finish
9076 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309077 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009078 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009079 unlock_extent_cached(io_tree, page_start, page_end,
9080 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009081 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009082 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083 btrfs_put_ordered_extent(ordered);
9084 goto again;
9085 }
9086
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009087 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309088 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009089 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309090 end = page_start + reserved_space - 1;
9091 spin_lock(&BTRFS_I(inode)->lock);
9092 BTRFS_I(inode)->outstanding_extents++;
9093 spin_unlock(&BTRFS_I(inode)->lock);
9094 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009095 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309096 }
9097 }
9098
Josef Bacikfbf19082009-10-01 17:10:23 -04009099 /*
9100 * XXX - page_mkwrite gets called every time the page is dirtied, even
9101 * if it was already dirty, so for space accounting reasons we need to
9102 * clear any delalloc bits for the range we are fixing to save. There
9103 * is probably a better way to do this, but for now keep consistent with
9104 * prepare_pages in the normal write path.
9105 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309106 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009107 EXTENT_DIRTY | EXTENT_DELALLOC |
9108 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009109 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009110
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309111 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009112 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009113 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009114 unlock_extent_cached(io_tree, page_start, page_end,
9115 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009116 ret = VM_FAULT_SIGBUS;
9117 goto out_unlock;
9118 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009119 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009120
9121 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009122 if (page_start + PAGE_SIZE > size)
9123 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009124 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009125 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009126
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009127 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009128 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009129 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009130 flush_dcache_page(page);
9131 kunmap(page);
9132 }
Chris Mason247e7432008-07-17 12:53:51 -04009133 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009134 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009135 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009136
Chris Mason257c62e2009-10-13 13:21:08 -04009137 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9138 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009139 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009140
Josef Bacik2ac55d42010-02-03 19:33:23 +00009141 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009142
9143out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009144 if (!ret) {
9145 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009146 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009147 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009148 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009149out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309150 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009151out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009152 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009153 return ret;
9154}
9155
Josef Bacika41ad392011-01-31 15:30:16 -05009156static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009157{
9158 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009159 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009160 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009161 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009162 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009163 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009164 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009165
Josef Bacik0ef8b722013-10-25 16:13:35 -04009166 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9167 (u64)-1);
9168 if (ret)
9169 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009170
Josef Bacikfcb80c22011-05-03 10:40:22 -04009171 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009172 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009173 * 3 things going on here
9174 *
9175 * 1) We need to reserve space for our orphan item and the space to
9176 * delete our orphan item. Lord knows we don't want to have a dangling
9177 * orphan item because we didn't reserve space to remove it.
9178 *
9179 * 2) We need to reserve space to update our inode.
9180 *
9181 * 3) We need to have something to cache all the space that is going to
9182 * be free'd up by the truncate operation, but also have some slack
9183 * space reserved in case it uses space during the truncate (thank you
9184 * very much snapshotting).
9185 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009186 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009187 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009188 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009189 * doesn't end up using space reserved for updating the inode or
9190 * removing the orphan item. We also need to be able to stop the
9191 * transaction and start a new one, which means we need to be able to
9192 * update the inode several times, and we have no idea of knowing how
9193 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009194 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009195 * Then there is the orphan item, which does indeed need to be held on
9196 * to for the whole operation, and we need nobody to touch this reserved
9197 * space except the orphan code.
9198 *
9199 * So that leaves us with
9200 *
9201 * 1) root->orphan_block_rsv - for the orphan deletion.
9202 * 2) rsv - for the truncate reservation, which we will steal from the
9203 * transaction reservation.
9204 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9205 * updating the inode.
9206 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009207 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009208 if (!rsv)
9209 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009210 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009211 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009212
Josef Bacik907cbce2011-08-08 13:46:15 -04009213 /*
Josef Bacik07127182011-08-19 10:29:59 -04009214 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009215 * 1 for updating the inode.
9216 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009217 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009218 if (IS_ERR(trans)) {
9219 err = PTR_ERR(trans);
9220 goto out;
9221 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009222
Josef Bacik907cbce2011-08-08 13:46:15 -04009223 /* Migrate the slack space for the truncate to our reserve */
9224 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009225 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009226 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009227
Chris Mason5a3f23d2009-03-31 13:27:11 -04009228 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009229 * So if we truncate and then write and fsync we normally would just
9230 * write the extents that changed, which is a problem if we need to
9231 * first truncate that entire inode. So set this flag so we write out
9232 * all of the extents in the inode to the sync log so we're completely
9233 * safe.
9234 */
9235 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009236 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009237
Yan, Zheng80825102009-11-12 09:35:36 +00009238 while (1) {
9239 ret = btrfs_truncate_inode_items(trans, root, inode,
9240 inode->i_size,
9241 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009242 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009243 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009244 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009245 }
Chris Mason39279cc2007-06-12 06:35:45 -04009246
Josef Bacikfcb80c22011-05-03 10:40:22 -04009247 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009248 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009249 if (ret) {
9250 err = ret;
9251 break;
9252 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009253
Yan, Zheng80825102009-11-12 09:35:36 +00009254 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009255 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009256
9257 trans = btrfs_start_transaction(root, 2);
9258 if (IS_ERR(trans)) {
9259 ret = err = PTR_ERR(trans);
9260 trans = NULL;
9261 break;
9262 }
9263
9264 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009265 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009266 BUG_ON(ret); /* shouldn't happen */
9267 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009268 }
9269
9270 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009271 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009272 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009273 if (ret)
9274 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009275 }
9276
Chris Mason917c16b2011-11-08 14:49:59 -05009277 if (trans) {
9278 trans->block_rsv = &root->fs_info->trans_block_rsv;
9279 ret = btrfs_update_inode(trans, root, inode);
9280 if (ret && !err)
9281 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009282
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009283 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009284 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009285 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009286out:
9287 btrfs_free_block_rsv(root, rsv);
9288
Josef Bacik3893e332011-01-31 16:03:11 -05009289 if (ret && !err)
9290 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009291
Josef Bacik3893e332011-01-31 16:03:11 -05009292 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009293}
9294
Sven Wegener3b963622008-06-09 21:57:42 -04009295/*
Chris Masond352ac62008-09-29 15:18:18 -04009296 * create a new subvolume directory/inode (helper for the ioctl).
9297 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009298int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009299 struct btrfs_root *new_root,
9300 struct btrfs_root *parent_root,
9301 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009302{
Chris Mason39279cc2007-06-12 06:35:45 -04009303 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009304 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009305 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009306
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009307 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9308 new_dirid, new_dirid,
9309 S_IFDIR | (~current_umask() & S_IRWXUGO),
9310 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009311 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009312 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009313 inode->i_op = &btrfs_dir_inode_operations;
9314 inode->i_fop = &btrfs_dir_file_operations;
9315
Miklos Szeredibfe86842011-10-28 14:13:29 +02009316 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009317 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009318 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009319
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009320 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9321 if (err)
9322 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009323 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009324 new_root->root_key.objectid, err);
9325
Yan, Zheng76dda932009-09-21 16:00:26 -04009326 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009327
Yan, Zheng76dda932009-09-21 16:00:26 -04009328 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009329 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009330}
9331
Chris Mason39279cc2007-06-12 06:35:45 -04009332struct inode *btrfs_alloc_inode(struct super_block *sb)
9333{
9334 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009335 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009336
9337 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9338 if (!ei)
9339 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009340
9341 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009342 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009343 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009344 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009345 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009346 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009347 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009348 ei->disk_i_size = 0;
9349 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009350 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009351 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009352 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009353 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009354 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009355 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009356
Josef Bacik9e0baf62011-07-15 15:16:44 +00009357 spin_lock_init(&ei->lock);
9358 ei->outstanding_extents = 0;
9359 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009360
Josef Bacik72ac3c02012-05-23 14:13:11 -04009361 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009362 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009363
Miao Xie16cdcec2011-04-22 18:12:22 +08009364 ei->delayed_node = NULL;
9365
chandan r9cc97d62012-07-04 12:48:07 +05309366 ei->i_otime.tv_sec = 0;
9367 ei->i_otime.tv_nsec = 0;
9368
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009369 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009370 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009371 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9372 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009373 ei->io_tree.track_uptodate = 1;
9374 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009375 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009376 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009377 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009378 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009379 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009380 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009381 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009382 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009383
9384 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009385}
9386
Josef Bacikaaedb552013-10-11 14:44:09 -04009387#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9388void btrfs_test_destroy_inode(struct inode *inode)
9389{
9390 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9391 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9392}
9393#endif
9394
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009395static void btrfs_i_callback(struct rcu_head *head)
9396{
9397 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009398 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9399}
9400
Chris Mason39279cc2007-06-12 06:35:45 -04009401void btrfs_destroy_inode(struct inode *inode)
9402{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009403 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009404 struct btrfs_root *root = BTRFS_I(inode)->root;
9405
Al Virob3d9b7a2012-06-09 13:51:19 -04009406 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009407 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009408 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9409 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009410 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9411 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009412 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009413
Chris Mason5a3f23d2009-03-31 13:27:11 -04009414 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009415 * This can happen where we create an inode, but somebody else also
9416 * created the same inode and we need to destroy the one we already
9417 * created.
9418 */
9419 if (!root)
9420 goto free;
9421
Josef Bacik8a35d952012-05-23 14:26:42 -04009422 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9423 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009424 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009425 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009426 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009427 }
Josef Bacik7b128762008-07-24 12:17:14 -04009428
Chris Masond3977122009-01-05 21:25:51 -05009429 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009430 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9431 if (!ordered)
9432 break;
9433 else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009434 btrfs_err(root->fs_info,
9435 "found ordered extent %llu %llu on inode cleanup",
9436 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009437 btrfs_remove_ordered_extent(inode, ordered);
9438 btrfs_put_ordered_extent(ordered);
9439 btrfs_put_ordered_extent(ordered);
9440 }
9441 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009442 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009443 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009444 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009445free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009446 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009447}
9448
Al Viro45321ac2010-06-07 13:43:19 -04009449int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009450{
9451 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009452
Naohiro Aota6379ef92013-06-06 09:56:34 +00009453 if (root == NULL)
9454 return 1;
9455
Liu Bofa6ac872013-02-20 14:10:23 +00009456 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009457 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009458 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009459 else
Al Viro45321ac2010-06-07 13:43:19 -04009460 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009461}
9462
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009463static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009464{
9465 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9466
9467 inode_init_once(&ei->vfs_inode);
9468}
9469
9470void btrfs_destroy_cachep(void)
9471{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009472 /*
9473 * Make sure all delayed rcu free inodes are flushed before we
9474 * destroy cache.
9475 */
9476 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009477 kmem_cache_destroy(btrfs_inode_cachep);
9478 kmem_cache_destroy(btrfs_trans_handle_cachep);
9479 kmem_cache_destroy(btrfs_transaction_cachep);
9480 kmem_cache_destroy(btrfs_path_cachep);
9481 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009482}
9483
9484int btrfs_init_cachep(void)
9485{
David Sterba837e1972012-09-07 03:00:48 -06009486 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009487 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009488 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9489 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009490 if (!btrfs_inode_cachep)
9491 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009492
David Sterba837e1972012-09-07 03:00:48 -06009493 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009494 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009495 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009496 if (!btrfs_trans_handle_cachep)
9497 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009498
David Sterba837e1972012-09-07 03:00:48 -06009499 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009500 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009501 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009502 if (!btrfs_transaction_cachep)
9503 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009504
David Sterba837e1972012-09-07 03:00:48 -06009505 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009506 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009507 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009508 if (!btrfs_path_cachep)
9509 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009510
David Sterba837e1972012-09-07 03:00:48 -06009511 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009512 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009513 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009514 if (!btrfs_free_space_cachep)
9515 goto fail;
9516
Chris Mason39279cc2007-06-12 06:35:45 -04009517 return 0;
9518fail:
9519 btrfs_destroy_cachep();
9520 return -ENOMEM;
9521}
9522
9523static int btrfs_getattr(struct vfsmount *mnt,
9524 struct dentry *dentry, struct kstat *stat)
9525{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009526 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009527 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009528 u32 blocksize = inode->i_sb->s_blocksize;
9529
Chris Mason39279cc2007-06-12 06:35:45 -04009530 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009531 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009532
9533 spin_lock(&BTRFS_I(inode)->lock);
9534 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9535 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009536 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009537 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009538 return 0;
9539}
9540
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009541static int btrfs_rename_exchange(struct inode *old_dir,
9542 struct dentry *old_dentry,
9543 struct inode *new_dir,
9544 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009545{
9546 struct btrfs_trans_handle *trans;
9547 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009548 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 struct inode *new_inode = new_dentry->d_inode;
9550 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009551 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009552 struct dentry *parent;
9553 u64 old_ino = btrfs_ino(old_inode);
9554 u64 new_ino = btrfs_ino(new_inode);
9555 u64 old_idx = 0;
9556 u64 new_idx = 0;
9557 u64 root_objectid;
9558 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009559 bool root_log_pinned = false;
9560 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561
9562 /* we only allow rename subvolume link between subvolumes */
9563 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9564 return -EXDEV;
9565
9566 /* close the race window with snapshot create/destroy ioctl */
9567 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9568 down_read(&root->fs_info->subvol_sem);
9569 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9570 down_read(&dest->fs_info->subvol_sem);
9571
9572 /*
9573 * We want to reserve the absolute worst case amount of items. So if
9574 * both inodes are subvols and we need to unlink them then that would
9575 * require 4 item modifications, but if they are both normal inodes it
9576 * would require 5 item modifications, so we'll assume their normal
9577 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9578 * should cover the worst case number of items we'll modify.
9579 */
9580 trans = btrfs_start_transaction(root, 12);
9581 if (IS_ERR(trans)) {
9582 ret = PTR_ERR(trans);
9583 goto out_notrans;
9584 }
9585
9586 /*
9587 * We need to find a free sequence number both in the source and
9588 * in the destination directory for the exchange.
9589 */
9590 ret = btrfs_set_inode_index(new_dir, &old_idx);
9591 if (ret)
9592 goto out_fail;
9593 ret = btrfs_set_inode_index(old_dir, &new_idx);
9594 if (ret)
9595 goto out_fail;
9596
9597 BTRFS_I(old_inode)->dir_index = 0ULL;
9598 BTRFS_I(new_inode)->dir_index = 0ULL;
9599
9600 /* Reference for the source. */
9601 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9602 /* force full log commit if subvolume involved. */
9603 btrfs_set_log_full_commit(root->fs_info, trans);
9604 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009605 btrfs_pin_log_trans(root);
9606 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 ret = btrfs_insert_inode_ref(trans, dest,
9608 new_dentry->d_name.name,
9609 new_dentry->d_name.len,
9610 old_ino,
9611 btrfs_ino(new_dir), old_idx);
9612 if (ret)
9613 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 }
9615
9616 /* And now for the dest. */
9617 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9618 /* force full log commit if subvolume involved. */
9619 btrfs_set_log_full_commit(dest->fs_info, trans);
9620 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009621 btrfs_pin_log_trans(dest);
9622 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 ret = btrfs_insert_inode_ref(trans, root,
9624 old_dentry->d_name.name,
9625 old_dentry->d_name.len,
9626 new_ino,
9627 btrfs_ino(old_dir), new_idx);
9628 if (ret)
9629 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 }
9631
9632 /* Update inode version and ctime/mtime. */
9633 inode_inc_iversion(old_dir);
9634 inode_inc_iversion(new_dir);
9635 inode_inc_iversion(old_inode);
9636 inode_inc_iversion(new_inode);
9637 old_dir->i_ctime = old_dir->i_mtime = ctime;
9638 new_dir->i_ctime = new_dir->i_mtime = ctime;
9639 old_inode->i_ctime = ctime;
9640 new_inode->i_ctime = ctime;
9641
9642 if (old_dentry->d_parent != new_dentry->d_parent) {
9643 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9644 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9645 }
9646
9647 /* src is a subvolume */
9648 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9649 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9650 ret = btrfs_unlink_subvol(trans, root, old_dir,
9651 root_objectid,
9652 old_dentry->d_name.name,
9653 old_dentry->d_name.len);
9654 } else { /* src is an inode */
9655 ret = __btrfs_unlink_inode(trans, root, old_dir,
9656 old_dentry->d_inode,
9657 old_dentry->d_name.name,
9658 old_dentry->d_name.len);
9659 if (!ret)
9660 ret = btrfs_update_inode(trans, root, old_inode);
9661 }
9662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009663 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 goto out_fail;
9665 }
9666
9667 /* dest is a subvolume */
9668 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9669 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9670 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9671 root_objectid,
9672 new_dentry->d_name.name,
9673 new_dentry->d_name.len);
9674 } else { /* dest is an inode */
9675 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9676 new_dentry->d_inode,
9677 new_dentry->d_name.name,
9678 new_dentry->d_name.len);
9679 if (!ret)
9680 ret = btrfs_update_inode(trans, dest, new_inode);
9681 }
9682 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009683 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 goto out_fail;
9685 }
9686
9687 ret = btrfs_add_link(trans, new_dir, old_inode,
9688 new_dentry->d_name.name,
9689 new_dentry->d_name.len, 0, old_idx);
9690 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009691 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 goto out_fail;
9693 }
9694
9695 ret = btrfs_add_link(trans, old_dir, new_inode,
9696 old_dentry->d_name.name,
9697 old_dentry->d_name.len, 0, new_idx);
9698 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009699 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 goto out_fail;
9701 }
9702
9703 if (old_inode->i_nlink == 1)
9704 BTRFS_I(old_inode)->dir_index = old_idx;
9705 if (new_inode->i_nlink == 1)
9706 BTRFS_I(new_inode)->dir_index = new_idx;
9707
Filipe Manana86e8aa02016-05-05 02:02:27 +01009708 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 parent = new_dentry->d_parent;
9710 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9711 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009712 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009714 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 parent = old_dentry->d_parent;
9716 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9717 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009718 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 }
9720out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009721 /*
9722 * If we have pinned a log and an error happened, we unpin tasks
9723 * trying to sync the log and force them to fallback to a transaction
9724 * commit if the log currently contains any of the inodes involved in
9725 * this rename operation (to ensure we do not persist a log with an
9726 * inconsistent state for any of these inodes or leading to any
9727 * inconsistencies when replayed). If the transaction was aborted, the
9728 * abortion reason is propagated to userspace when attempting to commit
9729 * the transaction. If the log does not contain any of these inodes, we
9730 * allow the tasks to sync it.
9731 */
9732 if (ret && (root_log_pinned || dest_log_pinned)) {
9733 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9734 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9735 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9736 (new_inode &&
9737 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9738 btrfs_set_log_full_commit(root->fs_info, trans);
9739
9740 if (root_log_pinned) {
9741 btrfs_end_log_trans(root);
9742 root_log_pinned = false;
9743 }
9744 if (dest_log_pinned) {
9745 btrfs_end_log_trans(dest);
9746 dest_log_pinned = false;
9747 }
9748 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009749 ret = btrfs_end_transaction(trans, root);
9750out_notrans:
9751 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9752 up_read(&dest->fs_info->subvol_sem);
9753 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9754 up_read(&root->fs_info->subvol_sem);
9755
9756 return ret;
9757}
9758
9759static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9760 struct btrfs_root *root,
9761 struct inode *dir,
9762 struct dentry *dentry)
9763{
9764 int ret;
9765 struct inode *inode;
9766 u64 objectid;
9767 u64 index;
9768
9769 ret = btrfs_find_free_ino(root, &objectid);
9770 if (ret)
9771 return ret;
9772
9773 inode = btrfs_new_inode(trans, root, dir,
9774 dentry->d_name.name,
9775 dentry->d_name.len,
9776 btrfs_ino(dir),
9777 objectid,
9778 S_IFCHR | WHITEOUT_MODE,
9779 &index);
9780
9781 if (IS_ERR(inode)) {
9782 ret = PTR_ERR(inode);
9783 return ret;
9784 }
9785
9786 inode->i_op = &btrfs_special_inode_operations;
9787 init_special_inode(inode, inode->i_mode,
9788 WHITEOUT_DEV);
9789
9790 ret = btrfs_init_inode_security(trans, inode, dir,
9791 &dentry->d_name);
9792 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009793 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794
9795 ret = btrfs_add_nondir(trans, dir, dentry,
9796 inode, 0, index);
9797 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009798 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799
9800 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009801out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009803 if (ret)
9804 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 iput(inode);
9806
Filipe Mananac9901612016-05-05 01:41:57 +01009807 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808}
9809
Chris Mason39279cc2007-06-12 06:35:45 -04009810static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 struct inode *new_dir, struct dentry *new_dentry,
9812 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009813{
9814 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009815 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009816 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9817 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009818 struct inode *new_inode = d_inode(new_dentry);
9819 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009820 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009821 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009822 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009823 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009824 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009825
Li Zefan33345d012011-04-20 10:31:50 +08009826 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009827 return -EPERM;
9828
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009829 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009830 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009831 return -EXDEV;
9832
Li Zefan33345d012011-04-20 10:31:50 +08009833 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9834 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009835 return -ENOTEMPTY;
9836
Chris Mason39279cc2007-06-12 06:35:45 -04009837 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009839 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009840
9841
9842 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009843 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009844 new_dentry->d_name.name,
9845 new_dentry->d_name.len);
9846
9847 if (ret) {
9848 if (ret == -EEXIST) {
9849 /* we shouldn't get
9850 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309851 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009852 return ret;
9853 }
9854 } else {
9855 /* maybe -EOVERFLOW */
9856 return ret;
9857 }
9858 }
9859 ret = 0;
9860
Chris Mason5a3f23d2009-03-31 13:27:11 -04009861 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009862 * we're using rename to replace one file with another. Start IO on it
9863 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009864 */
Chris Mason8d875f92014-08-12 10:47:42 -07009865 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009866 filemap_flush(old_inode->i_mapping);
9867
Yan, Zheng76dda932009-09-21 16:00:26 -04009868 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009869 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009870 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009871 /*
9872 * We want to reserve the absolute worst case amount of items. So if
9873 * both inodes are subvols and we need to unlink them then that would
9874 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009875 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009876 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9877 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009878 * If our rename has the whiteout flag, we need more 5 units for the
9879 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9880 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009881 */
Filipe Manana5062af32016-05-05 10:26:26 +01009882 trans_num_items = 11;
9883 if (flags & RENAME_WHITEOUT)
9884 trans_num_items += 5;
9885 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009886 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009887 ret = PTR_ERR(trans);
9888 goto out_notrans;
9889 }
Chris Mason5f39d392007-10-15 16:14:19 -04009890
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009891 if (dest != root)
9892 btrfs_record_root_in_trans(trans, dest);
9893
Yan, Zhenga5719522009-09-24 09:17:31 -04009894 ret = btrfs_set_inode_index(new_dir, &index);
9895 if (ret)
9896 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009897
Miao Xie67de1172013-12-26 13:07:06 +08009898 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009899 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009901 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009902 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009903 btrfs_pin_log_trans(root);
9904 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009905 ret = btrfs_insert_inode_ref(trans, dest,
9906 new_dentry->d_name.name,
9907 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009908 old_ino,
9909 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009910 if (ret)
9911 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009912 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009913
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009914 inode_inc_iversion(old_dir);
9915 inode_inc_iversion(new_dir);
9916 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009917 old_dir->i_ctime = old_dir->i_mtime =
9918 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009919 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009920
Chris Mason12fcfd22009-03-24 10:24:20 -04009921 if (old_dentry->d_parent != new_dentry->d_parent)
9922 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9923
Li Zefan33345d012011-04-20 10:31:50 +08009924 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009925 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9926 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9927 old_dentry->d_name.name,
9928 old_dentry->d_name.len);
9929 } else {
Al Viro92986792011-03-04 17:14:37 +00009930 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009931 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009932 old_dentry->d_name.name,
9933 old_dentry->d_name.len);
9934 if (!ret)
9935 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009937 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009938 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009939 goto out_fail;
9940 }
Chris Mason39279cc2007-06-12 06:35:45 -04009941
9942 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009943 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009944 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009945 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9947 root_objectid = BTRFS_I(new_inode)->location.objectid;
9948 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9949 root_objectid,
9950 new_dentry->d_name.name,
9951 new_dentry->d_name.len);
9952 BUG_ON(new_inode->i_nlink == 0);
9953 } else {
9954 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009955 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009956 new_dentry->d_name.name,
9957 new_dentry->d_name.len);
9958 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009959 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009960 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009961 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009962 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009963 goto out_fail;
9964 }
Chris Mason39279cc2007-06-12 06:35:45 -04009965 }
Josef Bacikaec74772008-07-24 12:12:38 -04009966
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009967 ret = btrfs_add_link(trans, new_dir, old_inode,
9968 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009969 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009970 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009971 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009972 goto out_fail;
9973 }
Chris Mason39279cc2007-06-12 06:35:45 -04009974
Miao Xie67de1172013-12-26 13:07:06 +08009975 if (old_inode->i_nlink == 1)
9976 BTRFS_I(old_inode)->dir_index = index;
9977
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009978 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009979 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009980
Josef Bacik6a912212010-11-20 09:48:00 +00009981 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009982 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009983 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009984 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009985
9986 if (flags & RENAME_WHITEOUT) {
9987 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9988 old_dentry);
9989
9990 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009991 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009992 goto out_fail;
9993 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009994 }
Chris Mason39279cc2007-06-12 06:35:45 -04009995out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009996 /*
9997 * If we have pinned the log and an error happened, we unpin tasks
9998 * trying to sync the log and force them to fallback to a transaction
9999 * commit if the log currently contains any of the inodes involved in
10000 * this rename operation (to ensure we do not persist a log with an
10001 * inconsistent state for any of these inodes or leading to any
10002 * inconsistencies when replayed). If the transaction was aborted, the
10003 * abortion reason is propagated to userspace when attempting to commit
10004 * the transaction. If the log does not contain any of these inodes, we
10005 * allow the tasks to sync it.
10006 */
10007 if (ret && log_pinned) {
10008 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
10009 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
10010 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
10011 (new_inode &&
10012 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
10013 btrfs_set_log_full_commit(root->fs_info, trans);
10014
10015 btrfs_end_log_trans(root);
10016 log_pinned = false;
10017 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010018 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010019out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010020 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -040010021 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010022
Chris Mason39279cc2007-06-12 06:35:45 -040010023 return ret;
10024}
10025
Miklos Szeredi80ace852014-07-23 15:15:32 +020010026static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10027 struct inode *new_dir, struct dentry *new_dentry,
10028 unsigned int flags)
10029{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010030 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010031 return -EINVAL;
10032
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010033 if (flags & RENAME_EXCHANGE)
10034 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10035 new_dentry);
10036
10037 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010038}
10039
Miao Xie8ccf6f192012-10-25 09:28:04 +000010040static void btrfs_run_delalloc_work(struct btrfs_work *work)
10041{
10042 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010043 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010044
10045 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10046 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010047 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010048 filemap_flush(inode->i_mapping);
10049 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10050 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010051 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010052
10053 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010054 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010055 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010056 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010057 complete(&delalloc_work->completion);
10058}
10059
10060struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010061 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062{
10063 struct btrfs_delalloc_work *work;
10064
David Sterba100d5702015-12-08 14:39:32 +010010065 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010066 if (!work)
10067 return NULL;
10068
10069 init_completion(&work->completion);
10070 INIT_LIST_HEAD(&work->list);
10071 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010072 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010073 WARN_ON_ONCE(!inode);
10074 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10075 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076
10077 return work;
10078}
10079
10080void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10081{
10082 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010083 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010084}
10085
Chris Masond352ac62008-09-29 15:18:18 -040010086/*
10087 * some fairly slow code that needs optimization. This walks the list
10088 * of all the inodes with pending delalloc and forces them to disk.
10089 */
Miao Xie6c255e62014-03-06 13:55:01 +080010090static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10091 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010092{
Chris Masonea8c2812008-08-04 23:17:27 -040010093 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010094 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010095 struct btrfs_delalloc_work *work, *next;
10096 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010097 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010098 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010099
Miao Xie8ccf6f192012-10-25 09:28:04 +000010100 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010101 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010102
Miao Xie573bfb72014-03-06 13:55:03 +080010103 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010104 spin_lock(&root->delalloc_lock);
10105 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010106 while (!list_empty(&splice)) {
10107 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010108 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010109
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 list_move_tail(&binode->delalloc_inodes,
10111 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010112 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010113 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010114 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010115 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010116 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010117 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010118
David Sterba651d4942015-11-27 19:24:16 +010010119 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010120 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010121 if (delay_iput)
10122 btrfs_add_delayed_iput(inode);
10123 else
10124 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010125 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010126 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010127 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010128 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010129 btrfs_queue_work(root->fs_info->flush_workers,
10130 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010131 ret++;
10132 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010133 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010134 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010135 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010136 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -040010138
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010139out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010140 list_for_each_entry_safe(work, next, &works, list) {
10141 list_del_init(&work->list);
10142 btrfs_wait_and_free_delalloc_work(work);
10143 }
10144
Miao Xieeb73c1b2013-05-15 07:48:22 +000010145 if (!list_empty_careful(&splice)) {
10146 spin_lock(&root->delalloc_lock);
10147 list_splice_tail(&splice, &root->delalloc_inodes);
10148 spin_unlock(&root->delalloc_lock);
10149 }
Miao Xie573bfb72014-03-06 13:55:03 +080010150 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 return ret;
10152}
10153
10154int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10155{
10156 int ret;
10157
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010158 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 return -EROFS;
10160
Miao Xie6c255e62014-03-06 13:55:01 +080010161 ret = __start_delalloc_inodes(root, delay_iput, -1);
10162 if (ret > 0)
10163 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010164 /*
10165 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -040010166 * we have to make sure the IO is actually started and that
10167 * ordered extents get created before we return
10168 */
10169 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010170 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010171 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -040010172 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010173 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10174 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -040010175 }
10176 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010177 return ret;
10178}
10179
Miao Xie6c255e62014-03-06 13:55:01 +080010180int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10181 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010182{
10183 struct btrfs_root *root;
10184 struct list_head splice;
10185 int ret;
10186
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010187 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010188 return -EROFS;
10189
10190 INIT_LIST_HEAD(&splice);
10191
Miao Xie573bfb72014-03-06 13:55:03 +080010192 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 spin_lock(&fs_info->delalloc_root_lock);
10194 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010195 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010196 root = list_first_entry(&splice, struct btrfs_root,
10197 delalloc_root);
10198 root = btrfs_grab_fs_root(root);
10199 BUG_ON(!root);
10200 list_move_tail(&root->delalloc_root,
10201 &fs_info->delalloc_roots);
10202 spin_unlock(&fs_info->delalloc_root_lock);
10203
Miao Xie6c255e62014-03-06 13:55:01 +080010204 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010206 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 goto out;
10208
Miao Xie6c255e62014-03-06 13:55:01 +080010209 if (nr != -1) {
10210 nr -= ret;
10211 WARN_ON(nr < 0);
10212 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 spin_lock(&fs_info->delalloc_root_lock);
10214 }
10215 spin_unlock(&fs_info->delalloc_root_lock);
10216
Miao Xie6c255e62014-03-06 13:55:01 +080010217 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 atomic_inc(&fs_info->async_submit_draining);
10219 while (atomic_read(&fs_info->nr_async_submits) ||
10220 atomic_read(&fs_info->async_delalloc_pages)) {
10221 wait_event(fs_info->async_submit_wait,
10222 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10223 atomic_read(&fs_info->async_delalloc_pages) == 0));
10224 }
10225 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010226out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010227 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010228 spin_lock(&fs_info->delalloc_root_lock);
10229 list_splice_tail(&splice, &fs_info->delalloc_roots);
10230 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010231 }
Miao Xie573bfb72014-03-06 13:55:03 +080010232 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010233 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010234}
10235
Chris Mason39279cc2007-06-12 06:35:45 -040010236static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10237 const char *symname)
10238{
10239 struct btrfs_trans_handle *trans;
10240 struct btrfs_root *root = BTRFS_I(dir)->root;
10241 struct btrfs_path *path;
10242 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010243 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010244 int err;
10245 int drop_inode = 0;
10246 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010247 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010248 int name_len;
10249 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010250 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010251 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010252 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010253
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010254 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010255 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10256 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010257
Josef Bacik9ed74f22009-09-11 16:12:44 -040010258 /*
10259 * 2 items for inode item and ref
10260 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010261 * 1 item for updating parent inode item
10262 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010263 * 1 item for xattr if selinux is on
10264 */
Filipe Manana9269d122015-12-31 18:16:29 +000010265 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010266 if (IS_ERR(trans))
10267 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010268
Li Zefan581bb052011-04-20 10:06:11 +080010269 err = btrfs_find_free_ino(root, &objectid);
10270 if (err)
10271 goto out_unlock;
10272
Josef Bacikaec74772008-07-24 12:12:38 -040010273 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010274 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -040010275 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010276 if (IS_ERR(inode)) {
10277 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010278 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010279 }
Chris Mason39279cc2007-06-12 06:35:45 -040010280
Casey Schauflerad19db72011-12-15 10:09:07 -050010281 /*
10282 * If the active LSM wants to access the inode during
10283 * d_instantiate it needs these. Smack checks to see
10284 * if the filesystem supports xattrs by looking at the
10285 * ops vector.
10286 */
10287 inode->i_fop = &btrfs_file_operations;
10288 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010289 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010290 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10291
10292 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10293 if (err)
10294 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010295
Chris Mason39279cc2007-06-12 06:35:45 -040010296 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010297 if (!path) {
10298 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010299 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010300 }
Li Zefan33345d012011-04-20 10:31:50 +080010301 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010302 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010303 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010304 datasize = btrfs_file_extent_calc_inline_size(name_len);
10305 err = btrfs_insert_empty_item(trans, root, path, &key,
10306 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010307 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010308 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010309 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010310 }
Chris Mason5f39d392007-10-15 16:14:19 -040010311 leaf = path->nodes[0];
10312 ei = btrfs_item_ptr(leaf, path->slots[0],
10313 struct btrfs_file_extent_item);
10314 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10315 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010316 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010317 btrfs_set_file_extent_encryption(leaf, ei, 0);
10318 btrfs_set_file_extent_compression(leaf, ei, 0);
10319 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10320 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10321
Chris Mason39279cc2007-06-12 06:35:45 -040010322 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010323 write_extent_buffer(leaf, symname, ptr, name_len);
10324 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010325 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010326
Chris Mason39279cc2007-06-12 06:35:45 -040010327 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010328 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010329 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010330 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010331 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010332 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010333 /*
10334 * Last step, add directory indexes for our symlink inode. This is the
10335 * last step to avoid extra cleanup of these indexes if an error happens
10336 * elsewhere above.
10337 */
10338 if (!err)
10339 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010340 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010341 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010342 goto out_unlock_inode;
10343 }
10344
10345 unlock_new_inode(inode);
10346 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010347
10348out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010349 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010350 if (drop_inode) {
10351 inode_dec_link_count(inode);
10352 iput(inode);
10353 }
Liu Bob53d3f52012-11-14 14:34:34 +000010354 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010355 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010356
10357out_unlock_inode:
10358 drop_inode = 1;
10359 unlock_new_inode(inode);
10360 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010361}
Chris Mason16432982008-04-10 10:23:21 -040010362
Josef Bacik0af3d002010-06-21 14:48:16 -040010363static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10364 u64 start, u64 num_bytes, u64 min_size,
10365 loff_t actual_len, u64 *alloc_hint,
10366 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010367{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010368 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10369 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010370 struct btrfs_root *root = BTRFS_I(inode)->root;
10371 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010372 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010373 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010374 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010375 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010376 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010377 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010378 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010379
Josef Bacik0af3d002010-06-21 14:48:16 -040010380 if (trans)
10381 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010382 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010383 if (own_trans) {
10384 trans = btrfs_start_transaction(root, 3);
10385 if (IS_ERR(trans)) {
10386 ret = PTR_ERR(trans);
10387 break;
10388 }
Yan Zhengd899e052008-10-30 14:25:28 -040010389 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010390
Byongho Leeee221842015-12-15 01:42:10 +090010391 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010392 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010393 /*
10394 * If we are severely fragmented we could end up with really
10395 * small allocations, so if the allocator is returning small
10396 * chunks lets make its job easier by only searching for those
10397 * sized chunks.
10398 */
10399 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010400 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10401 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010402 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010403 if (own_trans)
10404 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010405 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010406 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010407 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010408
Josef Bacik0b670dc2015-09-23 17:11:16 -040010409 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010410 ret = insert_reserved_file_extent(trans, inode,
10411 cur_offset, ins.objectid,
10412 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010413 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010414 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010415 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010416 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010417 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010418 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010419 if (own_trans)
10420 btrfs_end_transaction(trans, root);
10421 break;
10422 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010423
Chris Masona1ed8352009-09-11 12:27:37 -040010424 btrfs_drop_extent_cache(inode, cur_offset,
10425 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010426
Josef Bacik5dc562c2012-08-17 13:14:17 -040010427 em = alloc_extent_map();
10428 if (!em) {
10429 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10430 &BTRFS_I(inode)->runtime_flags);
10431 goto next;
10432 }
10433
10434 em->start = cur_offset;
10435 em->orig_start = cur_offset;
10436 em->len = ins.offset;
10437 em->block_start = ins.objectid;
10438 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010439 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010440 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010441 em->bdev = root->fs_info->fs_devices->latest_bdev;
10442 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10443 em->generation = trans->transid;
10444
10445 while (1) {
10446 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010447 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010448 write_unlock(&em_tree->lock);
10449 if (ret != -EEXIST)
10450 break;
10451 btrfs_drop_extent_cache(inode, cur_offset,
10452 cur_offset + ins.offset - 1,
10453 0);
10454 }
10455 free_extent_map(em);
10456next:
Yan Zhengd899e052008-10-30 14:25:28 -040010457 num_bytes -= ins.offset;
10458 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010459 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010460
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010461 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010462 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010463 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010464 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010465 (actual_len > inode->i_size) &&
10466 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010467 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010468 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010469 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010470 i_size = cur_offset;
10471 i_size_write(inode, i_size);
10472 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010473 }
10474
Yan Zhengd899e052008-10-30 14:25:28 -040010475 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010476
10477 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010478 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010479 if (own_trans)
10480 btrfs_end_transaction(trans, root);
10481 break;
10482 }
Yan Zhengd899e052008-10-30 14:25:28 -040010483
Josef Bacik0af3d002010-06-21 14:48:16 -040010484 if (own_trans)
10485 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010486 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010487 if (cur_offset < end)
10488 btrfs_free_reserved_data_space(inode, cur_offset,
10489 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010490 return ret;
10491}
10492
Josef Bacik0af3d002010-06-21 14:48:16 -040010493int btrfs_prealloc_file_range(struct inode *inode, int mode,
10494 u64 start, u64 num_bytes, u64 min_size,
10495 loff_t actual_len, u64 *alloc_hint)
10496{
10497 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10498 min_size, actual_len, alloc_hint,
10499 NULL);
10500}
10501
10502int btrfs_prealloc_file_range_trans(struct inode *inode,
10503 struct btrfs_trans_handle *trans, int mode,
10504 u64 start, u64 num_bytes, u64 min_size,
10505 loff_t actual_len, u64 *alloc_hint)
10506{
10507 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10508 min_size, actual_len, alloc_hint, trans);
10509}
10510
Chris Masone6dcd2d2008-07-17 12:53:50 -040010511static int btrfs_set_page_dirty(struct page *page)
10512{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010513 return __set_page_dirty_nobuffers(page);
10514}
10515
Al Viro10556cb2011-06-20 19:28:19 -040010516static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010517{
Li Zefanb83cc962010-12-20 16:04:08 +080010518 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010519 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010520
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010521 if (mask & MAY_WRITE &&
10522 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10523 if (btrfs_root_readonly(root))
10524 return -EROFS;
10525 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10526 return -EACCES;
10527 }
Al Viro2830ba72011-06-20 19:16:29 -040010528 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010529}
Chris Mason39279cc2007-06-12 06:35:45 -040010530
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010531static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10532{
10533 struct btrfs_trans_handle *trans;
10534 struct btrfs_root *root = BTRFS_I(dir)->root;
10535 struct inode *inode = NULL;
10536 u64 objectid;
10537 u64 index;
10538 int ret = 0;
10539
10540 /*
10541 * 5 units required for adding orphan entry
10542 */
10543 trans = btrfs_start_transaction(root, 5);
10544 if (IS_ERR(trans))
10545 return PTR_ERR(trans);
10546
10547 ret = btrfs_find_free_ino(root, &objectid);
10548 if (ret)
10549 goto out;
10550
10551 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10552 btrfs_ino(dir), objectid, mode, &index);
10553 if (IS_ERR(inode)) {
10554 ret = PTR_ERR(inode);
10555 inode = NULL;
10556 goto out;
10557 }
10558
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010559 inode->i_fop = &btrfs_file_operations;
10560 inode->i_op = &btrfs_file_inode_operations;
10561
10562 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010563 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10564
Chris Masonb0d5d102014-09-08 13:08:51 -070010565 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10566 if (ret)
10567 goto out_inode;
10568
10569 ret = btrfs_update_inode(trans, root, inode);
10570 if (ret)
10571 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010572 ret = btrfs_orphan_add(trans, inode);
10573 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010574 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010575
Filipe Manana5762b5c2014-08-01 00:10:32 +010010576 /*
10577 * We set number of links to 0 in btrfs_new_inode(), and here we set
10578 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10579 * through:
10580 *
10581 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10582 */
10583 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010584 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010585 d_tmpfile(dentry, inode);
10586 mark_inode_dirty(inode);
10587
10588out:
10589 btrfs_end_transaction(trans, root);
10590 if (ret)
10591 iput(inode);
10592 btrfs_balance_delayed_items(root);
10593 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010594 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010595
10596out_inode:
10597 unlock_new_inode(inode);
10598 goto out;
10599
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010600}
10601
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010602static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010603 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010604 .lookup = btrfs_lookup,
10605 .create = btrfs_create,
10606 .unlink = btrfs_unlink,
10607 .link = btrfs_link,
10608 .mkdir = btrfs_mkdir,
10609 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010610 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010611 .symlink = btrfs_symlink,
10612 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010613 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010614 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010615 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010616 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010617 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010618 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010619 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010620};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010621static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010622 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010623 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010624 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010625};
Yan, Zheng76dda932009-09-21 16:00:26 -040010626
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010627static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010628 .llseek = generic_file_llseek,
10629 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010630 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010631 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010632#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010633 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010634#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010635 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010636 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010637};
10638
David Sterba20e55062015-11-19 11:42:28 +010010639static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010640 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010641 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010642 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010643 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010644 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010645 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010646 .set_bit_hook = btrfs_set_bit_hook,
10647 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010648 .merge_extent_hook = btrfs_merge_extent_hook,
10649 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010650};
10651
Chris Mason35054392009-01-21 13:11:13 -050010652/*
10653 * btrfs doesn't support the bmap operation because swapfiles
10654 * use bmap to make a mapping of extents in the file. They assume
10655 * these extents won't change over the life of the file and they
10656 * use the bmap result to do IO directly to the drive.
10657 *
10658 * the btrfs bmap call would return logical addresses that aren't
10659 * suitable for IO and they also will change frequently as COW
10660 * operations happen. So, swapfile + btrfs == corruption.
10661 *
10662 * For now we're avoiding this by dropping bmap.
10663 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010664static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010665 .readpage = btrfs_readpage,
10666 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010667 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010668 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010669 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010670 .invalidatepage = btrfs_invalidatepage,
10671 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010672 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010673 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010674};
10675
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010676static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010677 .readpage = btrfs_readpage,
10678 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010679 .invalidatepage = btrfs_invalidatepage,
10680 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010681};
10682
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010683static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010684 .getattr = btrfs_getattr,
10685 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010686 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010687 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010688 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010689 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010690 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010691 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010692};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010693static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010694 .getattr = btrfs_getattr,
10695 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010696 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010697 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010698 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010699 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010700 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010701};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010702static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010703 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010704 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010705 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010706 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010707 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010708 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010709 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010710};
Yan, Zheng76dda932009-09-21 16:00:26 -040010711
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010712const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010713 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010714 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010715};